BahNahNah

DC data extractor

Apr 24th, 2015
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.20 KB | None | 0 0
  1. #!/usr/bin/env python
  2. '''
  3. DarkComet Rat Config Decoder
  4. '''
  5.  
  6. __description__ = 'DarkComet Rat Config Extractor'
  7. __author__ = 'Kevin Breen http://techanarchy.net'
  8. __version__ = '0.1'
  9. __date__ = '2014/03/15'
  10.  
  11. import sys
  12. import string
  13. from struct import unpack
  14. try:
  15.     import pefile
  16. except ImportError:
  17.     print "Couldnt Import pefile. Try 'sudo pip install pefile'"
  18. from optparse import OptionParser
  19. from binascii import *
  20.  
  21.  
  22.  
  23. def rc4crypt(data, key):
  24.     x = 0
  25.     box = range(256)
  26.     for i in range(256):
  27.         x = (x + box[i] + ord(key[i % len(key)])) % 256
  28.         box[i], box[x] = box[x], box[i]
  29.     x = 0
  30.     y = 0
  31.     out = []
  32.     for char in data:
  33.         x = (x + 1) % 256
  34.         y = (y + box[x]) % 256
  35.         box[x], box[y] = box[y], box[x]
  36.         out.append(chr(ord(char) ^ box[(box[x] + box[y]) % 256]))
  37.    
  38.     return ''.join(out)
  39.  
  40. def v51_data(data, enckey):
  41.     config = {"FWB": "", "GENCODE": "", "MUTEX": "", "NETDATA": "", "OFFLINEK": "", "SID": "", "FTPUPLOADK": "", "FTPHOST": "", "FTPUSER": "", "FTPPASS": "", "FTPPORT": "", "FTPSIZE": "", "FTPROOT": "", "PWD": ""}
  42.     dec = rc4crypt(unhexlify(data), enckey)
  43.     dec_list = dec.split('\n')
  44.     for entries in dec_list[1:-1]:
  45.         key, value = entries.split('=')
  46.         key = key.strip()
  47.         value = value.rstrip()[1:-1]
  48.         clean_value = filter(lambda x: x in string.printable, value)
  49.         config[key] = clean_value
  50.         config["Version"] = enckey[:-4]
  51.     return config
  52.  
  53. def v3_data(data, key):
  54.     config = {"FWB": "", "GENCODE": "", "MUTEX": "", "NETDATA": "", "OFFLINEK": "", "SID": "", "FTPUPLOADK": "", "FTPHOST": "", "FTPUSER": "", "FTPPASS": "", "FTPPORT": "", "FTPSIZE": "", "FTPROOT": "", "PWD": ""}
  55.     dec = rc4crypt(unhexlify(data), key)
  56.     config[str(entry.name)] = dec
  57.     config["Version"] = enckey[:-4]
  58.  
  59.     return config
  60.  
  61. def versionCheck(rawData):
  62.     if "#KCMDDC2#" in rawData:
  63.         return "#KCMDDC2#-890"
  64.        
  65.     elif "#KCMDDC4#" in rawData:
  66.         return "#KCMDDC4#-890"
  67.        
  68.     elif "#KCMDDC42#" in rawData:
  69.         return "#KCMDDC42#-890"
  70.  
  71.     elif "#KCMDDC42F#" in rawData:
  72.         return "#KCMDDC42F#-890"
  73.        
  74.     elif "#KCMDDC5#" in rawData:
  75.         return "#KCMDDC5#-890"
  76.  
  77.     elif "#KCMDDC51#" in rawData:
  78.         return "#KCMDDC51#-890"
  79.     else:
  80.         return None
  81.  
  82. def configExtract(rawData, key):           
  83.     config = {"FWB": "", "GENCODE": "", "MUTEX": "", "NETDATA": "", "OFFLINEK": "", "SID": "", "FTPUPLOADK": "", "FTPHOST": "", "FTPUSER": "", "FTPPASS": "", "FTPPORT": "", "FTPSIZE": "", "FTPROOT": "", "PWD": ""}
  84.  
  85.     pe = pefile.PE(data=rawData)
  86.     rt_string_idx = [
  87.     entry.id for entry in
  88.     pe.DIRECTORY_ENTRY_RESOURCE.entries].index(pefile.RESOURCE_TYPE['RT_RCDATA'])
  89.     rt_string_directory = pe.DIRECTORY_ENTRY_RESOURCE.entries[rt_string_idx]
  90.     for entry in rt_string_directory.directory.entries:
  91.         if str(entry.name) == "DCDATA":
  92.            
  93.             data_rva = entry.directory.entries[0].data.struct.OffsetToData
  94.             size = entry.directory.entries[0].data.struct.Size
  95.             data = pe.get_memory_mapped_image()[data_rva:data_rva+size]
  96.             config = v51_data(data, key)
  97.  
  98.         elif str(entry.name) in config.keys():
  99.  
  100.             data_rva = entry.directory.entries[0].data.struct.OffsetToData
  101.             size = entry.directory.entries[0].data.struct.Size
  102.             data = pe.get_memory_mapped_image()[data_rva:data_rva+size]
  103.             dec = rc4crypt(unhexlify(data), key)
  104.             config[str(entry.name)] = filter(lambda x: x in string.printable, dec)
  105.             config["Version"] = key[:-4]
  106.     return config
  107.  
  108.  
  109. def configClean(config):
  110.     try:
  111.         newConf = {}
  112.         newConf["FireWallBypass"] = config["FWB"]
  113.         newConf["FTPHost"] = config["FTPHOST"]
  114.         newConf["FTPPassword"] = config["FTPPASS"]
  115.         newConf["FTPPort"] = config["FTPPORT"]
  116.         newConf["FTPRoot"] = config["FTPROOT"]
  117.         newConf["FTPSize"] = config["FTPSIZE"]
  118.         newConf["FTPKeyLogs"] = config["FTPUPLOADK"]
  119.         newConf["FTPUserName"] = config["FTPUSER"]
  120.         newConf["Gencode"] = config["GENCODE"]
  121.         newConf["Mutex"] = config["MUTEX"]
  122.         newConf["Domains"] = config["NETDATA"]
  123.         newConf["OfflineKeylogger"] = config["OFFLINEK"]
  124.         newConf["Password"] = config["PWD"]
  125.         newConf["CampaignID"] = config["SID"]
  126.         newConf["Version"] = config["Version"]
  127.         return newConf
  128.     except:
  129.         return config
  130.    
  131. def run(data):
  132.     versionKey = versionCheck(data)
  133.     if versionKey != None:
  134.         config = configExtract(data, versionKey)
  135.         config = configClean(config)
  136.  
  137.         return config
  138.     else:
  139.         return None
  140.  
  141. if __name__ == "__main__":
  142.     parser = OptionParser(usage='usage: %prog inFile outConfig\n' + __description__, version='%prog ' + __version__)
  143.     (options, args) = parser.parse_args()
  144.     if len(args) > 0:
  145.         pass
  146.     else:
  147.         parser.print_help()
  148.         sys.exit()
  149.     try:
  150.         print "[+] Reading file"
  151.         fileData = open(args[0], 'rb').read()
  152.     except:
  153.         print "[+] Couldn't Open File {0}".format(args[0])
  154.     print "[+] Searching for Config"
  155.     config = run(fileData)
  156.     if config == None:
  157.         print "[+] Config not found"
  158.         sys.exit()
  159.     if len(args) == 2:
  160.         print "[+] Writing Config to file {0}".format(args[1])
  161.         with open(args[1], 'a') as outFile:
  162.             for key, value in sorted(config.iteritems()):
  163.                 outFile.write("Key: {0}\t Value: {1}\n".format(key,value))
  164.        
  165.     else:
  166.         print "[+] Printing Config to screen"
  167.         for key, value in sorted(config.iteritems()):
  168.             print "   [-] Key: {0}\t Value: {1}".format(key,value)
  169.         print "[+] End of Config"
Advertisement
Add Comment
Please, Sign In to add comment