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.

TypeHow it decidesStrengths and limits
Packet filter (stateless)Looks at each packet’s addresses, ports and protocolFast and simple; no memory of connections
Stateful firewallTracks connections and allows replies to permitted outgoing trafficStandard today; much more secure than stateless rules
Next-generation firewall (NGFW)Adds application awareness, user identity, intrusion prevention and TLS inspectionBetter visibility; needs careful tuning and capacity
Web application firewall (WAF)Inspects HTTP requests for attack patternsUseful safety net and virtual patching; not a substitute for secure code
Host-based firewallRuns on each computerProtects against threats already inside the network

Rule design principles

  1. Default deny inbound and, in sensitive networks, outbound too.
  2. Allow by specific source, destination, port and protocol, not “any to any”.
  3. Order rules carefully: firewalls usually evaluate from the top and stop at the first match.
  4. Document each rule with a reason, owner and review date.
  5. Review and remove unused or overly broad rules at least twice a year.
  6. 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

SystemRolePlacement
IDS (detection)Watches a copy of traffic or host events and raises alertsNetwork tap or span port; or on hosts
IPS (prevention)Sits in the path and can block malicious trafficInline at network boundaries and between segments
NIDS / NIPSNetwork-basedMonitors network segments
HIDSHost-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

ZoneContainsTypical policy
Internet (untrusted)Everything outsideOnly specific services in via the DMZ
DMZPublic web servers, mail gateways, reverse proxiesMay receive internet traffic; limited access to internal zones
User networkEmployee laptops and desktopsInternet via proxy; limited access to servers by need
Server networkApplication and database serversDatabases accept only application servers
Management networkAdmin tools, jump hosts, backup, monitoringReachable only from admin workstations with MFA
Guest and IoTVisitors, printers, cameras, smart devicesInternet 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

  1. 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).
  2. Write firewall rules so that only the “web” VM can reach the database port, and the analyst VM cannot.
  3. Verify with Nmap from each machine. Record the results in a table of expected and actual reachability.
  4. 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

  1. Explain the difference between stateless and stateful filtering using a postal analogy.
  2. Write five firewall rules for a small office: allow web browsing out, allow a web server in via the DMZ, block everything else.
  3. Describe one situation where an IDS is preferable to an IPS, and one where the reverse is true.
  4. Explain false positives and false negatives and how you would tune an alert that fires too often.
  5. 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.

Key takeaways
  • 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.

Related Lessons