hping3 + ngrep
hping3 is a command-line packet crafter that controls every field of a TCP, UDP, ICMP or raw IP packet. ngrep is 'grep for the network': it applies a BPF filter and then searches packet payloads with a regex or hex string. Use both only on your own or permitted networks.
77.1 Why is it used?
- hping3: test whether a port is open/closed/filtered with any flags, even when ICMP is blocked.
- hping3: TCP traceroute to port 80, firewall and IDS tests, timestamps, even moving a file inside packets.
- ngrep: find which packets carry a given text (an HTTP GET, a header, a DNS name) without opening Wireshark.
- Install: hping3 via apt/dnf/apk (needs root or CAP_NET_RAW), Homebrew + sudo on macOS, WSL on Windows; ngrep on Linux and macOS.
77.2 How it works
- Step 1
1.probe 192.168.97.10 runs hping3 -S -p 80
- Step 2
2.The SYN goes through the router 192.168.97.2 to srv 192.168.97.140
- Step 3
3.Port 80 is open: srv replies flags=SA
- Step 4
4.The same SYN to closed port 81: srv replies flags=RA
- Step 5
5.The reply flags tell you open or closed
- Step 1
1.ngrep runs on srv eth0; probe sends curl http://192.168.97.140/
- Step 2
2.The kernel BPF filter 'tcp port 80' lets only web traffic through
- Step 3
3.ngrep runs the regex GET|POST on each packet's payload
- Step 4
4.Match: it prints the header line (T src -> dst [AP]) and the payload
- Step 5
5.No match: it stays silent
77.3 hping3 base options
- -c count, -n numeric, -S SYN flag, -p destination port.
- -V verbose adds seq/ack numbers; -q prints only the summary.
- -i u10000 = 10 ms gap; --fast = 10 per second; --faster even faster; --flood ignores replies (own lab only).
- -I forces the interface; the output word 'tramitted' is hping3's own typo.
hping3 -c 3 -S -p 80 -n 192.168.97.140hping3 -c 2 -S -p 81 -n 192.168.97.140hping3 -q -i u10000 -c 20 -S -p 80 -n 192.168.97.140$ hping3 -c 3 -S -p 80 -n 192.168.97.140len=44 ip=192.168.97.140 ttl=63 DF id=0 sport=80 flags=SA seq=0 win=64240 rtt=0.7 mslen=44 ip=192.168.97.140 ttl=63 DF id=0 sport=80 flags=SA seq=1 win=64240 rtt=0.3 ms3 packets tramitted, 3 packets received, 0% packet loss$ hping3 -c 2 -S -p 81 -n 192.168.97.140len=40 ip=192.168.97.140 ttl=63 DF id=0 sport=81 flags=RA seq=0 win=0 rtt=0.4 mslen=40 ip=192.168.97.140 ttl=63 DF id=0 sport=81 flags=RA seq=1 win=0 rtt=0.8 ms$ hping3 -q -i u10000 -c 20 -S -p 80 -n 192.168.97.140HPING 192.168.97.140 (eth0 192.168.97.140): S set, 40 headers + 0 data bytes--- 192.168.97.140 hping statistic ---20 packets tramitted, 20 packets received, 0% packet lossround-trip min/avg/max = 0.4/3.5/27.7 ms77.4 Protocol selection
| Option | Mode | Lab result |
|---|---|---|
| (default) | TCP | SA on 80, RA on 81 |
| -1 | ICMP echo (like ping) | Echo replies, icmp_seq=0,1 |
| -2 | UDP | Port 53: ICMP Port Unreachable |
| -8 75-85 | Scan mode over a port range | Only 80 answered .S..A... |
| -0 | Raw IP header + data | - |
| -9 SIG | Listen mode (waits for a signature) | Received a file line |
hping3 -2 -p 53 -c 2 -n 192.168.97.140hping3 -8 75-85 -S -n 192.168.97.140$ hping3 -2 -p 53 -c 2 -n 192.168.97.140HPING 192.168.97.140 (eth0 192.168.97.140): udp mode set, 28 headers + 0 data bytesICMP Port Unreachable from ip=192.168.97.140ICMP Port Unreachable from ip=192.168.97.140$ hping3 -8 75-85 -S -n 192.168.97.140Scanning 192.168.97.140 (192.168.97.140), port 75-8511 ports to scan, use -V to see all the replies|port| serv name | flags |ttl| id | win | len | 80 http : .S..A... 63 0 64240 44All replies received. Done.77.5 IP options and TCP traceroute
- -t sets TTL: TTL 1 expires at the router, which replies time exceeded.
- --traceroute starts at TTL 1 and raises it per reply: a traceroute over TCP port 80 that works even when ICMP probes are blocked.
- -a spoofs the source IP, --rand-source randomises it; -N IP id; -f / -m fragments; -y Don't Fragment; -o TOS; -G record route.
hping3 -t 1 -S -p 80 -c 1 -n 192.168.97.140hping3 --traceroute -S -p 80 -c 3 -n 192.168.97.140$ hping3 -t 1 -S -p 80 -c 1 -n 192.168.97.140HPING 192.168.97.140 (eth0 192.168.97.140): S set, 40 headers + 0 data bytesTTL 0 during transit from ip=192.168.97.2$ hping3 --traceroute -S -p 80 -c 3 -n 192.168.97.140hop=1 TTL 0 during transit from ip=192.168.97.2hop=1 hoprtt=10.3 mslen=44 ip=192.168.97.140 ttl=63 DF id=0 sport=80 flags=SA seq=1 win=64240 rtt=1.0 mslen=44 ip=192.168.97.140 ttl=63 DF id=0 sport=80 flags=SA seq=2 win=64240 rtt=0.5 ms77.6 ICMP and TCP flag options
- --icmp-ts sends type 13 (timestamp); Linux replies with Originate/Receive/Transmit times.
- --icmp-addr sends type 17 (address mask); modern Linux ignores it (100% loss). -C sets type, -K code.
- -S -A -R -F -P -U set SYN, ACK, RST, FIN, PUSH, URG; -X / -Y set the two reserved bits.
- FIN+PUSH+URG (Xmas style): an open port stays silent (per the RFC), a closed port sends RST.
hping3 --icmp-ts -c 1 -n 192.168.97.140hping3 -F -P -U -p 80 -c 2 -n 192.168.97.140hping3 -F -P -U -p 81 -c 1 -n 192.168.97.140$ hping3 --icmp-ts -c 1 -n 192.168.97.140len=40 ip=192.168.97.140 ttl=63 id=35279 icmp_seq=0 rtt=10.9 msICMP timestamp: Originate=78270081 Receive=78270091 Transmit=78270091ICMP timestamp RTT tsrtt=11$ hping3 -F -P -U -p 80 -c 2 -n 192.168.97.140HPING 192.168.97.140 (eth0 192.168.97.140): FPU set, 40 headers + 0 data bytes--- 192.168.97.140 hping statistic ---2 packets tramitted, 0 packets received, 100% packet loss$ hping3 -F -P -U -p 81 -c 1 -n 192.168.97.140HPING 192.168.97.140 (eth0 192.168.97.140): FPU set, 40 headers + 0 data byteslen=40 ip=192.168.97.140 ttl=63 DF id=0 sport=81 flags=RA seq=0 win=0 rtt=0.2 ms- Other TCP options: -s source port, -k keep it fixed, -w window, -M/-L seq/ack, -Q collects target ISNs (predictability), -b bad checksum, --tcp-mss, --tcp-timestamp (guess uptime).
77.7 Data, file transfer and hex dump
- -d sets body size, -E puts a file's content in the packet, -e adds a signature.
- -9 SIG on the receiver prints data that follows the signature: a file travels inside one UDP packet.
- -j / -J dump the reply in hex / printable; -B and -u make file transfer reliable; --tcpexitcode returns the reply's TCP flags as exit code.
# srvhping3 -9 N077 -I eth0# probehping3 -2 -p 9999 -e N077 -E /tmp/msg.txt -d 24 -c 1 -n 192.168.97.140hping3 -1 -j -d 8 -c 1 -n 192.168.97.140srv$ hping3 -9 N077 -I eth0hping2 listen modelab note from probeprobe$ hping3 -1 -j -d 8 -c 1 -n 192.168.97.140len=36 ip=192.168.97.140 ttl=63 id=35987 icmp_seq=0 rtt=0.4 ms 4500 0024 8c93 0000 3f01 ab5e c0a8 618c c0a8 610a 0000 169e 8800 0000 5858 5858 5858 585877.8 ngrep: match expression and flags
- Syntax: ngrep [options] 'match' bpf-filter.
- Header line: T = TCP (U UDP, I ICMP), [AP] = ACK+PUSH, #4 = packet number.
- -W byline prints HTTP headers line by line; the trailing dot is CRLF.
- -q quiet, -i ignore case, -w whole word, -t timestamp, -x hex+ASCII dump, -v invert, -l line-buffered, -n N exit after N matches, -d interface.
- -X treats the match as hex: write 0x474554 (GET). Without 0x this build crashed (segmentation fault).
ngrep -d eth0 -q -W byline -n 1 'GET|POST' tcp port 80ngrep -d eth0 -q -x -n 1 'hello' port 80ngrep -d eth0 -q -n 1 -X '0x474554' port 80ngrep -d eth0 -q -n 1 -X '474554' port 80$ ngrep -d eth0 -q -W byline -n 1 'GET|POST' tcp port 80interface: eth0 (192.168.97.128/255.255.255.128)filter: ( tcp port 80 ) and ((ip || ip6) || (vlan && (ip || ip6)))match: GET|POSTT 192.168.97.10:54946 -> 192.168.97.140:80 [AP] #4GET /index.html HTTP/1.1.Host: 192.168.97.140.User-Agent: curl/8.7.1.Accept: */*.$ ngrep -d eth0 -q -x -n 1 'hello' port 80match: helloT 192.168.97.140:80 -> 192.168.97.10:44810 [AP] #8 68 65 6c 6c 6f 0a hello.$ ngrep -d eth0 -q -n 1 -X '0x474554' port 80match: 0x474554T 192.168.97.10:52580 -> 192.168.97.140:80 [AP] #4 GET / HTTP/1.1..Host: 192.168.97.140..User-Agent: curl/8.7.1..Accept: */*..$ ngrep -d eth0 -q -n 1 -X '474554' port 80interface: eth0 (192.168.97.128/255.255.255.128)Segmentation fault77.9 ngrep: pcap files and BPF filters
- -O file saves only matching packets to a pcap; -I file searches an old pcap offline (also opens in Wireshark).
- -A 5 shows 5 packets after a match, -K 3 kills a matching TCP connection with RSTs (own systems only), -s snaplen, -S inspect only N bytes.
- BPF: host/net/port with src/dst, tcp/udp/icmp/arp, len, less 64, greater 500, byte access like tcp[13], combined with and/or/not.
- -e shows empty packets: a SYN has no payload, so without -e ngrep prints nothing for it.
# probe: hping3 -a 192.168.97.50 -S -p 80 ...ngrep -d eth0 -q -l '' 'tcp[13] & 2 != 0'ngrep -d eth0 -q -l -e '' 'tcp[13] & 2 != 0'$ ngrep -d eth0 -q -l '' 'tcp[13] & 2 != 0'interface: eth0 (192.168.97.128/255.255.255.128)filter: ( tcp[13] & 2 != 0 ) and ((ip || ip6) || (vlan && (ip || ip6)))Terminated$ ngrep -d eth0 -q -l -e '' 'tcp[13] & 2 != 0'filter: ( tcp[13] & 2 != 0 ) and ((ip || ip6) || (vlan && (ip || ip6)))T 192.168.97.50:2544 -> 192.168.97.140:80 [S] #1T 192.168.97.140:80 -> 192.168.97.50:2544 [AS] #2T 192.168.97.140:80 -> 192.168.97.50:2544 [AS] #3T 192.168.97.140:80 -> 192.168.97.50:2544 [AS] #4- tcp[13] is the flags byte; & 2 tests the SYN bit. The spoofed SYN from .50 got SYN-ACK retries that went to the fake address.
77.10 Common mistakes
- Expecting ngrep to show SYNs or ACKs without -e: empty packets are dropped by default.
- Writing -X without the 0x prefix (crashed in this build).
- Thinking ngrep reveals HTTPS passwords: TLS payload is encrypted, so the regex never matches; only plaintext (HTTP, DNS, SMTP, SIP).
- Using --flood, -a spoofing or ngrep -K outside your own lab.
- Reading silence to an Xmas probe as 'closed': it means open or filtered.
77.11 Interview tips
- hping3 vs ping: ping only sends ICMP echo; hping3 controls every header field in TCP, UDP, ICMP and raw IP, so it can test port 80 with a SYN when ICMP is blocked.
- SA = open, RA = closed, no reply to FIN/PSH/URG = open|filtered.
- ngrep vs tcpdump: tcpdump shows headers, ngrep searches payload text (BPF first, then regex).
- TCP traceroute: hping3 --traceroute -S -p 80 finds the path through firewalls that drop ICMP/UDP probes.