Skip to content

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

fix(windows): redirected connections were dropped on VPN/Public networks (firewall rule for the relay)

AnoF-Cyberec9936b27699
diff --git a/Windows/installer/ProxyBridge.nsi b/Windows/installer/ProxyBridge.nsi
index 0245ba6..1461ffc 100644
--- a/Windows/installer/ProxyBridge.nsi
+++ b/Windows/installer/ProxyBridge.nsi
@@ -97,6 +97,10 @@ Section "MainSection" SEC01
; ProxyBridge app itself never needs administrator rights. The service ACL lets interactive
; users start it (never stop or reconfigure it), in case it was stopped.
nsExec::ExecToLog 'sc delete ProxyBridgeSvc'
+ ; Remove the inbound firewall rules the engine added for its relay (one per executable).
+ nsExec::ExecToLog 'netsh advfirewall firewall delete rule name=all program="$INSTDIR\ProxyBridgeSvc.exe"'
+ nsExec::ExecToLog 'netsh advfirewall firewall delete rule name=all program="$INSTDIR\ProxyBridge.exe"'
+ nsExec::ExecToLog 'netsh advfirewall firewall delete rule name=all program="$INSTDIR\ProxyBridge_CLI.exe"'
nsExec::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)"'
diff --git a/Windows/src/ProxyBridge.c b/Windows/src/ProxyBridge.c
index a5fa171..5a5e670 100644
--- a/Windows/src/ProxyBridge.c
+++ b/Windows/src/ProxyBridge.c
@@ -1062,6 +1062,69 @@ DWORD WINAPI cleanup_worker(LPVOID arg)
return 0;
}
+
+// Windows Firewall rule for the relay.
+//
+// Redirected connections reach the relay as *inbound* traffic on whichever interface the app used.
+// Windows only prompts to allow an app on the Private profile by default, so on an adapter that is
+// classified Public (VPN tunnels such as WireGuard/OpenVPN nearly always are) the firewall dropped
+// the redirected SYN: the Connections log looked normal, but the app timed out and the proxy never
+// saw it. Add an inbound allow rule for this executable on all profiles (the relay now verifies the
+// peer address, so this does not open it up to other hosts). Needs elevation; best effort.
+static DWORD fw_run(const wchar_t *args, DWORD wait_ms)
+{
+ wchar_t cmd[1400];
+ _snwprintf_s(cmd, 1400, _TRUNCATE, L"netsh.exe advfirewall firewall %s", args);
+ STARTUPINFOW si; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si);
+ si.dwFlags = STARTF_USESHOWWINDOW; si.wShowWindow = SW_HIDE;
+ PROCESS_INFORMATION pi; ZeroMemory(&pi, sizeof(pi));
+ if (!CreateProcessW(NULL, cmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi))
+ return (DWORD)-1;
+ DWORD code = (DWORD)-1;
+ if (WaitForSingleObject(pi.hProcess, wait_ms) == WAIT_OBJECT_0) GetExitCodeProcess(pi.hProcess, &code);
+ CloseHandle(pi.hThread); CloseHandle(pi.hProcess);
+ return code;
+}
+
+static DWORD WINAPI fw_ensure_thread(LPVOID arg)
+{
+ (void)arg;
+ BOOL elevated = FALSE; HANDLE tok = NULL;
+ if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &tok))
+ {
+ TOKEN_ELEVATION te; DWORD n = 0;
+ if (GetTokenInformation(tok, TokenElevation, &te, sizeof(te), &n)) elevated = te.TokenIsElevated != 0;
+ CloseHandle(tok);
+ }
+ if (!elevated) return 0;
+
+ wchar_t exe[MAX_PATH];
+ DWORD n = GetModuleFileNameW(NULL, exe, MAX_PATH);
+ if (n == 0 || n >= MAX_PATH) return 0;
+
+ // One rule per executable path; the name carries a hash of the (lower-cased) path.
+ unsigned h = 2166136261u;
+ for (const wchar_t *p = exe; *p; p++) { wchar_t c = *p; if (c >= L'A' && c <= L'Z') c += 32; h = (h ^ (unsigned)c) * 16777619u; }
+ wchar_t name[96];
+ _snwprintf_s(name, 96, _TRUNCATE, L"ProxyBridge relay %08x", h);
+
+ wchar_t args[1300];
+ _snwprintf_s(args, 1300, _TRUNCATE, L"show rule name=\"%s\"", name);
+ if (fw_run(args, 8000) == 0) return 0; // exit code 0 = the rule already exists
+
+ _snwprintf_s(args, 1300, _TRUNCATE,
+ L"add rule name=\"%s\" dir=in action=allow program=\"%s\" enable=yes profile=any "
+ L"description=\"Lets ProxyBridge's local relay receive redirected connections on every network profile (VPN adapters are Public).\"",
+ name, exe);
+ if (fw_run(args, 15000) == 0)
+ log_message("Windows Firewall: added an inbound allow rule for %ls on all network profiles "
+ "(needed for VPN and Public networks)", exe);
+ else
+ log_message("Windows Firewall: could not add the inbound rule for the relay. If proxying stops working on a "
+ "VPN or Public network, allow %ls through the firewall for all profiles.", exe);
+ return 0;
+}
+
PROXYBRIDGE_API BOOL ProxyBridge_Start(void)
{
char filter[FILTER_BUFFER_SIZE];
@@ -1083,6 +1146,8 @@ PROXYBRIDGE_API BOOL ProxyBridge_Start(void)
relay_pool_start(); // optional: connections fall back to blocking relay threads without it
+ { HANDLE ft = CreateThread(NULL, 0, fw_ensure_thread, NULL, 0, NULL); if (ft) CloseHandle(ft); }
+
proxy_thread = CreateThread(NULL, 1, local_proxy_server, NULL, 0, NULL);
if (proxy_thread == NULL)
{
diff --git a/Windows/src/pb_internal.h b/Windows/src/pb_internal.h
index 91d5f95..d84918f 100644
--- a/Windows/src/pb_internal.h
+++ b/Windows/src/pb_internal.h
@@ -19,6 +19,7 @@
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
+#pragma comment(lib, "advapi32.lib") // token elevation check for the firewall rule
#define MAXBUF 0xFFFF
#define LOCAL_PROXY_PORT 34010
diff --git a/Windows/src/pb_relay.c b/Windows/src/pb_relay.c
index 8a77302..2ab85db 100644
--- a/Windows/src/pb_relay.c
+++ b/Windows/src/pb_relay.c
@@ -395,6 +395,23 @@ DWORD WINAPI udp_relay_server(LPVOID arg)
return 0;
}
+// The relay is reached by packets ProxyBridge turns around so they look like they come from the
+// ORIGINAL destination, so the legitimate peer address of an accepted connection is exactly the
+// tracked flow's original destination (or a loopback address for local-to-local flows). The flow
+// used to be looked up by the peer's source PORT alone, which is only safe while a firewall keeps
+// outsiders away from the relay port; now that the relay is reachable on every network profile
+// (needed for VPN adapters), another host must not be able to claim a tracked flow just by
+// connecting from a matching source port.
+static BOOL relay_peer_ok_v4(UINT32 peer, UINT32 orig_dest)
+{
+ return ((peer & 0xFF) == 127) || peer == orig_dest;
+}
+static BOOL relay_peer_ok_v6(const struct in6_addr *peer, const UINT8 orig_dest6[16])
+{
+ static const UINT8 lb6[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
+ return memcmp(peer, lb6, 16) == 0 || memcmp(peer, orig_dest6, 16) == 0;
+}
+
DWORD WINAPI local_proxy_server(LPVOID arg)
{
WSADATA wsa_data;
@@ -516,7 +533,8 @@ DWORD WINAPI local_proxy_server(LPVOID arg)
conn_config->is_ipv6 = FALSE;
UINT16 client_port = ntohs(client_addr.sin_port);
- if (get_connection_full(client_port, FALSE, &conn_config->orig_dest_ip, &conn_config->orig_dest_port, &conn_config->proxy_config_id))
+ if (get_connection_full(client_port, FALSE, &conn_config->orig_dest_ip, &conn_config->orig_dest_port, &conn_config->proxy_config_id) &&
+ relay_peer_ok_v4(client_addr.sin_addr.s_addr, conn_config->orig_dest_ip))
{
HANDLE conn_thread = CreateThread(NULL, 1, connection_handler, (LPVOID)conn_config, 0, NULL);
if (conn_thread != NULL) { CloseHandle(conn_thread); }
@@ -543,7 +561,8 @@ DWORD WINAPI local_proxy_server(LPVOID arg)
conn_config->is_ipv6 = TRUE;
UINT16 client_port = ntohs(client_addr6.sin6_port);
- if (get_connection_full_v6(client_port, FALSE, conn_config->orig_dest_ip6, &conn_config->orig_dest_port, &conn_config->proxy_config_id))
+ if (get_connection_full_v6(client_port, FALSE, conn_config->orig_dest_ip6, &conn_config->orig_dest_port, &conn_config->proxy_config_id) &&
+ relay_peer_ok_v6(&client_addr6.sin6_addr, conn_config->orig_dest_ip6))
{
HANDLE conn_thread = CreateThread(NULL, 1, connection_handler, (LPVOID)conn_config, 0, NULL);
if (conn_thread != NULL) { CloseHandle(conn_thread); }