TExcelTAD

CIDR and Subnetting for Analysts

Security work is full of address ranges. A penetration-test scope says "10.10.0.0/26 only". A firewall rule allows 198.51.100.0/23. An alert names 10.10.0.77 and you need to know whether that is the server subnet, the guest Wi-Fi or outside the network entirely. Getting a prefix wrong by one bit doubles or halves a range, which can mean testing systems you have no permission to touch, or a firewall rule that opens twice what was intended. The arithmetic is taught in Subnetting & CIDR and VLSM; this lesson is about using it quickly and correctly in security tasks.

The idea in one picture

A prefix /n says the first n bits are the network and the remaining 32 − n are the host part. The network address has all host bits 0, the broadcast address all host bits 1. Move the prefix slider and watch the boundary between network bits (blue) and host bits (amber).

octet 1 = 10octet 2 = 10octet 3 = 0octet 4 = 77address10.10.0.77/2600001010000010100000000001001101mask255.255.255.19211111111111111111111111111000000network10.10.0.6400001010000010100000000001000000broadcast10.10.0.12700001010000010100000000001111111/26network: 26 bitshost: 6 bitsnetwork = address AND maskNetwork10.10.0.64/26Broadcast10.10.0.127First host10.10.0.65Last host10.10.0.126Kindprivate (RFC 1918)Mask255.255.255.192 = 0xFFFFFFC0Wildcard0.0.0.63Addresses2^6 = 64Usable hosts62network and broadcast addresses are not usable as hosts

With /26 the host part is 6 bits: blocks of 64 addresses starting at .0, .64, .128 and .192. 10.10.0.77 falls in 10.10.0.64/26: network .64, broadcast .127, hosts .65 to .126 (62 usable). Change the prefix to /24 and the same address is just one host of 254 in 10.10.0.0/24.

Definitions

  • CIDR notation a.b.c.d/n; the netmask is n one-bits (/26 = 255.255.255.192); the wildcard (used in ACLs on some routers) is its inverse (0.0.0.63).
  • Total addresses 232−n2^{32-n}; usable hosts 232−n−22^{32-n} - 2 (minus network and broadcast), except /31 (point-to-point links, both usable, RFC 3021) and /32 (one host).
  • Special ranges every analyst should recognise: private RFC 1918 (10/8, 172.16/12, 192.168/16), loopback 127/8, link-local 169.254/16 (a host that failed DHCP), carrier-grade NAT 100.64/10, documentation 192.0.2/24, 198.51.100/24, 203.0.113/24, multicast 224/4.

Scope checks with ipaddress

Python's ipaddress module does the bit work exactly. The program checks a list of targets against a written test scope and an exclusion list, the check a tester should run before every scan.

python · try it yourself
import ipaddress as ipa

scope = [ipa.ip_network("10.10.0.0/26"), ipa.ip_network("192.0.2.128/25")]
excluded = [ipa.ip_network("10.10.0.1/32"),      # the lab router: out of scope
            ipa.ip_network("192.0.2.200/29")]    # a partner's systems

targets = ["10.10.0.10", "10.10.0.1", "10.10.0.77", "192.0.2.130", "192.0.2.203", "192.0.2.12"]
for t in targets:
    a = ipa.ip_address(t)
    inside = next((n for n in scope if a in n), None)
    out = next((n for n in excluded if a in n), None)
    if inside and not out:
        verdict = f"IN SCOPE ({inside})"
    elif out:
        verdict = f"EXCLUDED by {out}"
    else:
        verdict = "NOT IN SCOPE"
    print(f"{t:<13} {verdict}")

n = scope[0]
print(f"\n{n}: netmask {n.netmask}, wildcard {n.hostmask}, {n.num_addresses} addresses, "
      f"hosts {next(n.hosts())} - {list(n.hosts())[-1]}, broadcast {n.broadcast_address}")
10.10.0.10    IN SCOPE (10.10.0.0/26)
10.10.0.1     EXCLUDED by 10.10.0.1/32
10.10.0.77    NOT IN SCOPE
192.0.2.130   IN SCOPE (192.0.2.128/25)
192.0.2.203   EXCLUDED by 192.0.2.200/29
192.0.2.12    NOT IN SCOPE

10.10.0.0/26: netmask 255.255.255.192, wildcard 0.0.0.63, 64 addresses, hosts 10.10.0.1 - 10.10.0.62, broadcast 10.10.0.63

10.10.0.77 is outside the /26, as the lab showed: a tester who read the scope as "10.10.0.x" would have scanned it without permission. Written scopes should use CIDR, and tools should read them from a file rather than from memory.

Summarising addresses from an incident

