Cowrie SSH Honeypot

Building a Deceptive SSH Service to Log Hacker Commands

Securing modern network infrastructure requires more than passive defense; it demands active intelligence through deceptive technologies. The Cowrie SSH Honeypot serves as a medium to high interaction decoy designed to log brute force attacks and the subsequent shell interaction performed by human or automated adversaries. In the context of critical infrastructure such as water treatment facilities or regional energy grids, Cowrie acts as an early warning system. By presenting an exposed but controlled surface to the public internet, it captures the specific tools and tactics used by threat actors before they reach production assets.

Cowrie works by mimicking a standard Linux environment. It provides a fake filesystem and a simulated shell that responds to commands such as ls, cd, and wget. Unlike a simple script that merely records login attempts, Cowrie captures the entire session; this includes the payload of any malicious scripts uploaded by the attacker. This encapsulation allows system architects to analyze attacker behavior without risking the integrity of the underlying host kernel. The primary objective is to reduce the latency between an initial intrusion attempt and the deployment of countermeasures across the broader technical stack.

Technical Specifications

| Requirements | Detail |
| :— | :— |
| Operating System | Linux (Ubuntu 22.04 LTS or Debian 11/12 recommended) |
| Default Port Range | 2222 (Internal), 22 (Public Facing via NAT) |
| Protocol / Standard | SSH RFC 4251, Telnet RFC 854 |
| Impact Level | 8/10 (High Intelligence / Critical Defensive Value) |
| Recommended Resources | 1 vCPU, 1GB RAM, 20GB SSD Storage |

THE CONFIGURATION PROTOCOL

Environment Prerequisites:

The deployment of Cowrie requires a clean environment to prevent side-channel leaks that might reveal the honeypot nature to an experienced attacker. Dependencies include python3, python3-venv, git, libssl-dev, and libffi-dev. The host system must be configured with a non-root user specifically dedicated to the honeypot service. All production SSH management services for the real host must be moved to a non-standard port; for example, port 22222; to avoid conflicts and accidental lockout.

Section A: Implementation Logic:

The engineering design of Cowrie relies on the concept of a virtualized file system combined with an emulated shell environment. When an attacker initiates a connection, the honeypot presents a standard SSH banner. If the credentials provided by the attacker match the defined database or if the honeypot is configured to allow all passwords, the attacker is granted access to a sandbox. This sandbox is idempotent in its initial state; any changes made by the attacker are logged but do not persist in a way that affects the host operating system. The logic is to provide enough responsiveness to keep the attacker engaged, thereby increasing the volume of data captured while ensuring the honeypot itself does not become a pivot point for lateral movement into the secure network.

Step-By-Step Execution

1. Repository Synchronization and Dependency Mapping

Execute the following command to prepare the host environment: sudo apt-get update && sudo apt-get install git python3-virtualenv libssl-dev libffi-dev build-essential -y.
System Note: This command updates the local package index and installs the necessary compilers and libraries required for the Python Twisted framework. Using apt-get ensures that we meet the software version requirements for the encryption libraries that Cowrie utilizes during the SSH handshake.

2. Service User Creation

Construct a limited-privilege service account: sudo adduser –disabled-password cowrie.
System Note: This action creates a user account that cannot log in via a password and lacks entry in the sudoers file. This is a critical security measure to prevent the honeypot process from interacting with the root filesystem or other kernel-level operations if the Python environment is compromised.

3. Source Acquisition and directory logic

Switch to the new user and clone the source: sudo su – cowrie then git clone https://github.com/cowrie/cowrie.git.
System Note: By operating under the cowrie user context, we ensure all file permissions for the honeypot remain within the /home/cowrie directory. This maintains a clean audit trail and prevents permission errors during the installation of Python modules.

4. Virtual Environment Encapsulation

Initialize the Python environment: cd cowrie && python3 -m venv cowrie-env && source cowrie-env/bin/activate.
System Note: Creating a virtual environment isolates the Cowrie dependencies from the system-wide Python libraries. This prevents version conflicts and ensures that the honeypot remains stable even if the host OS updates its global Python packages.

5. Dependency Installation within Environment

Install the required modules: pip install –upgrade pip && pip install -r requirements.txt.
System Note: This command uses pip to fetch the specific versions of Twisted, Cryptography, and other libraries. The build-essential package installed in step 1 is utilized here to compile any C-based Python extensions required for high throughput during session logging.

