Penetration Techniques - Obtaining Local User Hashes via SAM Database

Onedaysec
3 min read
0 views
docx image 1770017225691 0 75bc69fa8b

0x00 Preface

---

During penetration testing, after gaining access to a Windows system, the sekurlsa::logonpasswords command in mimikatz is typically used to attempt reading the lsass process information to obtain password details of currently logged-in users. However, to comprehensively acquire password information within the system, it is also necessary to extract data stored in the SAM database and export the hashes of all local users in the current system.

0x01 Introduction

---

This article will cover the following topics:

  • Various methods for obtaining user hashes via the SAM database
  • Principle analysis

0x02 Methods for obtaining user hashes via the SAM database

---

1. Online reading of the SAM database

Read the SAM database file of the current system to obtain hashes of all local users

(1) mimikatz

privilege::debug
token::elevate
lsadump::sam

Test as shown in the figure below

(1) mimikatz — technical illustration 1

(2) pwdump7

Download link:

http://passwords.openwall.net/b/pwdump/pwdump7.zip

Execute with administrator privileges, as shown in the figure below

(2) pwdump7 — technical illustration 2

(3) powershell

Download link:

https://raw.githubusercontent.com/EmpireProject/Empire/master/data/module_source/credentials/Invoke-PowerDump.ps1

Execute with administrator privileges, test as shown in the figure below

(3) powershell — technical illustration 3

2. Offline reading of SAM database

Obtain the SAM database file of the current system and read it on another system.

There are two methods to export the SAM database file:

(1) Save the registry

Administrator privileges

reg save HKLM\SYSTEM SystemBkup.hiv
reg save HKLM\SAM SamBkup.hiv

(2) Copy files

C:\Windows\System32\config\SYSTEM
C:\Windows\System32\config\SAM

Cannot be copied by default, as shown in the figure below

(2) Copy files — technical illustration 4

Requires the use of NinjaCopy, author Joe Bialek, reference download address:

An open-source project

After exporting the SAM database file, on another system, user hashes can be exported in the following ways:

(1) mimikatz

lsadump::sam /sam:SamBkup.hiv /system:SystemBkup.hiv

as shown in the figure below

(1) mimikatz — technical illustration 5

Note:

The official documentation of mimikatz has issues, the address is as follows:

https://github.com/gentilkiwi/mimikatz/wiki/module-~-lsadump

Export command:

lsadump::sam SystemBkup.hiv SamBkup.hiv

will report an error, as shown in the figure below

(1) mimikatz — technical illustration 6

The available command was provided by @我爱这个世界

Supplement:

The following tools will report errors when reading SAM database files of Win7 systems

(1) Pwdump7

as shown in the figure below

(1) Pwdump7 — technical illustration 7

(2) Pwdump5

Download link:

http://passwords.openwall.net/b/pwdump/pwdump5.zip

Incorrect read result, as shown below

(2) Pwdump5 — technical illustration 8

(3) cain

Test as shown below

(3) cain — technical illustration 9

0x03 Principle Analysis

---

1. Read HKLM\SYSTEM to obtain syskey

Read the contents of keys JD, Skew1, GBG, and Data under registry path HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa, then concatenate them to form syskey

Reference code:

https://github.com/johannwmeyer/quarkspwdump/blob/a68aa6330f37eb8d00055c73e6a4e3cb52bcdd6d/src/crypt.cpp#L222

https://github.com/gentilkiwi/mimikatz/blob/master/mimikatz/modules/kuhl_m_lsadump.c#L219

Complete calculation code can be found at:

https://raw.githubusercontent.com/3gstudent/Writeup/master/getsyskey.cpp

