Foundations / Networking hands-on
Networking hands-on cheat sheet
The OSI model
| Task | Command |
|---|---|
| Build a frame layer by layer | python3 build.py |
| Label the headers of a frame | python3 layers.py frame.hex |
| Layer 2: cards and MAC addresses | ip -br link |
| Layer 3: IP addresses | ip -4 -br addr |
| Layer 4: listening ports | ss -tln |
Addresses and routes
| Task | Command |
|---|---|
| List IPv4 addresses, one line each | ip -4 -br addr |
| Test the network stack | ping -c 2 127.0.0.1 |
| Show the routing table | ip route |
| Ask which route an address uses | ip route get 1.1.1.1 |
| Test an address against a network | python3 -c "import ipaddress as i; print(i.ip_address('192.168.1.50') in i.ip_network('192.168.1.0/24'))" |
Switching, routing and VLANs
| Task | Command |
|---|---|
| Show the ARP (neighbour) cache | ip neigh, ip neigh show dev v0 |
| Simulate a MAC table | python3 switch.py frames.txt |
| Pick a route by the longest prefix | python3 route.py routes.txt 10.30.5.9 |
| Read an 802.1Q tag | python3 vlan.py tagged.hex |
| Private network without root | unshare -Urn sh vlanlab.sh arp.sh, unshare -Urn sh vlanlab.sh tag.sh |
| Put a VLAN on an interface | ip link add link v0 name v0.10 type vlan id 10 |
Ports and connections
| Task | Command |
|---|---|
| Serve a folder on loopback only | python3 -m http.server 8000 --bind 127.0.0.1 |
| Find a listening socket | ss -tlnp | grep 8000 |
| List TCP and UDP listeners | ss -tulnp |
| Show live connections to a port | ss -tn state established dport = :8000 |
| Show closed connections that wait | ss -tn state time-wait sport = :8000 |
| Open a TCP connection in Python | socket.create_connection(("127.0.0.1", 8000), timeout=3) |
| Catch a closed port | except ConnectionRefusedError |
DHCP, NTP and IPv6
| Task | Command |
|---|---|
| Read a DHCP exchange and its lease | python3 dhcp.py exchange.txt |
| Offset and delay from four time stamps | python3 ntp.py ntp.txt |
| Is the clock synchronised? | timedatectl | grep -E 'synchronized|NTP service' |
| List IPv6 addresses | ip -6 -br addr |
| Classify IPv6 addresses | python3 ipv6.py addrs.txt |
| Short and long form of an address | ipaddress.ip_address(x).compressed, .exploded |
DNS
| Task | Command |
|---|---|
| Resolve like a normal program | getent hosts localhost |
| Read the resolver order | grep '^hosts' /etc/nsswitch.conf |
| Read the name servers | grep nameserver /etc/resolv.conf |
| Show only the answer lines | dig +noall +answer example.com A |
| Try other record types | for t in AAAA MX TXT NS; do dig +noall +answer example.com $t; done |
| Spot a missing name | dig example.invalid A | grep status |
| Ask one server and port | dig @127.0.0.1 -p 8053 www.lab.test A |
HTTP by hand
| Task | Command |
|---|---|
| Show request and response lines | curl -sv -o /dev/null URL 2>&1 | grep -E '^[<>]' |
| Ask for headers only | curl -sI URL |
| Print the status code | curl -s -o /dev/null -w '%{http_code}\n' URL |
| Force a method | curl -si -X POST URL |
| Save and send cookies | curl -si -c jar.txt URL, curl -s -b jar.txt URL |
| Add a request header | curl -H 'Cookie: sid=abc123' URL |
TLS and certificates
| Task | Command |
|---|---|
| Make a lab key and certificate | openssl req -x509 -newkey rsa:2048 -nodes -keyout key.pem -out cert.pem -days 30 -subj "/CN=localhost" -addext "subjectAltName=DNS:localhost,IP:127.0.0.1" |
| Read a certificate | openssl x509 -in cert.pem -noout -subject -issuer -dates -ext subjectAltName |
| Trust one certificate | curl -sS --cacert cert.pem https://localhost:8010/ |
| Test a name with no DNS | --resolve wrong.example:8010:127.0.0.1 |
| Inspect a handshake | echo | openssl s_client -connect localhost:8010 -servername localhost -CAfile cert.pem |
| Test a name against a certificate | openssl x509 -in cert.pem -noout -checkhost NAME |
| Check expiry in 30 days | openssl x509 -in cert.pem -noout -checkend 2592000 |
Capture and firewalls
| Task | Command |
|---|---|
| Watch packets live | sudo tcpdump -i lo -nn -A port 8000 |
| Save and read a capture | sudo tcpdump -i lo -nn -w web.pcap port 8000, tcpdump -nn -r web.pcap |
| Capture without root, in a private network | unshare -Urn sh caplab.sh, then tcpdump -nn -A -r web.pcap |
| Run the logging proxy | python3 proxy.py 8020 8000 |
| Bind to all addresses (folder with nothing secret) | cd pub && python3 -m http.server 8031 --bind 0.0.0.0 |
| Read firewall rules of a host | sudo nft list ruleset, sudo ufw status verbose, sudo iptables -L -n -v |
| Make a host a router | sysctl -w net.ipv4.ip_forward=1 (as root, in the private lab) |
| Default deny, replies allowed | nft add chain inet filter forward '{ type filter hook forward priority 0; policy drop; }', nft add rule inet filter forward ct state established,related accept |
| Drop one TCP port when forwarding | nft add rule inet filter forward tcp dport 8081 drop |
| Rewrite the source address | nft add rule ip nat post oifname s0 masquerade |
| Try a router and a firewall without root | unshare -Urn sh routerlab.sh forward.sh (also firewall.sh, nat.sh) |
| Count a token in a capture file without tcpdump | python3 -c "print(open('beacon.pcap','rb').read().count(b'T-4417-XK'))" |
Monitor and troubleshoot
| Task | Command |
|---|---|
| Climb the ladder on recorded output | python3 ladder.py cases.txt |
| Tell the rung from the curl exit code | 6 name, 7 refused, 28 timeout, 35 and 60 TLS |
| Time one request | curl -s -o /dev/null -w 'code=%{http_code} total=%{time_total}s connect=%{time_connect}s size=%{size_download}\n' URL |
| Latency, loss, throughput, availability | python3 stats.py probes.csv |
| Trace a request by stage | python3 trace.py https://localhost:8010/ --cacert cert.pem |
Safety habits
- Bind to the narrowest address that works. Keep a firewall as a second layer.
- Never turn off certificate checks. Trust the right certificate instead.
- Capture only networks you own or may test. Treat the file as secret.
- Keep
key.pemout of chat, mail and repositories. Use mode600. Serve only a folder that holds nothing secret. - Take the client address from a proxy header only when the proxy sent it.
- Test from outside. A rule list shows intent, not result.
- Name the layer in every ticket. Change one thing at a time, then run the failing test again.
- A failed ping alone is not a verdict. A success at a higher layer proves the layers below it.
- Keep IPv6 in your rules and your logs. Keep clocks in sync and write logs in UTC.