summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJose M Perez <jose.m.perez.ramos+git@gmail.com>2023-10-03 01:27:44 +0200
committerBrian May <brian@linuxpenguins.xyz>2023-10-04 08:11:52 +1100
commit794b14eaacda22edc5bb5c0bc7f71d62f50260af (patch)
treeca5ced93aa4a04d13e818840055d9db9ec12271d
parent670cc363bad1366a270636dbaeb7543d06622337 (diff)
tproxy: Apply DNS rules first
Having --dst-type LOCAL rules before DNS ones forces the usage of a dnsmasq-like program to retrigger DNS requests directed locally because they are fast-tracked through the firewall and ignored by sshuttle. As dns options documentation state that they capture the requests no matter the server, and other methods and older versions behave consistently, change the iptables rules to apply DNS ones first.
-rw-r--r--sshuttle/methods/tproxy.py22
-rw-r--r--tests/client/test_methods_tproxy.py26
2 files changed, 24 insertions, 24 deletions
diff --git a/sshuttle/methods/tproxy.py b/sshuttle/methods/tproxy.py
index b3d5fca..84eea3f 100644
--- a/sshuttle/methods/tproxy.py
+++ b/sshuttle/methods/tproxy.py
@@ -145,8 +145,18 @@ class Method(BaseMethod):
_ipt('-I', 'OUTPUT', '1', '-j', mark_chain)
_ipt('-I', 'PREROUTING', '1', '-j', tproxy_chain)
+ for _, ip in [i for i in nslist if i[0] == family]:
+ _ipt('-A', mark_chain, '-j', 'MARK', '--set-mark', tmark,
+ '--dest', '%s/32' % ip,
+ '-m', 'udp', '-p', 'udp', '--dport', '53')
+ _ipt('-A', tproxy_chain, '-j', 'TPROXY',
+ '--tproxy-mark', tmark,
+ '--dest', '%s/32' % ip,
+ '-m', 'udp', '-p', 'udp', '--dport', '53',
+ '--on-port', str(dnsport))
+
# Don't have packets sent to any of our local IP addresses go
- # through the tproxy or mark chains.
+ # through the tproxy or mark chains (except DNS ones).
#
# Without this fix, if a large subnet is redirected through
# sshuttle (i.e., 0/0), then the user may be unable to receive
@@ -169,16 +179,6 @@ class Method(BaseMethod):
_ipt('-A', tproxy_chain, '-m', 'socket', '-j', divert_chain,
'-m', 'udp', '-p', 'udp')
- for _, ip in [i for i in nslist if i[0] == family]:
- _ipt('-A', mark_chain, '-j', 'MARK', '--set-mark', tmark,
- '--dest', '%s/32' % ip,
- '-m', 'udp', '-p', 'udp', '--dport', '53')
- _ipt('-A', tproxy_chain, '-j', 'TPROXY',
- '--tproxy-mark', tmark,
- '--dest', '%s/32' % ip,
- '-m', 'udp', '-p', 'udp', '--dport', '53',
- '--on-port', str(dnsport))
-
for _, swidth, sexclude, snet, fport, lport \
in sorted(subnets, key=subnet_weight, reverse=True):
tcp_ports = ('-p', 'tcp')
diff --git a/tests/client/test_methods_tproxy.py b/tests/client/test_methods_tproxy.py
index b1e72ff..44184e5 100644
--- a/tests/client/test_methods_tproxy.py
+++ b/tests/client/test_methods_tproxy.py
@@ -123,6 +123,13 @@ def test_setup_firewall(mock_ipt_chain_exists, mock_ipt):
call(AF_INET6, 'mangle', '-I', 'OUTPUT', '1', '-j', 'sshuttle-m-1024'),
call(AF_INET6, 'mangle', '-I', 'PREROUTING', '1', '-j',
'sshuttle-t-1024'),
+ call(AF_INET6, 'mangle', '-A', 'sshuttle-m-1024', '-j', 'MARK',
+ '--set-mark', '0x01', '--dest', u'2404:6800:4004:80c::33/32',
+ '-m', 'udp', '-p', 'udp', '--dport', '53'),
+ call(AF_INET6, 'mangle', '-A', 'sshuttle-t-1024', '-j', 'TPROXY',
+ '--tproxy-mark', '0x01',
+ '--dest', u'2404:6800:4004:80c::33/32',
+ '-m', 'udp', '-p', 'udp', '--dport', '53', '--on-port', '1026'),
call(AF_INET6, 'mangle', '-A', 'sshuttle-t-1024', '-j', 'RETURN',
'-m', 'addrtype', '--dst-type', 'LOCAL'),
call(AF_INET6, 'mangle', '-A', 'sshuttle-m-1024', '-j', 'RETURN',
@@ -134,13 +141,6 @@ def test_setup_firewall(mock_ipt_chain_exists, mock_ipt):
'-j', 'sshuttle-d-1024', '-m', 'tcp', '-p', 'tcp'),
call(AF_INET6, 'mangle', '-A', 'sshuttle-t-1024', '-m', 'socket',
'-j', 'sshuttle-d-1024', '-m', 'udp', '-p', 'udp'),
- call(AF_INET6, 'mangle', '-A', 'sshuttle-m-1024', '-j', 'MARK',
- '--set-mark', '0x01', '--dest', u'2404:6800:4004:80c::33/32',
- '-m', 'udp', '-p', 'udp', '--dport', '53'),
- call(AF_INET6, 'mangle', '-A', 'sshuttle-t-1024', '-j', 'TPROXY',
- '--tproxy-mark', '0x01',
- '--dest', u'2404:6800:4004:80c::33/32',
- '-m', 'udp', '-p', 'udp', '--dport', '53', '--on-port', '1026'),
call(AF_INET6, 'mangle', '-A', 'sshuttle-m-1024', '-j', 'RETURN',
'--dest', u'2404:6800:4004:80c::101f/128',
'-m', 'tcp', '-p', 'tcp', '--dport', '8080:8080'),
@@ -227,6 +227,12 @@ def test_setup_firewall(mock_ipt_chain_exists, mock_ipt):
call(AF_INET, 'mangle', '-I', 'OUTPUT', '1', '-j', 'sshuttle-m-1025'),
call(AF_INET, 'mangle', '-I', 'PREROUTING', '1', '-j',
'sshuttle-t-1025'),
+ call(AF_INET, 'mangle', '-A', 'sshuttle-m-1025', '-j', 'MARK',
+ '--set-mark', '0x01', '--dest', u'1.2.3.33/32',
+ '-m', 'udp', '-p', 'udp', '--dport', '53'),
+ call(AF_INET, 'mangle', '-A', 'sshuttle-t-1025', '-j', 'TPROXY',
+ '--tproxy-mark', '0x01', '--dest', u'1.2.3.33/32',
+ '-m', 'udp', '-p', 'udp', '--dport', '53', '--on-port', '1027'),
call(AF_INET, 'mangle', '-A', 'sshuttle-t-1025', '-j', 'RETURN',
'-m', 'addrtype', '--dst-type', 'LOCAL'),
call(AF_INET, 'mangle', '-A', 'sshuttle-m-1025', '-j', 'RETURN',
@@ -238,12 +244,6 @@ def test_setup_firewall(mock_ipt_chain_exists, mock_ipt):
'-j', 'sshuttle-d-1025', '-m', 'tcp', '-p', 'tcp'),
call(AF_INET, 'mangle', '-A', 'sshuttle-t-1025', '-m', 'socket',
'-j', 'sshuttle-d-1025', '-m', 'udp', '-p', 'udp'),
- call(AF_INET, 'mangle', '-A', 'sshuttle-m-1025', '-j', 'MARK',
- '--set-mark', '0x01', '--dest', u'1.2.3.33/32',
- '-m', 'udp', '-p', 'udp', '--dport', '53'),
- call(AF_INET, 'mangle', '-A', 'sshuttle-t-1025', '-j', 'TPROXY',
- '--tproxy-mark', '0x01', '--dest', u'1.2.3.33/32',
- '-m', 'udp', '-p', 'udp', '--dport', '53', '--on-port', '1027'),
call(AF_INET, 'mangle', '-A', 'sshuttle-m-1025', '-j', 'RETURN',
'--dest', u'1.2.3.66/32', '-m', 'tcp', '-p', 'tcp',
'--dport', '80:80'),