Skip to main content

Overview

The honeypot middleware monitors requests to commonly attacked paths (like /admin, /.env, /wp-admin) and blocks IPs that access them. It acts as a trap for attackers and bots scanning for vulnerabilities. Use it when you need:
  • Detect automated vulnerability scanners
  • Block malicious IPs proactively
  • Reduce attack surface
  • Log attack attempts

Installation

Quick Start

Configuration

Options

Default Paths

Examples

Default Configuration

Custom Paths

Admin Path Honeypot

Config File Honeypot

Database Path Honeypot

With Logging

Custom Block Duration

Custom Response

Form Field Honeypot

Detect bots that fill hidden form fields:

Comprehensive Protection

Combined with IP Filter

API Reference

Functions

How It Works

  1. Request arrives at a honeypot path
  2. IP recorded in block list with expiration
  3. Callback triggered (if configured) for logging/alerting
  4. Fake response returned to attacker
  5. Future requests from that IP are blocked

Technical Details

Architecture

The honeypot middleware uses an in-memory block list with automatic cleanup to track and block malicious IPs:

Request Flow

  1. IP Extraction - Client IP is extracted with proxy support (X-Forwarded-For, X-Real-IP)
  2. Block Check - If IP is in block list and not expired, return 403 Forbidden
  3. Path Matching - Current path is checked against honeypot path map (O(1) lookup)
  4. Trap Trigger - If path matches:
    • IP is added to block list with expiration time
    • OnTrap callback is invoked (if configured)
    • Custom/default response is returned
  5. Pass Through - If no match, request continues to next handler

Implementation Details

  • Concurrent Access - Block list uses sync.RWMutex for thread-safe operations
  • Automatic Cleanup - Background goroutine runs every 10 minutes to remove expired entries
  • Path Lookup - Paths stored in map[string]bool for O(1) lookup performance
  • IP Detection Priority:
    1. X-Forwarded-For header
    2. X-Real-IP header
    3. Request.RemoteAddr

Memory Management

  • Block list grows with unique trapped IPs
  • Automatic cleanup prevents unbounded growth
  • Each entry: IP string + time.Time (approximately 24-32 bytes)
  • Recommended for moderate traffic; for high-traffic consider external storage

Form Honeypot Implementation

Form honeypots use a simpler stateless approach:
  • Check if hidden field contains any value
  • No IP blocking or state management
  • Returns 400 Bad Request immediately if filled

Security Considerations

  1. False Positives - Legitimate users might accidentally hit honeypot paths
  2. IP Rotation - Attackers may rotate IPs
  3. Proxy Detection - Consider X-Forwarded-For for accurate blocking
  4. Log Analysis - Monitor trapped IPs for patterns

Best Practices

  • Choose honeypot paths that legitimate users won’t access
  • Set appropriate block duration based on attack patterns
  • Log all triggered traps for security analysis
  • Combine with other security measures
  • Don’t use paths that might be legitimately requested

Testing

The honeypot middleware includes comprehensive test coverage for all functionality:

Running Tests