Skip to content

Commit 97c25e9

Browse files
authored
tproxy: Skip firewall chains if packets have local destination. (sshuttle#578)
If you use the tproxy method with a large subnet (such as 0/0), then (1) you may not receive UDP packets that sshuttle/tproxy can handle and (2) you are unable to connect to your machine using an IP that your computer recognizes as its own. To resolve those issues, any traffic to an IP that the host knows is local, does not go through the sshuttle chains.
1 parent b7730fc commit 97c25e9

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

docs/tproxy.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ There are some things you need to consider for TPROXY to work:
2525
Otherwise sshuttle may attempt to intercept the ssh packets, which will not
2626
work. Use the ``--exclude`` parameter for this.
2727

28-
- Similarly, UDP return packets (including DNS) could get intercepted and
29-
bounced back. This is the case if you have a broad subnet such as
30-
``0.0.0.0/0`` or ``::/0`` that includes the IP address of the client. Use the
31-
``--exclude`` parameter for this.
32-
3328
- You need the ``--method=tproxy`` parameter, as above.
3429

3530
- The routes for the outgoing packets must already exist. For example, if your

sshuttle/methods/tproxy.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,22 @@ def _ipt_proto_ports(proto, fport, lport):
194194
_ipt('-F', tproxy_chain)
195195
_ipt('-I', 'OUTPUT', tmark, '-j', mark_chain)
196196
_ipt('-I', 'PREROUTING', tmark, '-j', tproxy_chain)
197+
198+
# Don't have packets sent to any of our local IP addresses go
199+
# through the tproxy or mark chains.
200+
#
201+
# Without this fix, if a large subnet is redirected through
202+
# sshuttle (i.e., 0/0), then the user may be unable to receive
203+
# UDP responses or connect to their own machine using an IP
204+
# besides (127.0.0.1). Prior to including these lines, the
205+
# documentation reminded the user to use -x to exclude their
206+
# own IP addresses to receive UDP responses if they are
207+
# redirecting a large subnet through sshuttle (i.e., 0/0).
208+
_ipt('-A', tproxy_chain, '-j', 'RETURN', '-m', 'addrtype',
209+
'--dst-type', 'LOCAL')
210+
_ipt('-A', mark_chain, '-j', 'RETURN', '-m', 'addrtype',
211+
'--dst-type', 'LOCAL')
212+
197213
_ipt('-A', divert_chain, '-j', 'MARK', '--set-mark', tmark)
198214
_ipt('-A', divert_chain, '-j', 'ACCEPT')
199215
_ipt('-A', tproxy_chain, '-m', 'socket', '-j', divert_chain,

tests/client/test_methods_tproxy.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ def test_setup_firewall(mock_ipt_chain_exists, mock_ipt_ttl, mock_ipt):
133133
call(AF_INET6, 'mangle', '-I', 'OUTPUT', '1', '-j', 'sshuttle-m-1024'),
134134
call(AF_INET6, 'mangle', '-I', 'PREROUTING', '1', '-j',
135135
'sshuttle-t-1024'),
136+
call(AF_INET6, 'mangle', '-A', 'sshuttle-t-1024', '-j', 'RETURN',
137+
'-m', 'addrtype', '--dst-type', 'LOCAL'),
138+
call(AF_INET6, 'mangle', '-A', 'sshuttle-m-1024', '-j', 'RETURN',
139+
'-m', 'addrtype', '--dst-type', 'LOCAL'),
136140
call(AF_INET6, 'mangle', '-A', 'sshuttle-d-1024', '-j', 'MARK',
137141
'--set-mark', '1'),
138142
call(AF_INET6, 'mangle', '-A', 'sshuttle-d-1024', '-j', 'ACCEPT'),
@@ -233,6 +237,10 @@ def test_setup_firewall(mock_ipt_chain_exists, mock_ipt_ttl, mock_ipt):
233237
call(AF_INET, 'mangle', '-I', 'OUTPUT', '1', '-j', 'sshuttle-m-1025'),
234238
call(AF_INET, 'mangle', '-I', 'PREROUTING', '1', '-j',
235239
'sshuttle-t-1025'),
240+
call(AF_INET, 'mangle', '-A', 'sshuttle-t-1025', '-j', 'RETURN',
241+
'-m', 'addrtype', '--dst-type', 'LOCAL'),
242+
call(AF_INET, 'mangle', '-A', 'sshuttle-m-1025', '-j', 'RETURN',
243+
'-m', 'addrtype', '--dst-type', 'LOCAL'),
236244
call(AF_INET, 'mangle', '-A', 'sshuttle-d-1025',
237245
'-j', 'MARK', '--set-mark', '1'),
238246
call(AF_INET, 'mangle', '-A', 'sshuttle-d-1025', '-j', 'ACCEPT'),

0 commit comments

Comments
 (0)