From 352f322f6108105fbdb33d607f6e858e7c3f66d8 Mon Sep 17 00:00:00 2001 From: lbl8603 <49143209+lbl8603@users.noreply.github.com> Date: Wed, 24 Jul 2024 22:11:50 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E6=88=90guid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 1 + vnt/tun/Cargo.toml | 1 + vnt/tun/src/windows/tun/mod.rs | 15 ++++++++++----- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c9e5943..10045db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1968,6 +1968,7 @@ dependencies = [ "libloading", "log", "rand", + "sha2", "widestring", "winapi", ] diff --git a/vnt/tun/Cargo.toml b/vnt/tun/Cargo.toml index cdede0f..08b8d16 100644 --- a/vnt/tun/Cargo.toml +++ b/vnt/tun/Cargo.toml @@ -11,6 +11,7 @@ libc = "0.2.153" log = { version = "0.4.20", features = [] } rand = "0.8.5" +sha2 = { version = "0.10.6", features = ["oid"] } [target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies] ioctl = { version = "0.8", package = "ioctl-sys" } diff --git a/vnt/tun/src/windows/tun/mod.rs b/vnt/tun/src/windows/tun/mod.rs index d155c8f..8c826eb 100644 --- a/vnt/tun/src/windows/tun/mod.rs +++ b/vnt/tun/src/windows/tun/mod.rs @@ -1,9 +1,8 @@ #![allow(dead_code)] use libloading::Library; +use sha2::Digest; use std::io; use std::net::Ipv4Addr; - -use rand::Rng; use winapi::um::winbase; use winapi::um::{synchapi, winnt}; @@ -81,8 +80,7 @@ impl Device { if Self::delete_for_name(&win_tun, &name_utf16).is_ok() { std::thread::sleep(std::time::Duration::from_millis(500)); } - let mut guid_bytes: [u8; 16] = [0u8; 16]; - rand::thread_rng().fill(&mut guid_bytes); + let guid_bytes: [u8; 16] = hash_guid(&name); let guid = u128::from_ne_bytes(guid_bytes); //SAFETY: guid is a unique integer so transmuting either all zeroes or the user's preferred //guid to the winapi guid type is safe and will allow the windows kernel to see our GUID @@ -154,7 +152,14 @@ impl Device { Ok(()) } } - +fn hash_guid(input: &str) -> [u8; 16] { + let mut hasher = sha2::Sha256::new(); + hasher.update(b"VNT"); + hasher.update(input.as_bytes()); + hasher.update(b"2024"); + let hash: [u8; 32] = hasher.finalize().into(); + hash[..16].try_into().unwrap() +} impl IFace for Device { fn version(&self) -> io::Result { let version = unsafe { self.win_tun.WintunGetRunningDriverVersion() };