Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 31 ยท Wireless and Network Defense

Firewalls, IDS and Log Monitoring

Learn how firewalls (iptables, nftables), intrusion detection (Snort, Suricata) and SIEM log monitoring work together, how to tune noisy alerts, and test rules with a Python stand-in.

A network without controls lets any computer talk to any other on any port. Defense adds layers. A firewall decides which connections are allowed at all. An intrusion detection system (IDS) watches the allowed traffic and raises an alert when something looks wrong. Log monitoring collects what every system reports so a person can notice a pattern across machines. None of the three is enough alone: the firewall cannot see inside an allowed connection, the IDS cannot stop what it only watches, and logs are useless if nobody reads them.

This lesson explains the three layers, shows real firewall and rule syntax as read-only examples, and ends with Python stand-ins that apply sample rules to sample traffic so you can see how first-match logic and signatures behave. It builds on TCP/IP Essentials for Security and Reading Web Server Logs for Attacks. The tools themselves cannot run on this site; use a virtual machine from Building a Safe Lab to try them.

Three layers of defense

Firewall Allows or blocks connections.
IDS Watches traffic for known bad patterns.
Log monitoring Gathers events from many systems.

Prevent, detect, investigate.

1 / 2

Firewalls: default deny

A firewall compares each packet or connection with an ordered list of rules. The first rule that matches decides the outcome (accept, drop or reject). If nothing matches, the default policy applies. The strongest design is default deny: block everything, then allow only what you can name and justify.

Two properties separate firewall types:

  • A stateless filter judges each packet alone. A stateful firewall tracks connections, so the reply to a connection you started is allowed automatically while an unsolicited packet is not. Almost all modern firewalls are stateful.
  • A network firewall looks at addresses and ports. A web application firewall (WAF) reads HTTP, and can spot patterns such as SQL fragments, but it is a safety net rather than a cure for vulnerable code (see Injection and Input Validation: Defense).

On Linux the kernel filter is netfilter. You talk to it with iptables (the classic tool) or nftables (nft, the modern replacement). Frontends such as ufw and firewalld write the rules for you. A small server policy in iptables looks like this (read-only; run rules only on machines you administer, and keep a second way in so a mistake does not lock you out):

Terminal
# Default policy: drop incoming and forwarded packets, allow outgoing
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT

# Allow loopback and replies to connections we started
sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

# Allow SSH from the admin network only, and public web traffic
sudo iptables -A INPUT -p tcp --dport 22 -s 192.0.2.0/24 -j ACCEPT
sudo iptables -A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT

The same idea in nftables is written as one structured file:

TEXT
table inet filter {
  chain input {
    type filter hook input priority 0; policy drop;
    iifname "lo" accept
    ct state established,related accept
    ip saddr 192.0.2.0/24 tcp dport 22 accept
    tcp dport { 80, 443 } accept
  }
}

Order matters in both. A broad accept placed above a narrow drop makes the drop unreachable. When you edit rules, list them (iptables -L -n -v, nft list ruleset) and read them top to bottom. Remember that DROP silently discards a packet while REJECT tells the sender, which is friendlier on an internal network but gives scanners more information on the internet.

Also use the egress direction. Blocking outgoing traffic that servers have no reason to send limits the damage if one is compromised, because malware often needs to call home. Ports that exist only for management (SSH, databases, admin panels) belong behind a VPN or an allowlist, not open to the world. Network Exposure Auditor turns this into a project.

Intrusion detection: signatures and anomalies

An IDS inspects traffic that the firewall let through. It comes in two common forms: a network IDS (sensor on a switch mirror port or tap, such as Snort or Suricata) and a host IDS (agent on a machine that watches files and logs, such as OSSEC or Wazuh).

Detection works in two main ways:

  • Signature-based: a rule describes a known bad pattern. It is accurate for known threats and blind to new ones.
  • Anomaly-based: the system learns what normal looks like and flags deviations. It can catch new activity, but it produces more false alarms.

A Snort or Suricata rule has a header (action, protocol, source, destination) and options in parentheses. This example, written in the shared rule style, alerts when a web request tries to read a system file through the URL (read-only):

TEXT
alert http any any -> $HOME_NET any (
    msg:"Possible path traversal to /etc/passwd";
    flow:to_server,established;
    http.uri; content:"../"; content:"etc/passwd"; distance:0;
    classtype:web-application-attack;
    sid:1000001; rev:1;
)

Read it in parts: what to look at (http.uri content), how (content matches in order), direction (to_server), label (msg, classtype) and identity (sid is the rule number, rev its version; numbers from 1,000,000 up are reserved for local rules). Suricata can also write structured JSON events to a file (eve.json), which makes the data easy to ship to other tools.

