SkillPilotAI

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.
Match extension inside a rule
  1. 1.Client 192.168.118.20 sends ping 1 (ICMP type 8) to the server

  2. 2.INPUT rule checks basic matches first: -p icmp --icmp-type 8

  3. 3.Then the extension match: -m statistic --mode nth --every 3

  4. 4.Count 1 of 3 matches: DROP at the firewall

  5. 5.Ping 2: count does not match, packet passes and the reply comes back

98.2 Rate matches: limit vs hashlimit vs recent

One bucket vs one bucket per IP
  1. 1.Attacker .66 floods the web server; a normal user .20 opens the site

  2. 2.-m limit: one bucket for the whole rule

  3. 3.The flood eats the bucket, so the normal user is dropped too

  4. 4.-m hashlimit --hashlimit-mode srcip: one bucket per source IP

  5. 5.Attacker's bucket empties and is dropped; the user's packets reach the server

MatchHow it countsTypical use
-m limitOne global token bucket (default 3/hour, burst 5)Keeping LOG rules under control
-m hashlimitA bucket per srcip / dstip / port groupPer-IP rate limiting
-m recentDynamic IP list + seconds + hitcountSSH brute force: drop after 4 new connections in 60 s
-m quotaMatches until a byte counter reaches 0Data caps
-m rateestLive rate from a RATEEST estimator (bps/pps lt/gt/eq)Rate-based decisions
Output
--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

Terminal
grep -oE '^xt_[a-z]+' /proc/moduleszcat /proc/config.gz | grep -E \  'MATCH_(HL|LIMIT|RECENT)='
Output
$ 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

Terminal
# 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
Output
$ 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.
Terminal
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
Output
$ 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)

Terminal
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
Output
$ 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 DROP

In IPv6 the TTL is called hop limit: pings with -t 1 were dropped, the normal hop limit 64 pings passed.

98.6 Remaining matches

GroupMatchesWhat they check
Address / portiprange, multiport, mac, mark, length, pkttypeIP range without a subnet, up to 15 ports, source MAC, nfmark, packet size, unicast/broadcast/multicast
Lists / stateset, nfacct, realm, state, helperipset lookup (fast for thousands of IPs), accounting, routing realm, old conntrack subset, FTP-related packets
IPv6 headershbh, icmp6, ipv6header, mh, rtHop-by-hop, ICMPv6 type, which extension headers exist (--soft = any one), mobility, routing header
IPsecpolicy--dir in/out, --pol ipsec/none, --strict with --proto esp, --mode tunnel, tunnel src/dst
Load balancer / OSipvs, osf, socketIPVS VIP/port/method, passive OS guess from SYN, open local socket (transparent proxy)
Hostowner, physdev, rpfilterUser/group that created the packet (OUTPUT only), bridge ports, reverse path check against spoofing
Payloadstring, sctpText/hex pattern (--algo bm|kmp, --from/--to, --icase), SCTP ports and chunk types
Output
-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 pattern

98.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.