This article is about credentials, the keys to the realm.
๐๏ธ Clear Text Creds
Clear text creds are usually stored in memory. This memory usually belongs to some processes. For example, lsass
, tspkg
,
wdigest`.
๐บ TsPkg
and Wdigest
โ๏ธ
TsPkg
andWDigest
can be decrypted to retrieve plaintext passwords.
TsPkg
(Terminal Services Package): TsPkg is a security package used in Microsoft Windows operating systems. It is part of the authentication process for Remote Desktop Services (formerly known as Terminal Services). TsPkg is responsible for negotiating and exchanging credentials between the client and the server during the remote desktop session initiation.
Wdigest
: Wdigest is a Windows security package that handles storing and retrieving user credentials (such as usernames and passwords) for Windows authentication. It was primarily used in older versions of Windows, including Windows 7 and earlier. Wdigest stored user passwords in a less secure format than newer authentication protocols. As a result, it became a target for potential security vulnerabilities, leading to its deprecation in later versions of Windows. WDigest.dll
was introduced in the Windows XP operating system The Digest Authentication protocol is designed for use with Hypertext Transfer Protocol (HTTP) and Simple Authentication Security Layer (SASL) exchanges, as documented in RFCs 2617
and 2831
.โ
To prevent WDigest credentials from being stored in memory, a Group Policy setting can be applied to the UseLogonCredential registry entry under the following subkey:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\SecurityProviders\WDigest
- If the
UseLogonCredential
value is set to0
,WDigest
will not store credentials in memory. - If the
UseLogonCredential
value is set to1
,WDigest
will store credentials in memory.
๐๏ธ Tokens
With tokens, one can pretend to be someone else. This is how SSO works. These tokens have some attributes assigned to them. Some attributes, like SeImpersonate
, let one process access the context or tokens of another process.
Each logon and process in the system has a token. This token determines what privileges this fellow has. This token consists of:
- User SID
- Group SID
- Integrity level (mandatory label). Vista+/WinServer 2008+. This label determines the process’s privileges based on the assigned accesses and groups.
- Logon session SID
- Token type (primary or impersonation)
- Impersonation level
- User privileges list
- Other
Checks:
- What is your mandatory labelโ
- Compare this label to the object’s label.
- Take User and Group SIDs of the process and ACL of the object in question.
๐ docs
SeDebugPrivilege
- allows one process to access the memory space of another process (for example, RAM dumpers need that privilege).
SeDelegateSessionUserImpersonatePrivilege
- pretend to be others on the same system. Often used legitimately when a service acts from the user’s behalf.
๐๏ธ LM and NT Hashes
LM (โ๏ธ deprecated)
LM hashes were used a long time ago and were very weak. They used DES and the following algorithm to secure a password.
# Step 0. The length is at most 14 characters long
password = password[0:14]
# Step 1. Convert all characters to uppercase
password = upper(password)
pwd_len = len(all_caps)
hash = list(all_caps)
# Step 2. pad with 0s if the length is less than 14
for i in range(14-pwd_len): hash.append('0')
# Step 3. Split the password into two strings, each consisting of seven characters, and encrypt both parts individually.
first_part = encrypt_des(hash[0:7])
second_part = encrypt_des(hash[7:len(all_caps)])
# Step 4. Return a concatenated string.
return first_part + second_part
The root cause of the vulnerability in the LM hash is the practice of padding the password with a known value (0
s) and encrypting the two parts separately. This vulnerability becomes evident when the password length is equal to or less than 7 characters. In such cases, the second part of the string will always be the same value: AAD3B435B51404EE
. As a result, passwords with a length of 7 characters or less are easier to crack due to this predictable pattern. One would need a rainbow table to do that.
NTLM
DES + MD4 until SP3
NTLMv2
MD5 [username][sids][LM][NTLM]
โ๏ธLM hash is not generated if the password length is less than 15. โ๏ธPasswords are not salted.
โ๏ธIt is advisable not to utilize CredSSP on machines running an operating system older than Windows 8 when using batch scripts, as CredSSP caches credentials on remote systems. Additionally, CredSSP sends credentials over the network, making it vulnerable to Man-in-the-Middle (MiM) attacks.
Each process, file or any other object has a set of requirements. For some process to get access, it needs to fulfil these requirements. At the same time, these processes have passports or tickets ๐ซ which they can use to get something or somewhere. Requirements are called security descriptors; these “passports” are called access tokens. More information about both can be found here and here.
Access tokens can have the following information (not limited):
- (SID) for the user’s account or/and a group and their privileges
- ID for current logon session - logon SID
- Owner SID
- The source of the access token
- Primary or impersonalisation token?
- Integrity level
Security Descriptors also have a set of fields in it:
- Integrity level
- SID of the owner or group
- DACL (list of users allowed and what they can do)
- SACL (list of access attempts that will generate alerts โ ๏ธ)
๐๏ธ Tickets
Kerberos tickets are valid for 10 hours and stored in RAM. For more info on Kerberos and its abuse, see the article in attacks -> protocols section.
๐ ๏ธ Mimikatz
, ๐ ๏ธ WCE
, ๐ ๏ธ kerberoast
๐ RTFM
mimikatz > privilege::debug
mimikatz > kerberos::ptt [ticket]
mimikatz > exit
klist # see the cache
โ๏ธ Attacks
โ๏ธHashes are only present in RAM if the user is logged in interactively and is still logged in.
- Get the creds ๐ ๏ธ
fgdump
, cAceHash
, ๐ ๏ธPWDumpX
, ๐ ๏ธcreddump
,๐ ๏ธWCE
- Get from LSASS ๐ ๏ธ
Mimikatz
orWCE
(Windows Credential Editor). - Dump LSASS for the offline attack.
- Get from SAM hive in RAM or on disk ๐ ๏ธ
gsecdump
โgsecdump.exe -a > file.txt
- Get from the cache with ๐ ๏ธ
creddump
. One can get hashes, cached creds and LSA secrets from the hive.- ๐
pwdump.py SYSTEM SAM true
-> local NT hashes. - ๐
cachedump.py SYSTEM SECURITY true
-> Cached hashes.
- ๐
- Get LSA secrets from
- Get from LSASS ๐ ๏ธ
- Crack the hash with the tools like ๐ ๏ธ
hashcat
or ๐ ๏ธJohn-the-Ripper
. LM hashes are very weak (see LM hash section). Both ๐ ๏ธJohn the ripper
and ๐ ๏ธhashcat
can crack hashes extracted from the cache, but the password needs to be very, very easy or in the wordlist. Otherwise - inefficient. - Pass-the-hash. Use the hash in its original form. ๐ ๏ธ
Metasploit PsExec
, ๐ ๏ธWCE
, ๐ ๏ธSMBshell
. Limited to NTLM challenge-response protocol. Typically, use the SMB protocol to map file shares and perform PsExec-style remote execution or WMI. ๐sekurlsa::pth /user:someuser /domain:domaincontrollername /ntlm:hashstolen /run:".\psexec.exe -accepteula \\IP cmd.exe"
. - Escalate.
- Attribute change. With admin or
SYSTEM
privileges, one can addSeImpersonate
attribute to a token to steal the tokens of another process and use them to access resources they could not access otherwise ๐ ๏ธIncognito
, ๐ ๏ธMetasploit
, ๐ ๏ธPowerShell
, ๐ ๏ธMimikatz
- RID Hijacking. The system identifies users by their RIDs (the last portion of SID), not by username. What happens if we have manually changed the RID of the guest user? If we set it to
500
, the system would treat him as the default admin with all the corresponding rights.
- Attribute change. With admin or
- Reset Password. There is also a technique that allows resetting local account passwords by clearing
lmpw_len
(LM password hash length) andntpw_len
(NTLM password hash length) at0x2c
and0x30
, respectively [8].
๐ RTFM
# Changing TOKENS' attributes
mimikatz > privlege::debug # attacker being authenticated as a local admin adds debug attribute to his token
token::whoami
token::elevate /domainadmin # mimikatz tool looks for the domain admin token in memory and retrieves it.
๐ Detection and ๐ Investigation
๐บ Artefacts
Event logs
๐พ Detection Patterns
- New accounts created (Windows
4720
) - Anomalous logins (workstation to workstation, sensitive networks) -
4624
,4776
- After-hours logins
- Unusual locations
- Ex-employees
- Privileged account usage (Windows
4672
) - Watch out for registry key ๐
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\SecurityProviders\WDigest\UseLogonCredential
set1
.
By login type
- Console login, RunAs, PsExec alternate creds - look for logon type
2
. - Network use, PowerShell remoting (
Invoke-Command
andEnter-PSSession
), PsExec alternate creds, PsExec w/o explicit creds and Remote registry - type3
. - Remote Desktop -
10
. - Remote Scheduled tasks -
4
(password saved as LSA secret). - Run as Service -
5
(password saved as LSA secret).
๐ Further reading: https://learn.microsoft.com/en-us/security/privileged-access-workstations/privileged-access-access-model
๐ก๏ธ Defense
PowerShell remoting is, so far, the most secure option. It enables Remote Credential Guard by default.
- UAC (MAC + least privilege).
- A small number of apps should require admin privileges.
- Managed Service accounts (good defence against Kerberos attacks): long complex passwords, frequent password changes. -> Group Managed Service Accounts (flexible and admin-friendly).
- Windows 8+
- Doesn’t cache credentials (even when using CredSSP),
TsPkg
,Wdigest
. - New security groups were added. This restricted local admins from the network or remote interactive logons to domain-joint systems.
- Some processes are marked as protected, and protected processes can’t run unsigned code. LSASS process is one of the most important ones. This protection is off by default + one can sign the cred-dumping malware (like ๐ ๏ธ
Mimikatz
). - Remote Desktop with
/restrictedAdmin
switch -> creds are not pushed to the remote system. - Domain Protected Users, security group. They can’t use NTLM, CredSSP or Digest authentication mechanisms (protection against some of the ๐ ๏ธ
Mimikatz
techniques and pass-the-hash tools). Creds are not cached, nor are they delegated. Kerberos ticket ๐ซ lives for 4 hours tops. RC4 encryption is off (too weak). - Group Managed Service accounts (gMSA). With GMSA, one can use the same account for several services and can be used on multiple computers within the same domain. MSA and gMSA automate password management for services (managed by AD), eliminate the need to hardcode the credentials, adhere to the least privilege principle, and isolate service accounts.
- Doesn’t cache credentials (even when using CredSSP),
- Windows 10+
- Remote Credential Guard. Protects all the accounts, not only admin.
- Credential Guard. Uses machine virtualisation to isolate creds.
- Device Guard. Can lock a system to prevent the use of untrusted code.
- User PowerShell
- Don’t interact logon to remote machines with an admin account (console, RDP and
runas
). - Terminate RDP sessions properly. The disconnect is NOT closed. You can set a timeout to terminate disconnected sessions. This can be set via Group Policy.
- Assign
Account is Sensitive and Cannot be Delegated
attribute to prevent token delegation. - Limit the number of cached logon accounts in
SOFTWARE\Microsoft\Windows NT\Current Version\WInlogon
,cachedlogonscount
value. Be careful, though, services need cached creds. - Complex passwords.
- Add valuable users to the domain-protected users’ security group, not cache creds.
Built-in:
- can’t remotely write to
C$
andAdmin$
shares - can’t use some remote management tools like
schtasks
,at
,wmic
(if it’s disabled).
Custom:
- Unique, strong passwords
- No network logins for these accounts
- 2FA?
Important Security Patches
- KB2871997 - no clear text in LSASS, two security groups are created.
- KB2928120 - The security update modifies the Group Policy Management Editor window of the Group Policy Management Console (GPMC) by removing the ability to configure and distribute passwords using the following Group Policy Preferences extensions.
๐ Further reading: https://techcommunity.microsoft.com/t5/core-infrastructure-and-security/the-importance-of-kb2871997-and-kb2928120-for-credential/ba-p/258478
References
Expand…
[1] RID Hijaking by Sergey Klevogin, LPT Mater
[2] Analysis the Structure of SAM and Cracking Password Base on Windows Operating System, by Jiang Du and Jiwei Li
[3] ะะฝะธะท ะฟะพ ะบัะพะปะธััะตะน ะฝะพัะต ะบะฐะบ ัะฐะฑะพัะฐะตั ะฐััะตะฝัะธัะธะบะฐัะธั LSA, ะธะปะธ ะะพะด ะบะฐะฟะพัะพะผ ัะธััะตะผั ะฑะตะทะพะฟะฐัะฝะพััะธ Windows, ะััะตะผ ะกะธะฝะธััะฝ
[3] Diving into Windows Logon Process
[4]
[1] IBM Course on Coursera