# Detecting TSCookie for Volatility # # LICENSE # Please refer to the LICENSE.txt in the https://github.com/JPCERTCC/MalConfScan/ # # How to use: # 1. cd "Volatility Folder" # 2. mv tscookiescan.py volatility/plugins/malware # 3. python vol.py tscookieconfig -f images.mem --profile=Win7SP1x64 import volatility.plugins.taskmods as taskmods import volatility.win32.tasks as tasks import volatility.utils as utils import volatility.debug as debug import volatility.plugins.malware.malfind as malfind import re import pefile from struct import unpack, unpack_from from collections import OrderedDict try: import yara has_yara = True except ImportError: has_yara = False tscookie_sig = { 'namespace1' : 'rule TSCookie { \ strings: \ $v1 = "Mozilla/4.0 (compatible; MSIE 8.0; Win32)" wide\ $mz = { 4D 5A 90 00 } \ $b1 = { 68 D4 08 00 00 } \ condition: all of them}', 'namespace2' : 'rule TSC_Loader { \ strings: \ $v1 = "Mozilla/4.0 (compatible; MSIE 8.0; Win32)" wide\ $mz = { 4D 5A 90 00 } \ $b1 = { 68 78 0B 00 00 } \ condition: all of them}' } # MZ Header MZ_HEADER = b"\x4D\x5A\x90\x00" # Config pattern CONFIG_PATTERNS = [re.compile("\xC3\x90\x68\x00(...)\xE8(....)\x59\x6A\x01\x58\xC3", re.DOTALL), re.compile("\x6A\x04\x68(....)\x8D(.....)\x56\x50\xE8", re.DOTALL), re.compile("\x00\x00\x68(....)\xE8(....)\x59\x59\x6A\x01", re.DOTALL), re.compile("\x68(....)\xE8(....)\x59\x6A\x01\x58\xC3", re.DOTALL), re.compile("\x68(....)\xE8(....)\x59", re.DOTALL)] CONNECT_MODE = {0: 'TCP', 1: 'HTTP with Credentials', 2: 'HTTP with Credentials', 3: 'HTTP with Credentials', 5: 'HTTP', 6: 'HTTPS', 7: 'HTTPS', 8: 'HTTPS'} PROXY_MODE = {0: 'Detect proxy settings', 1: 'Use config'} INJECTION_MODE = {0 : 'Create process' , 1 : 'Injection running process'} PROCESS_NAME = {0 : 'svchost.exe', 1 : 'iexplorer.exe', 2 : 'explorer.exe', 3 : 'Default browser' , 4: 'Setting process'} class tscookieConfig(taskmods.DllList): """Parse the TSCookie configuration""" @staticmethod def is_valid_profile(profile): return (profile.metadata.get('os', 'unknown') == 'windows'), profile.metadata.get('memory_model', '32bit') def get_vad_base(self, task, address): for vad in task.VadRoot.traverse(): if address >= vad.Start and address < vad.End: return vad.Start, vad.End return None def rc4(self, data, key): x = 0 box = range(256) for i in range(256): x = (x + box[i] + ord(key[i % len(key)])) % 256 box[i], box[x] = box[x], box[i] x = 0 y = 0 out = [] for char in data: x = (x + 1) % 256 y = (y + box[x]) % 256 box[x], box[y] = box[y], box[x] out.append(chr(ord(char) ^ box[(box[x] + box[y]) % 256])) return ''.join(out) def parse_config(self, config): p_data = OrderedDict() for i in xrange(4): if config[0x10 + 0x100 * i] != "\x00": p_data["Server " + str(i)] = unpack_from("<240s", config, 0x10 + 0x100 * i)[0].replace("\0", "") p_data["Server " + str(i) + " (port 1)"] = unpack_from("I", config, 0x604)[0]) if len(config) > 0x89C: p_data["Sleep time"] = unpack_from("I", config, 0x400)[0]) p_data["Sleep count"] = unpack_from(" 0: if "TSCookie" in str(hit): config_data.append(self.parse_config(config)) else: config_data.append(self.parse_loader_config(config)) except: print("[!] Not found config data.\n") yield task, vad_base_addr, end, hit, memory_model, config_data break def render_text(self, outfd, data): delim = '-' * 70 for task, start, end, malname, memory_model, config_data in data: outfd.write("{0}\n".format(delim)) outfd.write("Process: {0} ({1})\n\n".format(task.ImageFileName, task.UniqueProcessId)) outfd.write("[Config Info]\n") for p_data in config_data: for id, param in p_data.items(): outfd.write("{0:<25}: {1}\n".format(id, param))