Penetration Basics - Obtaining Active Directory Information

Onedaysec
5 min read
1 views
docx image 1770018770092 0 ff0b330586

0x00 Preface

---

In domain penetration, obtaining Active Directory information is essential

This article will take obtaining all users, all computers, and all groups in Active Directory as examples to introduce common information acquisition methods

0x01 Introduction

---

This article will cover the following:

  • Methods for obtaining Active Directory information from outside the domain
  • Methods for obtaining Active Directory information from within the domain
  • Methods for obtaining information using C++ to call ADSI interfaces

0x02 Basics

---

Domain environments use a directory database to store objects such as user accounts, computer accounts, and groups

LDAP (Lightweight Directory Access Protocol) is used to query and update the directory database

Common Abbreviations

  • DN: Distinguished Name
  • CN: Common Name
  • OU: Organizational Unit
  • DC: Domain Controller

A Distinguished Name (DN) consists of three attributes: CN, OU, and DC.

Simple Explanation:

The Domain Controller typically has port 389 open by default for LDAP services.

0x03 Methods for Obtaining Active Directory Information from Outside the Domain

---

1. Querying Data Using ldapsearch on Kali Linux

The test environment is shown in the figure below.

1. Querying Data Using ldapsearch on Kali Linux — technical illustration 1

Prerequisite: We can access port 389 on the Domain Controller (DC), and we have obtained the credentials of at least one regular domain user.

In this test environment, we have obtained the password for the regular domain user 'testa' as DomainUser123!

The connection command is as follows:

ldapsearch -x -H ldap://192.168.1.1:389 -D "CN=testa,CN=Users,DC=test,DC=com" -w DomainUser123! -b "DC=test,DC=com"

Parameter description:

  • -x Perform simple authentication
  • -H Server address
  • -D DN used to bind to the server
  • -w Password for binding DN
  • -b Specify the root node to query

This command will display all information that can be queried, as shown in the figure below

1. Querying Data Using ldapsearch on Kali Linux — technical illustration 2

Next, add search conditions to categorize the results

(1) Query all domain users

Add search condition: "(&(objectClass=user)(objectCategory=person))"

Complete command is as follows:

ldapsearch -x -H ldap://192.168.1.1:389 -D "CN=testa,CN=Users,DC=test,DC=com" -w DomainUser123! -b "DC=test,DC=com" -b "DC=test,DC=com" "(&(objectClass=user)(objectCategory=person))"

This command will output all attributes of all domain users, as shown in the figure below

(1) Query all domain users — technical illustration 3

To facilitate name statistics, you can choose to list only CN (Common Name) and use the grep command to filter the output

The command is as follows:

ldapsearch -x -H ldap://192.168.1.1:389 -D "CN=testa,CN=Users,DC=test,DC=com" -w DomainUser123! -b "DC=test,DC=com" -b "DC=test,DC=com" "(&(objectClass=user)(objectCategory=person))" CN | grep cn

The result output is shown in the figure below

(1) Query all domain users — technical illustration 4

(2) Query all computers

Add search condition: "(&(objectCategory=computer)(objectClass=computer))"

The command is as follows:

ldapsearch -x -H ldap://192.168.1.1:389 -D "CN=testa,CN=Users,DC=test,DC=com" -w DomainUser123! -b "DC=test,DC=com" -b "DC=test,DC=com" "(&(objectCategory=computer)(objectClass=computer))" CN | grep cn

The result output is shown in the figure below

(2) Query all computers — technical illustration 5

(3) Query all groups

Add search condition: "(&(objectCategory=group))"

The command is as follows:

ldapsearch -x -H ldap://192.168.1.1:389 -D "CN=testa,CN=Users,DC=test,DC=com" -w DomainUser123! -b "DC=test,DC=com" -b "DC=test,DC=com" "(&(objectCategory=group))" CN | grep cn

The output result is shown in the figure below

