Skip to content

Force Proxy to any Windows/MacOS/Linux Application TCP and UDP traffic

fix(windows): harden installer ACL, service pipe identity and DLL loading

AnoF-Cyber4734c0da72ce
diff --git a/Windows/cli/main.c b/Windows/cli/main.c
index 8d47fc4..e9efd33 100644
--- a/Windows/cli/main.c
+++ b/Windows/cli/main.c
@@ -513,7 +513,7 @@ static bool load_dll(void)
// use only the resolved absolute path no bare-name fallback to prevent
// DLL hijacking via current working directory.
- g_hDll = LoadLibraryExA(dll_path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
+ g_hDll = LoadLibraryExA(dll_path, NULL, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32);
if (!g_hDll)
{
fprintf(stderr, "ERROR: Cannot load ProxyBridgeCore.dll (error %lu)\n",
diff --git a/Windows/gui/api/pb_api.h b/Windows/gui/api/pb_api.h
index 828c0d6..e7a2f60 100644
--- a/Windows/gui/api/pb_api.h
+++ b/Windows/gui/api/pb_api.h
@@ -80,10 +80,9 @@ static BOOL PB_LoadDirect(PBApi* api)
for (DWORD i = n; i > 0; i--) { if (path[i - 1] == L'\\') { path[i] = 0; break; } }
lstrcatW(path, L"ProxyBridgeCore.dll");
- api->dll = LoadLibraryW(path);
-#ifndef PB_NO_SEARCH_FALLBACK
- if (!api->dll) api->dll = LoadLibraryW(L"ProxyBridgeCore.dll"); // fallback to search path
-#endif
+ // Absolute path only, and its dependencies (WinDivert.dll) come from that folder or System32:
+ // never from the current directory or PATH (DLL planting).
+ api->dll = LoadLibraryExW(path, NULL, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32);
if (!api->dll) return FALSE;
#define PB_BIND(field, name) \
diff --git a/Windows/gui/api/pb_ipc.h b/Windows/gui/api/pb_ipc.h
index 837bf9c..79d4807 100644
--- a/Windows/gui/api/pb_ipc.h
+++ b/Windows/gui/api/pb_ipc.h
@@ -154,13 +154,40 @@ static void PbIpcTryStartService(void)
CloseServiceHandle(scm);
}
+// Any user can create a named pipe, so a process other than our service could be listening on
+// the pipe name (it would then receive the proxy credentials we send). Only talk to the process
+// that the Service Control Manager says is running ProxyBridgeSvc. (A standard user cannot open a
+// SYSTEM process to read its path, but may query a service's status, which includes its PID.)
+static BOOL PbIpcServerIsOurs(HANDLE h)
+{
+ ULONG pid = 0;
+ if (!GetNamedPipeServerProcessId(h, &pid) || pid == 0) return FALSE;
+ BOOL ok = FALSE;
+ SC_HANDLE scm = OpenSCManagerW(NULL, NULL, SC_MANAGER_CONNECT);
+ if (scm)
+ {
+ SC_HANDLE svc = OpenServiceW(scm, PB_SERVICE_NAME, SERVICE_QUERY_STATUS);
+ if (svc)
+ {
+ SERVICE_STATUS_PROCESS st; DWORD need = 0;
+ if (QueryServiceStatusEx(svc, SC_STATUS_PROCESS_INFO, (LPBYTE)&st, sizeof(st), &need))
+ ok = (st.dwProcessId == pid);
+ CloseServiceHandle(svc);
+ }
+ CloseServiceHandle(scm);
+ }
+ return ok;
+}
+
static HANDLE PbIpcOpen(const wchar_t* name, DWORD access, DWORD* err)
{
for (int tries = 0; tries < 20; tries++)
{
- HANDLE h = CreateFileW(name, access, 0, NULL, OPEN_EXISTING, 0, NULL);
+ // SECURITY_IDENTIFICATION: even a rogue server could not impersonate us.
+ HANDLE h = CreateFileW(name, access, 0, NULL, OPEN_EXISTING, SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION, NULL);
if (h != INVALID_HANDLE_VALUE)
{
+ if (!PbIpcServerIsOurs(h)) { CloseHandle(h); *err = ERROR_BAD_PIPE; return INVALID_HANDLE_VALUE; }
DWORD mode = PIPE_READMODE_MESSAGE;
SetNamedPipeHandleState(h, &mode, NULL, NULL);
return h;
diff --git a/Windows/installer/ProxyBridge.nsi b/Windows/installer/ProxyBridge.nsi
index 26e6f7c..9795efd 100644
--- a/Windows/installer/ProxyBridge.nsi
+++ b/Windows/installer/ProxyBridge.nsi
@@ -87,6 +87,11 @@ Section "MainSection" SEC01
File "..\output\WinDivert.dll"
File "..\output\WinDivert64.sys"
+ ; The service runs as SYSTEM from this folder, so ordinary users must never be able to modify it
+ ; (a folder chosen outside Program Files, e.g. D:\Tools, is usually writable by every user).
+ ; SYSTEM and Administrators: full control; Users: read/execute. SIDs, so it works on any language.
+ nsExec::ExecToLog 'icacls "$INSTDIR" /inheritance:r /grant:r "*S-1-5-18:(OI)(CI)F" "*S-1-5-32-544:(OI)(CI)F" "*S-1-5-32-545:(OI)(CI)RX"'
+
; Remove leftover native libraries from the old C#/Avalonia GUI. The native C GUI
; does not use them; without this an upgrade would keep these stale DLLs behind.
Delete "$INSTDIR\av_libglesv2.dll"
diff --git a/Windows/src/service/pb_service.c b/Windows/src/service/pb_service.c
index 3f55c93..d93b117 100644
--- a/Windows/src/service/pb_service.c
+++ b/Windows/src/service/pb_service.c
@@ -277,13 +277,14 @@ static void ServeClient(void)
static DWORD WINAPI WorkerMain(LPVOID arg)
{
(void)arg;
- BOOL first = TRUE;
+ // Every pipe instance is created with FILE_FLAG_FIRST_PIPE_INSTANCE: if another process already
+ // owns the pipe name (squatting), creation fails and we retry instead of serving next to it.
+ const BOOL first = TRUE;
while (!g_stop)
{
g_ctl = MakePipe(PB_PIPE_CTL, first, PIPE_ACCESS_DUPLEX);
g_evt = MakePipe(PB_PIPE_EVT, first, PIPE_ACCESS_OUTBOUND);
if (g_ctl == INVALID_HANDLE_VALUE || g_evt == INVALID_HANDLE_VALUE) { ClosePipes(); Sleep(1000); continue; }
- first = FALSE;
if (!ConnectNamedPipe(g_ctl, NULL) && GetLastError() != ERROR_PIPE_CONNECTED) { ClosePipes(); continue; }
if (g_stop) { ClosePipes(); break; }