IP Addressing, Subnets and NAT
Every offensive engagement starts with a list of addresses: the scope you were given, the hosts you discover, the internal network you see once you compromise a machine. If you can’t read an address together with its mask, you don’t know what’s in scope, which machines are your neighbours, or why a packet never arrives. This page gives you the full calculation, no calculator required.
Concepts
Section titled “Concepts”IPv4 in bits
Section titled “IPv4 in bits”An IPv4 address is 32 bits, written as four decimal octets (0–255) separated by dots. To understand masks you only need the value of each bit in an octet:
| Bit | 1st | 2nd | 3rd | 4th | 5th | 6th | 7th | 8th |
|---|---|---|---|---|---|---|---|---|
| Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
All eight bits set add up to 255; all eight clear, 0. Any other value is a sum of those powers: 192 = 128 + 64, 224 = 128 + 64 + 32.
Mask and CIDR notation
Section titled “Mask and CIDR notation”The IP alone doesn’t say where the network ends. The mask marks it: bits set to 1 are the network part (the same for every machine on that network) and bits set to 0 are the host part (different for each machine). The ones are always contiguous from the left, so counting them is enough: that count is CIDR notation.
| CIDR | Mask | Addresses | Usable hosts | Typical use |
|---|---|---|---|---|
| /8 | 255.0.0.0 | 16,777,216 | 16,777,214 | Huge blocks (10.0.0.0/8) |
| /16 | 255.255.0.0 | 65,536 | 65,534 | Large corporate network |
| /24 | 255.255.255.0 | 256 | 254 | Home network, typical VLAN |
| /26 | 255.255.255.192 | 64 | 62 | Small segment |
| /30 | 255.255.255.252 | 4 | 2 | Link between two routers |
| /31 | 255.255.255.254 | 2 | 2 | Point-to-point link (RFC 3021) |
| /32 | 255.255.255.255 | 1 | 1 | A single host |
Quick rule: usable hosts = 2^(32 − prefix) − 2. Two are subtracted because the first address names the network and the last one is the broadcast (except for /31 and /32, which are special cases).
Working out network, broadcast and range
Section titled “Working out network, broadcast and range”When the prefix lands on an octet boundary (/8, /16, /24) it’s immediate: with 192.168.1.50/24, the network is 192.168.1.0, the broadcast 192.168.1.255, and hosts run from .1 to .254.
When it doesn’t, use the block size: 256 minus the mask value in the “interesting” octet (the first one that is neither 255 nor 0).
Example: 10.20.30.77/26
- A /26 mask is
255.255.255.192. The interesting octet is the fourth (192). - Block size: 256 − 192 = 64. Networks start at 0, 64, 128, 192.
- 77 falls in the block starting at 64 → network
10.20.30.64. - The next block starts at 128 → broadcast
10.20.30.127. - Usable hosts:
10.20.30.65–10.20.30.126(62 machines).
flowchart LR
A["10.20.30.77/26"] --> B["Mask 255.255.255.192"]
B --> C["Block = 256 − 192 = 64"]
C --> D["Network .64 · Broadcast .127"]
D --> E["Hosts .65 – .126"]
Are they on the same network?
Section titled “Are they on the same network?”Two machines are on the same network if their network part matches using the same mask. 192.168.1.50 and 192.168.2.10 aren’t with /24 (the third octet differs), but they are with /16. That’s why an IP without a mask tells you nothing: the same pair of addresses can be neighbours or not depending on the prefix.
This decides how the packet travels: if the destination is on your network, your machine delivers it directly (finding the MAC through ARP); if not, it hands it to the default gateway. See networking fundamentals.
Ways of sending
Section titled “Ways of sending”- Unicast: to a single machine. The normal case.
- Broadcast: to every machine on the network (the broadcast address). Used by ARP and DHCP, and it doesn’t cross routers.
- Multicast: to the machines subscribed to a group (
224.0.0.0/4).
Public, private and special
Section titled “Public, private and special”A public IP is unique on the Internet and is assigned by your provider. Private addresses (RFC 1918) are ranges reserved for internal networks: they aren’t routed on the Internet and are reused in millions of networks.
| Range | CIDR | Where you’ll see it |
|---|---|---|
| 10.0.0.0 – 10.255.255.255 | 10.0.0.0/8 | Large companies, lab VPNs |
| 172.16.0.0 – 172.31.255.255 | 172.16.0.0/12 | Mid-size networks, Docker (172.17.0.0/16) |
| 192.168.0.0 – 192.168.255.255 | 192.168.0.0/16 | Home and small networks |
| Address | Meaning |
|---|---|
| 127.0.0.0/8 | Loopback: the machine itself. Never leaves through the network card. |
| 169.254.0.0/16 | Link-local (APIPA): the machine assigns it to itself when nobody else gives it an address. A sign that DHCP didn’t answer. |
| 0.0.0.0 | On a service, “listening on all my interfaces”; in a route, “any destination” (default route). |
| 100.64.0.0/10 | Shared address space for CGNAT (RFC 6598): neither public nor RFC 1918. |
| 224.0.0.0/4 | Multicast. |
NAT lets many machines with private IPs reach the Internet through a single public IP. The router replaces your source IP with its own and records in a table which connection belongs to whom, so it can hand the reply back to the right machine.
sequenceDiagram
participant PC as PC 192.168.1.50
participant R as Router (public 203.0.113.7)
participant S as Web server
PC->>R: source 192.168.1.50:51000 → destination S:443
Note over R: Records the connection in the NAT table
R->>S: source 203.0.113.7:40001 → destination S:443
S-->>R: reply to 203.0.113.7:40001
R-->>PC: translates it and delivers it to 192.168.1.50:51000
- PAT (NAT overload): besides the IP, it also changes the source port to tell simultaneous connections apart. Every home router does this.
- CGNAT: the ISP puts several customers behind the same public IP (using
100.64.0.0/10in between). If you’re behind CGNAT, you can’t receive inbound connections even if you open ports on your router.
Practical consequence: from outside you can’t start a connection towards an internal machine, because the router doesn’t know who to hand it to. It looks like a firewall, but NAT filters nothing: as soon as there’s a port forward, an outbound connection, or unfiltered IPv6, the machine is reachable.
In practice
Section titled “In practice”# Your IP, mask and default gatewayip -4 a # inet 192.168.1.50/24 → IP and prefixip r # "default via 192.168.1.1" → default gateway# Calculate a subnet without doing the maths (Kali ships ipcalc)ipcalc 10.20.30.77/26# Network: 10.20.30.64/26# HostMin: 10.20.30.65# HostMax: 10.20.30.126# Broadcast: 10.20.30.127# Hosts/Net: 62# Check the scope before scanning: -sL lists the targets# WITHOUT sending them a single packetnmap -sL -n 10.20.30.64/26 | head# Same thing in Python, handy in your scriptsimport ipaddress
net = ipaddress.ip_network("10.20.30.77/26", strict=False)print(net.network_address, net.broadcast_address, net.num_addresses - 2)# 10.20.30.64 10.20.30.127 62
print(ipaddress.ip_address("10.20.30.100") in net) # True: in scopeprint(ipaddress.ip_address("172.33.5.4").is_private) # False: it's publicNext offensive step
Section titled “Next offensive step”- Discover hosts in the range: network enumeration and nmap.
- Once you compromise a machine,
ip aandip rreveal new internal networks: the starting point for pivoting. - A network “protected by NAT” can still be exposed over IPv6: see IPv6.
Cheat sheet
Section titled “Cheat sheet”| Concept | What to remember |
|---|---|
| Octet | 8 bits: 128 64 32 16 8 4 2 1. All set = 255. |
| CIDR | Number of network bits. /24 = 255.255.255.0. |
| Usable hosts | 2^(32 − prefix) − 2. |
| Block size | 256 − mask value in the interesting octet. |
| Network · broadcast | First address of the block · last. |
| IP without mask | Useless: the mask decides who is a neighbour. |
| RFC 1918 | 10/8 · 172.16/12 (16 to 31) · 192.168/16. |
| 169.254.x.x | DHCP didn’t answer. |
| 100.64/10 | CGNAT: you can’t receive inbound connections. |
| NAT | Swaps the source for the public IP. Blocks inbound, but doesn’t filter. |
Review questions
Section titled “Review questions”-
What are the network address, broadcast and host range of
172.16.5.200/27?Answer
/27 = 255.255.255.224; block = 256 − 224 = 32. Blocks start at 192 and 224, so 200 falls in the 192 block. Network
172.16.5.192, broadcast172.16.5.223, hosts.193–.222(30 machines). -
Are
10.1.4.20and10.1.7.9on the same network with /22?Answer
Yes. /22 = 255.255.252.0; in the third octet the block is 256 − 252 = 4, and blocks start at 0, 4, 8… Both 4 and 7 fall in the 4–7 block, so both are in
10.1.4.0/22. -
Is
172.33.5.4a private address?Answer
No. The private range is 172.16.0.0 – 172.31.255.255; with 33 in the second octet it’s outside. It’s a public IP.
-
A machine has
169.254.3.7. What happened and where do you start?Answer
It self-assigned a link-local address because nobody gave it one via DHCP. Check whether the interface has link (
ip link), whether there’s a DHCP server on that network and, in a VM, the adapter’s network mode. -
Your Kali is behind your home NAT and you send a reverse shell to your public IP. Why doesn’t it arrive and how do you fix it?
Answer
The connection reaches the router’s public IP, which doesn’t know which internal machine to hand it to. You need a port forward to Kali or (the usual case in labs) to use the IP the lab VPN gives you, which the victim can reach. If you’re behind CGNAT, a port forward on your router isn’t enough either.
-
Why isn’t NAT a firewall?
Answer
NAT translates addresses; it doesn’t evaluate rules on traffic. It blocks unsolicited inbound connections only as a side effect, and stops doing so with a port forward, with connections the internal machine starts outwards, or with unfiltered IPv6.
References
Section titled “References”- RFC 791 — Internet Protocol
- RFC 1918 — Address Allocation for Private Internets
- RFC 4632 — Classless Inter-domain Routing (CIDR)
- RFC 3927 — Dynamic Configuration of IPv4 Link-Local Addresses
- RFC 6598 — IANA-Reserved IPv4 Prefix for Shared Address Space
- IANA — IPv4 Special-Purpose Address Registry
- Python — ipaddress module