Force Proxy to any Windows/MacOS/Linux Application TCP and UDP traffic
C ccybersecurityhttp3httpproxylinuxlinux-proxymacosmacos-appmacos-proxyproxifierproxifier-alternativeproxyclienttcpthick-clientudpwindivertwindows
fix(windows): only members of the 'ProxyBridge Users' group (and administrators) may drive the engine service
AnoF-Cyber86ce6d5d8678
diff --git a/Windows/gui/api/pb_ipc.h b/Windows/gui/api/pb_ipc.hindex 2cde8f5..837bf9c 100644--- a/Windows/gui/api/pb_ipc.h+++ b/Windows/gui/api/pb_ipc.h@@ -13,7 +13,7 @@#include "pb_api.h"#include "../../src/service/pb_proto.h"-enum { PB_IPC_OK = 0, PB_IPC_NO_SERVICE, PB_IPC_BUSY, PB_IPC_BAD_VERSION };+enum { PB_IPC_OK = 0, PB_IPC_NO_SERVICE, PB_IPC_BUSY, PB_IPC_BAD_VERSION, PB_IPC_DENIED };static HANDLE g_ipcCtl = INVALID_HANDLE_VALUE;static HANDLE g_ipcEvt = INVALID_HANDLE_VALUE;@@ -192,10 +192,10 @@ static int PB_LoadService(PBApi* api)ctl = PbIpcOpen(PB_PIPE_CTL, GENERIC_READ | GENERIC_WRITE, &err);}if (ctl == INVALID_HANDLE_VALUE)- return (err == ERROR_PIPE_BUSY) ? PB_IPC_BUSY : PB_IPC_NO_SERVICE;+ return (err == ERROR_PIPE_BUSY) ? PB_IPC_BUSY : (err == ERROR_ACCESS_DENIED) ? PB_IPC_DENIED : PB_IPC_NO_SERVICE;HANDLE evt = PbIpcOpen(PB_PIPE_EVT, GENERIC_READ, &err);- if (evt == INVALID_HANDLE_VALUE) { CloseHandle(ctl); return (err == ERROR_PIPE_BUSY) ? PB_IPC_BUSY : PB_IPC_NO_SERVICE; }+ if (evt == INVALID_HANDLE_VALUE) { CloseHandle(ctl); return (err == ERROR_PIPE_BUSY) ? PB_IPC_BUSY : (err == ERROR_ACCESS_DENIED) ? PB_IPC_DENIED : PB_IPC_NO_SERVICE; }g_ipcCtl = ctl; g_ipcEvt = evt;{diff --git a/Windows/gui/main.c b/Windows/gui/main.cindex cc44e96..8061e30 100644--- a/Windows/gui/main.c+++ b/Windows/gui/main.c@@ -1047,6 +1047,16 @@ static BOOL ConnectEngine(void)return FALSE;}+ if (rc == PB_IPC_DENIED)+ {+ // The service refuses this account. Offer the elevated stand-alone mode as a fallback.+ if (MessageBoxW(NULL, L"Your account is not allowed to use the ProxyBridge service.\n\n"+ L"Ask an administrator to add you to the local group \"ProxyBridge Users\" "+ L"(then sign out and back in).\n\nRun ProxyBridge as administrator instead?",+ APP_TITLE, MB_YESNO | MB_ICONWARNING) != IDYES)+ return FALSE;+ }+// No service: portable mode.if (!IsElevated()){diff --git a/Windows/installer/ProxyBridge.nsi b/Windows/installer/ProxyBridge.nsiindex 1461ffc..dd87d70 100644--- a/Windows/installer/ProxyBridge.nsi+++ b/Windows/installer/ProxyBridge.nsi@@ -104,6 +104,8 @@ Section "MainSection" SEC01nsExec::ExecToLog 'sc create ProxyBridgeSvc binPath= "\"$INSTDIR\ProxyBridgeSvc.exe\"" start= auto DisplayName= "ProxyBridge Service"'nsExec::ExecToLog 'sc description ProxyBridgeSvc "Privileged traffic-redirection engine for ProxyBridge. Lets the ProxyBridge app run without administrator rights."'nsExec::ExecToLog 'sc sdset ProxyBridgeSvc "D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWRPLOCRRC;;;IU)(A;;CCLCSWLOCRRC;;;SU)"'+ ; Local group that may drive the service; the signed-in user is added to it.+ nsExec::ExecToLog '"$INSTDIR\ProxyBridgeSvc.exe" --setup-users'nsExec::ExecToLog 'sc start ProxyBridgeSvc'CreateDirectory "$SMPROGRAMS\${PRODUCT_NAME}"@@ -148,6 +150,7 @@ Section UninstallnsExec::ExecToLog 'sc stop ProxyBridgeSvc'Sleep 1500nsExec::ExecToLog 'sc delete ProxyBridgeSvc'+ nsExec::ExecToLog 'net localgroup "ProxyBridge Users" /delete'nsExec::ExecToLog 'schtasks /Delete /F /TN "ProxyBridge"'; The per-user Run entry is removed for the account running the uninstaller; other users'; entries just point at a missing exe and are harmless.diff --git a/Windows/src/service/pb_service.c b/Windows/src/service/pb_service.cindex ab07b35..f725036 100644--- a/Windows/src/service/pb_service.c+++ b/Windows/src/service/pb_service.c@@ -11,16 +11,24 @@#define WIN32_LEAN_AND_MEAN#include <windows.h>#include <sddl.h>+#include <lm.h>+#include <wtsapi32.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include "pb_proto.h"+#pragma comment(lib, "advapi32.lib")+#pragma comment(lib, "netapi32.lib")+#pragma comment(lib, "wtsapi32.lib")#define PB_NO_SEARCH_FALLBACK // SYSTEM service: load the engine only from our own directory#include "../../gui/api/pb_api.h"-// pipe access: SYSTEM + Administrators full, interactive (logged-on, local) users read/write.-// Network logons are not in this list, and PIPE_REJECT_REMOTE_CLIENTS blocks remote access too.-#define PB_PIPE_SDDL L"D:(A;;GA;;;SY)(A;;GA;;;BA)(A;;GRGW;;;IU)"+// Who may drive the engine: SYSTEM and Administrators, plus members of the local group below+// (the installer adds the installing user; an administrator can add or remove others with the+// normal Windows tools). Everyone else gets "access denied" on the pipe. Network logons are+// never allowed (PIPE_REJECT_REMOTE_CLIENTS). The group is looked up every time a pipe is+// created, so membership/SID changes need no service restart (members must sign in again).+#define PB_USERS_GROUP L"ProxyBridge Users"static SERVICE_STATUS_HANDLE g_ssh = NULL;static SERVICE_STATUS g_status;@@ -178,10 +186,28 @@ static void ClosePipes(void)if (g_ctl != INVALID_HANDLE_VALUE) { DisconnectNamedPipe(g_ctl); CloseHandle(g_ctl); g_ctl = INVALID_HANDLE_VALUE; }}+static void BuildPipeSddl(wchar_t *out, size_t cap)+{+ wcscpy_s(out, cap, L"D:(A;;GA;;;SY)(A;;GA;;;BA)");+ BYTE sid[SECURITY_MAX_SID_SIZE]; DWORD sidLen = sizeof(sid);+ wchar_t dom[256]; DWORD domLen = 256; SID_NAME_USE use;+ if (!LookupAccountNameW(NULL, PB_USERS_GROUP, sid, &sidLen, dom, &domLen, &use) || use != SidTypeAlias)+ return; // no group: administrators only+ LPWSTR str = NULL;+ if (ConvertSidToStringSidW(sid, &str))+ {+ wcscat_s(out, cap, L"(A;;GRGW;;;");+ wcscat_s(out, cap, str);+ wcscat_s(out, cap, L")");+ LocalFree(str);+ }+}+static HANDLE MakePipe(const wchar_t *name, BOOL first, DWORD openMode){+ wchar_t sddl[256]; BuildPipeSddl(sddl, 256);SECURITY_ATTRIBUTES sa; ZeroMemory(&sa, sizeof(sa)); sa.nLength = sizeof(sa);- if (!ConvertStringSecurityDescriptorToSecurityDescriptorW(PB_PIPE_SDDL, SDDL_REVISION_1, &sa.lpSecurityDescriptor, NULL))+ if (!ConvertStringSecurityDescriptorToSecurityDescriptorW(sddl, SDDL_REVISION_1, &sa.lpSecurityDescriptor, NULL))return INVALID_HANDLE_VALUE;HANDLE h = CreateNamedPipeW(name,openMode | (first ? FILE_FLAG_FIRST_PIPE_INSTANCE : 0),@@ -292,8 +318,35 @@ static void WINAPI SvcMain(DWORD argc, LPWSTR *argv)SetState(SERVICE_STOPPED, 0);}-int wmain(void)+// "ProxyBridgeSvc.exe --setup-users" (run by the installer, elevated): create the local group+// and add the user who is signed in at the console. Returns 0 on success.+static int SetupUsers(void)+{+ LOCALGROUP_INFO_1 gi; gi.lgrpi1_name = (LPWSTR)PB_USERS_GROUP;+ gi.lgrpi1_comment = L"Users allowed to control the ProxyBridge engine service";+ NET_API_STATUS st = NetLocalGroupAdd(NULL, 1, (LPBYTE)&gi, NULL);+ if (st != NERR_Success && st != NERR_GroupExists && st != ERROR_ALIAS_EXISTS) return 2;++ DWORD sess = WTSGetActiveConsoleSessionId();+ if (sess == 0xFFFFFFFF) return 0; // nobody signed in: group only+ LPWSTR user = NULL, domain = NULL; DWORD n = 0;+ if (!WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, sess, WTSUserName, &user, &n) || !user || !user[0])+ { if (user) WTSFreeMemory(user); return 0; }+ WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, sess, WTSDomainName, &domain, &n);+ wchar_t full[512];+ if (domain && domain[0]) swprintf(full, 512, L"%s\\%s", domain, user); else wcscpy_s(full, 512, user);+ if (domain) WTSFreeMemory(domain);+ WTSFreeMemory(user);++ LOCALGROUP_MEMBERS_INFO_3 mi; mi.lgrmi3_domainandname = full;+ st = NetLocalGroupAddMembers(NULL, PB_USERS_GROUP, 3, (LPBYTE)&mi, 1);+ return (st == NERR_Success || st == ERROR_MEMBER_IN_ALIAS) ? 0 : 3;+}++int wmain(int argc, wchar_t **argv){+ if (argc >= 2 && wcscmp(argv[1], L"--setup-users") == 0)+ return SetupUsers();// The engine DLL is loaded from our own directory by absolute path (PB_LoadDirect), and the// service must never search the CWD / PATH for it.SetDllDirectoryW(L"");