A powerful Python-based packet filtering firewall with advanced security features, including IP spoofing detection, flood protection, intrusion prevention, and country-based blocking.
- 🔍 Deep Packet Inspection - Analyze network packets in real-time
- 🛑 Rule-Based Filtering - Configure custom allow/block rules with YAML
- 🕵️ IP Spoofing Detection - Identify and block spoofed packets with confidence scoring
- 🌊 Flood Protection - Prevent SYN and ICMP flood attacks
- 🦠 Malware Detection - Scan packets for known malicious signatures
- 🚫 Geolocation Blocking - Block traffic from specific countries
- 🔄 IPTables Integration - Apply rules directly to Linux kernel firewall
- 📊 Detailed Logging - Comprehensive SQLite database for packet analysis
This firewall has been specifically developed and tested on Kali Linux (running on VMware). The testing environment included Metasploitable2 and Kali Linux itself.
-
Clone the repository
git clone https://github.com/VoidVampire/PyFirewall.git cd PyFirewall -
Install dependencies
# Install system requirements sudo apt-get update sudo apt-get install -y python3-pip python3-scapy libpcap-dev # Install Python dependencies pip3 install -r requirements.txt
-
Configure the firewall by editing
firewall_config.yaml:db_path: firewall.db # Define filtering rules rules: - action: block protocol: 6 # TCP dst_port: 10000 # Block TCP port 10000 - action: allow protocol: 1 # ICMP src_ip: 192.168.137.131 # Allow this specific IP # Add more rules as needed # Optional: Specify regions to restrict (for testing purposes) blocked_countries: - XX # Replace with actual country codes as needed - YY
-
Launch the firewall
# Root privileges required for packet sniffing sudo python3 main.py
PyFirewall consists of several modular components:
| Module | Description |
|---|---|
firewall.py |
Core firewall functionality and packet processing |
rule_engine.py |
Rule parsing and matching logic |
spoof_detection.py |
Advanced IP spoofing detection algorithms |
flood_protection.py |
DDoS and flood attack prevention |
antivirus.py |
Malicious signature detection in packets |
config_loader.py |
YAML configuration parser |
Monitor firewall activity in real-time:
# View the latest logged packets
sqlite3 firewall.db "SELECT * FROM packet_logs ORDER BY timestamp DESC LIMIT 10;"
# Check detected spoofing attempts
sqlite3 firewall.db "SELECT * FROM detailed_spoof_logs ORDER BY timestamp DESC;"
# Monitor traffic from specific IP
sqlite3 firewall.db "SELECT * FROM packet_logs WHERE src_ip = '192.168.1.100';"- Edit
firewall_config.yamlto modify your ruleset - Restart the firewall for changes to take effect
- Monitor logs to verify rule effectiveness
PyFirewall implements multiple security mechanisms to protect your network:
The system employs a multi-layered approach to identify spoofed packets:
- TTL Analysis - Detects inconsistencies in Time-To-Live values that indicate packet manipulation
- Source Routing Detection - Identifies packets with suspicious routing options
- Private IP Validation - Verifies private addresses are coming from trusted networks
- Confidence Scoring - Multiple detection methods contribute to an overall confidence score
Protection against common DoS/DDoS attacks:
# Default thresholds (customizable)
max_connections_per_ip = 3
syn_timeout = 5 # seconds
icmp_timeout = 5 # secondsFor testing and educational purposes, the firewall can filter traffic based on country of origin, which can be useful in development environments.
The integrated antivirus module scans packet payloads for known malicious signatures and patterns.
Below are demonstrations of PyFirewall's capabilities in a real testing environment using Kali Linux and Metasploitable2 VMs:
Figure 1.a: Kali Linux terminal showing PyFirewall processing and allowing legitimate ICMP ping requests based on ruleset matches
Figure 1.b: Metasploitable2 terminal showing successful ping response - 2 packets transmitted, 2 packets received (0% packet loss)
Figure 2.b: Metasploitable2 terminal showing failed connection attempt - 8 packets transmitted, 0 received (100% packet loss)
Figure 3: PyFirewall detecting and mitigating an ICMP flood attack, automatically implementing rate-limiting protections
Figure 4: Multi-factor IP spoofing detection identifying a packet with manipulated source address and TTL anomalies
Figure 5: Geographic filtering mechanism blocking traffic from restricted regions as defined in configuration
Figure 6: Antivirus module identifying and blocking a packet containing known malicious signature patterns
This project is licensed under the MIT License - see the LICENSE file for details.
