From cd0bcae4c918adf558eb24ea17f9164a7eeb2197 Mon Sep 17 00:00:00 2001 From: faumaray Date: Mon, 26 Oct 2026 23:19:19 +0300 Subject: [PATCH] windows: never adopt a state directory someone else made A root not owned by SYSTEM or Administrators is moved aside; the root gets SYSTEM as owner and an OWNER RIGHTS entry, so owning a file below it grants nothing. Adds the pinned component manifest. Co-Authored-By: Claude Opus 5.5 --- windows/components.json | 103 +++++++++++++++++++++++++ windows/crates/ps-service/src/paths.rs | 52 +++++++++++-- 2 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 windows/components.json diff --git a/windows/components.json b/windows/components.json new file mode 100644 index 0000000..8d632fa --- /dev/null +++ b/windows/components.json @@ -0,0 +1,103 @@ +{ + "components": { + "sing-box": { + "version": "1.14.4", + "license": "GPL-3.0-or-later", + "homepage": "https://github.com/SagerNet/sing-box", + "sources": [ + { + "url": "https://github.com/SagerNet/sing-box/releases/download/v1.14.4/sing-box-1.14.4-windows-amd64.zip", + "sha256": "99185d75d314a07dbba4a130c8ad986cbeb629a07927975636b341ca61f991df", + "strip": "sing-box-1.14.4-windows-amd64/" + } + ] + }, + "xray": { + "version": "26.10.21", + "license": "MPL-2.0", + "homepage": "https://github.com/XTLS/Xray-core", + "sources": [ + { + "url": "https://github.com/XTLS/Xray-core/releases/download/v26.10.21/Xray-windows-64.zip", + "sha256": "fc58c39ee2f2717b4c54d28d374362389e5319b20ae44aea76ea45a5243ec827", + "strip": "", + "include": [ + "xray.exe", + "geoip.dat", + "geosite.dat", + "LICENSE" + ] + } + ] + }, + "zapret": { + "version": "1.10.4", + "license": "MIT", + "homepage": "https://github.com/Flowseal/zapret-discord-youtube", + "sources": [ + { + "url": "https://github.com/Flowseal/zapret-discord-youtube/releases/download/1.10.4/zapret-discord-youtube-1.10.4.zip", + "sha256": "291af673bd0238506f9548e0e94ca0abf244000f4c9a06960544fb01c5f97b34", + "strip": "zapret-discord-youtube-1.10.4/", + "exclude": [ + "service.bat", + "utils/" + ] + } + ] + }, + "zapret2": { + "version": "2026.10.21+de9795d", + "license": "MIT", + "homepage": "https://github.com/bol-van/zapret-win-bundle", + "sources": [ + { + "url": "https://codeload.github.com/bol-van/zapret-win-bundle/zip/de9795d71287fdb21cf8c20b8af1a09d0eb10ab2", + "strip": "zapret-win-bundle-de9795d71287fdb21cf8c20b8af1a09d0eb10ab2/zapret-winws/", + "files": { + "WinDivert.dll": "16abd6a029e65557c6a309bea7b13bf81fff4e193567582e1cddbf6719f323e0", + "WinDivert64.sys": "8da085332782708d8767bcace5327a6ec7283c17cfb85e40b03cd2323a90ddc2", + "cygwin1.dll": "103104a52e5293ce418944725df19e2bf81ad9269b9a120d71d39028e821499b", + "files/list-youtube.txt": "47ea8a9ca11eb9ad833e5f22d12e8857400880e8f555f45687230ce8ba57d299", + "files/quic_initial_www_google_com.bin": "f4589c57749f956bb30538197a521d7005f8b0a8723b4707e72405e51ddac50a", + "lua/zapret-antidpi.lua": "6d1fa9168e5de1682fef91fd174f68b58c50f6739ef00f42ca7db2b940b6246d", + "lua/zapret-auto.lua": "23a9b99a776d868d92f4cc0ee5110f5c1f12fe746620d9c0337022dbc2f52df0", + "lua/zapret-lib.lua": "6bf528fef1f0fc3c65e189216fc8a06bb873f6e25d1c29d2bfa4695a09b73440", + "lua/zapret-obfs.lua": "e2d75840a9f1e3bfb74fa7463886932e03eb02ab33c6146d6392d6a231d1cfd5", + "lua/zapret-pcap.lua": "6866c37c92fbc62075accf94228da48af618c7f416edc682eba99b2196e05f45", + "lua/zapret-tests.lua": "82664cb50a751a531b63dce094ac03de5045b16b489e726ff32a9a1034811459", + "windivert.filter/README.txt": "be9ad4622296c463f1cf278c4f97be0c7eabaff9e79ad36262a8255fafd2b216", + "windivert.filter/windivert_part.dht.txt": "0e0d071239995332c2e45e60c59c5bb3d70233426e430a7babb24b18b75e90c0", + "windivert.filter/windivert_part.discord_media.txt": "2f2deaf49ed68eb684258497134181753188f2b475161deb244488a53872b7c5", + "windivert.filter/windivert_part.quic_initial_ietf.txt": "f243ddc22758b9e04838211a3815e43fb3abe615e6be29e6a812ae4c72b392e0", + "windivert.filter/windivert_part.stun.txt": "2a14f470b00cd9704003e0cade500086398f9222c858d9ae944bdeb89f520595", + "windivert.filter/windivert_part.wireguard.txt": "da68586267462b29e3e370203ce0369ae2baa25a07aa2deab5fb44d69fef27be", + "winws2.exe": "ba4ceccdea3576da0a0991a50d7893ea911de197281338132dbec02a5b2fcf58" + } + }, + { + "url": "https://codeload.github.com/nfqws/nfqws2-keenetic/zip/fa22c177b340e73d8b8a94b295af20e0228c4c22", + "strip": "nfqws2-keenetic-fa22c177b340e73d8b8a94b295af20e0228c4c22/etc/nfqws2/", + "dest": "keenetic/", + "files": { + "blobs/quic_initial.bin": "f4589c57749f956bb30538197a521d7005f8b0a8723b4707e72405e51ddac50a", + "blobs/tls_clienthello.bin": "936c2bee4cfb80aa3c426b2dcbcc834b3fbcd1adb17172959dc569c73a14275c", + "lists/auto.list": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "lists/exclude.list": "e7f87f890ad7916da8e30ceb101e8340955bdbba1e16cd6e6439469cc847f796", + "lists/ipset.list": "88cfdebe5531fd14d3a1ff16e303ae28307563c2ecd0820090918bf92da90b88", + "lists/ipset_exclude.list": "0a332890f577839bcce0651d3cf287d3d820c1bf4495b3f568276856d6e85073", + "lists/user.list": "974016ffc0b0cef05fbb05718f12b721104a72b3fa7261a814d596974dc339cc", + "nfqws2.conf": "b5c6598d5ba03762393a877514e005d2a241e0eb51c4e03142cade544e1e5b30" + } + } + ] + }, + "tg-ws-proxy": { + "version": "1.11.2+py3.12.10", + "license": "MIT", + "homepage": "https://github.com/Flowseal/tg-ws-proxy", + "bundled": true, + "sources": [] + } + } +} diff --git a/windows/crates/ps-service/src/paths.rs b/windows/crates/ps-service/src/paths.rs index da3c2f3..9ae5340 100644 --- a/windows/crates/ps-service/src/paths.rs +++ b/windows/crates/ps-service/src/paths.rs @@ -69,6 +69,14 @@ impl Paths { /// holds the secrets and the programs the service runs as SYSTEM. Everyone else goes /// through the API. pub fn create_all(&self) -> std::io::Result<()> { + // A root someone else made first (ProgramData lets any user make folders) is theirs to + // fill with programs this service would run: it is moved aside, not adopted. + #[cfg(windows)] + if self.root.exists() && !owned_by_system_or_admins(&self.root)? { + let aside = self.root.with_extension(format!("untrusted-{}", std::process::id())); + tracing::warn!("{} is not owned by SYSTEM or Administrators; moving it to {}", self.root.display(), aside.display()); + std::fs::rename(&self.root, &aside)?; + } std::fs::create_dir_all(&self.root)?; #[cfg(windows)] protect(&self.root)?; @@ -92,8 +100,37 @@ pub fn same_content(path: &Path, data: &[u8]) -> bool { std::fs::read(path).is_ok_and(|old| old == data) } -/// A protected DACL on `dir`, inherited by everything below it: full control for SYSTEM and -/// Administrators, nothing for anyone else. +/// Whether `dir`'s owner is SYSTEM or Administrators. +#[cfg(windows)] +fn owned_by_system_or_admins(dir: &Path) -> std::io::Result { + use std::ffi::c_void; + use std::os::windows::ffi::OsStrExt; + use std::ptr::null_mut; + use windows_sys::Win32::Foundation::{ERROR_SUCCESS, LocalFree}; + use windows_sys::Win32::Security::Authorization::{GetNamedSecurityInfoW, SE_FILE_OBJECT}; + use windows_sys::Win32::Security::{ + IsWellKnownSid, OWNER_SECURITY_INFORMATION, WinBuiltinAdministratorsSid, WinLocalSystemSid, + }; + let path: Vec = dir.as_os_str().encode_wide().chain(Some(0)).collect(); + let mut owner: *mut c_void = null_mut(); + let mut sd: *mut c_void = null_mut(); + // SAFETY: NUL-terminated path; the owner points into sd, freed after use. + unsafe { + let rc = GetNamedSecurityInfoW( + path.as_ptr(), SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION, &mut owner, null_mut(), null_mut(), null_mut(), &mut sd, + ); + if rc != ERROR_SUCCESS { + return Err(std::io::Error::from_raw_os_error(rc as i32)); + } + let ok = IsWellKnownSid(owner, WinLocalSystemSid) != 0 || IsWellKnownSid(owner, WinBuiltinAdministratorsSid) != 0; + LocalFree(sd); + Ok(ok) + } +} + +/// SYSTEM as the owner and a protected DACL on `dir`, inherited by everything below it: full +/// control for SYSTEM and Administrators, nothing for anyone else, and (OWNER RIGHTS) nothing +/// implicit for whoever owns a file below it. #[cfg(windows)] fn protect(dir: &Path) -> std::io::Result<()> { use std::ffi::c_void; @@ -104,10 +141,11 @@ fn protect(dir: &Path) -> std::io::Result<()> { ConvertStringSecurityDescriptorToSecurityDescriptorW, SDDL_REVISION_1, SE_FILE_OBJECT, SetNamedSecurityInfoW, }; use windows_sys::Win32::Security::{ - DACL_SECURITY_INFORMATION, GetSecurityDescriptorDacl, PROTECTED_DACL_SECURITY_INFORMATION, ACL, + ACL, DACL_SECURITY_INFORMATION, GetSecurityDescriptorDacl, GetSecurityDescriptorOwner, OWNER_SECURITY_INFORMATION, + PROTECTED_DACL_SECURITY_INFORMATION, }; - let sddl: Vec = "D:P(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)".encode_utf16().chain(Some(0)).collect(); + let sddl: Vec = "O:SYD:P(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;OICI;0x0;;;OW)".encode_utf16().chain(Some(0)).collect(); let path: Vec = dir.as_os_str().encode_wide().chain(Some(0)).collect(); let mut sd: *mut c_void = null_mut(); // SAFETY: NUL-terminated inputs; the descriptor is freed below, and the DACL points into it. @@ -118,11 +156,13 @@ fn protect(dir: &Path) -> std::io::Result<()> { let (mut present, mut defaulted) = (0, 0); let mut dacl: *mut ACL = null_mut(); GetSecurityDescriptorDacl(sd, &mut present, &mut dacl, &mut defaulted); + let mut owner: *mut c_void = null_mut(); + GetSecurityDescriptorOwner(sd, &mut owner, &mut defaulted); let rc = SetNamedSecurityInfoW( path.as_ptr(), SE_FILE_OBJECT, - DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION, - null_mut(), + OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION, + owner, null_mut(), dacl, null_mut(),