# Full Hancitor malware analysis **muha2xmad.github.io/malware-analysis/fullHancitor/** ### Muhammad Hasan Ali Malware Analysis learner 12 minute read **As-salamu Alaykum** ## Introduction February 12, 2022 Hancitor is a famous malware loader that has been in use for years since first being observed in 2015. A malware loader drops the actual malicious content on the system then executes the first stage of the attack. Hancitor has been the attacker’s loader of choice to deliver malwares like: FickerStealer, Sendsafe, and Cobalt Strike if the victim characteristics are met. In recent months, more threat intelligence has been gathered to confirm the selection of Hancitor by Cuba Ransomware gangs as well. The popularity of [Hancitor among threat actors is considered to last for a while. ref](https://blog.group-ib.com/hancitor-cuba-ransomware) ----- Figure: How Hancitor infection happens. *paloaltonetworks photo ## Unpacking process [We tried in a later article to unpack Hancitor malware. see it from Here. But we will try another way to](https://muha2xmad.github.io/unpacking/hancitor/) unpack it in this sample. Open the sample in the `x32dbg and set two BPs on` `VirtualAlloc and` ``` VirtualProtect then run F9 . We hit VirtualAlloc and then Execute till return then Follow in Dump of EAX. Keep doing that to hit the VirtualProtect BP we will see M8Z an indicator of Aplib ``` compression. ----- Figure: Then if we scroll down to the offset xx4380 we will get the `MZ string which is the start of our unpacked file` used in the analysis. Then save it to a file to start the analysis. ## Abnormal Entry point In Hancitor, there are 3 exports `BNJAFSRSQIX,` `SDTECHWMHHONG,` `DllEntryPoint . And the first two` functions have the same address. For the first thought the entry point will be `DllEntryPoint, but if we` check this function it only returns 1. From the malicious document which we extract the maliciuos Hancitor malware. Launched the rundll32.exe command to execute the `BNJAFSRSQIX function, so it must be the` real entry point for this DLL. Figure(1): ## Gathering victim information We enter `BNJAFSRSQIX we see a call to a function which i renamed it to` `Hancitor_Main . Then enter` ``` sub_10001AA0 renamed info_gathering . First the malware gathers information about the host ``` contains: OS version, Computer name, Domains names, Victim IP address, whether the machine is x64 or x86 OS. ----- Figure(2): Figure(3): pseudocode of figure 2 ----- Then we enter `getVictimID then enter` `sub_10001C70 which is the function that generates a unique ID` for the victim. First, calling `GetAdaptersAddresses to get the addresses associated with the network` adapters on the victim machine then XOR-ing each (MAC) adapter with its address together. And calling ``` GetVolumeInformationA to retrieve the volume serial number of the machine then XORs it with the result to create the victim’s unique ID. ``` Figure(4): pseudocode of sub_10001C70 function How to get the IP of the victim? By sending a GET request to `hxxp://api[.]ipify[.]org . If the` malware is unable to contact the website, it uses `0.0.0.0 as the victim’s IP address.` Figure(5): Then the final gathering step is to concatinate the gathered victim’s information in two ways to be sent to C2 server: ``` GUID=&BUILD=&INFO=&EXT=&IP= &TYPE=1&WIN=.(x64) GUID=&BUILD=&INFO=&EXT=&IP= &TYPE=1&WIN=.(x32) ``` ----- Figure(6): ## Configuration Extraction Manually Extraction Malware authors use tricks to obfuscate thier C2 IoCs. One of them is to encrypt it into a big chunk of data as in Hancitor Malware. So we need to search for big chunk of data which later will be decrypted during the runtime. We will find our encrypted configuration in the begining of `.data section and if we select the` hex values then press `D we see it has refernces which means that it’s used somewhere.` ----- Figure(7): There are two chunks of data `Pbdata and` `byte_10005018 . If we press` `x over one of them it takes us` to a function which will be called `mw_Decrypt_Data which is the function decrypts the configuration of` the malware. And these two chuncks of data are parameters in this function. Figure(8): Now enter this `mw_Decrypt_Data function. We see there are 5 API calls:` ``` CryptCreateHash : initiates the hashing of a stream of data.CryptCreateHash the 2nd parameter is ``` `Algid which identifies the hash algorithm to use. Its value is` `0x8004u which used SHA1` see the decumentation of Algid ``` CryptDeriveKey : generates cryptographic session keys derived from a base data value CryptDeriveKey ``` the 2nd parameter is `Algid Its value is` `0x6801u which used RC4` ----- The 4th parameter is `dwflags which determinte the size of the key which the key is a set with the upper` 16 bits of `0x280011u which is` `0x28 divided by 8. Then the key size is 5 bytes.` Figure(9): We get the SHA1 key from `pbdata . And we get the RC4 data from` `byte_10005018 and decrypte the` RC4-encrypted data using the first 5 bytes of the SHA1 key. *How we select hex values only? Do as in the figure 9. then choose `hex string (unspaced) . then copy` what is in the `preview .` Figure(10): [Then open cyberchef using](https://gchq.github.io/CyberChef/) `from hex and` `SHA1 we get the SHA1 key` ``` ad818c687f8dc7f281135753e567eababa03d0ba . Then select the whole chunck of data of byte_10005018 and copy hex as we did in SHA1 and use RC4 and using the first 5 bytes of SHA1. ``` ----- Figure(11): Here are 3 C2 servers which the malware communicate with for further commands based on the collected victim host information. Build ID `2909_xplw .` ``` hxxp://forkineler(.)com/8/forum.php hxxp://yemodene(.)ru/8/forum.php hxxp://fordecits(.)ru/8/forum.php ## Automated Extraction ``` Figure(12): ----- ``` po pe e a se da a o a o a import re #Use a regular expression to locate our config data import struct # Convert binary data into numeric values. import hashlib #Generate a SHA1 hash file_path = r'file path' #file path data = open(file_path,'rb').read() # read it in binary pe = pefile.PE(data=data) # to parse data as PE file to acess sections and offsets # Use Regular Expression to Locate The Decryption Code key = rb'\x6a(.)\x68(....)\x68\x00\x20\x00\x00' #opcode as in the figure(6). m = re.search(key, data) # extract the data that was matched by the wildcard. if m != None: print("key length: %r" % m.group(1)) print("key address: %r" % m.group(2)) # Convert The Extracted Key Information struct.unpack('b', m.group(1))[0] #converte into bytes hex(struct.unpack('