Test Your Free /48 Firewall with Scapy Before Attackers Do
Disclosure up front: I run IPYE, a company that leases IP space. Most of this article is practical how-to; IPYE appears where a free /48, LOA, or ROA helps you try the work, with a short call to action at the end. A fir
Disclosure up front: I run IPYE, a company that leases IP space. Most of this article is practical how-to; IPYE appears where a free /48, LOA, or ROA helps you try the work, with a short call to action at the end.
A firewall rule set is a claim about what will happen to packets. Testing it with a browser and ping only checks the happy path. Scapy lets you craft exactly the packets you want, including the awkward ones, and see what comes back. This article builds a small Python test suite with Scapy for a firewall protecting a network numbered from a free IPv6 /48 from IPYE. The examples target 2001:db8:100:10::10, a web server that should accept TCP 443 and ICMPv6 echo, and nothing else. Run these tests only against your own addresses, from a machine outside the firewall.
Setup
pip install scapy
sudo python3
>>> from scapy.all import *
Scapy needs raw sockets, so run it as root or with CAP_NET_RAW. Set conf.verb = 0 to quieten output in scripts.
Basic reachability tests
from scapy.all import IPv6, TCP, ICMPv6EchoRequest, sr1, conf
conf.verb = 0
dst = "2001:db8:100:10::10"
def tcp_state(port):
r = sr1(IPv6(dst=dst)/TCP(dport=port, flags="S"), timeout=2)
if r is None:
return "filtered"
if r.haslayer(TCP) and r[TCP].flags & 0x12 == 0x12:
return "open"
if r.haslayer(TCP) and r[TCP].flags & 0x04:
return "closed"
return r.summary()
print("443:", tcp_state(443))
print("22:", tcp_state(22))
print("8080:", tcp_state(8080))
r = sr1(IPv6(dst=dst)/ICMPv6EchoRequest(), timeout=2)
print("echo:", "reply" if r else "none")
Expected output for this policy: 443 open, 22 and 8080 filtered, and an echo reply. "Closed" for a port that should be blocked means the packet reached the host, which answered with a reset. The firewall is not dropping it.
The kernel on your test machine sees the SYN-ACK for a connection it never opened and sends a reset. That is harmless, but you can suppress it with a local ip6tables or nftables rule if it confuses packet captures.
Extension headers
Extension headers are where IPv6 firewalls differ most. Decide what your policy is, then test it:
from scapy.all import IPv6ExtHdrHopByHop, IPv6ExtHdrDestOpt, IPv6ExtHdrRouting
tests = {
"hop-by-hop": IPv6(dst=dst)/IPv6ExtHdrHopByHop()/TCP(dport=443, flags="S"),
"dest-opts": IPv6(dst=dst)/IPv6ExtHdrDestOpt()/TCP(dport=443, flags="S"),
"routing-0": IPv6(dst=dst)/IPv6ExtHdrRouting(type=0, addresses=["2001:db8:100:10::11"])/TCP(dport=443, flags="S"),
}
for name, pkt in tests.items():
r = sr1(pkt, timeout=2)
print(name, "answered" if r else "no answer")
Routing header type 0 was deprecated by RFC 5095 and should always be dropped. Hop-by-hop and destination options to an allowed port are a policy decision. Many networks drop them at the edge, and that is reasonable for a web server.
Fragments
Attackers sometimes split a packet so the TCP header lands in a second fragment, hoping the firewall only inspects the first. A correctly configured stateful firewall reassembles or drops these:
from scapy.all import IPv6ExtHdrFragment, fragment6, send
big = IPv6(dst=dst)/IPv6ExtHdrFragment()/TCP(dport=22, flags="S")/("X" * 1500)
for frag in fragment6(big, 1280):
send(frag)
Watch on the server with tcpdump -ni eth0 'ip6 and tcp port 22'. Nothing should arrive. On Linux, nftables sees reassembled packets in its usual hooks, because conntrack reassembles before filtering, so rules apply to the whole packet.
Spoofed sources
From outside, send a packet that claims to come from inside your own /48:
r = sr1(IPv6(src="2001:db8:100:20::99", dst=dst)/TCP(dport=443, flags="S"), timeout=2)
You will not see a reply either way, because it would go to the spoofed address. Check the server or firewall log instead. Your edge should drop packets from outside that claim an inside source, and the log should show it.
Check ICMPv6 that must pass
A firewall that drops too much is also broken. Packet-too-big messages are the classic casualty, and you can test that they reach the server by sending one that quotes a fake earlier packet:
from scapy.all import ICMPv6PacketTooBig
inner = IPv6(src=dst, dst="2001:db8:ffff::1")/TCP(sport=443, dport=40000)
send(IPv6(dst=dst)/ICMPv6PacketTooBig(mtu=1280)/inner)
Capture on the server. If your rules accept packet-too-big statelessly, the message arrives, and the server ignores it because the quoted connection does not exist. A stateful firewall may instead classify it as invalid, because no matching connection is tracked. In that case, open a real connection first, for example a long curl download, and quote that connection's addresses and ports in the inner packet.
Make it a regression suite
Wrap the checks in pytest functions that assert the expected state, and run them after every firewall change:
def test_ssh_blocked():
assert tcp_state(22) == "filtered"
def test_https_open():
assert tcp_state(443) == "open"
Store the expected policy beside the tests. When someone changes the firewall, the tests either still pass or tell you exactly which promise broke.
Where IPYE fits
The addresses under test come from the free /48. The routing paperwork for it, the LOA, is in the IPYE customer area, and the prefix's ROA is generated automatically by IPYE on its ARIN or APNIC accounts.
Do this next
Claim a free /48, write the four basic reachability tests for one server, and run them before and after your next firewall change. Questions: [email protected].
About IPYE
IPYE (https://ipye.com) leases IPv4, IPv6 and ASN space with instant setup. Your first IPv6 /48 is free for the first year, a real allocation you can announce on your own ASN, with a LOA from the portal and the ROA created automatically. ASN and region changes apply automatically. IPv4 /24 to /22 leases include LOA, ROA and per-/24 rDNS; WHOIS is editable on IPv6 /44 and larger. Locations: Los Angeles, New York, Ashburn, Salt Lake City, London, Amsterdam, Frankfurt. Claim your free /48: https://my.ipye.com/products/ipv6-rental. Questions: [email protected].
Disclosure: I run IPYE
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.