Hello, it has been a while since I last played CTFs. I am quite busy and lazy. Last week, I participated HTB CA and luckily cleared Forensics.
Very Easy to Medium are very easy, so I won't deep into them. I write write-up for 3 challenges hard and insane.
After solving this challenge, the author has changed the evidence, so I will write both ways to solve this challenge.
Before changing the evidence:
This challenge give us docm
extention file. Like a bot, I use olevba
to extract macro inside it.
Basically, this code creates a new object name vbscript.regexp
with pattern sWcDWp36x5oIe2hJGnRy1iC92AcdQgO8RLioVZWlhCKJXHRSqO450AiqLZyLFeXYilCtorg0p3RdaoPa
, then it searches inside active document, if not found, the program will exit.
After finding the regex, the code takes 13082
byte from the first character of the pattern, xorring in JFqcfEGnc
function byte-to-byte with the initial XOR key is 50 and being changed every loop. The result is used to write to a new file name mailform.js
and then run this file by WScript.exe with argument vF8rdgMHKBrvCoCp0ulm
.
I wrote python script to do the macro code job and print the content of .js
file.
But the output is not what I expected.
So I think maybe we have more step to get the beautiful code, I read the macro code many time but I still can't understand how to get .js
file. After a while, I use CyberChef with Magic mode to guess XOR
key.
You know, in javascript, we initial the variable with var
keyword and the function has function
keyword. I tried function
keyword as know plain text but did not find anything, so I tried var
and got the xor key.
After xorring we got the beautiful code.
Let me explain the code:
First, the variableDASz
takes value from Wscript argument, meaning vF8rdgMHKBrvCoCp0ulm
from the vba macro code. Then variableIwlh
takes value from lyEK()
function.
Next, Iwlh
is decoded base64 in JrvS()
function and is decrypted rc4 in xR68()
function with the key is the value of DASz
. Then execute Iwlh
.
We have 2 ways to solve this, The first is if we don't know what algorithm to do, we just change "eval" to "WScript.Echo", and we will get this:
During participation, I used the second. I used Cyberchef to decode base64 and decrypt rc4 with key
Decode base64 -> flag
Flag: HTB{m4ld0cs_4r3_g3tt1ng_Tr1cki13r}
After changing the evidence
Xor key is changed to 45. And next steps are the same.
As described in the decription, in the Documents
folder, most files have been encrypted, except for some files with certain extensions.
We have to recovery the original file. To do it, we have to find the ransomware first.
In folder Downloads
, we can find the file ats_setup.bat
that make the reverse shell session to server.
The default shell being used is powershell
, so I find the powershell console_history.txt
to see what commands are used.
But it wasn't present in the disk image file. So I trace powershell event log instead.
First I used chainsaw to parse the log with Event ID 4104 in PowerShell log to json and then convert to csv.
Then load to Timeline Explorer. Sort with time, I can see some PowerShell command.
And found that attacker downloaded file from C2 server then unzip it.
Ran some execute file.
Then ran intel.exe
at 12:44:01
.
But the time files are encrypted is 12:55:16
. That means at this time the attacker failed to encrypt.
Continuing tracing, I saw something interesting.
The attacker disabled Windows Defender, which means the ransomware could been identified and quarantined by Windows Defender.
Let's check the Windows Defender logs. We can see here the MWD has detected and quarantine the malware.
Then attacker disabled UAC, Monitoring, IOAVProctection,…
Unzip and ran again.
At this time, the attacker successfully encrypted files.
After encrypted files, attacker deleted all execute files and the zip file.
Okay, I have been stuck at this point for a while until I remember the quarantine mechanism of Windows Defender.
The quarantine file has been encrypted and put in ProgramData/Microsoft/Windows Defender/Quarantine
To recover it, we have defender dump
, which gets files from the mounted image and decrypts the quarantine file.
https://github.com/knez/defender-dump
Export root
folder from Confinement.ad1
and use defender-dump.py
to export quarantine files
Unzip the tar file, we got
It's RE time
Using DIE, we can easily detect what programming language this program built in. That's C#.
Go to the main function, we can see here the program first check if the running machine hostname is not equal to specific hostname, the program will stop.
The program generate uid and pass it to alert
, maybe the program create and show alert to victim.
Then encrypted all files in current directory.
The CoreEncrypter class has 4 variables.
The Enc List Class got current directory and encrypt file with specific extension
EncryptFile function is in CoreEncrypter class. Let's break it.
This encrypting mechanism is quite easy to understand. It using the AES cryptography mode CBC with padding mode ISO10126, using rfc2898DeriveBytes to drive key from password using a technique called PBKDF2.
Back to the main function, we can see here the password is the hash of combination UID and salt.
But UID is generate randomly, so how can we get UID?
If you noticed the Alert, you can see the AttackID is UID.
And the AttackID will append in alert in the .hta
file at the same folder.
Okay we got UID here.
The GetHashCode function get password and salt, hash it with sha512 algorithm and then convert to base64.
I wrote the program to get the password.
And another C# program to decrypt file
So we got the flag
Flag: HTB{2_f34r_1s_4_ch01ce_322720914448bf9831435690c5835634}
This challenge gives us hibernate.sys
file, which is the machine state when hibernated. This file has RAM contents and CPU context before shutting down.
https://www.forensicxlab.com/posts/hibernation/
We can convert it to raw memory dump file by using plugin windows.hibernation.Dump
from volatility3.
https://github.com/forensicxlab/volatility3/tree/feature/hibernation-layer
After converting, we got memory_layer.raw
(I forgot to take a screenshot in the terminal :()
Based on the decription, we can easily found the suspicious process name "thegame.exe".
Okay, let's extract it and analyse.
There are many files name TheGame
Using dlllist plugin TheGame.dll
to see what process loaded it. Okay, it detach to TheGame.exe
, maybe this is some malware technique relate to dll.
I will extract dll and exe files.
TheGame.exe
is built in C/C++
Load it to IDA…I don't understand :(, I'm not a reverse guy.
So I quit it and check TheGame.dll
, luckily it is wrote in C#.
Load it to DnSpy, yeah, that's what I stuck. I could not see the code. Because the author used r2r stomping to hide the code.
https://research.checkpoint.com/2023/r2r-stomping-are-you-ready-to-run/
This technique is hide the code from ILSpy or DnSpy, not IDA hehe.
After loading it to IDA, we can see 2 arrays
And the function that pass 2 arrays as argument
It xor 2 array
So I wrote a program to see the result.
Okay we got flag.
Flag: HTB{IL_st0mp1ng_4nd_h1b3rn4t10n_f0r_th3_gr4nd_f1n4l!!}
Well, if I had known this technique before and noticed the description…