Skip to content

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

fix(windows): don't let unresolved owner/name turn a matching connection into DIRECT (#205)

AnoF-Cyberf861a7b62f42
diff --git a/Windows/src/pb_process.c b/Windows/src/pb_process.c
index aaf1107..cae4393 100644
--- a/Windows/src/pb_process.c
+++ b/Windows/src/pb_process.c
@@ -1,4 +1,5 @@
#include "pb_internal.h"
+#include <tlhelp32.h>
// Process resolution: src-port -> PID lookups and the PID cache.
@@ -184,6 +185,29 @@ DWORD get_process_id_from_udp_connection_v6(const UINT8 src_ip6[16], UINT16 src_
return pid; // buffer is reused, not freed
}
+// Basename of a running process from a toolhelp snapshot. Used when the process cannot be
+// opened (access denied, protected process) so the rule engine can still match it by name
+// instead of letting the connection fall through as DIRECT.
+static BOOL process_basename_from_snapshot(DWORD pid, char *name, DWORD name_size)
+{
+ HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+ if (snap == INVALID_HANDLE_VALUE) return FALSE;
+ PROCESSENTRY32W pe; pe.dwSize = sizeof(pe);
+ BOOL found = FALSE;
+ if (Process32FirstW(snap, &pe))
+ {
+ do {
+ if (pe.th32ProcessID == pid)
+ {
+ found = WideCharToMultiByte(CP_UTF8, 0, pe.szExeFile, -1, name, (int)name_size, NULL, NULL) > 0;
+ break;
+ }
+ } while (Process32NextW(snap, &pe));
+ }
+ CloseHandle(snap);
+ return found;
+}
+
BOOL get_process_name_from_pid(DWORD pid, char *name, DWORD name_size)
{
HANDLE hProcess;
@@ -206,7 +230,7 @@ BOOL get_process_name_from_pid(DWORD pid, char *name, DWORD name_size)
hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
if (hProcess == NULL)
{
- return FALSE;
+ return process_basename_from_snapshot(pid, name, name_size);
}
if (QueryFullProcessImageNameW(hProcess, 0, full_path_w, &path_len))
@@ -218,7 +242,7 @@ BOOL get_process_name_from_pid(DWORD pid, char *name, DWORD name_size)
}
CloseHandle(hProcess);
- return FALSE;
+ return process_basename_from_snapshot(pid, name, name_size);
}
// cache pid
diff --git a/Windows/src/pb_rules.c b/Windows/src/pb_rules.c
index 1131667..e6aa78b 100644
--- a/Windows/src/pb_rules.c
+++ b/Windows/src/pb_rules.c
@@ -18,6 +18,26 @@ BOOL is_ipv6_multicast_or_linklocal(const UINT8 ip6[16])
return FALSE;
}
+// A just-created outbound TCP connection can be missing from the owner-PID table for a moment
+// when the SYN reaches us. Treating that as "no owner" sends the connection DIRECT, unlogged
+// and unproxied, so retry briefly. The capture layer only sees traffic to/from this host (no
+// forwarded/NAT traffic), so a persistent miss is rare and the worst case is ~1.5 ms once.
+static DWORD retry_tcp_pid(DWORD pid, BOOL ipv6, UINT32 src_ip, const UINT8 *src_ip6, UINT16 src_port)
+{
+ LARGE_INTEGER freq, t0, now;
+ QueryPerformanceFrequency(&freq);
+ for (int attempt = 0; attempt < 3 && pid == 0; attempt++)
+ {
+ SwitchToThread();
+ QueryPerformanceCounter(&t0);
+ do { YieldProcessor(); QueryPerformanceCounter(&now); }
+ while ((now.QuadPart - t0.QuadPart) * 1000000 / freq.QuadPart < 500);
+ pid = ipv6 ? get_process_id_from_connection_v6(src_ip6, src_port)
+ : get_process_id_from_connection(src_ip, src_port);
+ }
+ return pid;
+}
+
RuleAction check_process_rule_v6(const UINT8 src_ip6[16], UINT16 src_port, const UINT8 dest_ip6[16], UINT16 dest_port, BOOL is_udp, DWORD *out_pid, UINT32 *out_proxy_config_id)
{
DWORD pid;
@@ -25,6 +45,8 @@ RuleAction check_process_rule_v6(const UINT8 src_ip6[16], UINT16 src_port, const
pid = is_udp ? get_process_id_from_udp_connection_v6(src_ip6, src_port)
: get_process_id_from_connection_v6(src_ip6, src_port);
+ if (pid == 0 && !is_udp)
+ pid = retry_tcp_pid(pid, TRUE, 0, src_ip6, src_port);
if (out_pid) *out_pid = pid;
if (pid == 0) return RULE_ACTION_DIRECT;
if (pid == g_current_process_id) return RULE_ACTION_DIRECT;
@@ -719,6 +741,8 @@ RuleAction check_process_rule(UINT32 src_ip, UINT16 src_port, UINT32 dest_ip, UI
pid = is_udp ? get_process_id_from_udp_connection(src_ip, src_port) : get_process_id_from_connection(src_ip, src_port);
if (pid == 0 && is_udp)
pid = get_process_id_from_connection(src_ip, src_port);
+ if (pid == 0 && !is_udp)
+ pid = retry_tcp_pid(pid, FALSE, src_ip, NULL, src_port);
// this may cause issues - need to find alternative
if (out_pid != NULL)