# Volatility mimikatz plugin # # Based on the research made by Gentil_Kiwi for his mimikatz # http://blog.gentilkiwi.com/mimikatz # https://code.google.com/p/mimikatz/ # # Author: Francesco Picasso # # April 2017, modified to reflect the new Construct API # # This plugin is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This plugin is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this plugin. If not, see . """ @author : Francesco Picasso @license : GPL 2 or later @contact : francesco.picasso@gmail.com @organization : www.realitynet.it """ import construct import os import re import struct import volatility.obj as obj import volatility.debug as debug import volatility.commands as commands import volatility.constants as constants import volatility.utils as utils import volatility.win32.tasks as tasks from Crypto.Cipher import AES from Crypto.Cipher import DES3 #------------------------------------------------------------------------------ class Credential(): """TODO: add description here.""" def __init__(self, module='', username='', domain='', epwd='', pwd=''): self.module = module self.username = username self.domain = domain self.epwd = epwd self.pwd = pwd self.signature = module + username + domain + epwd.encode('hex') def decrypt_epwd(self, decryptor): if self.epwd and decryptor: self.pwd = decryptor.decrypt(self.epwd) try: self.pwd = self.pwd.decode('utf-16-le').rstrip('\0') except UnicodeDecodeError: debug.warning('[Credential:decrypt_epwd] unicode decode error') self.pwd = self.pwd.encode('hex') def dump(self): debug.notice('m<{}> u<{}> d<{}> ep<{}> p<{}>'.format( self.module, self.username, self.domain, self.epwd.encode('hex'), self.pwd)) class Credentials(): """TODO: add description here.""" def __init__(self): self.credentials = [] def add_credential(self, credential): already_in = False for cred in self.credentials: if cred.signature == credential.signature: already_in = True if not already_in: self.credentials.append(credential) #------------------------------------------------------------------------------ class MemoryScanner(object): """An address space scanner based on scudette's Yara Scanner""" def __init__(self, task): self.task = task def _find_first(self, address_space, offset, maxlen, signature): """Raw memory scanner with overlap.""" # Allow some bytes for overlapping signatures overlap = 1024 i = offset while i < offset + maxlen: to_read = min( constants.SCAN_BLOCKSIZE + overlap, offset + maxlen - i) block = address_space.zread(i, to_read) if block: match = block.find(signature) if match >= 0: return match i += constants.SCAN_BLOCKSIZE def find_first(self, offset, signature): """Find the first match using VADs. It retuns a VA.""" task_as = self.task.get_process_address_space() task_vads = self.task.get_vads(skip_max_commit = True) for vad, __ in task_vads: if offset >= vad.Start and offset <= vad.Start + vad.Length: position = self._find_first(task_as, vad.Start, vad.Length, signature) if position: return position + vad.Start #------------------------------------------------------------------------------ class MimikatzBase(object): """The mimikatz base class, used to defined common attributes/methods.""" SIZEOF_LONG = 4 SIZEOF_PTR = None UNPACK_PTR = None UNPACK_LONG = ''.format(data_str)) return data_str else: debug.error('[Wdigest] get_unicode_string_at() unable to get data') return '' def add_entry(self, entry, found_at): if entry.usage_count: if entry.this_entry == found_at: user = domain = epwd = '' if entry.user_string_ptr and entry.user_len: user = self.get_unicode_string_at( entry.user_string_ptr, entry.user_max_len) if entry.domain_string_ptr and entry.domain_len: domain = self.get_unicode_string_at( entry.domain_string_ptr, entry.domain_max_len) if entry.password_encrypted_ptr and entry.password_len: epwd = data = self.get_data( entry.password_encrypted_ptr, entry.password_max_len) if user: cred_entry = Credential(self.MODULE_NAME, user, domain, epwd) self.credentials_obj.add_credential(cred_entry) def walk_entries(self): entry, found_at = self.get_first_entry() if entry: walk_num = 1 while walk_num < self.MAX_WALK: self.add_entry(entry, found_at) self.entries_seen[found_at] = 1 found_at = entry.previous entry = self.get_entry_at(found_at) if not entry: debug.error('Next entry not found!') break if entry.this_entry in self.entries_seen: break walk_num += 1 class Wdigest_x86(Wdigest, Mimikatz_x86): """TODO: add description.""" ''' WDIGEST_LIST_ENTRY = construct.Struct('WdigestListEntry', construct.Int32ul('previous'), construct.Int32ul('next'), construct.Int32ul('usage_count'), construct.Int32ul('this_entry'), construct.Int64ul('luid'), construct.Int64ul('flag'), construct.Int16ul('user_len'), construct.Int16ul('user_max_len'), construct.Int32ul('user_string_ptr'), construct.Int16ul('domain_len'), construct.Int16ul('domain_max_len'), construct.Int32ul('domain_string_ptr'), construct.Int16ul('password_len'), construct.Int16ul('password_max_len'), construct.Int32ul('password_encrypted_ptr')) ''' WDIGEST_LIST_ENTRY = construct.Struct( 'previous'/construct.Int32ul, 'next'/construct.Int32ul, 'usage_count'/construct.Int32ul, 'this_entry'/construct.Int32ul, 'luid'/construct.Int64ul, 'flag'/construct.Int64ul, 'user_len'/construct.Int16ul, 'user_max_len'/construct.Int16ul, 'user_string_ptr'/construct.Int32ul, 'domain_len'/construct.Int16ul, 'domain_max_len'/construct.Int16ul, 'domain_string_ptr'/construct.Int32ul, 'password_len'/construct.Int16ul, 'password_max_len'/construct.Int16ul, 'password_encrypted_ptr'/construct.Int32ul) def __init__(self, lsass_task, credentials_obj): Mimikatz_x86.__init__(self, lsass_task) Wdigest.__init__(self, credentials_obj) class Wdigest_x64(Wdigest, Mimikatz_x64): """TODO: add description.""" ''' WDIGEST_LIST_ENTRY = construct.Struct('WdigestListEntry', construct.Int64ul('previous'), construct.Int64ul('next'), construct.Int32ul('usage_count'), construct.Int32ul('align1'), construct.Int64ul('this_entry'), construct.Int64ul('luid'), construct.Int64ul('flag'), construct.Int16ul('user_len'), construct.Int16ul('user_max_len'), construct.Int32ul('align2'), construct.Int64ul('user_string_ptr'), construct.Int16ul('domain_len'), construct.Int16ul('domain_max_len'), construct.Int32ul('align3'), construct.Int64ul('domain_string_ptr'), construct.Int16ul('password_len'), construct.Int16ul('password_max_len'), construct.Int32ul('align4'), construct.Int64ul('password_encrypted_ptr')) ''' WDIGEST_LIST_ENTRY = construct.Struct( 'previous'/construct.Int64ul, 'next'/construct.Int64ul, 'usage_count'/construct.Int32ul, 'align1'/construct.Int32ul, 'this_entry'/construct.Int64ul, 'luid'/construct.Int64ul, 'flag'/construct.Int64ul, 'user_len'/construct.Int16ul, 'user_max_len'/construct.Int16ul, 'align2'/construct.Int32ul, 'user_string_ptr'/construct.Int64ul, 'domain_len'/construct.Int16ul, 'domain_max_len'/construct.Int16ul, 'align3'/construct.Int32ul, 'domain_string_ptr'/construct.Int64ul, 'password_len'/construct.Int16ul, 'password_max_len'/construct.Int16ul, 'align4'/construct.Int32ul, 'password_encrypted_ptr'/construct.Int64ul) def __init__(self, lsass_task, credentials_obj): Mimikatz_x64.__init__(self, lsass_task) Wdigest.__init__(self, credentials_obj) class Wdigest_Vista_x86(Wdigest_x86): """Class for Windows Vista x86.""" SIGNATURE = '\x74\x11\x8b\x0b\x39\x4e\x10' FIRST_ENTRY_OFFSET = -6 def __init__(self, lsass_task, credentials_obj): Wdigest_x86.__init__(self, lsass_task, credentials_obj) class Wdigest_Win7_x86(Wdigest_x86): """Class for Windows 7 x86.""" SIGNATURE = '\x74\x11\x8b\x0b\x39\x4e\x10' FIRST_ENTRY_OFFSET = -6 def __init__(self, lsass_task, credentials_obj): Wdigest_x86.__init__(self, lsass_task, credentials_obj) class Wdigest_Win7_x64(Wdigest_x64): """Class for Windows 7 x64.""" SIGNATURE = '\x48\x3b\xd9\x74' FIRST_ENTRY_OFFSET = -4 def __init__(self, lsass_task, credentials_obj): Wdigest_x64.__init__(self, lsass_task, credentials_obj) class Wdigest_Vista_x64(Wdigest_x64): """Class for Windows Vista x64.""" SIGNATURE = '\x48\x3b\xd9\x74' FIRST_ENTRY_OFFSET = -4 def __init__(self, lsass_task, credentials_obj): Wdigest_x64.__init__(self, lsass_task, credentials_obj) #------------------------------------------------------------------------------ class mimikatz(commands.Command): """mimikatz offline""" def __init__(self, config, *args, **kwargs): commands.Command.__init__(self, config, *args, **kwargs) self.profile = config.get_value('profile') self.credentials_obj = Credentials() def find_lsass(self): addr_space = utils.load_as(self._config) for task in tasks.pslist(addr_space): if str(task.ImageFileName) == 'lsass.exe': return task def init_objects(self, lsass_task): lsa_decryptor = None wdigest = None if len(self.profile) >= 7: arch = self.profile[-3:] sp = self.profile[-6:-3] os = self.profile[:-6] if os == 'Vista': if arch == 'x86': lsa_decryptor = LsaDecryptor_Vista_x86(lsass_task) wdigest = Wdigest_Vista_x86(lsass_task, self.credentials_obj) elif arch == 'x64': lsa_decryptor = LsaDecryptor_Vista_x64(lsass_task) wdigest = Wdigest_Vista_x64(lsass_task, self.credentials_obj) elif os == 'Win7': if arch == 'x86': lsa_decryptor = LsaDecryptor_Win7_x86(lsass_task) wdigest = Wdigest_Win7_x86(lsass_task, self.credentials_obj) elif arch == 'x64': lsa_decryptor = LsaDecryptor_Win7_x64(lsass_task) wdigest = Wdigest_Win7_x64(lsass_task, self.credentials_obj) else: pass return lsa_decryptor, wdigest def calculate(self): lsass_task = self.find_lsass() if not lsass_task: debug.error('lsass_task process not found!!') return lsa_decryptor, wdigest = self.init_objects(lsass_task) if not lsa_decryptor or not wdigest: return lsa_decryptor.acquire_crypto_material() wdigest.walk_entries() for cred in self.credentials_obj.credentials: cred.decrypt_epwd(lsa_decryptor) def render_text(self, outfd, data): self.table_header(outfd, [("Module", "8"), ("User", "16"), ("Domain", "16"), ("Password", "40")]) for cred in self.credentials_obj.credentials: self.table_row( outfd, cred.module, cred.username, cred.domain, cred.pwd)