basforge

Search titles, text and the “If it doesn’t work” cases.

    RU
    ManualLinuxtcpdump

    BPF filters in tcpdump: syntax, logic, and the tcp[13] byte

    A BPF expression compiles to bytecode and runs directly inside the Linux kernel before packets are copied to tcpdump. Covers address and port primitives, and/or/not precedence, filtering by tcp[13] flag bits, and inspecting any filter with -d.

    Anatomy of a BPF expression

    Every pcap-filter expression is built from primitives joined by the logical operators and (&&), or (||), and not (!). Each primitive has three axes:

    Axis Options Example
    Protocol (proto) ether, arp, ip, ip6, icmp, icmp6, tcp, udp, vlan tcp, udp, icmp
    Direction (dir) src, dst, src or dst (default), src and dst dst port 443, src net 10.0.0.0/24
    Type (type) host, net, port, portrange host 203.0.113.10, portrange 8000-8080

    Omit the direction and tcpdump matches both ends: host 203.0.113.10 catches both incoming and outgoing packets for that host. Omit the protocol and port 53 matches both UDP and TCP.

    Step by step: building practical filters

    1. Filter by hosts, subnets, and ports

      # All traffic to and from a specific IP (IPv4 or IPv6):
      sudo tcpdump -nn -i eth0 'host 203.0.113.10'
      sudo tcpdump -nn -i eth0 'host 2001:db8::10'
      
      # Only packets from a subnet heading to our HTTPS port:
      sudo tcpdump -nn -i eth0 'src net 198.51.100.0/24 and dst port 443'
      
      # A port range (for example passive FTP or local backends):
      sudo tcpdump -nn -i eth0 'tcp portrange 8000-8099'
      
      # Only IPv4 or only IPv6, only ARP or only ICMP:
      sudo tcpdump -nn -i eth0 'arp or icmp'
      sudo tcpdump -nn -i eth0 'ip6 and icmp6'

      When two protocols share a port — such as DNS over UDP and TCP — write 'port 53'. When you only want TCP on port 53 (to spot truncated responses falling back from UDP to TCP), write 'tcp port 53'.

    2. Combine conditions with parentheses and exclusions

      One rule keeps you out of trouble: wrap every expression containing and/or/not in single quotes so the shell leaves it alone, and parenthesize every mix of and and or instead of trusting operator precedence from memory:

      # Multiple ports for one host:
      sudo tcpdump -nn -i eth0 'host 203.0.113.10 and (port 80 or port 443)'
      
      # Everything except SSH and local DNS:
      sudo tcpdump -nn -i eth0 'not port 22 and not port 53'
      
      # Traffic strictly between two servers, ignoring other neighbors:
      sudo tcpdump -nn -i eth0 '(src host 10.0.0.11 and dst host 10.0.0.20) or (src host 10.0.0.20 and dst host 10.0.0.11)'

      When a filter grows across several lines (such as excluding a dozen noisy monitoring hosts), put it in a text file and pass it via -F /tmp/filter.bpf to avoid quoting mistakes.

    3. Match TCP flags with the tcp[13] byte

      TCP flags sit at byte offset 13 from the start of the TCP header (tcp[13], the fourteenth octet). Two operators behave in completely different ways here:

      • == — exact match on all 8 bits. 'tcp[13] == 0x02' catches only pure SYN packets (and skips SYN+ACK, which also has bit 0x10 set).
      • & mask != 0 — checks whether at least one of the listed bits is set. 'tcp[13] & 0x04 != 0' catches both bare RST ([R], 0x04) and RST+ACK ([R.], 0x14).

      With libpcap 1.9+, readable names work instead of raw hex: tcp-fin, tcp-syn, tcp-rst, tcp-push, tcp-ack, tcp-urg, tcp-ece, tcp-cwr, plus tcpflags as an alias for 13. Build an exact bitmask and toggle between == and & != 0 right here:

      Byte: 00010010Hex: 0x12Dec: 18Exactly these flags: ACK + SYN (all other bits = 0)
      sudo tcpdump -nn 'tcp[13] == 0x12'
      # by name: sudo tcpdump -nn 'tcp[tcpflags] == (tcp-ack|tcp-syn)'

      Three flag filters you will reach for most often:

      # All connection attempts and resets (SYN + SYN-ACK + RST):
      sudo tcpdump -nn -i eth0 'tcp[tcpflags] & (tcp-syn|tcp-rst) != 0'
      
      # Only new inbound connection requests (pure SYN, no SYN-ACK):
      sudo tcpdump -nn -i eth0 'tcp[13] == 0x02 and dst port 443'
      
      # Only packets carrying payload (PSH+ACK) — skip empty ACKs:
      sudo tcpdump -nn -i eth0 'tcp[13] == 0x18 and port 8080'

      Remember the libpcap limit: tcp[13] works only for IPv4. For plain IPv6 (with no extension header chain), the same flags byte lives at offset 40 + 13 = 53: 'ip6[6] == 6 and ip6[53] & 0x06 != 0'.

    4. Filter by byte offsets inside protocols (ICMP, HTTP, TLS)

      With proto[offset:size] (size 1, 2, or 4 bytes) you can inspect any field in an IPv4, ICMP, or UDP header or the start of the TCP payload:

      # Only ICMP Destination Unreachable (type 3) and Time Exceeded (type 11) — no ping:
      sudo tcpdump -nn -i eth0 'icmp[icmptype] == 3 or icmp[icmptype] == 11'
      
      # Only ICMP Fragmentation Needed (type 3, code 4 — MTU troubleshooting):
      sudo tcpdump -nn -i eth0 'icmp[0] == 3 and icmp[1] == 4'
      
      # HTTP GET requests by the first 4 bytes of TCP payload (0x47455420 = "GET "):
      sudo tcpdump -nn -A -i eth0 'tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x47455420'
      
      # TLS ClientHello on port 443 (record type 0x16 Handshake, handshake type 0x01 ClientHello):
      sudo tcpdump -nn -i eth0 'tcp port 443 and (tcp[((tcp[12:1] & 0xf0) >> 2):1] = 0x16) and (tcp[(((tcp[12:1] & 0xf0) >> 2) + 5):1] = 0x01)'

      The expression (tcp[12:1] & 0xf0) >> 2 computes the real TCP header length in bytes (the 4-bit Data Offset field in the upper nibble of byte 12, multiplied by 4), so TCP options such as TS val don’t shift the payload offset.

    5. Verify a complex filter without capturing via -d

      The -d flag compiles your BPF expression for the chosen interface, prints the generated bytecode, and exits immediately without capturing a single packet. It catches syntax mistakes, -i any incompatibilities (Limits of -i any), and misplaced vlan terms (Order of vlan in a filter) in a second:

      sudo tcpdump -i eth0 -d 'tcp[tcpflags] & (tcp-syn|tcp-rst) != 0'
      # (000) ldh      [12]              # EtherType:
      # (001) jeq      #0x800   jt 2  jf 10   # IPv4 (0x0800)? otherwise drop
      # (002) ldb      [23]              # IPv4 protocol:
      # (003) jeq      #0x6     jt 4  jf 10   # TCP (6)?
      # (004) ldh      [20]              # IP fragment offset:
      # (005) jset     #0x1fff  jt 10 jf 6    # non-first fragment? drop
      # (006) ldxb     4*([14]&0xf)      # IP header length (IHL * 4)
      # (007) ldb      [x + 27]          # byte 14 (Ethernet) + IHL + 13 (tcp[13])
      # (008) jset     #0x6     jt 9  jf 10   # bits 0x02 (SYN) | 0x04 (RST) != 0?
      # (009) ret      #262144           # accept frame
      # (010) ret      #0                # drop

      Lines (000)–(001) show right away why tcp[13] ignores IPv6: the filter checks only EtherType 0x800 (IPv4) and jumps straight to ret #0 for everything else.

    If it doesn’t work

    The shell fails with bash: syntax error near unexpected token '(' or event not found

    Cause
    Parentheses ( … ), exclamation marks !, and ampersands & were interpreted by bash before tcpdump saw them.
    Fix
    Always wrap the entire BPF expression in single quotes: sudo tcpdump -nn -i eth0 'host 203.0.113.10 and (port 80 or port 443)'.

    The filter 'host 203.0.113.10 and port 80 or port 443' captures port 443 traffic from every IP on the server

    Cause
    and and or share the same precedence and are left-associative in pcap-filter, so without parentheses it parses as (host 203.0.113.10 and port 80) or port 443.
    Fix
    Group alternatives explicitly with parentheses: 'host 203.0.113.10 and (port 80 or port 443)'.

    The filter 'tcp[13] == 0x02' catches zero SYN packets over IPv6 even though IPv6 traffic is flowing

    Cause
    A known libpcap compiler limitation (documented in man tcpdump): transport-header byte offsets such as tcp[13] or udp[8] only match IPv4.
    Fix
    For IPv6 without extension headers, index inside ip6 (40-byte IPv6 header + 13 = 53): 'ip6[6] == 6 and ip6[53] == 0x02'.

    The filter 'tcp port 80 and vlan' misses tagged traffic in a .pcap or on an outbound interface

    Cause
    The vlan primitive shifts L3/L4 offsets by 4 bytes only for the part of the expression to its right (Order of vlan in a filter).
    Fix
    Put vlan first: 'vlan and tcp port 80', or match both tagged and untagged frames with '(tcp port 80) or (vlan and tcp port 80)'.

    How to check

    Compare -d output for an exact match versus a bitmask and confirm tcpdump accepts both expressions on your target interface:

    sudo tcpdump -i eth0 -d 'tcp[13] == 0x02'
    sudo tcpdump -i eth0 -d 'tcp[tcpflags] & (tcp-syn|tcp-rst) != 0'

    The first output contains jeq #0x2 (exact match for 2), while the second contains jset #0x6 (either bit 0x02 or 0x04 set).