Skip to content

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

Enhance connection tracking to support IPv6 by updating function signatures and logic

AnoF-Cyber9c9dd1d42cfd
diff --git a/Windows/src/ProxyBridge.c b/Windows/src/ProxyBridge.c
index 38963b2..853147d 100644
--- a/Windows/src/ProxyBridge.c
+++ b/Windows/src/ProxyBridge.c
@@ -132,7 +132,7 @@ DWORD WINAPI packet_processor(LPVOID arg)
goto ipv6u_send;
}
- if (is_connection_tracked(sp, TRUE))
+ if (is_connection_tracked(sp, TRUE, TRUE))
{
udp_header->DstPort = htons(LOCAL_UDP_RELAY_PORT);
static const UINT8 _lb6u2[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
@@ -308,13 +308,13 @@ DWORD WINAPI packet_processor(LPVOID arg)
memcpy(ipv6_header->SrcAddr, tmp, 16);
addr.Outbound = FALSE;
}
- if (tcp_header->Fin || tcp_header->Rst) remove_connection(client_sp, FALSE);
+ if (tcp_header->Fin || tcp_header->Rst) remove_connection(client_sp, FALSE, TRUE);
goto ipv6_send;
}
- if (is_connection_tracked(sp, FALSE))
+ if (is_connection_tracked(sp, FALSE, TRUE))
{
- if (tcp_header->Fin || tcp_header->Rst) { remove_connection(sp, FALSE); port_clear(sp); }
+ if (tcp_header->Fin || tcp_header->Rst) { remove_connection(sp, FALSE, TRUE); port_clear(sp); }
tcp_header->DstPort = htons(g_local_relay_port);
static const UINT8 _lb6t[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
@@ -485,7 +485,7 @@ DWORD WINAPI packet_processor(LPVOID arg)
continue;
}
}
- else if (is_connection_tracked(ntohs(udp_header->SrcPort), TRUE))
+ else if (is_connection_tracked(ntohs(udp_header->SrcPort), TRUE, FALSE))
{
UINT16 src_port = ntohs(udp_header->SrcPort);
udp_header->DstPort = htons(LOCAL_UDP_RELAY_PORT);
@@ -708,15 +708,15 @@ DWORD WINAPI packet_processor(LPVOID arg)
}
if (tcp_header->Fin || tcp_header->Rst)
- remove_connection(dst_port, FALSE);
+ remove_connection(dst_port, FALSE, FALSE);
}
- else if (is_connection_tracked(ntohs(tcp_header->SrcPort), FALSE))
+ else if (is_connection_tracked(ntohs(tcp_header->SrcPort), FALSE, FALSE))
{
UINT16 src_port = ntohs(tcp_header->SrcPort);
if (tcp_header->Fin || tcp_header->Rst)
{
- remove_connection(src_port, FALSE);
+ remove_connection(src_port, FALSE, FALSE);
port_clear(src_port);
}
diff --git a/Windows/src/pb_conntrack.c b/Windows/src/pb_conntrack.c
index a68f803..2b7f7f6 100644
--- a/Windows/src/pb_conntrack.c
+++ b/Windows/src/pb_conntrack.c
@@ -43,9 +43,10 @@ void add_connection(UINT16 src_port, BOOL is_udp, UINT32 src_ip, UINT32 dest_ip,
int hash = src_port % CONNECTION_HASH_SIZE;
CONNECTION_INFO *existing = connection_hash_table[hash];
- // Match on (port, protocol): a TCP and a UDP flow may legitimately share a port.
+ // Match on (port, protocol, family): a TCP and a UDP flow - or an IPv4 and an
+ // IPv6 flow - may legitimately share a numeric local port at the same time.
while (existing != NULL) {
- if (existing->src_port == src_port && existing->is_udp == is_udp) {
+ if (existing->src_port == src_port && existing->is_udp == is_udp && !existing->is_ipv6) {
rev_unlink(existing); // dest may change (port reuse) - re-key the reverse index
existing->is_ipv6 = FALSE;
existing->src_ip = src_ip;
@@ -91,7 +92,7 @@ void add_connection_v6(UINT16 src_port, BOOL is_udp, const UINT8 src_ip6[16], co
CONNECTION_INFO *existing = connection_hash_table[hash];
while (existing != NULL) {
- if (existing->src_port == src_port && existing->is_udp == is_udp) {
+ if (existing->src_port == src_port && existing->is_udp == is_udp && existing->is_ipv6) {
rev_unlink(existing); // dest may change (port reuse) - re-key the reverse index
existing->is_ipv6 = TRUE;
memcpy(existing->src_ip6, src_ip6, 16);
@@ -174,7 +175,7 @@ BOOL find_v6_udp_sender(const UINT8 orig_dest_ip6[16], UINT16 orig_dest_port, UI
return found;
}
-BOOL is_connection_tracked(UINT16 src_port, BOOL is_udp)
+BOOL is_connection_tracked(UINT16 src_port, BOOL is_udp, BOOL is_ipv6)
{
BOOL tracked = FALSE;
AcquireSRWLockShared(&lock);
@@ -183,7 +184,7 @@ BOOL is_connection_tracked(UINT16 src_port, BOOL is_udp)
CONNECTION_INFO *conn = connection_hash_table[hash];
while (conn != NULL) {
- if (conn->src_port == src_port && conn->is_udp == is_udp && conn->is_tracked) {
+ if (conn->src_port == src_port && conn->is_udp == is_udp && conn->is_ipv6 == is_ipv6 && conn->is_tracked) {
tracked = TRUE;
break;
}
@@ -204,7 +205,7 @@ BOOL get_connection(UINT16 src_port, BOOL is_udp, UINT32 *dest_ip, UINT16 *dest_
while (conn != NULL)
{
- if (conn->src_port == src_port && conn->is_udp == is_udp)
+ if (conn->src_port == src_port && conn->is_udp == is_udp && !conn->is_ipv6)
{
*dest_ip = conn->orig_dest_ip;
*dest_port = conn->orig_dest_port;
@@ -230,7 +231,7 @@ BOOL get_connection_full(UINT16 src_port, BOOL is_udp, UINT32 *dest_ip, UINT16 *
while (conn != NULL)
{
- if (conn->src_port == src_port && conn->is_udp == is_udp)
+ if (conn->src_port == src_port && conn->is_udp == is_udp && !conn->is_ipv6)
{
*dest_ip = conn->orig_dest_ip;
*dest_port = conn->orig_dest_port;
@@ -257,7 +258,7 @@ UINT32 get_connection_proxy_id(UINT16 src_port, BOOL is_udp)
while (conn != NULL)
{
- if (conn->src_port == src_port && conn->is_udp == is_udp)
+ if (conn->src_port == src_port && conn->is_udp == is_udp && !conn->is_ipv6)
{
proxy_config_id = conn->proxy_config_id;
break;
@@ -269,7 +270,7 @@ UINT32 get_connection_proxy_id(UINT16 src_port, BOOL is_udp)
return proxy_config_id;
}
-void remove_connection(UINT16 src_port, BOOL is_udp)
+void remove_connection(UINT16 src_port, BOOL is_udp, BOOL is_ipv6)
{
AcquireSRWLockExclusive(&lock);
@@ -278,7 +279,7 @@ void remove_connection(UINT16 src_port, BOOL is_udp)
while (*conn_ptr != NULL)
{
- if ((*conn_ptr)->src_port == src_port && (*conn_ptr)->is_udp == is_udp)
+ if ((*conn_ptr)->src_port == src_port && (*conn_ptr)->is_udp == is_udp && (*conn_ptr)->is_ipv6 == is_ipv6)
{
CONNECTION_INFO *to_free = *conn_ptr;
*conn_ptr = (*conn_ptr)->next;
diff --git a/Windows/src/pb_internal.h b/Windows/src/pb_internal.h
index 69e3c41..06806a7 100644
--- a/Windows/src/pb_internal.h
+++ b/Windows/src/pb_internal.h
@@ -331,11 +331,11 @@ void add_connection(UINT16 src_port, BOOL is_udp, UINT32 src_ip, UINT32 dest_ip,
void add_connection_v6(UINT16 src_port, BOOL is_udp, const UINT8 src_ip6[16], const UINT8 dest_ip6[16], UINT16 dest_port, UINT32 proxy_config_id);
BOOL get_connection_full_v6(UINT16 src_port, BOOL is_udp, UINT8 dest_ip6[16], UINT16 *dest_port, UINT32 *proxy_config_id);
BOOL find_v6_udp_sender(const UINT8 orig_dest_ip6[16], UINT16 orig_dest_port, UINT8 src_ip6[16], UINT16 *src_port);
-BOOL is_connection_tracked(UINT16 src_port, BOOL is_udp);
+BOOL is_connection_tracked(UINT16 src_port, BOOL is_udp, BOOL is_ipv6);
BOOL get_connection(UINT16 src_port, BOOL is_udp, UINT32 *dest_ip, UINT16 *dest_port);
BOOL get_connection_full(UINT16 src_port, BOOL is_udp, UINT32 *dest_ip, UINT16 *dest_port, UINT32 *proxy_config_id);
UINT32 get_connection_proxy_id(UINT16 src_port, BOOL is_udp);
-void remove_connection(UINT16 src_port, BOOL is_udp);
+void remove_connection(UINT16 src_port, BOOL is_udp, BOOL is_ipv6);
void cleanup_stale_connections(void);
BOOL is_connection_already_logged(DWORD pid, UINT32 dest_ip, UINT16 dest_port, RuleAction action);
void add_logged_connection(DWORD pid, UINT32 dest_ip, UINT16 dest_port, RuleAction action);