This script executes a PowerShell command that attempts to extract...
This script executes a PowerShell command that attempts to extract Kerberos Ticket Granting Service (TGS) hashes from a domain environment. Here's a breakdown of what it does:
General Purpose:
The code is related to Kerberos ticket extraction, potentially used for pentesting, password cracking, or post-exploitation activities in an Active Directory environment. This process is commonly referred to as Kerberoasting. The extracted hashes can be used later for offline brute-forcing to crack service account passwords.
Detailed Explanation:
-
cmd.exe /d /s /c
:- This executes the following PowerShell commands in a Windows command prompt. The
/d
and/s
switches ensure clean invocation without command extensions and handle quotes correctly.
- This executes the following PowerShell commands in a Windows command prompt. The
-
PowerShell Command:
-ExecutionPolicy bypass
: Bypasses the script execution policy to allow the PowerShell script to run without restrictions.-WindowStyle Hidden
: Makes the PowerShell window hidden, so the user won't see anything on the screen.
-
The Script:
-
Load System Identity Assemblies:
- Loads the
System.IdentityModel
.NET assembly, which is used for dealing with Kerberos security tokens.
- Loads the
-
Query Active Directory (AD):
- A
DirectorySearcher
object is created using[ADSI]
. This is used to search Active Directory for objects that have a servicePrincipalName (SPN). SPNs are required for Kerberos authentication. - The LDAP filter
'(&(servicePrincipalName=*)(objectCategory=user))'
retrieves all user objects with SPNs from the directory.
- A
-
Iterate Over Results:
- For each result, the script retrieves the account name (
samAccountName
) and SPNs associated with the user, unless the account iskrbtgt
(which is critical for the Kerberos system and usually ignored).
- For each result, the script retrieves the account name (
-
Kerberos Ticket Extraction (Kerberoasting):
- For each SPN of a user account, the script attempts to request a Kerberos TGS ticket using the SPN as the service target.
- The ticket is requested via
System.IdentityModel.Tokens.KerberosRequestorSecurityToken
.
-
Encoding the TGS Ticket:
- If a ticket is successfully retrieved, it is converted into a hash-like format suitable for offline cracking.
- The
$krb5tgs$23
-style string for the hash is constructed (a common format used with cracking tools like Hashcat).
-
Output:
- The extracted Kerberos hash (with the
$krb5tgs$23$*
format) is output to the console for each service SPN.
- The extracted Kerberos hash (with the
-
It introduces random delays (
Start-Sleep -Seconds (Get-Random -Minimum 1 -Maximum 11)
) to slow down execution, possibly for stealth or evading detection.
-
What It Targets:
- Service accounts in Active Directory with SPNs. These accounts often represent services like SQL Server, Web Servers, etc., and may have weak or crackable passwords.
- The extracted hashes can be cracked offline (e.g., using Hashcat) to recover plain text passwords and gain further access to the network.
Use Case:
This type of script is typically employed in:
- Penetration Testing: By security professionals with permission to assess weaknesses in an organization's Active Directory.
- Malicious Activities: By attackers aiming to escalate privileges or gain unauthorized domain access.
Important Note:
- This script demonstrates potential misuse of functionality available in Windows/Active Directory environments. It should only be used with full authorization in ethical testing scenarios. Unauthorized execution of this code could violate laws and lead to serious consequences.