summaryrefslogtreecommitdiffstats
path: root/sshuttle/methods/pf.py
AgeCommit message (Collapse)Author
2023-08-07Add support for group-based routingFata Nugraha
2021-09-22Trim excess whitespacea1346054
2021-07-12Remove ttl hack & require -r option.Scott Kuhl
Previously, it was possible to run sshuttle locally without using ssh and connecting to a remote server. In this configuration, traffic was redirected to the sshuttle server running on the localhost. However, the firewall needed to distinguish between traffic leaving the sshuttle server and traffic that originated from the machine that still needed to be routed through the sshuttle server. The TTL of the packets leaving the sshuttle server were manipulated to indicate to the firewall what should happen. The TTL was adjusted for all packets leaving the sshuttle server (even if it wasn't necessary because the server and client were running on different machines). Changing the TTL caused trouble and some machines, and the --ttl option was added as a workaround to change how the TTL was set for traffic leaving sshuttle. All of this added complexity to the code for a feature (running the server on localhost) that is likely only used for testing and rarely used by others. This commit updates the associated documentation, but doesn't fully fix the ipfw method since I am unable to test that. This change will also make sshuttle fail to work if -r is used to specify a localhost. Pull request #610 partially addresses that issue. For example, see: #240, #490, #660, #606.
2021-06-30Print pfctl error message when it returns non-zero.Scott Kuhl
If pfctl returns non-zero when setting up the firewall, sshuttle exits and indicates the exit status code. This patch makes it so the output of pfctl is also printed so the user can get a better idea of what caused the problem. For example: issue #491
2021-05-27Fix --tmark optionScott Kuhl
Even when --tmark was used, the iptables code always used '1' for the mark. This patch corrects the problem. Previously, it wasn't clear if the tmark should be supplied in hexadecimal or as an integer. This makes it use hexadecimal, checks that the input is hexadecimal, and updates the associated documentation. This patch also makes --ttl information get passed to the firewall in a way that matches how other information gets passed. The ttl and tmark information are passed next to each other in many places and this patch also makes the order consistent.
2021-03-05firewall: Allow overriding the TTLVictor Kareh
In instances where a cluster pod in a local VM needs to access a server that is sshuttle'd from the host, since the packets arriving at the host already made a hop, their TTL is 63 and so get ignored by sshuttle. Allowing an override of the firewall TTL rule allows the packets to go through.
2021-01-01Refactor debug, log and Fatal messages.Scott Kuhl
This commit rewrites the log() function so that it will append a newline at the end of the message if none is present. It doesn't make sense to print a log message without a newline since the next log message (which will write a prefix) expects to be starting at the beginning of a line. Although it isn't strictly necessary, this commit also removes any newlines at the ends of messages. If I missed any, including the newline at the end of the message will continue to work as it did before. Previously, some calls were missing the newline at the end even though including it was necessary for subsequent messages to appear correctly. This code also cleans up some redundant prefixes. The log() method will prepend the prefix and the different processes should set their prefix as soon as they start. Multiline messages are still supported (although the prefix for the additional lines was changed to match the length of the prefix used for the first line).
2020-12-28Refactor automatic method selection.Scott Kuhl
Add an "is_supported()" function to the different methods so that each method can include whatever logic they wish to indicate if they are supported on a particular machine. Previously, methods/__init__.py contained all of the logic for selecting individual methods. Now, it iterates through a list of possible options and stops on the first method that it finds that is_supported(). Currently, the decision is made based on the presence of programs in the PATH. In the future, things such as the platform sshuttle is running on could be considered.
2020-10-23Improve consistency of PATH, environments, and which()Scott Kuhl
This patch attempts to fix (or aid in debugging) issue #350. sshuttle didn't explicitly search /sbin and /usr/sbin and they may be missing in the user's PATH. If PATH is missing, these folders wouldn't be searched either. There was also a program_exists function which is redundant to which(). This consolidates everything into the helpers.py file. This patch introduces get_path() to return PATH + some extra hardcoded paths. A new get_env() function can be called to create a consistent environment when calling external programs. The new which() wrapper function also ensures we use the same set of paths. If -vv is supplied, messages clearly indicate the programs we are looking for, if they are found, and where we looked if we failed to find them. I haven't tested the changes to ipfw or pf.
2020-05-10remove debug message for getpeername failureMark Heiges
2020-05-10fix crash triggered by port scans closing socketMark Heiges
2019-11-09Make hostwatch locale-independent (#379)Ben Wiederhake
* Make hostwatch locale-independent See #377: hostwatch used to call netstat and parse the result, without setting the locale. The problem is converting the binary output to a unicode string, as the locale may be utf-8, latin-1, or literally anything. Setting the locale to C avoids this issue, as netstat's source strings to not use non-ASCII characters. * Break line, check all other invocations
2019-09-22The size of pf_rule grew in OpenBSD 6.4Anthony Cornehl
2019-02-11Fix/pep8 (#277)Bastian Venthur
* re-organized imports according to pep8 * fixed all remaining pep8 issues * moved common config into setup.cfg, additionally test `tests` * removed --select=X -- the errors selected where by default not in flake8's --ignore list so effectively had no effect * update .travis.yml to reflect changes in tox.ini * make travis just use tox in order to avoid code duplaction * replace py.test with pytest * fixed .travis.yml * try different pypy toxenv * hopefully fixed testenv for pypy * added pypy basepython, removed unused python2.6 * install dev package before testing (fixes missing coverage) * fixed empty exception pass blocks with noqa * Added dummy log message on empty try-except-pass blocks to make dodacy happy :( * Replaced Exception with BaseException
2018-11-03Changes pf exclusion rules precedenceJoão Vieira
Before this change, in pf, exclusions used a pass out quick which gave them higher precedence than any other rule independent of subnet width. As reported in #265 this causes exclusion from one instance of sshuttle to also take effect on other instances because quick aborts the evaluation of rules across all anchors. This commit changes the precedence of rules so quick can now be dropped. The new order is defined by the following rule, from subnet_weight: "We need to go from smaller, more specific, port ranges, to larger, less-specific, port ranges. At each level, we order by subnet width, from most-specific subnets (largest swidth) to least-specific. On ties, excludes come first."
2018-10-23Fixes support for OpenBSD (6.1+) (#282)João Vieira
* Fixes support for OpenBSD (6.1+) As reported in #219, new versions of OpenBSD ship with a different pfioc_rule struct. This commit adjusts the offset to match the new struct. * Fixes tests for OpenBSD 6.1+
2017-11-13Fixes some style issues and minor bugsvieira
2017-11-08Changes methods that do not reference the instance to static methodsvieira
2017-10-21Load pf kernel module when enabling pfvieira
When the pf module is not loaded our calls to pfctl will fail with unhelpful messages. This change spares the user the pain of decrypting those messages and manually enabling pf. It also keeps track if pf was loaded by sshuttle and unloads on exit if that was the case. Also fixed the case where both ipv4 and ipv6 anchors were added by sshuttle but the first call of disable would disable pf before the second call had the chance of cleaning it's anchor.
2017-09-17Route traffic by linux usermax
2017-07-29Avoid port forwarding from loopback addressvieira
When doing port forwarding on lo0 avoid the special case where the traffic on lo0 did not came from sshuttle pass out rule but from the lo0 address itself. Fixes #159.
2017-05-07Adds support for tunneling specific port ranges (#144)João Vieira
* Adds support for tunneling specific port ranges This set of changes implements the ability of specifying a port or port range for an IP or subnet to only tunnel those ports for that subnet. Also supports excluding a port or port range for a given IP or subnet. When, for a given subnet, there are intercepting ranges being added and excluded, the most specific, i.e., smaller range, takes precedence. In case of a tie the exclusion wins. For different subnets, the most specific, i.e., largest swidth, takes precedence independent of any eventual port ranges. Examples: Tunnels all traffic to the 188.0.0.0/8 subnet except those to port 443. ``` sshuttle -r <server> 188.0.0.0/8 -x 188.0.0.0/8:443 ``` Only tunnels traffic to port 80 of the 188.0.0.0/8 subnet. ``` sshuttle -r <server> 188.0.0.0/8:80 ``` Tunnels traffic to the 188.0.0.0/8 subnet and the port range that goes from 80 to 89. ``` sshuttle -r <server> 188.0.0.0/8:80-89 -x 188.0.0.0/8:80-90 ``` * Allow subnets to be specified with domain names Simplifies the implementation of address parsing by using socket.getaddrinfo(), which can handle domain resolution, IPv4 and IPv6 addresses. This was proposed and mostly implemented by @DavidBuchanan314 in #146. Signed-off-by: David Buchanan <DavidBuchanan314@users.noreply.github.com> Signed-off-by: João Vieira <vieira@yubo.be> * Also use getaddrinfo for parsing listen addr:port * Fixes tests for tunneling a port range * Updates documentation to include port/port range Adds some examples with subnet:port and subnet:port-port. Also clarifies the versions of Python supported on the server while maintaining the recommendation for Python 2.7, 3.5 or later. Mentions support for pfSense. * In Py2 only named arguments may follow *expression Fixes issue in Python 2.7 where *expression may only be followed by named arguments. * Use right regex to extract ip4/6, mask and ports * Tests for parse_subnetport
2017-01-15Add support for PfSensevieira
PfSense is based on FreeBSD and its pf is pretty close to the one FreeBSD ships, however some structures have different fields and two offsets had to be fixed.
2017-01-09Set started_by_sshuttle False after disabling pfvieira
We set it to true when we enable pf, but do not set it back to False after disabling. When using IPv4 and IPv6 we end up trying to disable twice which procudes an error while undoing changes in FreeBSD 11.
2016-10-04Fix argument splitting for multi-word argumentsFelix Dreissig
By just splitting at spaces, multi-word arguments are torn apart even if quoted. In case of custom ssh-cmd, this makes it practically impossible to set certian options through `ssh -o`. shlex splits arguments like a shell and e.g. respects quotes.
2016-07-27Use == instead of is to compare with AF_INETvieira
2016-07-27Be more specific and consistent in some pf rulesvieira
2016-07-24IPv6 support for BSD and OSXvieira
Adds IPv6 support for OpenBSD and OSX.
2016-06-16Add <forward_subnets> to divert rule in OpenBSDJoão Vieira
Fixes bug where all traffic routed to loopback would end up being diverted to the same port.
2016-06-16Hack pf to enable multiple instances in Mac OS X 10.10 and aboveHuiqiang Liu
2016-04-23Ensure locale is set to C for external commandsBrian May
Otherwise the output can vary and confuse our attempts to parse it. Fixes: 93
2016-03-02Add support for OpenBSDvieira
2016-03-02Override the skip on lo that ends up in the chainvieira
In some cases (see #43) it seems that some network configurations may end up setting a skip on lo. As sshuttle adds rules that rely on filtering/translating packets on lo, this causes problem. This fix overrides the skip and makes the rules be applied again. Should fix at least some of the problems reported on #43.
2016-03-02Refactor OS specific portions of PFvieira
This will make it easier to support other platforms/versions in the future, e.g., OpenBSD.
2016-01-05Adds support for FreeBSD PFvieira
The PF firewall that is included in the FreeBSD base system does not have exactly the same data structures as the OSX version. This commit fixes the offsets and some field types that are also different. Tested with FreeBSD 10.2 and OSX 10.11.2.
2015-12-15Simplify selection of featuresBrian May
2015-12-14Print PF rules used.Brian May
Also support multiline debug output better.
2015-12-13Split setup_firewall method.Brian May
* setup_firewall sets the firewall up. * restore_firewall restores the firewall to initial state.
2015-12-07Fix logging with pf method and Python 3.5Brian May
2015-12-07Fix get_tcp_dstip with MacOSX/Python3.5Brian May
2015-12-06Fix more brokenness.Brian May
2015-12-06Fix another MacOSX/Python3.5 issue.Brian May
2015-12-05Fix more MacOSX/Python3.5 issues.Brian May
2015-12-05Fix MacOSX/Python3.5 issues.Brian May
Closes: #36.
2015-12-05Don't use Xtoken if not setBrian May
2015-12-05Remove reference to obsolete globalBrian May
2015-11-18Avoid hardcoding packed address lengths.Brian May
2015-11-18Fix tests under PyPy.Brian May
2015-11-17Tests for pf method.Brian May
2015-11-16Restructure codeBrian May
Pull out firewall methods code into seperate files. Fix problems starting with method=='auto'; we were making decisions based on the method, before the method had been finalized by the firewall. Only very basic testing so far. What could go wrong?