Firewalls, IDS/IPS and Network Segmentation Explained
Networks are protected by layers of controls that filter traffic, watch for suspicious behaviour and limit how far an intruder can travel. The core building blocks are firewalls, intrusion detection and prevention systems (IDS/IPS) and network segmentation. This lesson explains how each works, how to write real rules, and how to practise safely in your lab with free tools.
Firewalls: What They Do
A firewall allows or blocks network traffic according to rules. The best practice is a default-deny policy: block everything, then permit only what is needed.
| Type | How it decides | Strengths and limits |
|---|---|---|
| Packet filter (stateless) | Looks at each packet’s addresses, ports and protocol | Fast and simple; no memory of connections |
| Stateful firewall | Tracks connections and allows replies to permitted outgoing traffic | Standard today; much more secure than stateless rules |
| Next-generation firewall (NGFW) | Adds application awareness, user identity, intrusion prevention and TLS inspection | Better visibility; needs careful tuning and capacity |
| Web application firewall (WAF) | Inspects HTTP requests for attack patterns | Useful safety net and virtual patching; not a substitute for secure code |
| Host-based firewall | Runs on each computer | Protects against threats already inside the network |
Rule design principles
- Default deny inbound and, in sensitive networks, outbound too.
- Allow by specific source, destination, port and protocol, not “any to any”.
- Order rules carefully: firewalls usually evaluate from the top and stop at the first match.
- Document each rule with a reason, owner and review date.
- Review and remove unused or overly broad rules at least twice a year.
- Log denied traffic and key allowed traffic, and send logs to a central system.
Practice Lab 1: Build a Host Firewall on Linux
Use your lab Linux VM. The examples use ufw (Uncomplicated Firewall) and then show the underlying nftables idea.
# Start from a clean policy: deny incoming, allow outgoing
sudo ufw default deny incoming
sudo ufw default allow outgoing
# Allow SSH only from your analyst VM, and web from the lab network
sudo ufw allow from 192.168.56.101 to any port 22 proto tcp
sudo ufw allow from 192.168.56.0/24 to any port 80 proto tcp
sudo ufw logging on
sudo ufw enable
sudo ufw status verbose
Test from the analyst VM using the Nmap skills from the scanning lesson: ports you did not allow should now be filtered. Remove the rule for port 80, scan again and compare. Always keep console access to your VM so that you cannot lock yourself out.
A small nftables example
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
ct state established,related accept
iif lo accept
ip saddr 192.168.56.101 tcp dport 22 accept
ip saddr 192.168.56.0/24 tcp dport 80 accept
}
}
Read it line by line: the default policy is drop; return traffic for existing connections is allowed; local loopback is allowed; then two narrow allowances.
Intrusion Detection and Prevention
| System | Role | Placement |
|---|---|---|
| IDS (detection) | Watches a copy of traffic or host events and raises alerts | Network tap or span port; or on hosts |
| IPS (prevention) | Sits in the path and can block malicious traffic | Inline at network boundaries and between segments |
| NIDS / NIPS | Network-based | Monitors network segments |
| HIDS | Host-based, such as file integrity and log analysis (for example, Wazuh, OSSEC) | On servers and workstations |
Detection methods
- Signature-based: matches known attack patterns. Accurate for known threats; blind to new ones.
- Anomaly-based: learns normal behaviour and flags deviations. Can find new threats; may produce false positives.
- Behavioural and threat-intelligence-driven: uses indicators and models from external sources; see Threat Intelligence and Threat Hunting.
Every detection system produces false positives (alerts on harmless activity) and false negatives (missed attacks). Tuning balances the two and is continuous work.
Practice Lab 2: Install Suricata and Write a Rule
Suricata is a free IDS/IPS. Install it on a monitoring VM in your lab on the host-only network.
sudo apt install suricata
sudo suricata-update # fetches the free Emerging Threats Open rule set
sudo nano /etc/suricata/suricata.yaml # set the interface (for example enp0s8) and HOME_NET to 192.168.56.0/24
sudo systemctl restart suricata
sudo tail -f /var/log/suricata/fast.log
Write a simple custom rule in /var/lib/suricata/rules/local.rules (add it to the rule files in the config) that alerts when anything on the lab network connects to a made-up internal service port:
alert tcp $HOME_NET any -> $HOME_NET 4444 (msg:"LAB Unexpected connection to port 4444"; sid:1000001; rev:1;)
Reload the rules, then use nc or nmap from the analyst VM against that port on your own target and watch the alert appear in fast.log. Then run an Nmap scan and observe the standard scan-detection alerts. Examine each alert: source, destination, signature, severity.
Network Segmentation
Segmentation divides a network into zones so that a compromise in one area does not spread everywhere. It is among the most effective defences against lateral movement; see Privilege Escalation and Lateral Movement.
A typical zone design
| Zone | Contains | Typical policy |
|---|---|---|
| Internet (untrusted) | Everything outside | Only specific services in via the DMZ |
| DMZ | Public web servers, mail gateways, reverse proxies | May receive internet traffic; limited access to internal zones |
| User network | Employee laptops and desktops | Internet via proxy; limited access to servers by need |
| Server network | Application and database servers | Databases accept only application servers |
| Management network | Admin tools, jump hosts, backup, monitoring | Reachable only from admin workstations with MFA |
| Guest and IoT | Visitors, printers, cameras, smart devices | Internet only; no access to internal networks |
Methods include VLANs with firewall rules between them, separate physical networks, cloud security groups and, increasingly, micro-segmentation, which applies policy to individual workloads. See Zero Trust Architecture for where this leads.
Practice Lab 3: Segment Your Lab
- Add a third VM to your lab as a “database” and place it on a separate host-only network (or use a different subnet and firewall rules).
- Write firewall rules so that only the “web” VM can reach the database port, and the analyst VM cannot.
- Verify with Nmap from each machine. Record the results in a table of expected and actual reachability.
- Draw a network diagram and label each zone and permitted flow.
Common Mistakes
- Rules with “any” source or destination “just to make it work”, and never removed.
- Alerts that nobody reads, so detection is useless.
- Putting an IPS inline without testing, causing outages from false positives.
- Flat networks with no internal controls.
- Ignoring outbound traffic, which is often how malware calls home or data leaves.
Practice Exercises
- Explain the difference between stateless and stateful filtering using a postal analogy.
- Write five firewall rules for a small office: allow web browsing out, allow a web server in via the DMZ, block everything else.
- Describe one situation where an IDS is preferable to an IPS, and one where the reverse is true.
- Explain false positives and false negatives and how you would tune an alert that fires too often.
- Draw a segmented network for a hospital with clinical devices, administration and guest Wi-Fi.
Mentor tip: A firewall rule base is like a garden: it needs regular weeding. Schedule a rule review, delete what has not been hit in months and write the reason for every rule you keep.
Educational use only: configure and test these tools only on networks and systems you own or are authorised to administer.
- Use stateful firewalls with default-deny rules, and add host-based firewalls.
- IDS detects and alerts; IPS can block inline. Both need tuning to manage false positives.
- Signature detection catches known threats; anomaly detection can find new ones.
- Segment networks into zones to limit lateral movement.
- Review rules regularly and centralise logs.