nping and ndiff
nping (from the Nmap project) is a packet generator: it builds custom TCP, UDP, ICMP or ARP probes and measures the RTT of every reply. ndiff is a small Python tool that compares two nmap XML scans and shows what changed.
75.1 Why is it used?
- Test firewall rules and port reachability with exactly the packet you want (flags, TTL, ports, payload).
- Measure RTT and loss for TCP/UDP, not only ICMP like ping.
- Echo mode shows what changed on the path (TTL, NAT rewrites).
- ndiff spots new or closed ports between two scans: ideal for scheduled audits.
- Windows: Nmap installer + Npcap, Administrator prompt. Linux/macOS: nping/ndiff package, sudo for raw modes. Only on your own or permitted network.
75.2 How it works
- Step 1
1.PC 192.168.95.10 runs nping --tcp -p 80 srv
- Step 2
2.The SYN goes to the router (192.168.95.2 / .130)
- Step 3
3.The router forwards it to the server 192.168.95.140
- Step 4
4.The server replies SYN-ACK (SA) back through the router
- Step 5
5.RTT = RCVD time - SENT time
75.3 Probe modes and targets
| Mode | What it sends | Root? |
|---|---|---|
| (none) | ICMP echo (default for root) | Yes |
| --tcp-connect | Normal connect(); shows handshake completed / refused | No |
| --tcp | Raw TCP probes with any flags | Yes |
| --udp | UDP probes | Yes |
| --icmp | ICMP of any type/code | Yes |
| --arp | ARP, same LAN only | Yes |
| --traceroute / --tr | Probes with TTL 1, 2, 3... | Yes |
- Targets: one IP or hostname, a CIDR (192.168.95.0/25), octet ranges (192.168.95.1-20), several targets round-robin; IPv6 (-6) only as a full address.
nping -c 2 srvnping --tcp -p 80,443 -c 1 srvnping --udp -p 53 -c 1 srv$ nping -c 2 srvSENT (0.0257s) ICMP [192.168.95.10 > 192.168.95.140 Echo requestRCVD (0.0265s) ICMP [192.168.95.140 > 192.168.95.10 Echo replySENT (1.0266s) ICMP [192.168.95.10 > 192.168.95.140 Echo requestRCVD (1.0268s) ICMP [192.168.95.140 > 192.168.95.10 Echo replyMax rtt: 0.168ms | Min rtt: 0.085ms | Avg rtt: 0.126msRaw packets sent: 2 (56B) | Rcvd: 2 (56B) | Lost: 0 (0.00%)$ nping --tcp -p 80,443 -c 1 srvSENT (0.0454s) TCP 192.168.95.10:50612 > 192.168.95.140:80 S ttl=64RCVD (0.0459s) TCP 192.168.95.140:80 > 192.168.95.10:50612 SA ttl=63SENT (1.0466s) TCP 192.168.95.10:50612 > 192.168.95.140:443 S ttl=64RCVD (1.0471s) TCP 192.168.95.140:443 > 192.168.95.10:50612 RA ttl=63$ nping --udp -p 53 -c 1 srvSENT (0.0286s) UDP 192.168.95.10:53 > 192.168.95.140:53 ttl=64 id=7274 iplen=28RCVD (0.0290s) ICMP [192.168.95.140 > 192.168.95.10 Port unreachable (type=3/code=3) ]Raw packets sent: 1 (28B) | Rcvd: 1 (56B) | Lost: 0 (0.00%)- SA = port 80 open, RA = port 443 closed, ICMP port unreachable = UDP 53 closed.
- ttl=63 in replies: the server is one router hop away.
75.4 TCP connect and raw TCP options
nping --tcp-connect -p 80,443 -c 1 srvnping --tcp -p 80 --flags ack -c 1 srv$ nping --tcp-connect -p 80,443 -c 1 srvSENT (0.0094s) Starting TCP Handshake > srv:80 (192.168.95.140:80)RCVD (0.0095s) Handshake with srv:80 (192.168.95.140:80) completedSENT (1.0110s) Starting TCP Handshake > srv:443 (192.168.95.140:443)RCVD (1.0110s) Possible TCP RST received from 192.168.95.140:443 --> Connection refusedTCP connection attempts: 2 | Successful connections: 1 | Failed: 1 (50.00%)$ nping --tcp -p 80 --flags ack -c 1 srvSENT (0.0374s) TCP 192.168.95.10:49222 > 192.168.95.140:80 A ttl=64RCVD (0.0380s) TCP 192.168.95.140:80 > 192.168.95.10:49222 R ttl=63Raw packets sent: 1 (40B) | Rcvd: 1 (40B) | Lost: 0 (0.00%)- A lone ACK answered by RST means the packet reached the host unfiltered: a useful firewall-testing signal.
- -p destination port(s), -g source port, --seq, --ack, --win set header fields.
- --flags takes names (syn, ack, rst, fin, psh, urg, ecn, cwr), initials (SA), hex (0x12), all or none.
- --badsum sends a bad checksum: a reply means a firewall/IDS in between answered by itself.
75.5 ICMP and traceroute mode
- --icmp-type / --icmp-code take a number or a name (echo 8, dest-unreach 3, time-exceeded 11, timestamp 13/14, mask 17/18).
- Codes have meaning inside their type: dest-unreach code 3 = port unreachable.
- Modern Linux ignores the old address mask request, so it shows 100% loss.
- --tr starts every probe at TTL 1; --ttl sets it by hand.
nping --icmp --icmp-type timestamp -c 1 srvnping --icmp --icmp-type mask -c 1 srvnping --tcp -p 80 --tr -c 2 srv$ nping --icmp --icmp-type timestamp -c 1 srvSENT (0.0182s) ICMP [192.168.95.10 > 192.168.95.140 Timestamp request (type=13/code=0)RCVD (0.0186s) ICMP [192.168.95.140 > 192.168.95.10 Timestamp reply (type=14/code=0) id=32609 seq=1 orig=0 recv=77569919 trans=77569919]$ nping --icmp --icmp-type mask -c 1 srvSENT (0.0210s) ICMP [192.168.95.10 > 192.168.95.140 Address mask request (type=17/code=0)Max rtt: N/A | Min rtt: N/A | Avg rtt: N/ARaw packets sent: 1 (32B) | Rcvd: 0 (0B) | Lost: 1 (100.00%)$ nping --tcp -p 80 --tr -c 2 srvSENT (0.1442s) TCP 192.168.95.10:35380 > 192.168.95.140:80 S ttl=1RCVD (0.1446s) ICMP [192.168.95.2 > 192.168.95.10 TTL=0 during transit (type=11/code=0) ]SENT (1.1446s) TCP 192.168.95.10:35380 > 192.168.95.140:80 S ttl=2RCVD (1.1449s) TCP 192.168.95.140:80 > 192.168.95.10:35380 SA ttl=63- Step 1
1.nping --tr sends a SYN with TTL 1
- Step 2
2.The router decrements TTL to 0 and drops it
- Step 3
3.The router sends ICMP time exceeded: hop 1 = 192.168.95.2
- Step 4
4.The next SYN has TTL 2, crosses the router and reaches the server
- Step 5
5.The server answers SA: destination reached
75.6 ARP, IP, Ethernet and payload options
- ARP does not cross a router, so ARP mode targets the router 192.168.95.2 on the same LAN.
- --arp-type (arp-request 1, arp-reply 2, rarp, drarp, inarp, arp-nak) and --arp-sender/target-mac/ip set every field.
- IP: -S source (a spoofed source means the reply goes elsewhere), --tos, --id, --df/--mf, --ttl, --badsum-ip, --ip-options, --mtu; IPv6: --flow, --traffic-class, --hop-limit.
- Ethernet: --dest-mac, --source-mac (a wrong one means no replies), --ether-type (ipv4 0x0800, arp 0x0806, ipv6 0x86DD).
- Payload: --data 0xdeadbeef, --data-string, --data-length; timing: --delay, --rate.
nping --arp -c 1 192.168.95.2nping --udp -p 9 --data-string lab -c 1 srvnping --icmp --delay 200ms -c 3 srv$ nping --arp -c 1 192.168.95.2SENT (0.0479s) ARP who has 192.168.95.2? Tell 192.168.95.10RCVD (0.0482s) ARP reply 192.168.95.2 is at CE:8B:21:8A:7A:DC$ nping --udp -p 9 --data-string lab -c 1 srvSENT (0.0385s) UDP 192.168.95.10:53 > 192.168.95.140:9 ttl=64 id=4235 iplen=31RCVD (0.0390s) ICMP [192.168.95.140 > 192.168.95.10 Port 9 unreachable (type=3/code=3) ]$ nping --icmp --delay 200ms -c 3 srvSENT (0.0342s) ICMP [192.168.95.10 > 192.168.95.140 Echo requestSENT (0.2346s) ICMP [192.168.95.10 > 192.168.95.140 Echo requestSENT (0.4347s) ICMP [192.168.95.10 > 192.168.95.140 Echo requestRaw packets sent: 3 (84B) | Rcvd: 3 (84B) | Lost: 0 (0.00%)- --data-string lab added 3 bytes: iplen went from 28 to 31.
75.7 Echo mode
- Step 1
1.The server runs nping --es lab (echo server)
- Step 2
2.The client opens a side-channel TCP connection on port 9929
- Step 3
3.The client sends a SYN probe; it crosses the router (TTL - 1)
- Step 4
4.The server captures the probe as it arrived
- Step 5
5.It echoes the captured packet back over the side channel: the client compares SENT and CAPT
Catches TTL changes and NAT rewrites on the path
# srv (192.168.95.140)nping --es lab --nc --once -e eth0# pc (192.168.95.10)nping --ec lab --nc --tcp -p 80 -c 1 srv$ nping --ec lab --nc --tcp -p 80 -c 1 srvSENT (0.2293s) TCP 192.168.95.10:11164 > 192.168.95.140:80 S ttl=64CAPT (0.2444s) TCP 192.168.95.10:11164 > 192.168.95.140:80 S ttl=63RCVD (0.2297s) TCP 192.168.95.140:80 > 192.168.95.10:11164 SA ttl=63Raw packets sent: 1 (40B) | Rcvd: 1 (44B) | Lost: 0 (0.00%)| Echoed: 1 (40B)- SENT ttl=64 but CAPT ttl=63: the router decremented TTL by one.
- --nc turns encryption off (lab only), --once stops after one client, --ep changes port 9929, --safe-payloads zeroes data before echoing.
75.8 Output options
- -H (hide sent) shows only replies: handy when checking many ports.
- Verbosity -v-4 (nothing) to -v3 (hex dump); -v-1 = stats only, best for scripts. Debug -d0 to -d6.
- Misc: -c count (0 = non-stop), -e interface, -N no capture, --bpf-filter, --privileged/--unprivileged.
nping -H --tcp -p 22,80,443 -c 1 srvnping -v-1 -c 2 srv$ nping -H --tcp -p 22,80,443 -c 1 srvRCVD (0.0194s) TCP 192.168.95.140:22 > 192.168.95.10:28724 RARCVD (1.0201s) TCP 192.168.95.140:80 > 192.168.95.10:28724 SARCVD (2.0225s) TCP 192.168.95.140:443 > 192.168.95.10:28724 RA$ nping -v-1 -c 2 srvMax rtt: 0.105ms | Min rtt: 0.036ms | Avg rtt: 0.070msRaw packets sent: 2 (56B) | Rcvd: 2 (56B) | Lost: 0 (0.00%)Nping done: 1 IP address pinged in 1.04 seconds75.9 ndiff: comparing two scans
- Save scans as XML (nmap -oX), change something, scan again, then ndiff old new.
- - lines are only in the first scan, + lines only in the second.
- -v also shows unchanged hosts/ports; --xml gives machine-readable output; --text is the default.
nmap -n -p 22,80,8080 -oX s1.xml srv# srv: port 80 closed, 8080 opennmap -n -p 22,80,8080 -oX s2.xml srvndiff s1.xml s2.xml; echo "exit $?"$ ndiff s1.xml s2.xml; echo "exit $?" srv (192.168.95.140): PORT STATE SERVICE VERSION-80/tcp open http+80/tcp closed http-8080/tcp closed http-proxy+8080/tcp open http-proxyexit 1$ ndiff s1.xml s1.xml; echo "exit $?"exit 0$ ndiff no.xml s2.xml; echo "exit $?"Can't open file: [Errno 2] No such file or directory: 'no.xml'exit 2| ndiff exit code | Meaning |
|---|---|
| 0 | Scans are the same |
| 1 | A difference was found (not an error) |
| 2 | Error, e.g. a missing file |
75.10 Common mistakes
- Treating ndiff exit 1 as a failure: it means 'changed'; only 2 is an error.
- Using ARP mode on a host behind a router: ARP never crosses a router.
- Spoofing -S or --source-mac and then waiting for replies: they go to the fake address.
- Running raw modes without root/Admin: use --tcp-connect instead.
75.11 Interview tips
- nping vs ping: ping only sends ICMP echo; nping builds TCP, UDP, ICMP or ARP probes with custom flags, TTL and ports.
- SYN -> RA = host up, port closed; UDP -> ICMP port unreachable = port closed, no firewall drop.
- Echo mode shows changes on the path (TTL, NAT) by comparing SENT with CAPT.
- ndiff exit codes: 0 same, 1 changed, 2 error; handle 1 and 2 separately in scripts.