7. Detecting Security Anomalies & Attacks in PCAPs
Identifying Port Scans: SYN, NULL, and XMAS
Port scanning is the initial stage of reconnaissance where an attacker probes a target to identify open services. SYN scans (half-open) initiate a handshake but never complete it. NULL scans send packets with no flags set, while XMAS scans set the FIN, URG, and PUSH flags. Wireshark detects these through custom display filters like 'tcp.flags.syn == 1 && tcp.flags.ack == 0'.
The following table compares how these packets appear on the wire when scanning closed ports.
| Scan Type | Flag Configuration |
|---|---|
| SYN Scan | Only SYN bit set. |
| NULL Scan | No flags (0x00) set. |
| XMAS Scan | FIN, PSH, and URG set. |
tshark -r traffic.pcap -Y 'tcp.flags.syn==1 && tcp.flags.ack==0' -T fields -e ip.src -e tcp.srcport | sort | uniq -c | sort -nr
Detecting Brute-Force Attacks
Brute-force attacks rely on repetitive trial-and-error attempts to gain unauthorized access. At the network level, this manifests as a high frequency of authentication failures. For SSH, we look for SSH-2.0-... headers followed by repeated Disconnect messages. For HTTP, we monitor for status codes 401 (Unauthorized) or 403 (Forbidden) occurring at a rate exceeding human interaction thresholds.
The script below calculates the frequency of 401 errors per IP address to isolate potential brute-force bots.
import collections # Mock log data of HTTP status codes per source IP log_data = [('192.168.1.5', 401), ('192.168.1.5', 401), ('10.0.0.2', 200), ('192.168.1.5', 401)] def detect_brute(data, threshold=2): counts = collections.Counter([ip for ip, code in data if code == 401]) return [ip for ip, count in counts.items() if count > threshold] print(f'Suspected attackers: {detect_brute(log_data)}')
Locating Cleartext Credentials
Protocols like FTP, Telnet, and HTTP Basic Auth transmit data in cleartext. Attackers capture these credentials using passive sniffing. In Wireshark, you can use the 'Follow TCP Stream' feature to view the conversation directly. When analyzing raw PCAPs programmatically, we search for strings following headers like USER, PASS, or 'Authorization: Basic'.
Below is a demonstration of how to extract potential credentials using a regex search pattern against a packet stream.
import re packet_data = b'POST /login HTTP/1.1\r\nUser: admin\r\nPass: s3cret123\r\n' # Regex to capture pass value match = re.search(b'Pass: (.*)\\r\\n', packet_data) if match: print(f'Captured: {match.group(1).decode()}')
Spotting DNS Tunneling
DNS tunneling encodes non-DNS traffic into the query field of a DNS request. Since DNS is rarely blocked, attackers use it for C2 (Command and Control) channels. You can spot this by observing unusually long, randomized hostnames (e.g., a1b2c3d4e5f6g7h8.evil-domain.com) or an excessive volume of TXT or NULL record queries.
Use TShark to extract all DNS queries and count the character length of the query segments to flag anomalies.
tshark -r capture.pcap -Y 'dns.flags.response == 0' -T fields -e dns.qry.name | awk '{print length($0), $0}' | sort -nr | head -n 5
Knowledge Check
Ready to test your understanding of 7. Detecting Security Anomalies & Attacks in PCAPs?