@@ -21,6 +21,7 @@ const (
21
21
type LinuxJail struct {
22
22
config JailConfig
23
23
namespace string
24
+ vethHost string // Host-side veth interface name for iptables rules
24
25
logger * slog.Logger
25
26
}
26
27
@@ -221,6 +222,9 @@ func (l *LinuxJail) setupNetworking() error {
221
222
vethHost := fmt .Sprintf ("veth_h_%s" , uniqueID ) // veth_h_1234567 = 14 chars
222
223
vethNetJail := fmt .Sprintf ("veth_n_%s" , uniqueID ) // veth_n_1234567 = 14 chars
223
224
225
+ // Store veth interface name for iptables rules
226
+ l .vethHost = vethHost
227
+
224
228
cmd := exec .Command ("ip" , "link" , "add" , vethHost , "type" , "veth" , "peer" , "name" , vethNetJail )
225
229
err := cmd .Run ()
226
230
if err != nil {
@@ -306,42 +310,40 @@ options timeout:2 attempts:2
306
310
return nil
307
311
}
308
312
309
- // setupIptables configures iptables rules for traffic redirection
313
+ // setupIptables configures iptables rules for comprehensive TCP traffic interception
310
314
func (l * LinuxJail ) setupIptables () error {
311
315
// Enable IP forwarding
312
316
cmd := exec .Command ("sysctl" , "-w" , "net.ipv4.ip_forward=1" )
313
317
cmd .Run () // Ignore error
314
318
315
- // NAT rules for outgoing traffic
319
+ // NAT rules for outgoing traffic (MASQUERADE for return traffic)
316
320
cmd = exec .Command ("iptables" , "-t" , "nat" , "-A" , "POSTROUTING" , "-s" , "192.168.100.0/24" , "-j" , "MASQUERADE" )
317
321
err := cmd .Run ()
318
322
if err != nil {
319
323
return fmt .Errorf ("failed to add NAT rule: %v" , err )
320
324
}
321
325
322
- // Redirect HTTP traffic to proxy
323
- cmd = exec .Command ("ip" , "netns" , "exec" , l .namespace , "iptables" , "-t" , "nat" , "-A" , "OUTPUT" ,
324
- "-p" , "tcp" , "--dport" , "80" , "-j" , "DNAT" , "--to-destination" , fmt .Sprintf ("192.168.100.1:%d" , l .config .HTTPPort ))
326
+ // COMPREHENSIVE APPROACH: Intercept ALL TCP traffic from namespace
327
+ // Use PREROUTING on host to catch traffic after it exits namespace but before routing
328
+ // This ensures NO TCP traffic can bypass the proxy
329
+ cmd = exec .Command ("iptables" , "-t" , "nat" , "-A" , "PREROUTING" , "-i" , l .vethHost , "-p" , "tcp" , "-j" , "REDIRECT" , "--to-ports" , fmt .Sprintf ("%d" , l .config .HTTPSPort ))
325
330
err = cmd .Run ()
326
331
if err != nil {
327
- return fmt .Errorf ("failed to add HTTP redirect rule: %v" , err )
328
- }
329
-
330
- // Redirect HTTPS traffic to proxy
331
- cmd = exec .Command ("ip" , "netns" , "exec" , l .namespace , "iptables" , "-t" , "nat" , "-A" , "OUTPUT" ,
332
- "-p" , "tcp" , "--dport" , "443" , "-j" , "DNAT" , "--to-destination" , fmt .Sprintf ("192.168.100.1:%d" , l .config .HTTPSPort ))
333
- err = cmd .Run ()
334
- if err != nil {
335
- return fmt .Errorf ("failed to add HTTPS redirect rule: %v" , err )
332
+ return fmt .Errorf ("failed to add comprehensive TCP redirect rule: %v" , err )
336
333
}
337
334
335
+ l .logger .Debug ("Comprehensive TCP jailing enabled" , "interface" , l .vethHost , "proxy_port" , l .config .HTTPSPort )
338
336
return nil
339
337
}
340
338
341
339
// removeIptables removes iptables rules
342
340
func (l * LinuxJail ) removeIptables () error {
341
+ // Remove comprehensive TCP redirect rule
342
+ cmd := exec .Command ("iptables" , "-t" , "nat" , "-D" , "PREROUTING" , "-i" , l .vethHost , "-p" , "tcp" , "-j" , "REDIRECT" , "--to-ports" , fmt .Sprintf ("%d" , l .config .HTTPSPort ))
343
+ cmd .Run () // Ignore errors during cleanup
344
+
343
345
// Remove NAT rule
344
- cmd : = exec .Command ("iptables" , "-t" , "nat" , "-D" , "POSTROUTING" , "-s" , "192.168.100.0/24" , "-j" , "MASQUERADE" )
346
+ cmd = exec .Command ("iptables" , "-t" , "nat" , "-D" , "POSTROUTING" , "-s" , "192.168.100.0/24" , "-j" , "MASQUERADE" )
345
347
cmd .Run () // Ignore errors during cleanup
346
348
347
349
return nil
0 commit comments