0

I use mimikatz to extract NTLM hashes for security audit. The output of mimikatz is along the following lines:

RID  : 000001f4 (500)
User : Administrator

RID  : 000001f5 (501)
User : Guest

RID  : 000001f7 (503)
User : DefaultAccount

RID  : 000001f8 (504)
User : WDAGUtilityAccount
Hash NTLM: 6373ee9aae046ab1319d89b1cfd36306

RID  : 000003e9 (1001)
User : admin
Hash NTLM: f1320e0960da374b88e40cffbec44885

I want to grap the "User" and "Hash NTLM", i.e.,

User : admin
Hash NTLM: f1320e0960da374b88e40cffbec44885 

To do this I use the following regular expression:

[regex]$regex = '(?m)(User\s:\s\w+\r\n\s*Hash NTLM:\s\w+)'

Now, if I use the regular expression directly on the mimikatz output:

$ntlm = Invoke-Expression -Command "mimikatz.exe ""lsadump::sam /sam:C:\sam /system:C:\system"" exit"  
[regex]$regex = '(?m)(User\s:\s\w+\r\n\s*Hash NTLM:\s\w+)'
$regex.Matches($ntlm).value

nothing is returned.

However, if I first store the output to a file and then reads it using -Raw, then it works fine:

$ntlm = Invoke-Expression -Command "mimikatz.exe ""lsadump::sam /sam:C:\sam /system:C:\system"" exit" 
$ntlm | Out-File -FilePath "C:\Users\nlykkei\Desktop\mimikatz.txt"
$Text = Get-Content "C:\Users\nlykkei\Desktop\mimikatz.txt" -Raw
$regex.Matches($Text).value

In this case, the following lines are returned:

User : WDAGUtilityAccount
  Hash NTLM: 6373ee9aae046ab1319d89b1cfd36306
User : admin
  Hash NTLM: f1320e0960da374b88e40cffbec44885

How can I do a proper match without storing the output to a file? I know of \r\n, but they don't seem to work.

1 Answer 1

2

Add Out-String at the end of the command to convert it first to text:

$ntlm = Invoke-Expression -Command "mimikatz.exe "".... exit | Out-String"  

Otherwise powershell will threat it as object, and regex know only text ...

It's just like using the Get-Content -Raw in your second code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.