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 NETCAP iptables chain for easy management

  • Dual-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

Requirement
Details

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

Blocks traffic by adding a DROP rule to iptables.

Parameter
Type
Default
Description

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

Rejects traffic with an ICMP response (more informative than DROP).

Parameter
Type
Default
Description

target

string

"source"

Block source or destination IP

duration

string/int

30m

Block duration

iptables_log

Logs matching traffic (currently logs to stdout, iptables LOG target planned).

Parameter
Type
Default
Description

prefix

string

"NETCAP: "

Log message prefix

iptables_rate_limit

Rate-limits traffic from/to an IP (placeholder - full implementation pending).

Parameter
Type
Default
Description

rate

string

"10/minute"

Rate limit specification

burst

int

5

Initial burst allowance

Note: Rate limiting requires the hashlimit iptables 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():

  1. Cleanup goroutine stopped

  2. All rules flushed

  3. Jump rules removed

  4. Custom chain deleted

Monitoring & Statistics

Available Statistics

Metric
Description

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)

Metric
Description

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

  1. Use Thresholds: Avoid blocking on single events

  2. Set Appropriate Durations: Match severity to block duration

  3. Combine Actions: Use logging with blocking

Whitelist Management

  1. Always whitelist critical infrastructure:

    • Gateways and routers

    • DNS servers

    • Management IPs

    • Monitoring systems

  2. Include your own IPs:

    • SSH access IPs

    • Admin workstations

    • CI/CD systems

Testing

  1. Start with dry-run mode:

  2. Monitor logs during initial deployment:

  3. Test with known traffic:

Production Deployment

  1. Monitor statistics regularly

  2. Review blocked IPs periodically

  3. Keep whitelists up to date

  4. Set up alerting for high block rates

  5. Log all firewall actions for audit

Troubleshooting

Manager Creation Fails

Solutions:

  • Verify iptables is installed: which iptables

  • Check permissions: Run as root or with CAP_NET_ADMIN

  • Verify kernel modules: lsmod | grep xt_

Rules Not Blocking

  1. Check firewall manager is set:

  2. Verify action configuration:

  3. Check whitelist:

Blocks Not Expiring

  1. Verify cleanup is running:

  2. Check expiration time:

Rules Persist After Shutdown

If rules remain after unclean shutdown:

Viewing iptables Rules

Next Steps

Last updated