ToolPie
Personal Rating: Medium
Getting an Overview
According to the story, the designs of a toolmaker have been stolen. We have to investigate the incident and expose Lord Malakar as guilty. This forensics challenge starts off with a pcap file. The first thing that looked interesting was a request to a web page with a nice tale, related to the lore of the CTF:
Another interesting request was made to /script.html
This html suggests that there was a web page that could be used to execute code in some way.
A POST in json format to /execute can be made with the script, apparently providing the means to execute Python code on the server. And indeed, a POST to the server can be found!
At first glance, the script bz2-decompresses a bytestream and then loads and executes it. The necessary libraries are imported first and a keyboard interrupt stops the execution.
I saved the compressed stream in raw hex format.
The next TCP stream looks like something that happened on the server, which seems to be a Windows host:
Almost nothing of this can be determined, except the machine and user at the start
ec2amaz-bktvi3e\administrator
The blue parts at the beginning are the only server data transmitted. The rest is 8.504 kB of client data.
This is the next stream:
And this is the final related one:
The first and last two lines do not seem encrypted, the rest does.
Start:
ec2amaz-bktvi3e\administrator
<SEPARATOR>5UUfizsRsP7oOCAq
End:
ec2amaz-bktvi3e\administrator
<SEPARATOR>LFca75ceNdmiGtrZ
I had the suspicion that these requests are the start and end requests of an encrypted C2 communication. The two seemingly random strings could be encryption keys.
From the investigation of the aforementioned requests in Wireshark, I could already determine the relevant IPs that were involved in the suspicious traffic:
Server IP: 172.31.47.152 Sent Mal. Script: 194.59.6.66 Potential C2: 13.61.7.218:55155
Analyzing the Python Script
I stored the script and cleaned it up in an effort to see what it does.
From the start of the script you can tell that the following data must be a bz2 steam with a block size of 9, as it starts with BZh9
\x
is the Python Byte delimiter. The extra \
comes from html encoding. This could be reversed:
The resulting script worked if you replaced "exec" with "print".
This also reveals the program that was used to obfuscate the code. The goal now is to recover the obfuscated code from the marshal object.
Unescaping the result again and extracting strings from it gave some insights into what it might be doing.
The results indicate Py-Fuscate was used to obfuscate code that can send AES-encrypted files to or from the IP 13.61.7.218
I created a script to output the disassembled version that yielded some more info.
import marshal,lzma,gzip,bz2,binascii,zlib
indicates that lzma, binascii, lzma and zlib are potentially used inside the marshal.
I read through the disassembly and noted any relevant information that caught my eye.
Disas initial:
Process opened
AES Crypto.Cipher and Crypto.Util.Padding are used/created
There is the '<SEPARATOR>' string
There are the code objects enc_mes, dec_file_mes, dec_mes, receive_file, receive
The main function is created. A socket is opened for a client. A connection to ('13.61.7.218', 55155) is prepared. encode is used
receive_thread with ('target', 'args') is used
A sleep time of 50 is done
Disas enc_mes:
An AES CBC encryption is prepared with the variables cypher, key and cypher_block and padding
Disas dec_file_mes:
The key is encoded and used for AES CBC decryption. An unpadding is done and the vars key, cypher, cypher_block are used. The cipher is a byte object initially
Disas dec_mes:
The cipher is a byte object. The vars cypher, cypher_block and key are used to unpad and AES CBC decrypt the byte object
Disas receive_file:
A socket is opened, ('13.61.7.218', 54163). The vars k, client2 and more are used.
enc_received, dec_mes, enc_received and ok_enc are used. a SEPARATOR var is used
enc_mes and ok_enc are used. A byte object is received and read. files can be written.
There is the error ('Error transporting file')
Disas <genexpr>:
A random ASCII letter string apperas to be generated
Disas receive:
A message is received and decrpyted. The vars msg and message are used. The message is encrypted again to check if decryption was correct
enc_answ is used. A file can be sent to "target". A file can be received and get_file is used, as well as enc_mes
dec_mes is used and a file can be written.
enc_mes, dec_mes and a send operation can be seen again
The message + \n is set
An answer is encoded and send. The error message ('Bad command!') appears. The error ('Error uploading file') also appears.
A client connect to ('13.61.7.218', 55155) can be seen again
From what we have seen before, we have the encryption key(s) from the network traffic.
Decrypting
Testing around with different Key-IV combinations in Cyberchef, it appeared that the first 16 Bytes after the SEPARATOR were the key and the second string was the IV.
I changed the Wireshark view to raw bytes. TCP stream 4 could be decrypted to a PDF file this way!
Using the "save as file" function of CyberChef worked perfectly to retrieve the PDF.
Here is a comparison of the start bytes of the decrypted file and the sample PDF:
Extract:
Sample:
Replacing the first Bytes to the ones of the sample report worked and the challenge was solved.
Challenge Questions
What is the IP address responsible for compromising the website?
194.59.6.66
- This is clearly the IP that sent the malicious script to the server when you inspect the http streams
What is the name of the endpoint exploited by the attacker?
execute
- You can see that the malicious script was POSTed to /execute
What is the name of the obfuscation tool used by the attacker?
PyFuscate
- This could be seen after decompressing the initial byte object
What is the IP address and port used by the malware to establish a connection with the Command and Control (C2) server?
13.61.7.218:55155
- This could also be found in the decompressed string and also in Wireshark
What encryption key did the attacker use to secure the data?
5UUfizsRsP7oOCAq
- This can be found at the start of the encrypted data in Wireshark
What is the MD5 hash of the file exfiltrated by the attacker?
- This can be generated after the PDF has been AES CBC decrypted and the first Bytes of the PDF corrected with the ones from a sample pdf
Last updated