During an incident you collect addresses from logs and want the smallest set of blocks that covers them, for a block list or a report. collapse_addresses merges adjacent networks, and checking each block against the special ranges tells you whether it can be "the Internet" at all.

python · try it yourself
import ipaddress as ipa

seen = ["203.0.113.8", "203.0.113.9", "203.0.113.10", "203.0.113.11",
        "198.51.100.64", "198.51.100.65", "10.10.0.5", "169.254.10.3"]
SPECIAL = [("10.0.0.0/8", "private (RFC 1918)"), ("172.16.0.0/12", "private (RFC 1918)"),
           ("192.168.0.0/16", "private (RFC 1918)"), ("169.254.0.0/16", "link-local"),
           ("100.64.0.0/10", "carrier-grade NAT"), ("192.0.2.0/24", "documentation (RFC 5737)"),
           ("198.51.100.0/24", "documentation (RFC 5737)"), ("203.0.113.0/24", "documentation (RFC 5737)")]

def kind(block):
    return next((label for net, label in SPECIAL if block.subnet_of(ipa.ip_network(net))), "public")

blocks = list(ipa.collapse_addresses(ipa.ip_network(a + "/32") for a in seen))
for b in blocks:
    print(f"{str(b):<18} {b.num_addresses:>2} address(es)  {kind(b)}")

# A firewall rule meant to cover 198.51.100.0-198.51.100.255, written with a prefix one bit too short
rule = ipa.ip_network("198.51.100.0/23", strict=False)
print("\nrule", rule, "covers", rule.num_addresses, "addresses:", rule[0], "-", rule[-1])
10.10.0.5/32        1 address(es)  private (RFC 1918)
169.254.10.3/32     1 address(es)  link-local
198.51.100.64/31    2 address(es)  documentation (RFC 5737)
203.0.113.8/30      4 address(es)  documentation (RFC 5737)

rule 198.51.100.0/23 covers 512 addresses: 198.51.100.0 - 198.51.101.255

Four consecutive addresses collapsed into one /30. The last lines show the classic error: /23 covers 512 addresses, including the 256 of the next block, which was never meant to be allowed.

How mistakes are detected

  • Scope files and allow lists reviewed as CIDR, with the address count printed next to each entry. A reviewer notices "512" faster than a wrong digit in a mask.
  • Overlap checks: two firewall rules or two VLANs whose prefixes overlap (a.overlaps(b) in Python) usually mean one of them is wrong.
  • Link-local addresses (169.254.x.x) in logs mean a host failed to get a DHCP lease, which in turn can point to a DHCP outage or a rogue server.

How they are prevented

  • Generate rules and scan target lists from one source of truth (an IP address management system or a reviewed scope file), not by hand.
  • Use the smallest prefix that does the job and list exclusions explicitly.
  • Prefer ipaddress (or your tool's CIDR parser) to string matching: "10.10.0.1" in "10.10.0.100" is True as strings.

Pitfalls

  • Confusing netmask and wildcard: 0.0.0.63 in a Cisco-style ACL is a /26, not a host.
  • Forgetting that /31 and /32 have no network/broadcast pair.
  • ip_network("10.10.0.77/26") raises an error because host bits are set; strict=False silently turns it into 10.10.0.64/26. Know which you want.

Practice

1. How many usable hosts are in 10.10.4.0/22, and what is its broadcast address?

210−2=10222^{10} - 2 = 1022 usable hosts; the block runs 10.10.4.0 to 10.10.7.255, so the broadcast is 10.10.7.255.

2. A scope says 192.0.2.64/27. Is 192.0.2.97 in scope?

No. A /27 is 32 addresses: 192.0.2.64 to 192.0.2.95. .97 belongs to the next block, 192.0.2.96/27.

3. Which single prefix covers exactly 203.0.113.8 to 203.0.113.15?

203.0.113.8/29 (8 addresses, aligned on a multiple of 8).

Practice problems in 21 languages: Parse an IPv4 Address, Subnet Address Range, Is the Address in the Network?.

Test yourself

Which /26 block contains 10.10.0.77?

  1. 10.10.0.0/26
  2. 10.10.0.64/26
  3. 10.10.0.77/26
  4. 10.10.0.128/26

How many addresses does 198.51.100.0/23 cover?

  1. 256
  2. 510
  3. 512
  4. 1024

A host in your logs has the address 169.254.10.3. What does it usually mean?

  1. It is a public server
  2. It failed to get a DHCP lease and self-assigned a link-local address
  3. It is a multicast group
  4. It is behind carrier-grade NAT

What is the wildcard mask of a /26?

  1. 255.255.255.192
  2. 0.0.0.63
  3. 0.0.0.64
  4. 255.255.255.63