A stateful firewall simulation written in Go that models packet filtering, rule-based access control, and connection state tracking.
This project simulates the core behaviour of a stateful network firewall. It generates randomised inbound and outbound network traffic, applies deny/allow rules based on IP address and port, and tracks active connections in a state table — automatically expiring stale entries over time.
Built as a learning exercise in Go concurrency, network concepts, and security-focused programming.
- Packet generation — produces 200–500 randomised packets with either private or public IP addresses across common ports (22, 25, 53, 80, 110, 443, and a random port)
- Rule engine — generates 5–250 firewall rules targeting packets on vulnerable ports; each rule is keyed by IP + port and assigned an
allowordenyaction - Stateful inspection — outgoing allowed packets create entries in a connection state table; subsequent inbound packets matching an established connection are automatically permitted
- State table cleanup — a background goroutine purges stale connections after a 30-second inactivity timeout
- Traffic simulation — inbound and outbound packets are shuffled together and processed sequentially with a short delay, simulating a live traffic feed
makePackets()generates a mix of safe (private/loopback) and unsafe (public) packetscreateRules()builds a rule map targeting packets on known vulnerable ports- Traffic is combined into a
[]DirectedPacketslice and shuffled - Each packet passes through
checkStateful(), which:- Allows packets belonging to an established connection (state table hit)
- Falls back to rule lookup, defaulting to
allowif no rule matches - Adds new outgoing allowed connections to the state table
- Results are printed to stdout in a formatted log
--- Firewall Simulation Starting ---
[Outgoing] Action: allow | IP: 192.168.1.45 | Port: 443
[Ingoing] Action: deny | IP: 203.0.113.12 | Port: 22
[Ingoing] Action: allow (established)| IP: 192.168.1.45 | Port: 443
- Go 1.22+ (uses
math/rand/v2)
git clone https://github.com/KieranPritchard/Firewall-Simulation-Update.git
cd Firewall-Simulation-Update
go run firewall_simulation.goFirewall-Simulation-Update/
├── firewall_simulation.go # Main simulation logic
└── go.mod # Go module definition
- Struct-based data modelling (
Packet,RuleKey,Connection,DirectedPacket) - Concurrent state management with
sync.Mutex - Background goroutines for periodic cleanup
- IP parsing and classification via the
netpackage - Randomised simulation with
math/rand/v2