bhylu.com

Advertisements

Cybersecurity Firewalls

Evaluate a small default-deny policy against synthetic traffic.

How it works

A firewall applies rules to traffic at a defined boundary. Rules may consider addresses, ports, connection state and application context. Ordering matters when the first matching rule wins. Default deny makes the unhandled case explicit, but an allowed connection still needs authentication and application controls. The program models a narrow policy; it does not change your PC firewall.

Run the code

Save the complete source as firewalls.py in a separate learning folder, then run python firewalls.py. The reviewed runtime is Python 3.12.14 with the standard library; no third-party packages are required. These are local examples, not browser playground programs. Apache, MySQL and PHP are not needed.

Download the complete source

Local lab setup

Example

firewalls.py

def allowed(source_zone, destination_port):
    return source_zone == "staff" and destination_port == 443

assert allowed("staff", 443)
assert not allowed("guest", 443)
assert not allowed("staff", 22)
print("one allowed; two denied")

Expected result

one allowed; two denied

The rule permits only the conjunction of the staff zone and port 443. Testing a guest and an unexpected port catches two common overly broad implementations. A real deployment must establish how zones are assigned and include reply traffic, IPv6 and management access in its tested policy.

Technical reference: OWASP Cheat Sheet Series. Check the guidance for your actual platform and deployment.