Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Firewalls and intrusion prevention

Concise study notes on firewalls and intrusion prevention systems: definitions, designs, rule processing, IDS vs IPS, detection metrics, examples with iptables and Snort-style rules, common mistakes and exam guidance.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Firewalls and intrusion prevention are basic building blocks of network security. An engineer uses them to control which traffic can enter or leave a network and to detect or block attacks in real time, so they protect servers, clients and sensitive data from unauthorised access and denial-of-service.

Key ideas

(Definitions come first; each bold term is defined before use.)

  • Firewall: a device or software that enforces a security policy by controlling network traffic between two or more networks. A firewall inspects packets and makes allow/deny decisions according to rules.

  • Security policy: an explicit statement of which traffic is permitted and which is denied. Example policies: "only internal hosts may SSH to the admin server" or "deny all incoming traffic except HTTP and HTTPS to the web server".

  • Packet: a unit of network data at the IP layer (contains IP header and payload). We use the term when discussing packet-filtering firewalls.

  • Packet-filtering firewall (stateless): a firewall that inspects packet header fields (source/destination IP, ports, protocol, flags) and makes decisions per-packet, without maintaining connection state.

  • Stateful inspection firewall: a firewall that keeps a connection table (also called state table) tracking established flows (for TCP, by observing the handshake). It can allow return traffic for an established session without separate rules.

  • Proxy (application) firewall: a firewall that terminates a client connection and opens a new connection to the server on behalf of the client. It understands application protocols (HTTP, FTP) and can inspect payloads.

  • Network Address Translation (NAT): translating IP addresses and/or ports on packets so internal hosts use private addresses while external traffic sees a single public address. Firewalls often implement NAT.

  • Demilitarised Zone (DMZ): a network segment that hosts servers exposed to the internet (web, mail). Draw a diagram: a cloud labelled "Internet" connecting to a box labelled "Firewall"; behind the firewall show two subnets: "DMZ" with web server, and "Internal LAN" with workstations. Label the public IP on the firewall and private IP ranges in the LAN.

  • Intrusion Detection System (IDS): a system that monitors traffic to detect suspicious patterns or attacks and usually raises alerts. It can be network-based (NIDS) or host-based (HIDS).

  • Intrusion Prevention System (IPS): like an IDS but placed inline so it can actively block or drop malicious traffic. An IPS works like a firewall plus detection.

  • Signature-based detection: the IDS/IPS looks for known byte patterns or sequences (signatures) of attacks. Good for known attacks, low false positives for exact matches.

  • Anomaly-based detection: the IDS/IPS builds a model of normal behaviour and flags deviations as suspicious. Good for unknown attacks, but higher false positives.

  • False positive: benign traffic classified as malicious.

  • False negative: malicious traffic not detected (missed).

  • Default deny (also called principle of least privilege): a rule policy where everything not explicitly allowed is denied. This is a recommended safe stance.

  • Rule evaluation model (typical): rules are evaluated in sequence; the first matching rule determines the action. If no rule matches, the default action is applied (often deny).

  • Deep Packet Inspection (DPI): inspecting packet payloads beyond headers to detect application-layer attacks (used by proxies and IPS).

  • Practical deployment patterns to draw and label: single firewall at network edge; two-firewall design with external perimeter firewall and internal firewall around the DMZ; host-based firewall on servers in addition to network firewall.

  • Trade-offs: packet-filtering is fast but limited; stateful adds security for connections; application proxies inspect content but add latency and complexity. IPS in-line can stop attacks but risks availability if it fails. Also, encrypted traffic (e.g., TLS) limits what DPI/IDS/IPS can see unless TLS interception or endpoint inspection is used. Inline IPS deployments should consider fail-open vs fail-closed behavior and high availability to avoid outages.

Areas I am unsure about and want the reviewer to check: exact default ordering of iptables chains in Linux and wording about where NAT interacts with stateful inspection—I state the usual behaviour but kernel internals vary by version.

Formulas

TP = number of true positives — TP is the count of detected attacks that are actually attacks (dimensionless count). Applies to IDS/IPS detection results.

FP = number of false positives — FP is the count of benign events incorrectly flagged (count).

FN = number of false negatives — FN is the count of attacks missed (count).

TN = number of true negatives — TN is the count of benign events correctly ignored (count).

Precision = TP / (TP + FP) — fraction of flagged events that are real attacks (dimensionless, applies when TP+FP>0).

Recall (Detection rate) = TP / (TP + FN) — fraction of actual attacks that were detected (dimensionless, applies when TP+FN>0).

F1 = 2 · (Precision · Recall) / (Precision + Recall) — harmonic mean of precision and recall (dimensionless, applies when precision+recall>0).

Accuracy = (TP + TN) / (TP + TN + FP + FN) — overall correct classification fraction (dimensionless).

Common commands/syntax to remember (rule-style):

iptables -A INPUT -s 10.1.1.0/24 -p tcp --dport 22 -j ACCEPT — append a rule to allow SSH from subnet (Linux iptables syntax). Note: In Linux netfilter, DNAT is applied in the nat PREROUTING chain (or nat OUTPUT for locally generated traffic) before the filter table; SNAT/MASQUERADE is applied in nat POSTROUTING after the filter table; packets traverse filter chains (INPUT/FORWARD/OUTPUT) top-to-bottom, the first terminating target (ACCEPT/DROP) applies, and if no rule triggers the chain policy is used.

snort -c /etc/snort/snort.conf -i eth0 — start Snort with a config file on interface eth0 (example invocation).

Example Snort rule format to remember: alert tcp any any -> $HOME_NET 23 (msg:"Telnet attempt"; flags:S; sid:1000001; rev:1;) — header explains protocol and direction; options in parentheses.

Worked examples

  1. Packet-filter rule trace (sequential evaluation) Data: Rules (top to bottom):
  1. deny tcp from any to 192.168.1.10 dport 23 (deny telnet to host)
  2. allow tcp from 10.1.1.0/24 to 192.168.1.10 dport 22 (allow SSH from LAN)
  3. allow ip from 192.168.1.0/24 to any (allow internal outbound) Default: deny Packet: TCP from 10.1.1.5 to 192.168.1.10 port 23 (telnet) Steps:
  • Check rule 1: matches (destination 192.168.1.10 and dport 23) → action DENY. Stop evaluating.
  • Result: packet dropped. Clear answer: packet is dropped by rule 1.
  1. Stateful behaviour example (connection table) Scenario: Internal host 10.0.0.5 initiates TCP connection to external web server 203.0.113.20:80. Firewall rules: allow established, allow outbound from internal to any Sequence:
  • SYN from 10.0.0.5: firewall sees new connection, rule permits outbound, adds entry to connection table with 5-tuple and state SYN_SENT.
  • SYN-ACK returns: firewall checks connection table, finds matching entry in handshake state, allows packet.
  • Later a packet from 203.0.113.20 to 10.0.0.5 with different five-tuple that does not match existing entry is dropped. Worked answer: stateful firewall allows returning packets because it records state for the flow.
  1. Detection metrics numeric example Given: TP=80, FP=20, FN=20, TN=880 Compute precision, recall, F1, accuracy. Formulas: Precision = TP/(TP+FP) = 80/(80+20) = 0.80 Recall = TP/(TP+FN) = 80/(80+20) = 0.80 F1 = 2·(0.8·0.8)/(0.8+0.8) = 0.8 Accuracy = (TP+TN)/(TP+TN+FP+FN) = (80+880)/1000 = 0.96 Final answers: Precision = 0.80, Recall = 0.80, F1 = 0.80, Accuracy = 0.96.

  2. Snort rule matching trace (short) Rule: alert tcp any any -> $HOME_NET 23 (msg:"Telnet attempt"; flags:S; sid:1000002;) Packet: TCP from 198.51.100.10:12345 to 192.168.1.10:23 with SYN flag set. Trace: header matches protocol TCP and direction any->HOME_NET port 23; flags:S matches SYN flag; action: generate alert. Result: alert raised.

Common mistakes

  • Forgetting the default policy: exam answers must state whether default is deny or allow and justify why default deny is safer.
  • Mixing up NAT and firewall rule direction: NAT can change ports and addresses, so match rules must consider post- or pre-NAT addresses depending on implementation.
  • Overlooking ordering: rules are checked in order and first match wins — answers that ignore order can be marked wrong.
  • Treating signature-based detection as effective for zero-day attacks — it is not; anomaly-based methods aim to detect unknown attacks.

For your exam

Question types you will see: definitions (define firewall, IDS, IPS, DMZ), short notes (compare packet filter vs stateful vs application firewall), derivations/explanations (show how stateful inspection works or explain rule evaluation), numerical problems (compute precision/recall from TP/FP/FN/TN), trace problems (given rules and a packet, say accept/drop), and configuration snippets (write an iptables or Snort rule to meet a requirement).

What a full-marks answer includes:

  • Clear definitions of terms used.
  • A drawing or textual description for topology questions (label Internet, firewall, DMZ, internal network, public and private addresses).
  • Precise step-by-step rule evaluation for trace problems (state which rule matched and why).
  • For metrics, show formula, substitute numbers and compute with units (counts or dimensionless ratios), and give the final result explicitly.
  • For configuration tasks, give correct syntax and explain which part enforces the requirement (e.g. in iptables, explain -s, --dport, -j).

Quick check

  1. Define a stateful firewall in one sentence.
  2. What is a false positive in IDS terms?
  3. Given TP=30, FP=10, FN=20, what is recall?

Answers: 1) A stateful firewall tracks connection state and uses that state to allow return traffic for established sessions. 2) A false positive is benign traffic incorrectly classified as malicious. 3) Recall = TP/(TP+FN) = 30/(30+20) = 0.60.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?