(3) Query all groups — technical illustration 6

2. Querying data through PowerView on Windows systems

The test environment is shown in the figure below

2. Querying data through PowerView on Windows systems — technical illustration 7

Prerequisite: We can access port 389 of the domain controller (DC), and we have obtained at least the password of one ordinary user within the domain

In this test environment, we have obtained the password for the ordinary domain user testa as DomainUser123!

PowerView address:

https://github.com/PowerShellMafia/PowerSploit/blob/master/Recon/PowerView.ps1

(1) Query all domain users

Credentials are required here, so the complete command is as follows:

$uname="testa"
$pwd=ConvertTo-SecureString "DomainUser123!" -AsPlainText –Force
$cred=New-Object System.Management.Automation.PSCredential($uname,$pwd)
Get-NetUser -Domain test.com -DomainController 192.168.1.1 -ADSpath "LDAP://DC=test,DC=com" -Credential $cred

To facilitate name statistics, you can choose to list only the name field. The complete command is as follows:

$uname="testa"
$pwd=ConvertTo-SecureString "DomainUser123!" -AsPlainText –Force
$cred=New-Object System.Management.Automation.PSCredential($uname,$pwd)
Get-NetUser -Domain test.com -DomainController 192.168.1.1 -ADSpath "LDAP://DC=test,DC=com" -Credential $cred | fl name

The output result is shown in the figure below

(1) Query all domain users — technical illustration 8

(2) Query all computers

$uname="testa"
$pwd=ConvertTo-SecureString "DomainUser123!" -AsPlainText –Force
$cred=New-Object System.Management.Automation.PSCredential($uname,$pwd)
Get-NetComputer -Domain test.com -DomainController 192.168.1.1 -ADSpath "LDAP://DC=test,DC=com" -Credential $cred | fl name

The output result is shown in the figure below

(2) Query all computers — technical illustration 9

(3) Query all groups

$uname="testa"
$pwd=ConvertTo-SecureString "DomainUser123!" -AsPlainText –Force
$cred=New-Object System.Management.Automation.PSCredential($uname,$pwd)
Get-NetGroup -Domain test.com -DomainController 192.168.1.1 -ADSpath "LDAP://DC=test,DC=com" -Credential $cred | fl name

The output result is as shown in the figure below

(3) Query all groups — technical illustration 10

0x04 Methods for obtaining Active Directory information within the domain

---

The prerequisite is that access to a host within the domain has already been obtained

The test environment is as shown in the figure below

0x04 Methods for obtaining Active Directory information within the domain — technical illustration 11

Principle: Perform LDAP queries through ADSI (Active Directory Services Interface) to obtain results

1. Implement using PowerShell

Referencing PowerView, URL:

https://github.com/PowerShellMafia/PowerSploit/blob/master/Recon/PowerView.ps1

2. Implemented in C#

Referencing SharpView, URL:

https://github.com/tevora-threat/SharpView

3. Implemented in C++

Reference URLs:

https://github.com/microsoft/Windows-classic-samples/tree/master/Samples/Win7Samples/netds/adsi/activedir/QueryUsers/vc

https://github.com/outflanknl/Recon-AD

Microsoft's code is in exe format, only introduces the QueryUser method, but supports query conditions (filtering specific users) and displays brief information (outputs only names for easy statistics)

Recon-AD's code is in dll format, includes multiple functions, but by default only displays detailed information

Therefore, I merged the code from both, and the code supports the following features:

  • exe format
  • includes multiple functions, supports querying users, computers, groups, etc.
  • supports query conditions and displays brief information

Code has been uploaded to GitHub, address as follows:

An open-source project

The code can specify ADS path and search conditions, usage as follows:

(1) Query domain users

List all domain users, displaying only brief name information, command as follows:

QueryADObject.exe Current "(&(objectClass=user)(objectCategory=person))" ShortData

Result output as shown in the figure below

