Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
sshuttle
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
pub
sshuttle
Commits
2e1beefc
Commit
2e1beefc
authored
May 27, 2016
by
Huiqiang Liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Hack pf to enable multiple instances in Mac OS X 10.10 and above
parent
5a20783b
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
31 deletions
+44
-31
pf.py
sshuttle/methods/pf.py
+38
-25
test_methods_pf.py
sshuttle/tests/client/test_methods_pf.py
+6
-6
No files found.
sshuttle/methods/pf.py
View file @
2e1beefc
...
@@ -59,7 +59,8 @@ class Generic(object):
...
@@ -59,7 +59,8 @@ class Generic(object):
pfctl
(
'-e'
)
pfctl
(
'-e'
)
_pf_context
[
'started_by_sshuttle'
]
=
True
_pf_context
[
'started_by_sshuttle'
]
=
True
def
disable
(
self
):
def
disable
(
self
,
anchor
):
pfctl
(
'-a
%
s -F all'
%
anchor
)
if
_pf_context
[
'started_by_sshuttle'
]:
if
_pf_context
[
'started_by_sshuttle'
]:
pfctl
(
'-d'
)
pfctl
(
'-d'
)
...
@@ -96,12 +97,12 @@ class Generic(object):
...
@@ -96,12 +97,12 @@ class Generic(object):
def
_get_natlook_port
(
self
,
xport
):
def
_get_natlook_port
(
self
,
xport
):
return
xport
return
xport
def
add_anchors
(
self
,
status
=
None
):
def
add_anchors
(
self
,
anchor
,
status
=
None
):
if
status
is
None
:
if
status
is
None
:
status
=
pfctl
(
'-s all'
)[
0
]
status
=
pfctl
(
'-s all'
)[
0
]
self
.
status
=
status
self
.
status
=
status
if
b
'
\n
anchor "sshuttle"'
not
in
status
:
if
(
'
\n
anchor "
%
s"'
%
anchor
)
.
encode
(
'ASCII'
)
not
in
status
:
self
.
_add_anchor_rule
(
self
.
PF_PASS
,
b
"sshuttle"
)
self
.
_add_anchor_rule
(
self
.
PF_PASS
,
anchor
.
encode
(
'ASCII'
)
)
def
_add_anchor_rule
(
self
,
type
,
name
,
pr
=
None
):
def
_add_anchor_rule
(
self
,
type
,
name
,
pr
=
None
):
if
pr
is
None
:
if
pr
is
None
:
...
@@ -120,10 +121,19 @@ class Generic(object):
...
@@ -120,10 +121,19 @@ class Generic(object):
'I'
,
self
.
PF_CHANGE_ADD_TAIL
),
4
)
# action = PF_CHANGE_ADD_TAIL
'I'
,
self
.
PF_CHANGE_ADD_TAIL
),
4
)
# action = PF_CHANGE_ADD_TAIL
ioctl
(
pf_get_dev
(),
pf
.
DIOCCHANGERULE
,
pr
)
ioctl
(
pf_get_dev
(),
pf
.
DIOCCHANGERULE
,
pr
)
def
add_rules
(
self
,
rules
):
def
add_rules
(
self
,
anchor
,
rules
):
assert
isinstance
(
rules
,
bytes
)
assert
isinstance
(
rules
,
bytes
)
debug3
(
"rules:
\n
"
+
rules
.
decode
(
"ASCII"
))
debug3
(
"rules:
\n
"
+
rules
.
decode
(
"ASCII"
))
pfctl
(
'-a sshuttle -f /dev/stdin'
,
rules
)
pfctl
(
'-a
%
s -f /dev/stdin'
%
anchor
,
rules
)
def
has_running_instances
(
self
):
# This should cover most scenarios.
p
=
ssubprocess
.
Popen
([
'pgrep'
,
'-f'
,
'python.*sshuttle'
],
stdout
=
ssubprocess
.
PIPE
,
stderr
=
ssubprocess
.
PIPE
)
o
,
e
=
p
.
communicate
()
return
len
(
o
.
splitlines
())
>
0
class
FreeBsd
(
Generic
):
class
FreeBsd
(
Generic
):
...
@@ -153,11 +163,11 @@ class FreeBsd(Generic):
...
@@ -153,11 +163,11 @@ class FreeBsd(Generic):
def
__init__
(
self
):
def
__init__
(
self
):
super
(
FreeBsd
,
self
)
.
__init__
()
super
(
FreeBsd
,
self
)
.
__init__
()
def
add_anchors
(
self
):
def
add_anchors
(
self
,
anchor
):
status
=
pfctl
(
'-s all'
)[
0
]
status
=
pfctl
(
'-s all'
)[
0
]
if
b
'
\n
rdr-anchor "sshuttle"'
not
in
status
:
if
(
'
\n
rdr-anchor "
%
s"'
%
anchor
)
.
encode
(
'ASCII'
)
not
in
status
:
self
.
_add_anchor_rule
(
self
.
PF_RDR
,
b
'sshuttle'
)
self
.
_add_anchor_rule
(
self
.
PF_RDR
,
anchor
.
encode
(
'ASCII'
)
)
super
(
FreeBsd
,
self
)
.
add_anchors
(
status
=
status
)
super
(
FreeBsd
,
self
)
.
add_anchors
(
anchor
,
status
=
status
)
def
_add_anchor_rule
(
self
,
type
,
name
):
def
_add_anchor_rule
(
self
,
type
,
name
):
pr
=
self
.
pfioc_rule
()
pr
=
self
.
pfioc_rule
()
...
@@ -168,7 +178,7 @@ class FreeBsd(Generic):
...
@@ -168,7 +178,7 @@ class FreeBsd(Generic):
memmove
(
addressof
(
pr
)
+
self
.
POOL_TICKET_OFFSET
,
ppa
[
4
:
8
],
4
)
memmove
(
addressof
(
pr
)
+
self
.
POOL_TICKET_OFFSET
,
ppa
[
4
:
8
],
4
)
super
(
FreeBsd
,
self
)
.
_add_anchor_rule
(
type
,
name
,
pr
=
pr
)
super
(
FreeBsd
,
self
)
.
_add_anchor_rule
(
type
,
name
,
pr
=
pr
)
def
add_rules
(
self
,
includes
,
port
,
dnsport
,
nslist
):
def
add_rules
(
self
,
anchor
,
includes
,
port
,
dnsport
,
nslist
):
tables
=
[
tables
=
[
b
'table <forward_subnets> {
%
s}'
%
b
','
.
join
(
includes
)
b
'table <forward_subnets> {
%
s}'
%
b
','
.
join
(
includes
)
]
]
...
@@ -195,7 +205,7 @@ class FreeBsd(Generic):
...
@@ -195,7 +205,7 @@ class FreeBsd(Generic):
rules
=
b
'
\n
'
.
join
(
tables
+
translating_rules
+
filtering_rules
)
\
rules
=
b
'
\n
'
.
join
(
tables
+
translating_rules
+
filtering_rules
)
\
+
b
'
\n
'
+
b
'
\n
'
super
(
FreeBsd
,
self
)
.
add_rules
(
rules
)
super
(
FreeBsd
,
self
)
.
add_rules
(
anchor
,
rules
)
class
OpenBsd
(
Generic
):
class
OpenBsd
(
Generic
):
...
@@ -225,14 +235,15 @@ class OpenBsd(Generic):
...
@@ -225,14 +235,15 @@ class OpenBsd(Generic):
self
.
pfioc_natlook
=
pfioc_natlook
self
.
pfioc_natlook
=
pfioc_natlook
super
(
OpenBsd
,
self
)
.
__init__
()
super
(
OpenBsd
,
self
)
.
__init__
()
def
add_anchors
(
self
):
def
add_anchors
(
self
,
anchor
):
# before adding anchors and rules we must override the skip lo
# before adding anchors and rules we must override the skip lo
# that comes by default in openbsd pf.conf so the rules we will add,
# that comes by default in openbsd pf.conf so the rules we will add,
# which rely on translating/filtering packets on lo, can work
# which rely on translating/filtering packets on lo, can work
if
not
self
.
has_running_instances
():
pfctl
(
'-f /dev/stdin'
,
b
'match on lo
\n
'
)
pfctl
(
'-f /dev/stdin'
,
b
'match on lo
\n
'
)
super
(
OpenBsd
,
self
)
.
add_anchors
()
super
(
OpenBsd
,
self
)
.
add_anchors
(
anchor
)
def
add_rules
(
self
,
includes
,
port
,
dnsport
,
nslist
):
def
add_rules
(
self
,
anchor
,
includes
,
port
,
dnsport
,
nslist
):
tables
=
[
tables
=
[
b
'table <forward_subnets> {
%
s}'
%
b
','
.
join
(
includes
)
b
'table <forward_subnets> {
%
s}'
%
b
','
.
join
(
includes
)
]
]
...
@@ -259,7 +270,7 @@ class OpenBsd(Generic):
...
@@ -259,7 +270,7 @@ class OpenBsd(Generic):
rules
=
b
'
\n
'
.
join
(
tables
+
translating_rules
+
filtering_rules
)
\
rules
=
b
'
\n
'
.
join
(
tables
+
translating_rules
+
filtering_rules
)
\
+
b
'
\n
'
+
b
'
\n
'
super
(
OpenBsd
,
self
)
.
add_rules
(
rules
)
super
(
OpenBsd
,
self
)
.
add_rules
(
anchor
,
rules
)
class
Darwin
(
FreeBsd
):
class
Darwin
(
FreeBsd
):
...
@@ -295,16 +306,18 @@ class Darwin(FreeBsd):
...
@@ -295,16 +306,18 @@ class Darwin(FreeBsd):
_pf_context
[
'Xtoken'
]
=
\
_pf_context
[
'Xtoken'
]
=
\
re
.
search
(
b
'Token : (.+)'
,
o
[
1
])
.
group
(
1
)
re
.
search
(
b
'Token : (.+)'
,
o
[
1
])
.
group
(
1
)
def
disable
(
self
):
def
disable
(
self
,
anchor
):
pfctl
(
'-a
%
s -F all'
%
anchor
)
if
_pf_context
[
'Xtoken'
]
is
not
None
:
if
_pf_context
[
'Xtoken'
]
is
not
None
:
pfctl
(
'-X
%
s'
%
_pf_context
[
'Xtoken'
]
.
decode
(
"ASCII"
))
pfctl
(
'-X
%
s'
%
_pf_context
[
'Xtoken'
]
.
decode
(
"ASCII"
))
def
add_anchors
(
self
):
def
add_anchors
(
self
,
anchor
):
# before adding anchors and rules we must override the skip lo
# before adding anchors and rules we must override the skip lo
# that in some cases ends up in the chain so the rules we will add,
# that in some cases ends up in the chain so the rules we will add,
# which rely on translating/filtering packets on lo, can work
# which rely on translating/filtering packets on lo, can work
if
not
self
.
has_running_instances
():
pfctl
(
'-f /dev/stdin'
,
b
'pass on lo
\n
'
)
pfctl
(
'-f /dev/stdin'
,
b
'pass on lo
\n
'
)
super
(
Darwin
,
self
)
.
add_anchors
()
super
(
Darwin
,
self
)
.
add_anchors
(
anchor
)
def
_add_natlook_ports
(
self
,
pnl
,
src_port
,
dst_port
):
def
_add_natlook_ports
(
self
,
pnl
,
src_port
,
dst_port
):
pnl
.
sxport
.
port
=
socket
.
htons
(
src_port
)
pnl
.
sxport
.
port
=
socket
.
htons
(
src_port
)
...
@@ -396,8 +409,9 @@ class Method(BaseMethod):
...
@@ -396,8 +409,9 @@ class Method(BaseMethod):
snet
.
encode
(
"ASCII"
),
snet
.
encode
(
"ASCII"
),
swidth
))
swidth
))
pf
.
add_anchors
()
anchor
=
'sshuttle-
%
d'
%
port
pf
.
add_rules
(
includes
,
port
,
dnsport
,
nslist
)
pf
.
add_anchors
(
anchor
)
pf
.
add_rules
(
anchor
,
includes
,
port
,
dnsport
,
nslist
)
pf
.
enable
()
pf
.
enable
()
def
restore_firewall
(
self
,
port
,
family
,
udp
):
def
restore_firewall
(
self
,
port
,
family
,
udp
):
...
@@ -408,8 +422,7 @@ class Method(BaseMethod):
...
@@ -408,8 +422,7 @@ class Method(BaseMethod):
if
udp
:
if
udp
:
raise
Exception
(
"UDP not supported by pf method_name"
)
raise
Exception
(
"UDP not supported by pf method_name"
)
pfctl
(
'-a sshuttle -F all'
)
pf
.
disable
(
'sshuttle-
%
d'
%
port
)
pf
.
disable
()
def
firewall_command
(
self
,
line
):
def
firewall_command
(
self
,
line
):
if
line
.
startswith
(
'QUERY_PF_NAT '
):
if
line
.
startswith
(
'QUERY_PF_NAT '
):
...
...
sshuttle/tests/client/test_methods_pf.py
View file @
2e1beefc
...
@@ -217,7 +217,7 @@ def test_setup_firewall_darwin(mock_pf_get_dev, mock_ioctl, mock_pfctl):
...
@@ -217,7 +217,7 @@ def test_setup_firewall_darwin(mock_pf_get_dev, mock_ioctl, mock_pfctl):
assert
mock_pfctl
.
mock_calls
==
[
assert
mock_pfctl
.
mock_calls
==
[
call
(
'-f /dev/stdin'
,
b
'pass on lo
\n
'
),
call
(
'-f /dev/stdin'
,
b
'pass on lo
\n
'
),
call
(
'-s all'
),
call
(
'-s all'
),
call
(
'-a sshuttle -f /dev/stdin'
,
call
(
'-a sshuttle
-1025
-f /dev/stdin'
,
b
'table <forward_subnets> {!1.2.3.66/32,1.2.3.0/24}
\n
'
b
'table <forward_subnets> {!1.2.3.66/32,1.2.3.0/24}
\n
'
b
'table <dns_servers> {1.2.3.33}
\n
'
b
'table <dns_servers> {1.2.3.33}
\n
'
b
'rdr pass on lo0 proto tcp '
b
'rdr pass on lo0 proto tcp '
...
@@ -237,7 +237,7 @@ def test_setup_firewall_darwin(mock_pf_get_dev, mock_ioctl, mock_pfctl):
...
@@ -237,7 +237,7 @@ def test_setup_firewall_darwin(mock_pf_get_dev, mock_ioctl, mock_pfctl):
method
.
restore_firewall
(
1025
,
2
,
False
)
method
.
restore_firewall
(
1025
,
2
,
False
)
assert
mock_ioctl
.
mock_calls
==
[]
assert
mock_ioctl
.
mock_calls
==
[]
assert
mock_pfctl
.
mock_calls
==
[
assert
mock_pfctl
.
mock_calls
==
[
call
(
'-a sshuttle -F all'
),
call
(
'-a sshuttle
-1025
-F all'
),
call
(
"-X abcdefg"
),
call
(
"-X abcdefg"
),
]
]
mock_pf_get_dev
.
reset_mock
()
mock_pf_get_dev
.
reset_mock
()
...
@@ -298,7 +298,7 @@ def test_setup_firewall_freebsd(mock_pf_get_dev, mock_ioctl, mock_pfctl):
...
@@ -298,7 +298,7 @@ def test_setup_firewall_freebsd(mock_pf_get_dev, mock_ioctl, mock_pfctl):
]
]
assert
mock_pfctl
.
mock_calls
==
[
assert
mock_pfctl
.
mock_calls
==
[
call
(
'-s all'
),
call
(
'-s all'
),
call
(
'-a sshuttle -f /dev/stdin'
,
call
(
'-a sshuttle
-1025
-f /dev/stdin'
,
b
'table <forward_subnets> {!1.2.3.66/32,1.2.3.0/24}
\n
'
b
'table <forward_subnets> {!1.2.3.66/32,1.2.3.0/24}
\n
'
b
'table <dns_servers> {1.2.3.33}
\n
'
b
'table <dns_servers> {1.2.3.33}
\n
'
b
'rdr pass on lo0 proto tcp '
b
'rdr pass on lo0 proto tcp '
...
@@ -318,7 +318,7 @@ def test_setup_firewall_freebsd(mock_pf_get_dev, mock_ioctl, mock_pfctl):
...
@@ -318,7 +318,7 @@ def test_setup_firewall_freebsd(mock_pf_get_dev, mock_ioctl, mock_pfctl):
method
.
restore_firewall
(
1025
,
2
,
False
)
method
.
restore_firewall
(
1025
,
2
,
False
)
assert
mock_ioctl
.
mock_calls
==
[]
assert
mock_ioctl
.
mock_calls
==
[]
assert
mock_pfctl
.
mock_calls
==
[
assert
mock_pfctl
.
mock_calls
==
[
call
(
'-a sshuttle -F all'
),
call
(
'-a sshuttle
-1025
-F all'
),
call
(
"-d"
),
call
(
"-d"
),
]
]
mock_pf_get_dev
.
reset_mock
()
mock_pf_get_dev
.
reset_mock
()
...
@@ -376,7 +376,7 @@ def test_setup_firewall_openbsd(mock_pf_get_dev, mock_ioctl, mock_pfctl):
...
@@ -376,7 +376,7 @@ def test_setup_firewall_openbsd(mock_pf_get_dev, mock_ioctl, mock_pfctl):
assert
mock_pfctl
.
mock_calls
==
[
assert
mock_pfctl
.
mock_calls
==
[
call
(
'-f /dev/stdin'
,
b
'match on lo
\n
'
),
call
(
'-f /dev/stdin'
,
b
'match on lo
\n
'
),
call
(
'-s all'
),
call
(
'-s all'
),
call
(
'-a sshuttle -f /dev/stdin'
,
call
(
'-a sshuttle
-1025
-f /dev/stdin'
,
b
'table <forward_subnets> {!1.2.3.66/32,1.2.3.0/24}
\n
'
b
'table <forward_subnets> {!1.2.3.66/32,1.2.3.0/24}
\n
'
b
'table <dns_servers> {1.2.3.33}
\n
'
b
'table <dns_servers> {1.2.3.33}
\n
'
b
'pass in on lo0 inet proto tcp divert-to 127.0.0.1 port 1025
\n
'
b
'pass in on lo0 inet proto tcp divert-to 127.0.0.1 port 1025
\n
'
...
@@ -395,7 +395,7 @@ def test_setup_firewall_openbsd(mock_pf_get_dev, mock_ioctl, mock_pfctl):
...
@@ -395,7 +395,7 @@ def test_setup_firewall_openbsd(mock_pf_get_dev, mock_ioctl, mock_pfctl):
method
.
restore_firewall
(
1025
,
2
,
False
)
method
.
restore_firewall
(
1025
,
2
,
False
)
assert
mock_ioctl
.
mock_calls
==
[]
assert
mock_ioctl
.
mock_calls
==
[]
assert
mock_pfctl
.
mock_calls
==
[
assert
mock_pfctl
.
mock_calls
==
[
call
(
'-a sshuttle -F all'
),
call
(
'-a sshuttle
-1025
-F all'
),
call
(
"-d"
),
call
(
"-d"
),
]
]
mock_pf_get_dev
.
reset_mock
()
mock_pf_get_dev
.
reset_mock
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment