From 26532bfdd63d55ecf1466e6cda67d880be94862c Mon Sep 17 00:00:00 2001 From: gpotter2 <10530980+gpotter2@users.noreply.github.com> Date: Tue, 18 Aug 2026 10:28:59 +0200 Subject: [PATCH 1/3] fwdm: improve TLS handling AI-Assisted: no --- doc/scapy/_static/vethrelay.sh | 49 ++++++++++++---------- scapy/base_classes.py | 2 + scapy/fwdmachine.py | 70 +++++++++++++++++++++---------- scapy/layers/http.py | 15 ++++--- scapy/layers/tls/cert.py | 76 +++++++++++++++++++++++++++++++--- scapy/supersocket.py | 4 +- test/regression.uts | 2 + 7 files changed, 161 insertions(+), 57 deletions(-) diff --git a/doc/scapy/_static/vethrelay.sh b/doc/scapy/_static/vethrelay.sh index 0e62f7b4903..f9c6406b794 100755 --- a/doc/scapy/_static/vethrelay.sh +++ b/doc/scapy/_static/vethrelay.sh @@ -1,7 +1,8 @@ #!/bin/bash -# Setup iptables for IP relay by creating an interface configured -# to be the destination of TPROXY rules. +# Setup nftables for IP relay by creating an interface configured +# to be the destination of TPROXY rules. All nft rules live in a +# dedicated 'scapy-tproxy' table. if [ "$EUID" -ne 0 ] then echo "Please run as root" @@ -15,18 +16,19 @@ fi IFACE="vethrelay" IP="2.2.2.2" +NFT_TABLE="scapy-tproxy" # Linux doc about TPROXY and example regarding this: # https://www.kernel.org/doc/Documentation/networking/tproxy.txt # https://powerdns.org/tproxydoc/tproxy.md.html function checkSetup() { - iptables -t mangle -n --list "DIVERT" >/dev/null 2>&1 + nft list table ip "$NFT_TABLE" >/dev/null 2>&1 return $? } if [ "$1" == "setup" ]; then - # Add "DIVERT" chain if it doesn't exist + # Add the scapy-tproxy table if it doesn't exist checkSetup if [ $? -eq 0 ]; then echo "vethrelay already setup !" @@ -37,13 +39,18 @@ if [ "$1" == "setup" ]; then sysctl net.ipv6.conf.$IFACE.disable_ipv6=1 >/dev/null ip link set dev $IFACE up ip addr add dev $IFACE $IP/32 - # Create mangle "DIVERT" chain as an optimisation. -m socket matches - # packets from already established sockets. Those are marked as 1 then - # accepted directly. - iptables -t mangle -N DIVERT - iptables -t mangle -A PREROUTING -p tcp -m socket -j DIVERT - iptables -t mangle -A DIVERT -j MARK --set-mark 1 - iptables -t mangle -A DIVERT -j ACCEPT + # All TPROXY rules live in a dedicated nftables table so that + # iptables-nft (which manages the ip filter/nat/mangle tables) + # is left untouched. + # The DIVERT chain is an optimisation. The socket match catches + # packets from already established sockets. Those are marked as 1 + # then accepted directly so that TPROXY does not run again. + nft "add table ip "$NFT_TABLE"" + nft "add chain ip "$NFT_TABLE" DIVERT" + nft "add chain ip "$NFT_TABLE" PREROUTING { type filter hook prerouting priority mangle; policy accept; }" + nft "add rule ip "$NFT_TABLE" PREROUTING ip protocol tcp socket wildcard 0 jump DIVERT" + nft "add rule ip "$NFT_TABLE" DIVERT meta mark set 0x1" + nft "add rule ip "$NFT_TABLE" DIVERT accept" # Packets marked with 1 are routed through table 100 instead of the # default routing table ip rule add fwmark 1 lookup 100 @@ -52,9 +59,12 @@ if [ "$1" == "setup" ]; then echo -e "\x1b[32mInterface $IFACE is now setup with IPv4: $IP !\x1b[0m\n" echo -e "Add listening rules as follow:\n" echo "# TPROXY incoming TCP packets on port 80 to $IFACE on port 8080" - echo "iptables -t mangle -A PREROUTING -p tcp --dport 80 -j TPROXY --tproxy-mark 0x1/0x1 --on-port 8080 --on-ip $IP" + echo "nft add rule ip "$NFT_TABLE" PREROUTING tcp dport 80 meta mark set 0x1 tproxy ip to $IP:8080 accept" echo - echo "# Listen on wlp4s0 for incoming packets on port 80 (on the interface where it really comes from)" + echo "# Note: you need to allow INPUT on the port that you are adding a listening rule on. For instance, to listen" + echo "# on wlp4s0 for incoming packets on port 80 (on the interface where it really comes from), one can do" + echo "nft add rule ip INPUT iifname Date: Wed, 18 Nov 2026 21:36:19 +0100 Subject: [PATCH 2/3] fwdm: better binding to vethrelay AI-Assisted: no --- .gitattributes | 1 + doc/scapy/_static/vethrelay.sh | 6 ++---- scapy/fwdmachine.py | 32 ++++++++++++++++++++------------ scapy/layers/tls/crypto/h_mac.py | 3 ++- test/regression.uts | 2 +- 5 files changed, 26 insertions(+), 18 deletions(-) diff --git a/.gitattributes b/.gitattributes index 7ddf3f340cb..0250658a1b5 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,3 +1,4 @@ scapy/__init__.py export-subst * text=auto +*.sh text eol=lf *.bat text eol=crlf diff --git a/doc/scapy/_static/vethrelay.sh b/doc/scapy/_static/vethrelay.sh index f9c6406b794..7d37a7e3986 100755 --- a/doc/scapy/_static/vethrelay.sh +++ b/doc/scapy/_static/vethrelay.sh @@ -15,7 +15,6 @@ if [ "$1" != "setup" ] && [ "$1" != "unsetup" ]; then fi IFACE="vethrelay" -IP="2.2.2.2" NFT_TABLE="scapy-tproxy" # Linux doc about TPROXY and example regarding this: @@ -38,7 +37,6 @@ if [ "$1" == "setup" ]; then ip link add dev $IFACE type dummy sysctl net.ipv6.conf.$IFACE.disable_ipv6=1 >/dev/null ip link set dev $IFACE up - ip addr add dev $IFACE $IP/32 # All TPROXY rules live in a dedicated nftables table so that # iptables-nft (which manages the ip filter/nat/mangle tables) # is left untouched. @@ -56,10 +54,10 @@ if [ "$1" == "setup" ]; then ip rule add fwmark 1 lookup 100 # In routing table 100, all IPs are local to 'vethrelay' ip route add local 0.0.0.0/0 dev $IFACE table 100 - echo -e "\x1b[32mInterface $IFACE is now setup with IPv4: $IP !\x1b[0m\n" + echo -e "\x1b[32mInterface $IFACE is now setup !\x1b[0m\n" echo -e "Add listening rules as follow:\n" echo "# TPROXY incoming TCP packets on port 80 to $IFACE on port 8080" - echo "nft add rule ip "$NFT_TABLE" PREROUTING tcp dport 80 meta mark set 0x1 tproxy ip to $IP:8080 accept" + echo "nft add rule ip "$NFT_TABLE" PREROUTING tcp dport 80 meta mark set 0x1 tproxy ip to :8080 accept" echo echo "# Note: you need to allow INPUT on the port that you are adding a listening rule on. For instance, to listen" echo "# on wlp4s0 for incoming packets on port 80 (on the interface where it really comes from), one can do" diff --git a/scapy/fwdmachine.py b/scapy/fwdmachine.py index a3674a2256e..d0c492fcd90 100644 --- a/scapy/fwdmachine.py +++ b/scapy/fwdmachine.py @@ -78,8 +78,9 @@ class ForwardMachine: :param remote_port: the port to use in SERVER mode. (else use 'port') :param remote_af: (optional) if provided, use a different address family to connect to the remote host. - :param bind_address: the IP to bind locally. "0.0.0.0" by default in SERVER mode, - but "2.2.2.2" by default in TPROXY (if you are using the provided + :param bind_address: the IP to bind locally. "0.0.0.0" by default + :param iface: the interface to bind (defaults to ANY in SERVER, + but "vethrelay" by default in TPROXY (if you are using the provided 'vethrelay.sh' script). :param tls: enable TLS (in both the server and client) :param no_check_certificate: with TLS, do not check the certificate of the @@ -116,6 +117,7 @@ def __init__( remote_port: int = None, remote_af: Optional[socket.AddressFamily] = None, bind_address: str = None, + iface: str = None, tls: bool = False, no_check_certificate: bool = False, crtfile: Optional[str] = None, @@ -140,18 +142,18 @@ def __init__( self.MTU = MTU self.remote_address = remote_address self.remote_port = remote_port - # Chose 'bind_address' depending on the mode - self.bind_address = bind_address - if self.bind_address is None: - if self.mode == ForwardMachine.MODE.SERVER: - self.bind_address = "0.0.0.0" - elif self.mode == ForwardMachine.MODE.TPROXY: - self.bind_address = "2.2.2.2" - else: - raise ValueError("Unknown mode :/") - red = lambda z: functools.reduce(lambda x, y: x + y, z) + if self.tls or self.af == 40: # TLS or VSOCK + self.sockcls = StreamSocketPeekless + else: + self.sockcls = StreamSocket + self.bind_address = bind_address or "0.0.0.0" + # Chose 'iface' depending on the mode + if iface is None and self.mode == ForwardMachine.MODE.TPROXY: + iface = "vethrelay" + self.iface = iface # Utils self.ct = DefaultTheme() + red = lambda z: functools.reduce(lambda x, y: x + y, z) self.local_ips = red(red(list(x.ips.values())) for x in conf.ifaces.values()) self.cache = {} super(ForwardMachine, self).__init__(**kwargs) @@ -164,6 +166,12 @@ def run(self): self.ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) if self.mode == ForwardMachine.MODE.TPROXY: self.ssock.setsockopt(socket.SOL_IP, socket.IP_TRANSPARENT, 1) # TPROXY ! + if self.iface is not None: + self.ssock.setsockopt( + socket.SOL_SOCKET, + socket.SO_BINDTODEVICE, + self.iface.encode(), + ) self.ssock.bind((self.bind_address, self.port)) self.ssock.listen(5) print(self.ct.green("Relay server waiting on port %s" % self.port)) diff --git a/scapy/layers/tls/crypto/h_mac.py b/scapy/layers/tls/crypto/h_mac.py index db984dc22a2..3af543a337a 100644 --- a/scapy/layers/tls/crypto/h_mac.py +++ b/scapy/layers/tls/crypto/h_mac.py @@ -8,7 +8,7 @@ HMAC classes. """ -from scapy.config import conf +from scapy.config import conf, crypto_validator from scapy.layers.tls.crypto.hash import _tls_hash_algs if conf.crypto_valid: @@ -61,6 +61,7 @@ class _GenericHMAC(metaclass=_GenericHMACMetaclass): def __init__(self, key=None): self.key = key or b"" + @crypto_validator def digest(self, tbd): if self.key is None: raise HMACError diff --git a/test/regression.uts b/test/regression.uts index a8a727cef2e..233a95f24bf 100644 --- a/test/regression.uts +++ b/test/regression.uts @@ -4257,7 +4257,7 @@ assert Net("1.2.3.0/24") == Net("1.2.3.0", "1.2.3.255") assert hash(Net("1.2.3.0/24")) == hash(Net("1.2.3.0", "1.2.3.255")) -assert "%" in Net("1.1.1.1%eth0") +assert "%" in Net("1.1.1.1", scope=conf.iface) = Net with a scope identifier From 1b0346606faa5ef19f34effbff8804214792e955 Mon Sep 17 00:00:00 2001 From: gpotter2 <10530980+gpotter2@users.noreply.github.com> Date: Thu, 19 Nov 2026 00:17:24 +0100 Subject: [PATCH 3/3] fwdm: better vethrelay.sh, support OUTPUT AI-Assisted: no --- doc/scapy/_static/vethrelay.sh | 60 ++++++++++++++++++------- doc/scapy/advanced_usage/fwdmachine.rst | 2 +- scapy/fwdmachine.py | 12 ++++- 3 files changed, 55 insertions(+), 19 deletions(-) diff --git a/doc/scapy/_static/vethrelay.sh b/doc/scapy/_static/vethrelay.sh index 7d37a7e3986..d39767d62a8 100755 --- a/doc/scapy/_static/vethrelay.sh +++ b/doc/scapy/_static/vethrelay.sh @@ -17,6 +17,18 @@ fi IFACE="vethrelay" NFT_TABLE="scapy-tproxy" +if [ "$1" == "setup" ]; then + if ! modprobe nft_tproxy; then + echo -e "\x1b[31mError: nft_tproxy is required for transparent proxying.\x1b[0m" >&2 + exit 1 + fi + NFT_SOCKET_AVAILABLE=true + if ! modprobe nft_socket; then + echo -e "\x1b[33mWarning: nft_socket is unavailable; skipping the socket-match optimisation.\x1b[0m" >&2 + NFT_SOCKET_AVAILABLE=false + fi +fi + # Linux doc about TPROXY and example regarding this: # https://www.kernel.org/doc/Documentation/networking/tproxy.txt # https://powerdns.org/tproxydoc/tproxy.md.html @@ -43,27 +55,41 @@ if [ "$1" == "setup" ]; then # The DIVERT chain is an optimisation. The socket match catches # packets from already established sockets. Those are marked as 1 # then accepted directly so that TPROXY does not run again. - nft "add table ip "$NFT_TABLE"" - nft "add chain ip "$NFT_TABLE" DIVERT" - nft "add chain ip "$NFT_TABLE" PREROUTING { type filter hook prerouting priority mangle; policy accept; }" - nft "add rule ip "$NFT_TABLE" PREROUTING ip protocol tcp socket wildcard 0 jump DIVERT" - nft "add rule ip "$NFT_TABLE" DIVERT meta mark set 0x1" - nft "add rule ip "$NFT_TABLE" DIVERT accept" - # Packets marked with 1 are routed through table 100 instead of the - # default routing table + nft add table ip "$NFT_TABLE" + nft add chain ip "$NFT_TABLE" DIVERT "{ type filter hook prerouting priority -150; }" + if [ "$NFT_SOCKET_AVAILABLE" == "true" ]; then + nft add rule ip "$NFT_TABLE" DIVERT meta l4proto tcp socket transparent 1 meta mark set 1 accept + fi + # Also add an OUTPUT rule so that we can test from loopback + nft add chain ip "$NFT_TABLE" OUTPUT "{ type route hook output priority mangle; policy accept; }" + # Packets marked with 1 -> use table 100 ip rule add fwmark 1 lookup 100 # In routing table 100, all IPs are local to 'vethrelay' ip route add local 0.0.0.0/0 dev $IFACE table 100 - echo -e "\x1b[32mInterface $IFACE is now setup !\x1b[0m\n" - echo -e "Add listening rules as follow:\n" - echo "# TPROXY incoming TCP packets on port 80 to $IFACE on port 8080" - echo "nft add rule ip "$NFT_TABLE" PREROUTING tcp dport 80 meta mark set 0x1 tproxy ip to :8080 accept" + + echo -e "\n\x1b[32mInterface $IFACE is now setup !\x1b[0m" + echo -e "\n\x1b[1mIncoming (FORWARD) traffic\x1b[0m" + echo " Redirect TCP port 80 to the proxy on port 8080:" + echo + printf '\033[36m nft add rule ip %s DIVERT \\\n tcp dport 80 tproxy to :8080 meta mark set 0x1 accept\033[0m\n' "$NFT_TABLE" + echo + echo -e "\x1b[1mOutgoing traffic (optional)\x1b[0m" + echo " Outgoing traffic also requires the Incoming (FORWARD) rule above." + echo " To redirect outgoing TCP port 80, additionally add:" + echo " Priority 1 excludes the proxy's upstream connections." + echo + printf '\033[36m nft add rule ip %s OUTPUT \\\n meta priority != 0:1 tcp dport 80 meta mark set 0x1\033[0m\n' "$NFT_TABLE" + echo + echo -e "\x1b[1mAllow incoming traffic\x1b[0m" + echo " Allow INPUT on the original destination port and incoming interface." + echo " Example: TCP port 80 arriving on wlp4s0 (replace ):" + echo + printf '\033[36m nft add rule ip INPUT \\\n iifname wlp4s0 tcp dport 80 accept\033[0m\n' + echo + echo " Or, using iptables:" + echo + echo -e "\x1b[36m iptables -A INPUT -i wlp4s0 -p tcp --dport 80 -j ACCEPT\x1b[0m" echo - echo "# Note: you need to allow INPUT on the port that you are adding a listening rule on. For instance, to listen" - echo "# on wlp4s0 for incoming packets on port 80 (on the interface where it really comes from), one can do" - echo "nft add rule ip INPUT iifname