Beginner · Networking for security · topic 7 of 43 · 30 min
IP addresses, subnets and CIDR
Explanation
Why this matters for security
Logs are full of IP addresses. The first questions an analyst asks are: is this address inside our network or outside? Is it private? Which office or cloud range does it belong to? Is it in the list of addresses we are allowed to test? All of these need subnet maths.
IPv4 addresses
An IPv4 address is 32 bits (ones and zeros), written as four numbers from 0 to 255 separated by dots, like 192.168.1.10. Each number is one octet (8 bits). There are about 4.3 billion IPv4 addresses, which is not enough for every device, so IPv6 was created: 128-bit addresses written in hexadecimal, like 2001:db8::1.
Private and public addresses
Some ranges are reserved for private networks inside homes and offices. They are not routed on the internet. Your router uses NAT (network address translation) to share one public address among all private devices.
| Range | CIDR | Use |
|---|---|---|
| 10.0.0.0 – 10.255.255.255 | 10.0.0.0/8 | Private (large companies, cloud networks) |
| 172.16.0.0 – 172.31.255.255 | 172.16.0.0/12 | Private |
| 192.168.0.0 – 192.168.255.255 | 192.168.0.0/16 | Private (home routers) |
| 127.0.0.0 – 127.255.255.255 | 127.0.0.0/8 | Loopback: the machine itself (127.0.0.1) |
| 169.254.0.0 – 169.254.255.255 | 169.254.0.0/16 | Link-local: no DHCP answer was received |
Three small ranges are reserved for documentation and examples: 192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24. This course uses them in example logs so we never point at a real company.
CIDR: describing a range
CIDR (Classless Inter-Domain Routing) notation writes a network as an address plus a slash and a number, the prefix length. The prefix length says how many of the 32 bits are fixed for the network. The rest are free for hosts (devices).
| CIDR | Fixed bits | Addresses | Usable hosts |
|---|---|---|---|
| /24 | 24 | 256 | 254 |
| /25 | 25 | 128 | 126 |
| /26 | 26 | 64 | 62 |
| /28 | 28 | 16 | 14 |
| /30 | 30 | 4 | 2 |
| /32 | 32 | 1 | one single address |
The number of addresses is 2 to the power of (32 minus the prefix). In a normal subnet two addresses are not used by hosts: the first is the network address and the last is the broadcast address. So a /24 has 256 addresses and 254 usable hosts.
Doing it in Python
import ipaddress
net = ipaddress.ip_network("192.168.10.0/26")
print(net.num_addresses) # 64
print(net.network_address, net.broadcast_address)
print(ipaddress.ip_address("192.168.10.50") in net) # True
print(ipaddress.ip_address("192.168.10.70") in net) # False
print(ipaddress.ip_address("10.4.2.1").is_private) # TrueSeeing your own address
$ ip -brief address
lo UNKNOWN 127.0.0.1/8 ::1/128
enp0s3 UP 192.168.1.23/24 fe80::a00:27ff:fe4e:66a1/64PS> Get-NetIPAddress -AddressFamily IPv4 | Select-Object InterfaceAlias, IPAddress, PrefixLength
InterfaceAlias IPAddress PrefixLength
-------------- --------- ------------
Wi-Fi 192.168.1.42 24
Loopback... 127.0.0.1 8Common mistakes
- Thinking a private IP in a log means "safe". An attacker already inside your network uses private addresses too.
- Thinking an IP address always equals one person. Many people share one public IP through NAT, mobile networks and VPNs.
- Writing a firewall rule for 10.0.0.0/8 when you meant one /24. A small typo in the prefix can open a huge range.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
IP addresses, subnets and CIDR · 1/7
Logs are full of IP addresses. Subnet maths answers: inside or outside, private or public, in scope or not.
Prefer a video by a teacher? Find free videos on “subnetting CIDR explained for beginners private IP ranges” on YouTube ↗
In real life: Is this login from inside the office?
A company in Hyderabad has its office network on 10.20.0.0/16 and its VPN users on 10.99.0.0/24. An alert says an admin logged in from 10.99.0.15 at 3 am. The analyst checks: the address is inside the VPN range, so the next question is which VPN account it was and whether that person was really working at 3 am.
import ipaddress
vpn = ipaddress.ip_network("10.99.0.0/24")
print(ipaddress.ip_address("10.99.0.15") in vpn)Practice (0/3 done)
How many addresses are in 192.168.5.0/24?
Use the ipaddress module. Write three functions: usable_hosts(cidr) returns the number of addresses minus 2 (network and broadcast); contains(cidr, ip) returns True if the ip is inside the network; is_private(ip) returns True for private addresses.
Before any test you must check scope. Write in_scope(ip, scope) where scope is a list of CIDR strings. Return True if the ip is inside ANY of them.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Ports and protocolsNetworking for security · same module
- DNS: the internet's phone bookNetworking for security · same module
- VPC basics: CIDR, subnets and gatewaysCloud Computing (AWS, Azure, Google Cloud) · Networking and databases: VPC and RDS
- IPv4 address structure and special rangesComputer Networking (CCNA level) · IPv4 addressing
- Network, broadcast and usable host rangeComputer Networking (CCNA level) · IPv4 addressing
- Validating addresses and checking the same subnetComputer Networking (CCNA level) · IPv4 addressing
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Which address is private?
Answer: 172.20.1.5
172.16.0.0/12 covers 172.16.x.x to 172.31.x.x, so 172.20.1.5 is private; 172.32.1.5 is not.
How many usable hosts are in a /28?
Answer: 14
2^(32−28) = 16 addresses, minus network and broadcast = 14.
What does 127.0.0.1 mean?
Answer: The machine itself (loopback)
127.0.0.0/8 is loopback: traffic stays on the same machine.
Which range is reserved for documentation examples?
Answer: 203.0.113.0/24
192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24 are for documentation.
A login comes from 10.4.8.20. What can you safely conclude?
Answer: It came from inside a private network, which still needs checking
Private means internal, not trusted. Insiders and attackers who got in also use private addresses.
How many usable addresses does an AWS subnet 10.0.5.0/26 have?Related
Answer: 59
A /26 has 64 addresses; AWS reserves 5, leaving 59.
Why do private addresses and NAT exist?Related
Answer: Because there are not enough public IPv4 addresses for every device
IPv4 has about 4.3 billion addresses, fewer than the number of devices. Private ranges are reused everywhere and NAT shares a few public addresses.
Is 192.168.0.255/23 a usable host address?Related
Answer: Yes, the subnet runs from 192.168.0.0 to 192.168.1.255
In a /23 the broadcast is 192.168.1.255. 192.168.0.255 is in the middle of the range, so it is a normal host.
Which of these is a valid IPv4 address?Related
Answer: 172.16.254.1
172.16.254.1 has four parts, each from 0 to 255.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 3 more topics in “Networking for security” before its exam: Ports and protocols, DNS: the internet's phone book, HTTP, HTTPS and TLS.
- Module 2 exam: Networking for security · 12 questions · 20 min, without help. 5 topics of this module still to finish first.
- Then the next module, Systems and cryptography. 43 topics left in the course before the final exam.