Firewall Response Actions
NETCAP can automatically execute firewall actions (like blocking IPs) when detection rules match. This enables automated incident response by integrating with the Linux iptables firewall subsystem.
Table of Contents
Overview
Features
Automated Blocking: Automatically block IPs based on detection rules
Time-based Expiration: Blocks automatically expire after configurable durations
Whitelist Protection: Prevent blocking of critical infrastructure
Custom Chain: Uses dedicated
NETCAPiptables chain for easy managementDual-Stack Support: Works with both IPv4 and IPv6
Dry-Run Mode: Test configurations without modifying firewall
Statistics Tracking: Monitor block counts, expirations, and errors
Graceful Cleanup: All rules removed on shutdown
Architecture
Requirements
Operating System
Linux (iptables required)
Privileges
Root or CAP_NET_ADMIN capability
iptables
iptables and/or ip6tables installed
Kernel Modules
xt_comment module for rule comments
Non-Linux Platforms
On non-Linux platforms (macOS, Windows), the firewall manager returns an error:
Rules with firewall response actions will be skipped with a warning.
Configuration
Firewall Manager Configuration
Rule Configuration with Response Actions
Add an actions array to detection rules:
Response Action Types
iptables_block
iptables_blockBlocks traffic by adding a DROP rule to iptables.
target
string
"source"
Block source (source/src) or destination (destination/dst) IP
duration
string/int
30m
Block duration. String ("30m", "2h") or int (minutes)
iptables_reject
iptables_rejectRejects traffic with an ICMP response (more informative than DROP).
target
string
"source"
Block source or destination IP
duration
string/int
30m
Block duration
iptables_log
iptables_logLogs matching traffic (currently logs to stdout, iptables LOG target planned).
prefix
string
"NETCAP: "
Log message prefix
iptables_rate_limit
iptables_rate_limitRate-limits traffic from/to an IP (placeholder - full implementation pending).
rate
string
"10/minute"
Rate limit specification
burst
int
5
Initial burst allowance
Note: Rate limiting requires the
hashlimitiptables module and is not fully implemented.
Rule Examples
Port Scanning Detection & Block
SSH Brute Force Protection
DNS Tunneling Detection
Web Attack Detection
SYN Flood Protection
Firewall Manager API
Creating a Manager
Blocking IPs
Unblocking IPs
Querying State
Whitelist Management
Cleanup
Integration with Rules Engine
Safety Features
Whitelist Protection
IPs/CIDRs in the whitelist are never blocked:
Automatic Expiration
All blocks expire automatically:
Configurable per-rule duration
Default: 30 minutes
Background cleanup every minute
Zero duration = permanent until restart
Custom Chain Isolation
All rules are placed in a dedicated NETCAP chain:
Dry-Run Mode
Test configurations without modifying firewall:
Graceful Shutdown
On Close():
Cleanup goroutine stopped
All rules flushed
Jump rules removed
Custom chain deleted
Monitoring & Statistics
Available Statistics
blocks_created
Total blocks created since start
blocks_removed
Blocks manually removed
blocks_expired
Blocks removed due to expiration
duplicates_skip
Duplicate block requests skipped
whitelist_skip
Block requests skipped due to whitelist
errors
Total errors encountered
active_blocks
Currently active blocks
Action Statistics (Rules Engine)
actions_executed
Total response actions executed
actions_success
Successful actions
actions_failed
Failed actions
ips_blocked
Total IPs blocked
Viewing Active Blocks
Best Practices
Rule Design
Use Thresholds: Avoid blocking on single events
Set Appropriate Durations: Match severity to block duration
Combine Actions: Use logging with blocking
Whitelist Management
Always whitelist critical infrastructure:
Gateways and routers
DNS servers
Management IPs
Monitoring systems
Include your own IPs:
SSH access IPs
Admin workstations
CI/CD systems
Testing
Start with dry-run mode:
Monitor logs during initial deployment:
Test with known traffic:
Production Deployment
Monitor statistics regularly
Review blocked IPs periodically
Keep whitelists up to date
Set up alerting for high block rates
Log all firewall actions for audit
Troubleshooting
Manager Creation Fails
Solutions:
Verify iptables is installed:
which iptablesCheck permissions: Run as root or with
CAP_NET_ADMINVerify kernel modules:
lsmod | grep xt_
Rules Not Blocking
Check firewall manager is set:
Verify action configuration:
Check whitelist:
Blocks Not Expiring
Verify cleanup is running:
Check expiration time:
Rules Persist After Shutdown
If rules remain after unclean shutdown:
Viewing iptables Rules
Next Steps
Review RULES_ENGINE.md for detection rule syntax
See FILTERING.md for expression syntax
Check example rules in configs/firewall-rules.yml
Explore injection rules for real-time packet actions
Last updated