UnixDroid

Lesson 11 of 12 · 8 min

Packet sniffing with Scapy

Every action on a network becomes packets on the wire. A sniffer reads them - the fastest way to grasp how networks really work.

Every action on a network becomes packets on the wire. A sniffer reads them — and understanding one is the fastest way to truly grasp how networks work, and why unencrypted traffic is so dangerous. This lesson uses scapy, Python's packet Swiss-army knife. Sniff only on a network you own or are authorised to monitor.

What sniffing shows you

Normally your network card ignores packets not addressed to it. A sniffer asks it to show you everything it sees — source, destination, protocol, and often the raw contents.

A five-line sniffer

from scapy.all import sniff

def show(packet):
    print(packet.summary())

sniff(prn=show, count=10)   # needs sudo

Run it with sudo and you'll watch real packets scroll past — DNS lookups, web requests, pings.

Looking inside a packet

from scapy.all import sniff, IP

def inspect(packet):
    if packet.haslayer(IP):
        print(packet[IP].src, '->', packet[IP].dst)

sniff(prn=inspect, filter='tcp', count=20)

The filter='tcp' uses BPF syntax — the same filter language as Wireshark — to grab only what you care about.

LayerTells you
EthernetHardware (MAC) addresses
IPSource and destination IP
TCP/UDPPorts, i.e. which service
RawThe actual payload — plaintext if unencrypted

Tip: This is why HTTPS matters. On open Wi-Fi, plain HTTP traffic — logins included — travels as readable text any sniffer can grab. Encryption is what turns that payload into noise. Capturing other people's traffic without authorisation is illegal — keep it to your own network.

From packets to understanding

Once you've watched your own DNS and HTTP requests fly past in plaintext, you'll never think about network security the same way — and you'll understand exactly what tools like Wireshark are showing you.

Try it yourself
  1. Run the five-line sniffer with sudo and capture 10 packets.
  2. Add filter='udp' and watch DNS lookups as you browse.
  3. Print just the source and destination IP of each captured packet.
No Linux machine? Practise in the free online terminal

Quick check

Plain HTTP traffic is readable to a sniffer; what protocol encrypts it so the payload becomes unreadable?

Need a hint?

It's HTTP with an S for secure.

Finished this lesson?

Done lessons keep your daily streak going 🔥

0 day streak

0 lessons done

best 0 days

Log in to keep your streak on every device →