Skip to content

Beginner · Networking for security · topic 7 of 43 · 30 min

IP addresses, subnets and CIDR

Explanation

An IP address identifies a device on a network, like 192.168.1.10. A subnet is a group of addresses written in CIDR form, like 192.168.1.0/24, where /24 says how many bits are fixed. Defenders use this every day: to tell private from public addresses, to write firewall rules and to check whether an address is inside a range.

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.

RangeCIDRUse
10.0.0.0 – 10.255.255.25510.0.0.0/8Private (large companies, cloud networks)
172.16.0.0 – 172.31.255.255172.16.0.0/12Private
192.168.0.0 – 192.168.255.255192.168.0.0/16Private (home routers)
127.0.0.0 – 127.255.255.255127.0.0.0/8Loopback: the machine itself (127.0.0.1)
169.254.0.0 – 169.254.255.255169.254.0.0/16Link-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).

CIDRFixed bitsAddressesUsable hosts
/2424256254
/2525128126
/26266462
/28281614
/303042
/32321one 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

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)     # True
The ipaddress module in the standard library does subnet maths for you.

Seeing your own address

bash
$ ip -brief address
lo        UNKNOWN  127.0.0.1/8 ::1/128
enp0s3    UP       192.168.1.23/24 fe80::a00:27ff:fe4e:66a1/64
Linux: your address and prefix on each interface.
powershell
PS> Get-NetIPAddress -AddressFamily IPv4 | Select-Object InterfaceAlias, IPAddress, PrefixLength

InterfaceAlias IPAddress     PrefixLength
-------------- ---------     ------------
Wi-Fi          192.168.1.42            24
Loopback...    127.0.0.1                8
Windows PowerShell: the same information. The older ipconfig command also works.

Common 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.
Tip: Learn by heart: /24 = 256 addresses, /16 = 65,536, /8 = about 16.7 million. Each step of one bit doubles or halves the size.

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

Inside? Private? In scope?

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.

python
import ipaddress
vpn = ipaddress.ip_network("10.99.0.0/24")
print(ipaddress.ip_address("10.99.0.15") in vpn)
Shows True: the login came through the VPN.

Practice (0/3 done)

Task 1 · Question

How many addresses are in 192.168.5.0/24?

Task 2 · Code · Python

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.

Task 3 · Code · Python

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

Question 1

1.Which address is private?

Question 2

2.How many usable hosts are in a /28?

Question 3

3.What does 127.0.0.1 mean?

Question 4

4.Which range is reserved for documentation examples?

Question 5

5.A login comes from 10.4.8.20. What can you safely conclude?

Read more (free)

Before this, learn

This topic builds on these. If something here feels new, open them first.

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.

From VPC basics: CIDR, subnets and gateways

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.

From IPv4 address structure and special ranges

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.

From Network, broadcast and usable host range

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.

From Validating addresses and checking the same subnet

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

  1. 3 more topics in “Networking for security” before its exam: Ports and protocols, DNS: the internet's phone book, HTTP, HTTPS and TLS.
  2. Module 2 exam: Networking for security · 12 questions · 20 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Systems and cryptography. 43 topics left in the course before the final exam.

← Previous: TCP/IP and the network layers