(1) Query domain users — technical illustration 12

Query all information of a specified user, command as follows:

QueryADObject.exe Current "(&(objectClass=user)(objectCategory=person)(name=testa))" AllData

Result output as shown in the figure below

(1) Query domain users — technical illustration 13

(2) Query computers

List all computer accounts, displaying only brief name information, command as follows:

QueryADObject.exe Current "(&(objectCategory=computer)(objectClass=computer))" ShortData

The output is shown in the figure below

(2) Query computers — technical illustration 14

To query detailed information about domain controllers, you need to know the ADS path as "OU=Domain Controllers,DC=test,DC=com". The command is as follows:

QueryADObject.exe "OU=Domain Controllers,DC=test,DC=com" "(&(objectCategory=computer)(objectClass=computer))" AllData

The output is shown in the figure below

(2) Query computers — technical illustration 15

(3) Query groups

List all groups, displaying only brief name information. The command is as follows:

QueryADObject.exe Current "(&(objectCategory=group))" ShortData

List detailed information of the administrator group. The command is as follows:

QueryADObject.exe Current "(&(objectCategory=group)(name=Domain Admins))" Alldata

The output is shown in the figure below

(3) Query groups — technical illustration 16

(4) Query OUs

List all OUs, displaying only brief name information. The command is as follows:

QueryADObject.exe Current "(&(objectCategory=organizationalUnit))" ShortData

The output result is shown in the following figure

(4) Query OUs — technical illustration 17

0x05 Summary

---

This article takes obtaining all users, all computers, and all groups in Active Directory as examples, introducing methods for acquiring information from outside and inside the domain respectively.

Related Questions & Answers

How does the C++ tool QueryADObject.exe improve upon Microsoft’s sample code for AD enumeration?

The C++ tool merges features from Microsoft’s sample and Recon-AD, providing an exe format with multiple functions for querying users, computers, and groups. It supports search conditions and offers a `ShortData` output mode for concise name listing, unlike Microsoft’s limited `QueryUser` method. This makes enumeration more efficient and is especially useful during penetration tests where speed matters, similar to the approaches in [obtaining installed programs](/news/penetration-basics-obtaining-the-list-of-installed-programs-on-the-current-system).

What is the significance of the LDAP filter `(&(objectCategory=computer)(objectClass=computer))` when querying Active Directory?

This filter retrieves all computer objects in the domain. It is used in ldapsearch, PowerView’s `Get-NetComputer`, or the custom C++ tool to list every domain-joined machine. The combination of `objectCategory` and `objectClass` ensures only computer accounts are returned, not users, groups, or other object types.

What tools can be used to gather Active Directory information from within a compromised domain host?

From inside the domain, you can use PowerShell with PowerView scripts, C# with SharpView, or C++ programs that call ADSI interfaces. The article provides a custom C++ tool (`QueryADObject.exe`) that supports querying users, computers, and groups with flexible search conditions and output modes, similar to the approaches used in [obtaining domain user login information](/news/penetration-basics-obtaining-domain-user-login-information).

How can you list all domain users from outside the domain using ldapsearch on Kali Linux?

Use the ldapsearch command with simple authentication, providing the domain user's DN and password, then add a search filter like `(&(objectClass=user)(objectCategory=person))` and pipe the output to `grep cn` to display only common names. This efficiently enumerates all domain users as shown in the article’s examples.

What are the prerequisites for querying Active Directory information from outside the domain using LDAP?

To query AD from outside the domain, you need network access to the Domain Controller’s port 389 (LDAP) and valid credentials for at least one regular domain user. As described in [this article](/news/penetration-basics-obtaining-active-directory-information), tools like ldapsearch on Kali can then be used with the DN and password to bind and query objects like users, computers, and groups. For environments where AV might interfere, see [Bypass AV techniques](/news/penetration-basics-active-directory-information-gathering-2-bypass-av).

Continue Reading