/usr/lib/python2.7/dist-packages/rekall/plugins/windows/malware/apihooks.py is in python-rekall-core 1.6.0+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 | # Rekall Memory Forensics
#
# Based on original code by:
# Copyright (C) 2007-2013 Volatility Foundation
#
# Authors:
# Michael Hale Ligh <michael.ligh@mnin.org>
#
# This code:
# Copyright 2014 Google Inc. All Rights Reserved.
#
# Authors:
# Michael Cohen <scudette@google.com>
#
# This program 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 program 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 program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
import struct
from rekall import testlib
from rekall import utils
from rekall.plugins.windows import common
from rekall.plugins.overlays.windows import pe_vtypes
class DecodingError(Exception):
"""Raised when unable to decode an instruction."""
class HookHeuristic(object):
"""A Hook heuristic detects possible hooks.
This heuristic emulates some common CPU instructions to try and detect
control flow jumps within the first few instructions of a function.
These are essentially guesses based on the most common hook types. Be aware
that these are pretty easy to defeat which will cause the hook to be missed.
See rekall/src/hooks/amd64.asm and rekall/src/hooks/i386.asm For the test
cases which illustrate the type of hooks that we will detect.
"""
def __init__(self, session=None):
self.session = session
self.Reset()
def Reset(self):
# Keep track of registers, stack and main memory.
self.regs = {}
self.stack = []
self.memory = {}
def WriteToOperand(self, operand, value):
if operand["type"] == "REG":
self.regs[operand["reg"]] = value
elif operand["type"] == "IMM":
self.memory[operand["address"]] = value
elif operand["type"] == "MEM":
self.memory[self._get_mem_operand_target(operand)] = value
else:
raise DecodingError("Operand not supported")
def ReadFromOperand(self, operand):
"""Read the operand.
We support the following forms:
- Immediate (IMM): JMP 0x123456
- Absolute Memory Address (MEM): JMP [0x123456]
- Register (REG): JMP [EAX]
"""
# Read from register.
if operand["type"] == 'REG':
return self.regs.get(operand["reg"], 0)
# Immediate operand.
elif operand["type"] == 'IMM':
return operand["address"]
# Read the content of memory.
elif operand["type"] == "MEM":
return self._GetMemoryAddress(
self._get_mem_operand_target(operand), operand["size"])
else:
raise DecodingError("Operand not supported")
def _get_mem_operand_target(self, operand):
reg_base = operand["base"]
if reg_base == "RIP":
return operand["address"]
else:
# Register reference [base_reg + disp + index_reg * scale]
return (self.regs.get(reg_base, 0) +
operand["disp"] +
self.regs.get(operand["index"], 0) * operand["scale"])
def _GetMemoryAddress(self, offset, size):
try:
# First check our local cache for a previously written value.
return self.memory[offset]
except KeyError:
data = self.address_space.read(offset, size)
format_string = {1: "b", 2: "H", 4: "I", 8: "Q"}[size]
return struct.unpack(format_string, data)[0]
def process_lea(self, instruction):
"""Copies the address from the second operand to the first."""
operand = instruction.operands[1]
if operand["type"] == 'MEM':
self.WriteToOperand(instruction.operands[0],
self._get_mem_operand_target(operand))
else:
raise DecodingError("Invalid LEA source.")
def process_push(self, instruction):
value = self.ReadFromOperand(instruction.operands[0])
self.stack.append(value)
def process_pop(self, instruction):
try:
value = self.stack.pop(-1)
except IndexError:
value = 0
self.WriteToOperand(instruction.operands[0], value)
def process_ret(self, _):
if self.stack:
return self.stack.pop(-1)
def process_mov(self, instruction):
value = self.ReadFromOperand(instruction.operands[1])
self.WriteToOperand(instruction.operands[0], value)
def process_inc(self, instruction):
value = self.ReadFromOperand(instruction.operands[0])
self.WriteToOperand(instruction.operands[0], value + 1)
def process_dec(self, instruction):
value = self.ReadFromOperand(instruction.operands[0])
self.WriteToOperand(instruction.operands[0], value - 1)
def process_cmp(self, instruction):
"""We dont do anything with the comparison since we dont test for it."""
_ = instruction
def process_test(self, instruction):
"""We dont do anything with the comparison since we dont test for it."""
_ = instruction
def _Operate(self, instruction, operator):
value1 = self.ReadFromOperand(instruction.operands[0])
value2 = self.ReadFromOperand(instruction.operands[1])
self.WriteToOperand(
instruction.operands[0], operator(value1, value2))
def process_xor(self, instruction):
return self._Operate(instruction, lambda x, y: x ^ y)
def process_add(self, instruction):
return self._Operate(instruction, lambda x, y: x + y)
def process_sub(self, instruction):
return self._Operate(instruction, lambda x, y: x - y)
def process_and(self, instruction):
return self._Operate(instruction, lambda x, y: x & y)
def process_or(self, instruction):
return self._Operate(instruction, lambda x, y: x | y)
def process_shl(self, instruction):
return self._Operate(instruction, lambda x, y: x << (y % 0xFF))
def process_shr(self, instruction):
return self._Operate(instruction, lambda x, y: x >> (y % 0xFF))
def Inspect(self, function, instructions=10):
"""The main entry point to the Hook processor.
We emulate the function instructions and try to determine the jump
destination.
Args:
function: A basic.Function() instance.
"""
self.Reset()
self.address_space = function.obj_vm
for instruction in function.disassemble(instructions=instructions):
if instruction.is_return():
# RET Instruction terminates processing.
return self.process_ret(instruction)
elif instruction.mnemonic == "call":
return self.ReadFromOperand(instruction.operands[0])
# A JMP instruction.
elif instruction.is_branch():
return self.ReadFromOperand(instruction.operands[0])
else:
try:
handler = getattr(self, "process_%s" % instruction.mnemonic)
except AttributeError:
continue
# Handle the instruction.
try:
handler(instruction)
except Exception:
self.session.logging.error(
"Unable to handle instruction %s", instruction.op_str)
return
class CheckPEHooks(common.WindowsCommandPlugin):
"""Checks a pe file mapped into memory for hooks."""
name = "check_pehooks"
__args = [
dict(name="image_base", default=0,
positional=True, type="SymbolAddress",
help="The base address of the pe image in memory."),
dict(name="type", default="all",
choices=["all", "iat", "inline", "eat"],
type="Choice", help="Type of hook to display."),
dict(name="thorough", default=False, type="Boolean",
help="By default we take some optimization. This flags forces "
"thorough but slower checks."),
]
table_header = [
dict(name="Type", width=10),
dict(name="source", width=20),
dict(name="target", width=20),
dict(name="source_func", width=60),
dict(name="target_func"),
]
def reported_access(self, address):
"""Determines if the address should be reported.
This assesses the destination address for suspiciousness. For example if
the address resides in a VAD region which is not mapped by a dll then it
might be suspicious.
"""
destination_names = self.session.address_resolver.format_address(
address)
# For now very simple: If any of the destination_names start with vad_*
# it means that the address resolver cant determine which module they
# came from.
destination = hex(address)
for destination in destination_names:
if not destination.startswith("vad_"):
return False
return destination
def detect_IAT_hooks(self):
"""Detect Import Address Table hooks.
An IAT hook is where malware changes the IAT entry for a dll after its
loaded so that when it is called from within the DLL, flow control is
directed to the malware instead.
We determine the IAT entry is hooked if the address is outside the dll
which is imported.
"""
pe = pe_vtypes.PE(image_base=self.plugin_args.image_base,
session=self.session)
# First try to find all the names of the imported functions.
imports = [
(dll, func_name) for dll, func_name, _ in pe.ImportDirectory()]
resolver = self.session.address_resolver
for idx, (dll, func_address, _) in enumerate(pe.IAT()):
try:
target_dll, target_func_name = imports[idx]
target_dll = self.session.address_resolver.NormalizeModuleName(
target_dll)
except IndexError:
# We can not retrieve these function's name from the
# OriginalFirstThunk array - possibly because it is not mapped
# in.
target_dll = dll
target_func_name = ""
self.session.report_progress(
"Checking function %s!%s", target_dll, target_func_name)
# We only want the containing module.
module = resolver.GetContainingModule(func_address)
if module and target_dll == module.name:
continue
# Use ordinal if function has no name
if not len(target_func_name):
target_func_name = "(%s)" % idx
function_name = "%s!%s" % (target_dll, target_func_name)
# Function_name is the name which the PE file want
yield function_name, func_address
def collect_iat_hooks(self):
for function_name, func_address in self.detect_IAT_hooks():
yield dict(Type="IAT",
source=function_name,
target=utils.FormattedAddress(
self.session.address_resolver,
func_address, max_count=1, hex_if_unknown=True),
target_func=self.session.profile.Function(
func_address))
def detect_EAT_hooks(self, size=0):
"""Detect Export Address Table hooks.
An EAT hook is where malware changes the EAT entry for a dll after its
loaded so that a new DLL wants to link against it, the new DLL will use
the malware's function instead of the exporting DLL's function.
We determine the EAT entry is hooked if the address lies outside the
exporting dll.
"""
address_space = self.session.GetParameter("default_address_space")
pe = pe_vtypes.PE(image_base=self.plugin_args.image_base,
session=self.session,
address_space=address_space)
start = self.plugin_args.image_base
end = self.plugin_args.image_base + size
# If the dll size is not provided we parse it from the PE header.
if not size:
for _, _, virtual_address, section_size in pe.Sections():
# Only count executable sections.
section_end = (self.plugin_args.image_base +
virtual_address + section_size)
if section_end > end:
end = section_end
resolver = self.session.address_resolver
for dll, func, name, hint in pe.ExportDirectory():
self.session.report_progress("Checking export %s!%s", dll, name)
# Skip zero or invalid addresses.
if address_space.read(func.v(), 10) == "\x00" * 10:
continue
# Report on exports which fall outside the dll.
if start < func.v() < end:
continue
function_name = "%s:%s (%s)" % (
resolver.NormalizeModuleName(dll), name, hint)
yield function_name, func
def collect_eat_hooks(self):
for function_name, func_address in self.detect_EAT_hooks():
yield dict(Type="EAT",
source=function_name,
target=utils.FormattedAddress(
self.session.address_resolver,
func_address, max_count=1, hex_if_unknown=True),
target_func=self.session.profile.Function(
func_address))
def detect_inline_hooks(self):
"""A Generator of hooked exported functions from this PE file.
Yields:
A tuple of (function, name, jump_destination)
"""
# Inspect the export directory for inline hooks.
pe = pe_vtypes.PE(image_base=self.plugin_args.image_base,
address_space=self.session.GetParameter(
"default_address_space"),
session=self.session)
pfn_db = self.session.profile.get_constant_object("MmPfnDatabase")
heuristic = HookHeuristic(session=self.session)
ok_pages = set()
for _, function, name, _ in pe.ExportDirectory():
# Dereference the function pointer.
function_address = function.deref().obj_offset
self.session.report_progress(
"Checking function %#x (%s)", function, name)
# Check if the page is private or a file mapping. Usually if a
# mapped page is modified it will be converted to a private page due
# to Windows copy on write semantics. We assume that hooks are only
# placed in memory, and therefore functions which are still mapped
# to disk files are not hooked and can be safely skipped.
if not self.plugin_args.thorough:
# We must do the vtop in the process address space. This is the
# physical page backing the function preamble.
phys_address = function.obj_vm.vtop(function_address)
# Page not mapped.
if phys_address == None:
continue
phys_page = phys_address >> 12
# We determined this page is ok before - we can skip it.
if phys_page in ok_pages:
continue
# Get the PFN DB record.
pfn_obj = pfn_db[phys_page]
# The page is controlled by a prototype PTE which means it is
# still a file mapping. It has not been changed.
if pfn_obj.IsPrototype:
ok_pages.add(phys_page)
continue
# Try to detect an inline hook.
destination = heuristic.Inspect(function, instructions=3) or ""
# If we did not detect a hook we skip this function.
if destination:
yield function, name, destination
def collect_inline_hooks(self):
for function, _, destination in self.detect_inline_hooks():
hook_detected = False
# Try to resolve the destination into a name.
destination_name = self.reported_access(destination)
# We know about it. We suppress the output for jumps that go into a
# known module. These should be visible using the regular vad
# module.
if destination_name:
hook_detected = True
# Skip non hooked results if verbosity is too low.
if self.plugin_args.verbosity < 10 and not hook_detected:
continue
# Only highlight results if verbosity is high.
highlight = ""
if hook_detected and self.plugin_args.verbosity > 1:
highlight = "important"
yield dict(Type="Inline",
source=utils.FormattedAddress(
self.session.address_resolver,
function.deref(), max_count=1),
target=utils.FormattedAddress(
self.session.address_resolver,
destination, max_count=1),
source_func=function.deref(),
target_func=self.session.profile.Function(
destination),
highlight=highlight)
def collect(self):
if self.plugin_args.type in ["all", "inline"]:
for x in self.collect_inline_hooks():
yield x
if self.plugin_args.type in ["all", "iat"]:
for x in self.collect_iat_hooks():
yield x
if self.plugin_args.type in ["all", "eat"]:
for x in self.collect_eat_hooks():
yield x
class EATHooks(common.WinProcessFilter):
"""Detect EAT hooks in process and kernel memory"""
name = "hooks_eat"
table_header = [
dict(name="divider", type="Divider"),
dict(name="_EPROCESS", hidden=True),
dict(name="Type", hidden=True),
dict(name="source", width=20),
dict(name="target", width=20),
dict(name="target_func"),
]
checker_method = CheckPEHooks.collect_eat_hooks
def column_types(self):
return dict(_EPROCESS=self.session.profile._EPROCESS,
source="",
target="",
target_func=self.session.profile.Function())
def collect_hooks(self, task, dll):
checker = self.session.plugins.check_pehooks(
image_base=dll.base, thorough=self.plugin_args.thorough)
for info in self.checker_method(checker):
info["_EPROCESS"] = task
yield info
def collect(self):
cc = self.session.plugins.cc()
with cc:
for task in self.filter_processes():
cc.SwitchProcessContext(task)
yield dict(divider="Process %s (%s)" % (task.name, task.pid))
for dll in task.get_load_modules():
for x in self.collect_hooks(task, dll):
yield x
class TestEATHooks(testlib.SimpleTestCase):
PLUGIN = "hooks_eat"
PARAMETERS = dict(
commandline="hooks_eat %(pids)s"
)
class IATHooks(EATHooks):
"""Detect IAT/EAT hooks in process and kernel memory"""
name = "hooks_iat"
checker_method = CheckPEHooks.collect_iat_hooks
class TestIATHooks(testlib.SimpleTestCase):
PLUGIN = "hooks_iat"
PARAMETERS = dict(
commandline="hooks_iat %(pids)s"
)
class InlineHooks(EATHooks):
"""Detect API hooks in process and kernel memory"""
name = "hooks_inline"
checker_method = CheckPEHooks.collect_inline_hooks
table_header = [
dict(name="divider", type="Divider"),
dict(name="_EPROCESS", hidden=True),
dict(name="source", width=20),
dict(name="target", width=20),
dict(name="Type", hidden=True),
dict(name="source_func", width=60),
dict(name="target_func"),
]
class TestInlineHooks(testlib.SimpleTestCase):
PLUGIN = "hooks_inline"
PARAMETERS = dict(
commandline="hooks_inline %(pids)s"
)
|