iptables / ip6tables (2/3)
Match extensions loaded with -m add extra conditions to an iptables rule. This part covers the rate matches (limit, hashlimit, recent), statistic, icmp and hl run in a real lab, and the remaining matches from string and set to owner, policy and the IPv6 header matches.
98.1 How a match extension works
- A rule = matches + one target, e.g. -p tcp --dport 22 ... -j DROP.
- -m name loads an extension match that adds an extra condition, e.g. -m statistic --mode nth.
- Linux only; ip6tables uses the same syntax for IPv6 (iptables-nft or legacy).
- Every match needs an xt_* kernel module (statistic -> xt_statistic), so check that your kernel has it.
- Step 1
1.Client 192.168.118.20 sends ping 1 (ICMP type 8) to the server
- Step 2
2.INPUT rule checks basic matches first: -p icmp --icmp-type 8
- Step 3
3.Then the extension match: -m statistic --mode nth --every 3
- Step 4
4.Count 1 of 3 matches: DROP at the firewall
- Step 5
5.Ping 2: count does not match, packet passes and the reply comes back
98.2 Rate matches: limit vs hashlimit vs recent
- Step 1
1.Attacker .66 floods the web server; a normal user .20 opens the site
- Step 2
2.-m limit: one bucket for the whole rule
- Step 3
3.The flood eats the bucket, so the normal user is dropped too
- Step 4
4.-m hashlimit --hashlimit-mode srcip: one bucket per source IP
- Step 5
5.Attacker's bucket empties and is dropped; the user's packets reach the server
| Match | How it counts | Typical use |
|---|---|---|
| -m limit | One global token bucket (default 3/hour, burst 5) | Keeping LOG rules under control |
| -m hashlimit | A bucket per srcip / dstip / port group | Per-IP rate limiting |
| -m recent | Dynamic IP list + seconds + hitcount | SSH brute force: drop after 4 new connections in 60 s |
| -m quota | Matches until a byte counter reaches 0 | Data caps |
| -m rateest | Live rate from a RATEEST estimator (bps/pps lt/gt/eq) | Rate-based decisions |
--hashlimit-name N table ka naam--hashlimit-upto R rate <= R pe match--hashlimit-above R rate > R pe match R = 50/sec, 3/min, 512kb/s (b = bytes)--hashlimit-burst N initial burst (5)--hashlimit-mode M srcip,dstip,srcport--hashlimit-srcmask P source /P groups--hashlimit-dstmask P dest /P groups--hashlimit-htable-size N buckets--hashlimit-htable-max N max entries--hashlimit-htable-expire ms expiry--hashlimit-htable-gcinterval ms GC--hashlimit-rate-match sirf classify--hashlimit-rate-interval s sampling- recent: --set adds the source IP to the list, --rcheck only checks, --update checks and refreshes the time, --remove deletes.
- --seconds N + --hitcount N make the classic brute-force rule; --rsource/--rdest, --mask, --reap, --rttl refine it.
- /proc/net/xt_recent/LIST: echo +IP, -IP or / to add, delete or flush by hand.
98.3 Lab: which matches the kernel has
grep -oE '^xt_[a-z]+' /proc/moduleszcat /proc/config.gz | grep -E \ 'MATCH_(HL|LIMIT|RECENT)='$ grep -oE '^xt_[a-z]+' /proc/modulesxt_statisticxt_natxt_conntrackxt_setxt_addrtype$ zcat /proc/config.gz | grep -E ...CONFIG_NETFILTER_XT_MATCH_HL=yCONFIG_NETFILTER_XT_MATCH_LIMIT=mCONFIG_NETFILTER_XT_MATCH_RECENT=mCONFIG_IP6_NF_MATCH_HL=m- Lab: Docker network n098-lan, server 192.168.118.10, client 192.168.118.20 (kernel 6.18 WSL2).
- =y is built in, =m is a module; limit and recent were modules not loaded, so only statistic, icmp and hl were run.
98.4 -m statistic (nth and random) + -m icmp
# srv 192.168.118.10iptables -A INPUT -p icmp \ --icmp-type echo-request \ -m statistic --mode nth \ --every 3 --packet 0 -j DROP# cli 192.168.118.20ping -c 6 -i 0.3 192.168.118.10# srviptables -S INPUT$ ping -c 6 -i 0.3 192.168.118.1064 bytes from 192.168.118.10: icmp_seq=2 ttl=64 time=0.131 ms64 bytes from 192.168.118.10: icmp_seq=3 ttl=64 time=0.135 ms64 bytes from 192.168.118.10: icmp_seq=5 ttl=64 time=0.134 ms64 bytes from 192.168.118.10: icmp_seq=6 ttl=64 time=0.107 ms6 packets transmitted, 4 received, 33.3333% packet loss, time 1580ms$ iptables -S INPUT-P INPUT ACCEPT-A INPUT -p icmp -m icmp --icmp-type 8 -m statistic --mode nth --every 3 --packet 0 -j DROP- nth --every 3 --packet 0 dropped seq 1 and 4: 33% loss.
- The name echo-request is saved as the number type 8.
iptables -R INPUT 1 -p icmp \ --icmp-type 8 -m statistic \ --mode random --probability 0.5 \ -j DROPping -c 20 -i 0.2 -q 192.168.118.10iptables -S INPUT -v$ ping -c 20 -i 0.2 -q 192.168.118.10--- 192.168.118.10 ping statistics ---20 packets transmitted, 8 received, 60% packet loss, time 3873ms$ iptables -S INPUT -v-P INPUT ACCEPT -c 12 1008-A INPUT -p icmp -m icmp --icmp-type 8 -m statistic --mode random --probability 0.50000000000 -c 12 1008 -j DROP- random --probability 0.5: each packet has a 50% chance; 12 of 20 dropped in this run (varies per run). Useful to simulate packet loss in tests.
- icmp match: --icmp-type echo-request (name), 8 (number) or 3/4 (type/code = fragmentation needed); iptables -p icmp -h lists valid names; ! inverts.
98.5 -m hl (IPv6 hop limit)
ip6tables -A OUTPUT -o lo \ -p ipv6-icmp -m hl --hl-eq 1 \ -j DROPping -6 -c 2 -t 1 ::1ping -6 -c 2 ::1ip6tables -S OUTPUT -v# also: --hl-lt N --hl-gt N$ ping -6 -c 2 -t 1 ::1--- ::1 ping statistics ---2 packets transmitted, 0 received, 100% packet loss, time 1010ms$ ping -6 -c 2 ::164 bytes from ::1: icmp_seq=1 ttl=64 time=0.066 ms64 bytes from ::1: icmp_seq=2 ttl=64 time=0.069 ms2 packets transmitted, 2 received, 0% packet loss, time 1016ms$ ip6tables -S OUTPUT -v-P OUTPUT ACCEPT -c 4 416-A OUTPUT -o lo -p ipv6-icmp -m hl --hl-eq 1 -c 2 208 -j DROPIn IPv6 the TTL is called hop limit: pings with -t 1 were dropped, the normal hop limit 64 pings passed.
98.6 Remaining matches
| Group | Matches | What they check |
|---|---|---|
| Address / port | iprange, multiport, mac, mark, length, pkttype | IP range without a subnet, up to 15 ports, source MAC, nfmark, packet size, unicast/broadcast/multicast |
| Lists / state | set, nfacct, realm, state, helper | ipset lookup (fast for thousands of IPs), accounting, routing realm, old conntrack subset, FTP-related packets |
| IPv6 headers | hbh, icmp6, ipv6header, mh, rt | Hop-by-hop, ICMPv6 type, which extension headers exist (--soft = any one), mobility, routing header |
| IPsec | policy | --dir in/out, --pol ipsec/none, --strict with --proto esp, --mode tunnel, tunnel src/dst |
| Load balancer / OS | ipvs, osf, socket | IPVS VIP/port/method, passive OS guess from SYN, open local socket (transparent proxy) |
| Host | owner, physdev, rpfilter | User/group that created the packet (OUTPUT only), bridge ports, reverse path check against spoofing |
| Payload | string, sctp | Text/hex pattern (--algo bm|kmp, --from/--to, --icase), SCTP ports and chunk types |
-m iprange --src-range A-B source range-m multiport --dports L dest ports-m set --match-set NAME src ipset lookup-m icmp6 --icmpv6-type NAME | 128[/code]-m ipv6header --header LIST exactly ye --soft koi bhi ek-m owner --uid-owner USER (sirf OUTPUT)-m rpfilter reverse path-m string --algo bm|kmp search algo --string 'GET /' text pattern --hex-string '|0D 0A|' hex pattern98.7 Common mistakes
- Using plain -m limit for per-IP protection: one bucket for everyone, so an attacker starves real users.
- Relying on -m string to block website content: HTTPS hides the text, and bm can miss a pattern across fragments. Never a main control.
- Blocking all ICMPv6 with icmp6: neighbour solicitation is required in IPv6.
- Using a match whose xt_* module is not available in the kernel.
- Using -m owner outside OUTPUT: it only works for locally created packets.
98.8 Interview tips
- limit = one global token bucket; hashlimit = a bucket per srcip/dstip/port; recent = IP list + seconds + hitcount.
- SSH brute force per IP: use recent or hashlimit, not limit.
- statistic nth/random is handy to simulate packet loss.
- For thousands of IPs use -m set with ipset instead of one rule per IP.
- rpfilter drops spoofed source IPs; policy matches IPsec traffic.