An IDS can be placed inline as an IPS (intrusion prevention system) so that matching traffic is dropped, not just reported. The risk is obvious: a rule that is too broad blocks legitimate users. Many teams run new rules in alert-only mode until they have measured the false positives.

Encrypted traffic is the big limit. A network sensor cannot read inside TLS, so it sees addresses, sizes and timing but not the request. That is why host-based logs and web-server logs are still essential.

Log monitoring and the SIEM idea

Every system writes logs: the web server, the SSH daemon (/var/log/auth.log or the journal), the firewall, the database. Looking at one machine at a time does not scale, and an intruder who controls a machine can erase its local logs. A SIEM (security information and event management) tool solves both problems:

  1. Collect: agents or syslog forward events to a central, protected store.
  2. Normalize: different formats become common fields (time, source address, user, action, outcome).
  3. Correlate: rules combine events: five failed logins followed by a success from one address is more interesting than either event alone.
  4. Alert and investigate: people receive a ranked list and can search the history.
  5. Retain: keep logs long enough for investigations and legal needs, and no longer than necessary, because they contain personal data.

Open-source examples include Wazuh, Graylog and the Elastic stack; commercial products fill the same role. The common Linux building block is rsyslog or journald shipping to a remote host.

flowchart LR
    A["Firewall logs"] --> S["Central log store (SIEM)"]
    B["IDS alerts"] --> S
    C["Web server logs"] --> S
    D["Auth logs"] --> S
    S --> R["Correlation rules"]
    R --> T["Analyst triage"]

Many sources, one place to search and correlate.

Tuning: fewer, better alerts

The most common failure of monitoring is not a missed alert, it is alert fatigue: thousands of low-value alerts bury the one that matters. Tuning is the ongoing work of fixing that.

  • Measure each rule: how often it fires, and how many alerts turn out to be real.
  • Suppress known-good sources (your own vulnerability scanner will trip your IDS by design) instead of disabling the rule.
  • Threshold repeated events: alert when ten failures happen in a minute, not on each failure.
  • Prioritize by asset value. A probe against a test machine is not the same as one against the payroll database.
  • Add context to the alert (who owns the machine, what normal looks like) so the analyst can decide in a minute.
  • Test the rules. If nobody has ever triggered an alert in a safe way, you do not know it works. Authorized scans from How Port Scanning Works in your lab are a good check.

A stand-in: first-match firewall rules

This stand-in applies an ordered rule list to sample packets. It reproduces the logic of a firewall: match the fields, stop at the first matching rule, and fall back to the default policy. It does not touch a network.

Python Apply ordered firewall rules to sample packets

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The SSH packet from 198.51.100.9 matched no rule and fell to the default, which is exactly the point of default deny: you did not have to think of that case in advance. Try moving the drop rule above an accept rule for the same port and watch which one wins.

A stand-in: signature matching and thresholds

Next, a tiny IDS: signatures (name plus pattern) scan sample HTTP requests, and a threshold turns repeated hits from one address into a single alert, the way tuning reduces noise.

Python Signatures with a threshold

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two lessons appear. The innocent search for "union station schedule" did not match the SQL signature because the pattern needs select after union: precise patterns mean fewer false positives. And three similar hits became one alert, while the single SQL probe was only logged. Whether that threshold is right depends on the system: for a login page you would alert on far fewer.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does a default-deny firewall policy mean?

    Default deny drops anything that no rule explicitly allows, so a forgotten case fails safe.
  2. 2. Why does the order of firewall rules matter?

    iptables and most firewall rule lists stop at the first match. Place specific rules before general ones and review the list top to bottom.
  3. 3. What is the difference between an IDS and an IPS?

    An IDS watches and reports. An IPS is placed in the traffic path and can drop matching packets, with the risk of blocking legitimate traffic.
  4. 4. Which action best reduces alert fatigue?

    Tuning keeps the rules but removes noise: thresholds group repeated events, suppression handles expected sources and prioritization puts important assets first.
  5. 5. Why is it useful to send logs to a central, protected store?

    Central collection preserves evidence off the compromised host and lets rules combine events from many sources.

Summary

  • Firewalls decide which connections exist, an IDS watches those that are allowed, and log monitoring brings the evidence together.
  • Use default deny, stateful rules, specific-before-general ordering, and restrict outgoing traffic as well as incoming.
  • iptables and nftables are the Linux tools; ufw and firewalld are friendlier frontends. Always keep a way back in when changing rules.
  • Snort and Suricata match signatures in traffic; anomaly detection finds the unknown but is noisier. Encryption limits what a network sensor can read.
  • A SIEM collects, normalizes, correlates, alerts and retains logs from many sources.
  • Tuning with thresholds, suppression and priorities is what keeps alerts useful.
  • The Python examples are stand-ins that model rule order and signature matching on invented data; the real tools are not run here.

Next: Exploitation, Privilege Escalation and Post-Exploitation Concepts.