6. Configuration File Modification

Generate the active configuration: cp etc/cowrie.cfg.dist etc/cowrie.cfg.
System Note: The etc/cowrie.cfg file is where the honeypot parameters are defined. You must edit the hostname variable to match a plausible naming convention like web-prod-01 rather than the default cowrie to avoid detection by automated scanners.

7. Global Port Redirection

Redirect traffic from the standard SSH port to the honeypot port: sudo iptables -t nat -A PREROUTING -p tcp –dport 22 -j REDIRECT –to-port 2222.
System Note: This iptables rule allows Cowrie to run as a non-privileged user on port 2222 while still capturing traffic directed at port 22. This prevents the honeypot service from requiring root access to bind to a low-numbered port, significantly reducing the attack surface.

8. Service Initiation

Start the honeypot: bin/cowrie start.
System Note: This script initializes the Twisted reactor and begins listening for connections. You should verify the service status using bin/cowrie status or systemctl if you have integrated it as a system-level service.

Section B: Dependency Fault-Lines:

Installation failures often occur during the compilation of the cryptography library if the libssl-dev headers are missing or mismatched. Another common bottleneck is a conflict with the host’s actual SSH daemon. If the host daemon is still listening on port 22, the iptables redirection will fail to function correctly. Ensure that the file /etc/ssh/sshd_config on the host has its Port variable changed to a value other than 22 or 2222 before proceeding with the honeypot deployment.

THE TROUBLESHOOTING MATRIX

Section C: Logs & Debugging:

The primary diagnostic tool for Cowrie is the log file located at var/log/cowrie/cowrie.log. If the service fails to start, this file will typically indicate a port binding error or a syntax error in the configuration file. For an interactive analysis of captured sessions, refer to the JSON logs at var/log/cowrie/cowrie.json. These logs provide higher granularity, including the attacker’s public IP address, the local timestamp, and the specific SSH version string they are using.

If you encounter the error [ERROR] Twisted setup failed, verify that your virtual environment is active. The Twisted framework is highly sensitive to the Python path; if the libraries are not in the current environment’s path, the service will terminate immediately. For network-level issues where no connections are appearing in the logs, use tcpdump -i eth0 port 22 to verify that packets are reaching the physical or virtual interface.

OPTIMIZATION & HARDENING

– Performance Tuning: Use the sysctl utility to increase the maximum open file descriptors if you expect high concurrency from distributed botnets. Adjusting net.core.somaxconn to a higher value will help the system manage large bursts of connection requests without high packet-loss.
– Security Hardening: Implement fail2ban or a similar tool on the host’s management port (e.g., port 22222) while leaving the honeypot port (22) entirely open. You should also regularly update the honeyfs by adding custom files that mimic a real production environment. This reduces the likelihood of an attacker realizing they are in a sandbox after the first few commands.
– Scaling Logic: For high-traffic networks, deploy multiple Cowrie instances behind a load balancer. Use a centralized database like MySQL or PostgreSQL for log storage by modifying the [output_mysql] section in the configuration file. This allows for unified threat intelligence gathering across a distributed sensor grid.

THE ADMIN DESK

How do I view the commands the attacker typed?
Navigate to var/log/cowrie/cowrie.json and filter for the input field. Alternatively, use the bin/playlog utility to replay a session file from the var/lib/cowrie/tty/ directory to see the real-time shell interaction.

How do I change the fake files attackers see?
Modify the contents of the honeyfs/ directory. You can add dummy configuration files or logs here. To update the filesystem structure itself, you must edit the data/fs.pickle file using the provided bin/createfs tool.

Why is my honeypot capturing no traffic?
Check your firewall rules. The most common cause is a hardware firewall or a cloud security group blocking traffic on port 22. Ensure that iptables is correctly redirecting traffic to the internal port 2222.

Can attackers escape the honeypot to the host?
Cowrie is a simulated environment; it does not use real virtualization like KVM. While a breakout is unlikely because there is no real shell, a vulnerability in the Python Twisted library could theoretically exist. Always run Cowrie as a limited user.

Where are the files attackers download?
Any file an attacker attempts to download via wget or curl is intercepted and saved in the var/lib/cowrie/downloads/ directory. The files are saved by their SHA-256 hash to prevent duplicate storage and for easy integration with malware analysis tools.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top