Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
scapy/__init__.py export-subst
* text=auto
*.sh text eol=lf
*.bat text eol=crlf
89 changes: 60 additions & 29 deletions doc/scapy/_static/vethrelay.sh
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -14,19 +15,31 @@ if [ "$1" != "setup" ] && [ "$1" != "unsetup" ]; then
fi

IFACE="vethrelay"
IP="2.2.2.2"
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

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 !"
Expand All @@ -36,39 +49,57 @@ 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
# 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
# Packets marked with 1 are routed through table 100 instead of the
# default routing table
# 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 "{ 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 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 -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 <mytable>):"
echo
printf '\033[36m nft add rule ip <mytable> 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 "# Listen on wlp4s0 for incoming packets on port 80 (on the interface where it really comes from)"
echo "iptables -A INPUT -i wlp4s0 -p tcp --dport 80 -j ACCEPT"
elif [ "$1" == "unsetup" ]; then
checkSetup
if [ $? -ne 0 ]; then
echo "vethrelay not setup !"
exit 1
fi
# Remove all setup rules
sudo ip rule del fwmark 1 lookup 100
sudo ip route del local 0.0.0.0/0 dev $IFACE table 100
sudo iptables -t mangle -D DIVERT -j ACCEPT
sudo iptables -t mangle -D DIVERT -j MARK --set-mark 1
sudo iptables -t mangle -D PREROUTING -p tcp -m socket -j DIVERT
sudo iptables -t mangle -X DIVERT
sudo ip link del dev $IFACE
# Remove all setup rules by deleting the whole nftables table
ip rule del fwmark 1 lookup 100
ip route del local 0.0.0.0/0 dev $IFACE table 100
nft delete table ip "$NFT_TABLE"
ip link del dev $IFACE
echo -e "\x1b[32mInterface $IFACE unsetup !\x1b[0m"
fi
2 changes: 1 addition & 1 deletion doc/scapy/advanced_usage/fwdmachine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ initial destinations afterwards. More details on how to set up TPROXY are provid
# Run it
NOPFwdMachine(
mode=ForwardMachine.MODE.TPROXY,
port=80,
port=8080,
cls=HTTP, # we specify the class of the payload we are receiving
).run()

Expand Down
2 changes: 2 additions & 0 deletions scapy/base_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ def __contains__(self, other):
if isinstance(other, int):
return self.start <= other <= self.stop
if isinstance(other, str):
if other == "%":
return isinstance(self, (Net, _ScopedIP)) and bool(self.scope)
return self.__class__(other) in self
if type(other) is not self.__class__:
return False
Expand Down
104 changes: 73 additions & 31 deletions scapy/fwdmachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from scapy.asn1.asn1 import ASN1_OID
from scapy.config import conf
from scapy.consts import LINUX
from scapy.data import MTU
from scapy.packet import Packet
from scapy.supersocket import StreamSocket, StreamSocketPeekless
Expand All @@ -27,6 +28,7 @@

from scapy.layers.tls.all import (
Cert,
CertTree,
PrivKeyECDSA,
)
from scapy.layers.x509 import (
Expand Down Expand Up @@ -77,8 +79,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
Expand Down Expand Up @@ -115,6 +118,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,
Expand Down Expand Up @@ -143,18 +147,14 @@ def __init__(
self.sockcls = StreamSocketPeekless
else:
self.sockcls = StreamSocket
# 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)
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)
Expand All @@ -167,6 +167,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 and False:
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))
Expand Down Expand Up @@ -237,10 +243,21 @@ class CONTEXT:
CONTEXT object kept during a session
"""

def __init__(self, fwdm, addr, dest):
def __init__(self, fwdm, addr, dest, remote_af, proto, tls, cls):
self.addr = addr
self.dest = dest
self.remote_af = remote_af
self.proto = proto
self.tls = tls
self.tls_sni_name = None # Retrieved when receiving a connection
self.cls = cls

@property
def sockcls(self):
if self.tls or self.remote_af == 40: # TLS or VSOCK
return StreamSocketPeekless
else:
return StreamSocket

def vprint(self, evt, ctx, cs, req, rep):
if evt == self.FORWARD:
Expand Down Expand Up @@ -294,7 +311,16 @@ def _getpeersock(self, dest, ctx, server_hostname=None):
"""
Get peer socket
"""
s = socket.socket(self.remote_af, self.proto)
s = socket.socket(ctx.remote_af, ctx.proto)
if (
LINUX
and self.mode == ForwardMachine.MODE.TPROXY
and ctx.remote_af in (socket.AF_INET, socket.AF_INET6)
):
# Here's a trick: we set SO_PRIORITY to 1 so that we can exclude those
# packets from the nftables rules that setup the TPROXY when an OUTPUT
# rule is configured.
s.setsockopt(socket.SOL_SOCKET, socket.SO_PRIORITY, 1)
s.settimeout(self.timeout)
ndest = self.destalias(dest)
if ndest != dest:
Expand All @@ -308,15 +334,21 @@ def gen_alike_chain(self, certs, privkey):
Modify a real certificate chain to be served by our own privatekey
"""
c, certs = certs[0], certs[1:]

