extracting-password-hashes-from-the-ntds-dit-file

AD Attack #3 – Ntds.dit Extraction

With so much attention paid to detecting credential-based attacks such as Pass-the-Hash (PtH) and Pass-the-Ticket (PtT), other more serious and effective attacks are often overlooked. One such attack is focused on exfiltrating the Ntds.dit file from Active Directory Domain Controllers. Let’s take a look at what this threat entails and how it can be performed. Then we can review some mitigating controls to be sure you are protecting your own environment from such attacks.

What is the Ntds.dit File?

The Ntds.dit file is a database that stores Active Directory data, including information about user objects, groups, and group membership. It includes the password hashes for all users in the domain.

By extracting these hashes, it is possible to use tools such as Mimikatz to perform pass-the-hash attacks, or tools like Hashcat to crack these passwords. The extraction and cracking of these passwords can be performed offline, so they will be undetectable. Once an attacker has extracted these hashes, they are able to act as any user on the domain, including Domain Administrators.

Performing an Attack on the Ntds.dit File

In order to retrieve password hashes from the Ntds.dit, the first step is getting a copy of the file. This isn’t as straightforward as it sounds, as this file is constantly in use by AD and locked. If you try to simply copy the file, you will see an error message similar to:

There are several ways around this using capabilities built into Windows, or with PowerShell libraries.  These approaches include:

  1. Use Volume Shadow Copies via the VSSAdmin command
  2. Leverage the NTDSUtil diagnostic tool available as part of Active Directory
  3. Use the PowerSploit penetration testing PowerShell modules
  4. Leverage snapshots if your Domain Controllers are running as virtual machines

In this post, I’ll quickly walk you through two of these approaches: VSSAdmin and PowerSploit’s NinjaCopy.

Using VSSAdmin to Steal the Ntds.dit File

Step 1 – Create a Volume Shadow Copy

Creation of a volume shadow copy using VSSAdmin

Step 2 – Retrieve Ntds.dit file from Volume Shadow Copy

Copying Ntds.dit file from a volume shadow copy

Step 3 – Copy SYSTEM file from registry or Volume Shadow Copy. This contains the Boot Key that will be needed to decrypt the Ntds.dit file later.

Extracting the SYSTEM file through the registry

Extracting the System file from the volume shadow copy

Step 4 – Delete your tracks

Deleting the volume shadow copy that was created

Using PowerSploit NinjaCopy to Steal the Ntds.dit File

PowerSploit is a PowerShell penetration testing framework that contains various capabilities that can be used for exploitation of Active Directory. One module is Invoke-NinjaCopy, which copies a file from an NTFS-partitioned volume by reading the raw volume. This approach is another way to access files that are locked by Active Directory without alerting any monitoring systems.

Extracting the Ntds.dit file using Invoke-NinjaCopy command within PowerSploit.

Extracting Password Hashes

Regardless of which approach was used to retrieve the Ntds.dit file, the next step is to extract password information from the database. As mentioned earlier, the value of this attack is that once you have the files necessary, the rest of the attack can be performed offline to avoid detection. DSInternals provides a PowerShell module that can be used for interacting with the Ntds.dit file, including extraction of password hashes.

PS E:\DOWNLOADS\DSInternals_v4.3\DSInternals> $key = Get-Bootkey -SystemHiveFilePath E:\Data\SYS                        PS E:\DOWNLOADS\DSInternals_v4.3\DSInternals> Get-ADDBAccount -All -BootKey $key -DBPath E:\DATA\ntds.dit

 

Retrieving account information from the Ntds.dit file using Get_ADDBAccount command, including NTLM hash.

Once you have extracted the password hashes from the Ntds.dit file, you are able to leverage tools like Mimikatz to perform pass-the-hash (PtH) attacks. Furthermore, you can use tools like Hashcat to crack these passwords and obtain their clear text values.  Once you have the credentials, there are no limitations to what you can do with them.

didn’t succeed? then

E:\DATA>ESENTUTL /p e:\data\ntds.dit /!10240/08 /o

Initiating REPAIR mode...
Database: e:\data\ntds.dit
Temp. Database: TEMPREPAIR12236.EDB

Checking database integrity.

The database is not up-to-date. This operation may find that
this database is corrupt because data from the log files has
yet to be placed in the database.

To ensure the database is up-to-date please use the 'Recovery' operation.


Scanning Status (% complete)

0 10 20 30 40 50 60 70 80 90 100
|----|----|----|----|----|----|----|----|----|----|
...................................................

Initiating DEFRAGMENTATION mode...
Database: e:\data\ntds.dit

Defragmentation Status (% complete)

0 10 20 30 40 50 60 70 80 90 100
|----|----|----|----|----|----|----|----|----|----|
...................................................


Moving 'TEMPREPAIR12236.EDB' to 'e:\data\ntds.dit'... DONE!

Moving 'TEMPREPAIR12236.jfm' to 'e:\data\ntds.jfm'... DONE!

Note:
It is recommended that you immediately perform a full backup
of this database. If you restore a backup made before the
defragmentation, the database will be rolled back to the state
it was in at the time of that backup.

Operation completed successfully in 10.32 seconds.


E:\DATA>

How to Protect the Ntds.dit File

The best way to stay protected against this attack is to limit the number of users who can log onto Domain Controllers, including commonly protected groups such as Domain and Enterprise Admins, but also Print Operators, Server Operators, and Account Operators. These groups should be limited, monitored for changes, and frequently recertified.

In addition, leveraging monitoring software to alert on and prevent users from retrieving files off Volume Shadow Copies will be beneficial to reduce the attack surface.

Here are the other blogs in the series:

  • AD Attack #1 – Performing Domain Reconnaissance (PowerShell) Read Now
  • AD Attack #2 – Local Admin Mapping (Bloodhound) Read Now
  • AD Attack #4 – Stealing Passwords from Memory (Mimikatz) Read Now

To watch the AD Attacks webinar, please click here.

Learn how StealthDEFEND helps protect against AD attacks here.

reference used:

  • https://www.dsinternals.com/en/downloads/
  • https://github.com/PowerShellMafia/PowerSploit/blob/master/Exfiltration/Invoke-NinjaCopy.ps1
  • https://attack.stealthbits.com/ntds-dit-security-active-directory