(Steal from http://www.zcgonvh.com/post/ntds_dit_pwd_dumper.html)

2. Use syskey to decrypt HKLM\SAM

Read the contents of the F and V items for each user under the registry key HKEY_LOCAL_MACHINE\SAM\SAM\Domains\Account\Users, and use syskey to perform a series of decryptions

Detailed decryption process can be referred to the following link:

http://www.xfocus.net/articles/200306/550.html

In summary, to obtain user hashes via the SAM database, two files are required: HKLM\SYSTEM and HKLM\SAM

The most direct export method is to read the registry HKLM\SYSTEM and HKLM\SAM from the current system, but system privileges are required

For methods to switch from admin to system privileges, refer to the previous article: 'Penetration Techniques - Switching from Admin Privileges to System Privileges'

0x04 Summary

---

This article introduces multiple methods to obtain all user hashes via the SAM database, with the key being to read HKLM\SYSTEM and HKLM\SAM

Related Questions & Answers

What is the underlying principle behind extracting hashes from the SAM database?

The process involves two steps: first, read the syskey from `HKLM\SYSTEM` by concatenating values from specific registry keys. Second, use that syskey to decrypt the encrypted hash data stored in the `F` and `V` registry values under `HKLM\SAM\SAM\Domains\Account\Users` for each user. This decryption chain ensures that only someone with access to both hives can recover plaintext hashes. The full cryptographic logic is referenced in the code of tools like mimikatz and quarkspwdump, as outlined in [Penetration Techniques - Obtaining Local User Hashes via SAM Database](/news/penetration-techniques-obtaining-local-user-hashes-via-sam-database).

Why do both the SYSTEM and SAM registry hives need to be obtained to decrypt user hashes?

The SAM hive stores encrypted user hashes, while the SYSTEM hive contains the boot key (syskey) needed for decryption. The syskey is derived from registry values under `HKLM\SYSTEM\CurrentControlSet\Control\Lsa` (keys JD, Skew1, GBG, Data). Without the SYSTEM hive, the encryption key cannot be reconstructed, making the SAM data useless. This principle is explained in depth in [Penetration Techniques - Obtaining Local User Hashes via SAM Database](/news/penetration-techniques-obtaining-local-user-hashes-via-sam-database).

Which tools can be used to read the SAM database online, and what privilege level is required?

Common online tools include mimikatz (using `privilege::debug`, `token::elevate`, `lsadump::sam`), pwdump7, and PowerShell scripts like Invoke-PowerDump. All require administrator privileges, and some may fail on Windows 7 (e.g., pwdump7 and Cain). For switching from admin to system privileges, refer to the related article [Penetration Techniques - Switching from Admin Privileges to System Privileges](https://example.com/placeholder) (link placeholder for the referenced previous article). These methods are detailed in [Penetration Techniques - Obtaining Local User Hashes via SAM Database](/news/penetration-techniques-obtaining-local-user-hashes-via-sam-database).

What are the steps to perform an offline extraction of local user hashes from a Windows system?

First, export the SAM and SYSTEM registry hives using `reg save HKLM\SYSTEM SystemBkup.hiv` and `reg save HKLM\SAM SamBkup.hiv` with administrative privileges. Then, on another system, use mimikatz with the command `lsadump::sam /sam:SamBkup.hiv /system:SystemBkup.hiv` to extract the hashes. Note that the official mimikatz documentation may contain an erroneous syntax; the correct command uses the `/sam:` and `/system:` parameters. The same technique is described in [Penetration Techniques - Obtaining Local User Hashes via SAM Database](/news/penetration-techniques-obtaining-local-user-hashes-via-sam-database).

Why is it necessary to extract hashes from the SAM database during a penetration test, even after using sekurlsa::logonpasswords?

While `sekurlsa::logonpasswords` retrieves credentials of currently logged-in users by reading lsass process memory, it does not cover all local accounts. To comprehensively obtain password hashes for every local user, you must extract data from the SAM database. This is covered in detail in [Penetration Techniques - Obtaining Local User Hashes via SAM Database](/news/penetration-techniques-obtaining-local-user-hashes-via-sam-database). Both online and offline methods can be used to dump these hashes.

Continue Reading