# Set SubjectPublicKeyInfo to the one from our private key
c.setSubjectPublicKeyFromPrivateKey(privkey)
c.updateSubjectKeyIdentifier()

if certs:
# Recursive: if there are certificates above this one in the chain, do them
# first.
certs = self.gen_alike_chain(certs, privkey)
c.updateAuthorityKeyIdentifier(certs[0])
else:
# Last certificate of the chain. Make it self-signed
c.tbsCertificate.issuer = c.tbsCertificate.subject
# Set SubjectPublicKeyInfo to the one from our private key
c.setSubjectPublicKeyFromPrivateKey(privkey)
c.updateAuthorityKeyIdentifier(c)

# Filter out extensions that would cause trouble
c.tbsCertificate.serialNumber.val = int(
RandInt()
Expand All @@ -330,8 +362,6 @@ def gen_alike_chain(self, certs, privkey):
"2.5.29.31", # cRLDistributionPoints
"1.3.6.1.5.5.7.1.1", # authorityInfoAccess
"1.3.6.1.4.1.11129.2.4.2", # SCT
"2.5.29.14", # subjectKeyIdentifier
"2.5.29.35", # authorityKeyIdentifier
]
]
# For now, we only provide a RSA private key, so we can only sign with that :/
Expand All @@ -355,27 +385,38 @@ def get_key_and_alike_chain(self, cas, dest, server_name):
return self.cache[ident]
# Parse CAs
certs = [Cert(c.public_bytes()) for c in cas]
# certs = certs[:1]
# XXX - Only get last cert. We shouldn't require this
# but we need to add support for multiple private keys...
chain = CertTree(certs, certs)
certs = [chain.getleaves()[0]]
# Generate Private Key
privkey = PrivKeyECDSA()
# Iterate
certs = self.gen_alike_chain(certs, privkey)
# Build a chain object. This checks that everything is properly signed, and
# re-order the certs.
# chain = Chain(certs, cert0=certs[-1])
self.cache[ident] = privkey, certs
return privkey, certs

def handler(self, sock, addr, dest):
"""
Handler of a client socket
"""
ctx = self.CONTEXT(self, addr, dest) # we have a context object
# we have a context object
ctx = self.CONTEXT(
self,
addr=addr,
dest=dest,
remote_af=self.remote_af,
proto=self.proto,
tls=self.tls,
cls=self.cls,
)
self.newconn(ctx)

# Initialize peer socket
ss = self._getpeersock(dest, ctx)

# Wrap both server and peer sockets in SSL
if self.tls:
if ctx.tls:
# Build client SSL context
if self.no_check_certificate:
clisslcontext = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
Expand Down Expand Up @@ -453,8 +494,8 @@ def cb_sni(sock, server_name, _):
return
ss = _clisock[0]
# Wrap the sockets
sock = self.sockcls(sock, self.cls)
ss = self.sockcls(ss, self.cls)
sock = ctx.sockcls(sock, ctx.cls)
ss = ctx.sockcls(ss, ctx.cls)
sock.streamsession = ss.streamsession
try:
while True:
Expand All @@ -471,8 +512,9 @@ def cb_sni(sock, server_name, _):
# get data
try:
data = thissock.recv(self.MTU)
except EOFError:
raise RuntimeError
except EOFError as ex:
othersock.ins.shutdown(socket.SHUT_RDWR)
raise RuntimeError(str(ex))
if not data:
# Session needs more data
continue
Expand All @@ -497,7 +539,7 @@ def cb_sni(sock, server_name, _):
ctx,
server_hostname=ex.server_hostname,
)
ss = self.sockcls(ss, self.cls)
ss = ctx.sockcls(ss, ctx.cls)
self.vprint(self.REDIRECT_TO, ctx, 1, ctx.dest, ex.dest)
ctx.dest = ex.dest # update context
# Shut the old one.
Expand Down Expand Up @@ -537,8 +579,8 @@ def cb_sni(sock, server_name, _):
traceback.print_exception(ex)
othersock.send(data)
self.vprint(self.FORWARD, ctx, cs, data, None)
except RuntimeError:
print(self.ct.red("%s DISCONNECTED !" % repr(addr)))
except RuntimeError as ex:
print(self.ct.red("%s DISCONNECTED !: %s" % (repr(addr), str(ex))))
self.delconn(ctx)
sock.close()
ss.close()
Loading
Loading