2021-08-15 02:41:37 +02:00
|
|
|
import pefile # type: ignore
|
2021-09-03 06:33:25 +02:00
|
|
|
import struct
|
|
|
|
import sys
|
2021-09-04 04:48:49 +02:00
|
|
|
from iced_x86 import Decoder, Instruction, Formatter, FormatterSyntax, FormatMnemonicOptions
|
2021-09-03 06:33:25 +02:00
|
|
|
from typing import Any, List, Dict, Optional
|
|
|
|
|
|
|
|
|
|
|
|
class Memory:
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.values: Dict[int, int] = {}
|
2021-09-04 04:48:49 +02:00
|
|
|
self.defaults: Dict[int, bytes] = {}
|
2021-09-03 06:33:25 +02:00
|
|
|
|
2021-09-09 03:04:17 +02:00
|
|
|
def init(self, min_offset: int, max_offset: int) -> None:
|
|
|
|
for i in range(min_offset + 1, max_offset + 1):
|
|
|
|
self.store(i, self.load(i, 1))
|
|
|
|
|
2021-09-03 06:33:25 +02:00
|
|
|
def store(self, offset: int, data: bytes) -> None:
|
|
|
|
for i, b in enumerate(data):
|
|
|
|
self.values[i + offset] = b
|
|
|
|
|
|
|
|
def load(self, offset: int, length: int) -> bytes:
|
|
|
|
data: List[int] = []
|
|
|
|
|
|
|
|
for i in range(offset, offset + length):
|
|
|
|
if i in self.values:
|
2021-09-04 04:48:49 +02:00
|
|
|
# Return modified value.
|
2021-09-03 06:33:25 +02:00
|
|
|
data.append(self.values[i])
|
|
|
|
else:
|
2021-09-04 04:48:49 +02:00
|
|
|
# Attempt to return the default.
|
|
|
|
for virtual_start in self.defaults:
|
|
|
|
if offset >= virtual_start and offset < (virtual_start + len(self.defaults[virtual_start])):
|
|
|
|
data.append(self.defaults[virtual_start][offset - virtual_start])
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# Nothing here, return initialized RAM.
|
|
|
|
data.append(0)
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
return bytes(data)
|
|
|
|
|
|
|
|
|
|
|
|
class Registers:
|
2021-09-04 00:42:31 +02:00
|
|
|
def __init__(self, stack: int) -> None:
|
2021-09-03 06:33:55 +02:00
|
|
|
self.rax = 0
|
|
|
|
self.rbx = 0
|
|
|
|
self.rcx = 0
|
|
|
|
self.rdx = 0
|
|
|
|
self.rsi = 0
|
|
|
|
self.rdi = 0
|
|
|
|
self.rbp = 0
|
2021-09-04 00:42:31 +02:00
|
|
|
self.rsp = stack
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
self.zf = False
|
2021-09-03 06:35:53 +02:00
|
|
|
self.sf = False
|
2021-08-15 02:41:37 +02:00
|
|
|
|
|
|
|
|
2021-09-04 04:48:49 +02:00
|
|
|
class JumpException(Exception):
|
|
|
|
def __init__(self, address: int, message: str) -> None:
|
|
|
|
super().__init__(message)
|
|
|
|
self.address = address
|
|
|
|
|
|
|
|
|
|
|
|
class RetException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-08-15 02:41:37 +02:00
|
|
|
class PEFile:
|
|
|
|
def __init__(self, data: bytes) -> None:
|
2021-09-03 06:33:25 +02:00
|
|
|
self.data = data
|
2021-08-15 02:41:37 +02:00
|
|
|
self.__pe = pefile.PE(data=data, fast_load=True)
|
2021-09-03 06:33:25 +02:00
|
|
|
self.__adhoc_mapping: Dict[int, int] = {}
|
2021-08-15 02:41:37 +02:00
|
|
|
|
|
|
|
def virtual_to_physical(self, offset: int) -> int:
|
|
|
|
for section in self.__pe.sections:
|
|
|
|
start = section.VirtualAddress + self.__pe.OPTIONAL_HEADER.ImageBase
|
|
|
|
end = start + section.SizeOfRawData
|
|
|
|
|
|
|
|
if offset >= start and offset < end:
|
|
|
|
return (offset - start) + section.PointerToRawData
|
|
|
|
|
2021-09-03 06:33:25 +02:00
|
|
|
for virtual, physical in self.__adhoc_mapping.items():
|
|
|
|
if offset == virtual:
|
|
|
|
return physical
|
|
|
|
|
2021-08-15 02:41:37 +02:00
|
|
|
raise Exception(f"Couldn't find physical offset for virtual offset 0x{offset:08x}")
|
|
|
|
|
2021-09-03 06:33:25 +02:00
|
|
|
def physical_to_virtual(self, offset: int) -> int:
|
|
|
|
for section in self.__pe.sections:
|
|
|
|
start = section.PointerToRawData
|
|
|
|
end = start + section.SizeOfRawData
|
|
|
|
|
|
|
|
if offset >= start and offset < end:
|
|
|
|
return (offset - start) + section.VirtualAddress + self.__pe.OPTIONAL_HEADER.ImageBase
|
|
|
|
|
|
|
|
for virtual, physical in self.__adhoc_mapping.items():
|
|
|
|
if offset == physical:
|
|
|
|
return virtual
|
|
|
|
|
|
|
|
raise Exception(f"Couldn't find virtual offset for physical offset 0x{offset:08x}")
|
|
|
|
|
2021-08-15 02:41:37 +02:00
|
|
|
def is_virtual(self, offset: int) -> bool:
|
|
|
|
return offset >= self.__pe.OPTIONAL_HEADER.ImageBase
|
|
|
|
|
|
|
|
def is_64bit(self) -> bool:
|
|
|
|
return hex(self.__pe.FILE_HEADER.Machine) == '0x8664'
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
def emulate_code(self, start: int, end: int, verbose: bool = False) -> None:
|
|
|
|
if self.is_virtual(start):
|
|
|
|
# Assume this is virtual
|
|
|
|
start = self.virtual_to_physical(start)
|
|
|
|
if self.is_virtual(end):
|
|
|
|
# Assume this is virtual
|
|
|
|
end = self.virtual_to_physical(end)
|
|
|
|
|
2021-09-04 04:48:49 +02:00
|
|
|
registers = Registers(0xFFFFFFFFFFFFFFFF if self.is_64bit() else 0xFFFFFFFF)
|
|
|
|
memory = self.__to_memory()
|
|
|
|
|
|
|
|
decoder = Decoder(64 if self.is_64bit() else 32, self.data[start:end], ip=self.physical_to_virtual(start))
|
|
|
|
self.__emulate_chunk(registers, memory, [i for i in decoder], verbose)
|
|
|
|
|
|
|
|
# Replace memory that we care about.
|
|
|
|
self.__update(memory)
|
|
|
|
|
|
|
|
def emulate_function(self, start: int, verbose: bool = False) -> None:
|
|
|
|
if self.is_virtual(start):
|
|
|
|
# Assume this is virtual
|
|
|
|
start = self.virtual_to_physical(start)
|
|
|
|
|
|
|
|
registers = Registers(0xFFFFFFFFFFFFFFFF if self.is_64bit() else 0xFFFFFFFF)
|
|
|
|
memory = self.__to_memory()
|
|
|
|
|
|
|
|
# Need to fetch one at a time, emulating until we get a ret.
|
|
|
|
loc = start
|
|
|
|
end = len(self.data)
|
|
|
|
while True:
|
|
|
|
decoder = Decoder(64 if self.is_64bit() else 32, self.data[loc:end], ip=self.physical_to_virtual(loc))
|
|
|
|
chunk = [decoder.decode()]
|
|
|
|
|
|
|
|
try:
|
|
|
|
# First attempt to just run the instruction as normal.
|
|
|
|
self.__emulate_chunk(registers, memory, chunk, verbose)
|
|
|
|
loc = self.virtual_to_physical(chunk[0].next_ip)
|
|
|
|
except JumpException as jmp:
|
|
|
|
# We need to jump elsewhere.
|
|
|
|
loc = self.virtual_to_physical(jmp.address)
|
|
|
|
except RetException:
|
|
|
|
# We're done!
|
|
|
|
break
|
|
|
|
|
|
|
|
# Replace memory that we care about.
|
|
|
|
self.__update(memory)
|
|
|
|
|
|
|
|
def __to_memory(self) -> Memory:
|
|
|
|
memory = Memory()
|
|
|
|
|
|
|
|
for section in self.__pe.sections:
|
|
|
|
virtual = section.VirtualAddress + self.__pe.OPTIONAL_HEADER.ImageBase
|
|
|
|
length = section.SizeOfRawData
|
|
|
|
physical = self.virtual_to_physical(virtual)
|
|
|
|
memory.defaults[virtual] = self.data[physical:(physical + length)]
|
|
|
|
|
|
|
|
for virtual, physical in self.__adhoc_mapping.items():
|
|
|
|
memory.values[virtual] = self.data[physical]
|
|
|
|
|
|
|
|
return memory
|
|
|
|
|
|
|
|
def __update(self, memory: Memory) -> None:
|
|
|
|
newdata = [x for x in self.data]
|
|
|
|
for virtual in sorted(memory.values):
|
|
|
|
try:
|
|
|
|
physical = self.virtual_to_physical(virtual)
|
|
|
|
newdata[physical] = memory.values[virtual]
|
|
|
|
except Exception:
|
|
|
|
# This is outside of the data we are tracking. Its really not ideal
|
|
|
|
# that we are just shoving this at the end of the data, but it should
|
|
|
|
# work for what we care about.
|
|
|
|
physical = len(newdata)
|
|
|
|
self.__adhoc_mapping[virtual] = physical
|
|
|
|
newdata.append(memory.values[virtual])
|
|
|
|
|
|
|
|
self.data = bytes(newdata)
|
|
|
|
self.__pe = pefile.PE(data=self.data, fast_load=True)
|
|
|
|
|
|
|
|
def __emulate_chunk(self, registers: Registers, memory: Memory, chunk: List[Instruction], verbose: bool) -> None:
|
2021-09-03 06:33:25 +02:00
|
|
|
if verbose:
|
|
|
|
def vprint(*args: Any, **kwargs: Any) -> None:
|
|
|
|
print(*args, **kwargs, file=sys.stderr)
|
|
|
|
else:
|
|
|
|
def vprint(*args: Any, **kwargs: Any) -> None:
|
|
|
|
pass
|
|
|
|
|
2021-09-04 04:48:49 +02:00
|
|
|
# Stuck here so that jump can bind to it.
|
|
|
|
loc: int = 0
|
2021-09-03 06:33:25 +02:00
|
|
|
|
2021-09-04 04:48:49 +02:00
|
|
|
def jump(destination: int) -> None:
|
|
|
|
nonlocal loc
|
|
|
|
|
|
|
|
for i in range(len(chunk)):
|
|
|
|
if chunk[i].ip == destination:
|
|
|
|
# Jump to this instruction.
|
|
|
|
loc = i
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
if destination == chunk[-1].next_ip:
|
|
|
|
# Jump to the end, we're done.
|
|
|
|
loc = len(chunk)
|
|
|
|
else:
|
|
|
|
raise JumpException(destination, f"Jumping to {hex(destination)} which is outside of our evaluation range!")
|
2021-09-03 06:33:25 +02:00
|
|
|
|
2021-09-04 04:48:49 +02:00
|
|
|
formatter = Formatter(FormatterSyntax.NASM) # type: ignore
|
|
|
|
|
|
|
|
while loc < len(chunk):
|
|
|
|
inst = chunk[loc]
|
2021-09-03 06:33:25 +02:00
|
|
|
loc = loc + 1
|
|
|
|
mnemonic = formatter.format_mnemonic(inst, FormatMnemonicOptions.NO_PREFIXES) # type: ignore
|
|
|
|
|
|
|
|
if mnemonic == "mov":
|
|
|
|
dest = formatter.format_operand(inst, 0)
|
|
|
|
src = formatter.format_operand(inst, 1)
|
|
|
|
|
|
|
|
vprint(f"mov {dest}, {src}")
|
|
|
|
|
|
|
|
size = get_size(src) or get_size(dest)
|
|
|
|
if size is None:
|
|
|
|
raise Exception(f"Could not determine size of {mnemonic} operation!")
|
|
|
|
result = fetch(registers, memory, size, src)
|
|
|
|
assign(registers, memory, size, dest, result)
|
|
|
|
|
2021-09-04 00:42:47 +02:00
|
|
|
elif mnemonic == "add":
|
|
|
|
dest = formatter.format_operand(inst, 0)
|
|
|
|
amt = formatter.format_operand(inst, 1)
|
|
|
|
|
|
|
|
vprint(f"add {dest}, {amt}")
|
|
|
|
|
|
|
|
size = get_size(amt) or get_size(dest)
|
|
|
|
if size is None:
|
|
|
|
raise Exception(f"Could not determine size of {mnemonic} operation!")
|
2021-09-09 03:04:17 +02:00
|
|
|
|
|
|
|
# Special case for adjusting ESP, to make sure our memory contains zeros for reading
|
|
|
|
# out the stack later.
|
|
|
|
if dest == "esp":
|
|
|
|
before = fetch(registers, memory, size, dest)
|
|
|
|
after = before + fetch(registers, memory, size, amt)
|
|
|
|
memory.init(min(before, after), max(before, after))
|
|
|
|
assign(registers, memory, size, dest, after)
|
|
|
|
else:
|
|
|
|
result = fetch(registers, memory, size, dest) + fetch(registers, memory, size, amt)
|
|
|
|
assign(registers, memory, size, dest, result)
|
2021-09-04 00:42:47 +02:00
|
|
|
|
2021-09-03 06:33:25 +02:00
|
|
|
elif mnemonic == "sub":
|
|
|
|
dest = formatter.format_operand(inst, 0)
|
|
|
|
amt = formatter.format_operand(inst, 1)
|
|
|
|
|
|
|
|
vprint(f"sub {dest}, {amt}")
|
|
|
|
|
|
|
|
size = get_size(amt) or get_size(dest)
|
|
|
|
if size is None:
|
|
|
|
raise Exception(f"Could not determine size of {mnemonic} operation!")
|
2021-09-09 03:04:17 +02:00
|
|
|
|
|
|
|
# Special case for adjusting ESP, to make sure our memory contains zeros for reading
|
|
|
|
# out the stack later.
|
|
|
|
if dest == "esp":
|
|
|
|
before = fetch(registers, memory, size, dest)
|
|
|
|
after = before - fetch(registers, memory, size, amt)
|
|
|
|
memory.init(min(before, after), max(before, after))
|
|
|
|
assign(registers, memory, size, dest, after)
|
|
|
|
else:
|
|
|
|
result = fetch(registers, memory, size, dest) - fetch(registers, memory, size, amt)
|
|
|
|
assign(registers, memory, size, dest, result)
|
2021-09-03 06:33:25 +02:00
|
|
|
|
2021-09-04 00:42:47 +02:00
|
|
|
elif mnemonic == "imul":
|
|
|
|
dest = formatter.format_operand(inst, 0)
|
|
|
|
mult = formatter.format_operand(inst, 1)
|
|
|
|
try:
|
|
|
|
const = formatter.format_operand(inst, 2)
|
|
|
|
vprint(f"imul {dest}, {mult}, {const}")
|
|
|
|
except Exception:
|
|
|
|
const = None
|
|
|
|
vprint(f"imul {dest}, {mult}")
|
|
|
|
|
2021-09-04 04:48:49 +02:00
|
|
|
size = get_size(mult) or get_size(dest) or (get_size(const) if const is not None else None)
|
2021-09-04 00:42:47 +02:00
|
|
|
if size is None:
|
|
|
|
raise Exception(f"Could not determine size of {mnemonic} operation!")
|
|
|
|
if const is None:
|
|
|
|
result = fetch(registers, memory, size, dest) * fetch(registers, memory, size, mult)
|
|
|
|
else:
|
|
|
|
result = fetch(registers, memory, size, mult) * get_value(const)
|
|
|
|
assign(registers, memory, size, dest, result)
|
|
|
|
|
2021-09-03 06:33:25 +02:00
|
|
|
elif mnemonic == "push":
|
|
|
|
src = formatter.format_operand(inst, 0)
|
|
|
|
|
|
|
|
vprint(f"push {src}")
|
|
|
|
|
|
|
|
size = get_size(src)
|
|
|
|
if size is None:
|
|
|
|
raise Exception(f"Could not determine size of {mnemonic} operation!")
|
|
|
|
result = fetch(registers, memory, size, src)
|
2021-09-03 06:33:55 +02:00
|
|
|
registers.rsp -= size
|
|
|
|
assign(registers, memory, size, "[rsp]" if self.is_64bit() else "[esp]", result)
|
|
|
|
|
|
|
|
elif mnemonic == "pop":
|
|
|
|
dest = formatter.format_operand(inst, 0)
|
|
|
|
|
|
|
|
vprint(f"pop {dest}")
|
|
|
|
|
|
|
|
size = get_size(src)
|
|
|
|
if size is None:
|
|
|
|
raise Exception(f"Could not determine size of {mnemonic} operation!")
|
|
|
|
result = fetch(registers, memory, size, "[rsp]" if self.is_64bit() else "[esp]")
|
|
|
|
assign(registers, memory, size, dest, result)
|
|
|
|
registers.rsp += size
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
elif mnemonic == "test":
|
|
|
|
op1 = formatter.format_operand(inst, 0)
|
|
|
|
op2 = formatter.format_operand(inst, 1)
|
|
|
|
|
|
|
|
vprint(f"test {op1}, {op2}")
|
|
|
|
|
|
|
|
size = get_size(op1) or get_size(op2)
|
|
|
|
if size is None:
|
|
|
|
raise Exception(f"Could not determine size of {mnemonic} operation!")
|
|
|
|
result = fetch(registers, memory, size, op1) & fetch(registers, memory, size, op2)
|
2021-09-03 06:35:53 +02:00
|
|
|
|
2021-09-03 06:33:25 +02:00
|
|
|
registers.zf = result == 0
|
2021-09-03 06:35:53 +02:00
|
|
|
if size == 1:
|
|
|
|
registers.sf = (result & 0x80) != 0
|
|
|
|
if size == 2:
|
|
|
|
registers.sf = (result & 0x8000) != 0
|
|
|
|
if size == 4:
|
|
|
|
registers.sf = (result & 0x80000000) != 0
|
|
|
|
if size == 8:
|
|
|
|
registers.sf = (result & 0x8000000000000000) != 0
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
elif mnemonic == "jne":
|
|
|
|
dest = formatter.format_operand(inst, 0)
|
|
|
|
|
2021-09-03 06:33:55 +02:00
|
|
|
vprint(f"jnz {dest}")
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
if not registers.zf:
|
|
|
|
destination = get_value(dest)
|
|
|
|
if destination is None:
|
|
|
|
raise Exception(f"Jumping to unsupported destination {dest}")
|
2021-09-04 04:48:49 +02:00
|
|
|
jump(destination)
|
2021-09-03 06:33:25 +02:00
|
|
|
|
2021-09-03 06:35:53 +02:00
|
|
|
elif mnemonic == "je":
|
|
|
|
dest = formatter.format_operand(inst, 0)
|
|
|
|
|
|
|
|
vprint(f"jz {dest}")
|
|
|
|
|
|
|
|
if registers.zf:
|
|
|
|
destination = get_value(dest)
|
|
|
|
if destination is None:
|
|
|
|
raise Exception(f"Jumping to unsupported destination {dest}")
|
2021-09-04 04:48:49 +02:00
|
|
|
jump(destination)
|
2021-09-03 06:35:53 +02:00
|
|
|
|
|
|
|
elif mnemonic == "jns":
|
|
|
|
dest = formatter.format_operand(inst, 0)
|
|
|
|
|
|
|
|
vprint(f"jns {dest}")
|
|
|
|
|
|
|
|
if not registers.sf:
|
|
|
|
destination = get_value(dest)
|
|
|
|
if destination is None:
|
|
|
|
raise Exception(f"Jumping to unsupported destination {dest}")
|
2021-09-04 04:48:49 +02:00
|
|
|
jump(destination)
|
2021-09-03 06:35:53 +02:00
|
|
|
|
|
|
|
elif mnemonic == "js":
|
|
|
|
dest = formatter.format_operand(inst, 0)
|
|
|
|
|
|
|
|
vprint(f"js {dest}")
|
|
|
|
|
|
|
|
if registers.sf:
|
|
|
|
destination = get_value(dest)
|
|
|
|
if destination is None:
|
|
|
|
raise Exception(f"Jumping to unsupported destination {dest}")
|
2021-09-04 04:48:49 +02:00
|
|
|
jump(destination)
|
2021-09-03 06:35:53 +02:00
|
|
|
|
2021-09-03 06:33:25 +02:00
|
|
|
elif mnemonic == "jmp":
|
|
|
|
dest = formatter.format_operand(inst, 0)
|
|
|
|
|
|
|
|
vprint(f"jmp {dest}")
|
|
|
|
|
|
|
|
destination = get_value(dest)
|
|
|
|
if destination is None:
|
|
|
|
raise Exception(f"Jumping to unsupported destination {dest}")
|
2021-09-04 04:48:49 +02:00
|
|
|
jump(destination)
|
2021-09-03 06:33:25 +02:00
|
|
|
|
2021-09-04 00:42:47 +02:00
|
|
|
elif mnemonic == "and":
|
|
|
|
dest = formatter.format_operand(inst, 0)
|
|
|
|
src = formatter.format_operand(inst, 1)
|
|
|
|
|
|
|
|
vprint(f"and {dest}, {src}")
|
|
|
|
|
|
|
|
size = get_size(src) or get_size(dest)
|
|
|
|
if size is None:
|
|
|
|
raise Exception(f"Could not determine size of {mnemonic} operation!")
|
|
|
|
result = fetch(registers, memory, size, dest) & fetch(registers, memory, size, src)
|
|
|
|
assign(registers, memory, size, dest, result)
|
|
|
|
|
2021-09-03 06:33:25 +02:00
|
|
|
elif mnemonic == "or":
|
|
|
|
dest = formatter.format_operand(inst, 0)
|
|
|
|
src = formatter.format_operand(inst, 1)
|
|
|
|
|
|
|
|
vprint(f"or {dest}, {src}")
|
|
|
|
|
|
|
|
size = get_size(src) or get_size(dest)
|
|
|
|
if size is None:
|
|
|
|
raise Exception(f"Could not determine size of {mnemonic} operation!")
|
|
|
|
result = fetch(registers, memory, size, dest) | fetch(registers, memory, size, src)
|
|
|
|
assign(registers, memory, size, dest, result)
|
|
|
|
|
|
|
|
elif mnemonic == "xor":
|
|
|
|
dest = formatter.format_operand(inst, 0)
|
|
|
|
src = formatter.format_operand(inst, 1)
|
|
|
|
|
|
|
|
vprint(f"xor {dest}, {src}")
|
|
|
|
|
|
|
|
size = get_size(src) or get_size(dest)
|
|
|
|
if size is None:
|
|
|
|
raise Exception(f"Could not determine size of {mnemonic} operation!")
|
|
|
|
result = fetch(registers, memory, size, dest) ^ fetch(registers, memory, size, src)
|
|
|
|
assign(registers, memory, size, dest, result)
|
|
|
|
|
2021-09-03 06:35:53 +02:00
|
|
|
elif mnemonic == "lea":
|
|
|
|
dest = formatter.format_operand(inst, 0)
|
|
|
|
src = formatter.format_operand(inst, 1)
|
|
|
|
|
|
|
|
vprint(f"lea {dest}, {src}")
|
|
|
|
|
|
|
|
size = get_size(src) or get_size(dest)
|
|
|
|
if size is None:
|
|
|
|
raise Exception(f"Could not determine size of {mnemonic} operation!")
|
|
|
|
result = get_address(registers, src)
|
|
|
|
if result is None:
|
|
|
|
raise Exception(f"Could not compute effective address for {mnemonic} operation!")
|
|
|
|
assign(registers, memory, size, dest, result)
|
|
|
|
|
2021-09-04 04:48:49 +02:00
|
|
|
elif mnemonic == "ret":
|
|
|
|
vprint("ret")
|
2021-09-03 06:33:25 +02:00
|
|
|
|
2021-09-04 04:48:49 +02:00
|
|
|
raise RetException("Encountered {mnemonic} instruction but we aren't in function context!")
|
2021-09-03 06:33:25 +02:00
|
|
|
|
2021-09-04 04:48:49 +02:00
|
|
|
else:
|
|
|
|
raise Exception(f"Unsupported mnemonic {mnemonic}!")
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
def sanitize(indirect: str) -> str:
|
2021-09-03 06:33:55 +02:00
|
|
|
"""
|
|
|
|
Given an indirect address or a value from iced-x86 as formatted by the
|
|
|
|
operand formatter, sanitize it by getting rid of size specifiers.
|
|
|
|
"""
|
|
|
|
|
2021-09-03 06:33:25 +02:00
|
|
|
if indirect[:5] == "near ":
|
|
|
|
indirect = indirect[5:]
|
|
|
|
|
2021-09-04 00:42:47 +02:00
|
|
|
if indirect[:4] == "rel ":
|
|
|
|
indirect = indirect[4:]
|
|
|
|
|
2021-09-03 06:33:25 +02:00
|
|
|
if indirect[:6] == "short ":
|
|
|
|
indirect = indirect[6:]
|
|
|
|
|
|
|
|
if indirect[:5] == "byte ":
|
|
|
|
indirect = indirect[5:]
|
|
|
|
|
|
|
|
if indirect[:5] == "word ":
|
|
|
|
indirect = indirect[5:]
|
|
|
|
|
|
|
|
if indirect[:6] == "dword ":
|
|
|
|
indirect = indirect[6:]
|
|
|
|
|
2021-09-03 06:33:55 +02:00
|
|
|
if indirect[:6] == "qword ":
|
|
|
|
indirect = indirect[6:]
|
|
|
|
|
2021-09-03 06:33:25 +02:00
|
|
|
return indirect
|
|
|
|
|
|
|
|
|
|
|
|
def get_address(registers: Registers, indirect: str) -> Optional[int]:
|
2021-09-03 06:33:55 +02:00
|
|
|
"""
|
|
|
|
Given an indirect reference as formatted by the iced-x86 operand formatter,
|
|
|
|
resolve it to an actual 32-bit address that we should load from or store to.
|
|
|
|
This optionally supports indirect register address format so that we can
|
|
|
|
conveniently specify fetches and stores from the stack. If the value we
|
|
|
|
receive is not actually an indirect reference, return None.
|
|
|
|
"""
|
|
|
|
|
2021-09-03 06:33:25 +02:00
|
|
|
indirect = sanitize(indirect)
|
|
|
|
|
|
|
|
if indirect[0] == "[" and indirect[-1] == "]":
|
2021-09-04 00:42:47 +02:00
|
|
|
indirect = sanitize(indirect[1:-1])
|
2021-09-03 06:35:53 +02:00
|
|
|
|
|
|
|
adjust = 0
|
|
|
|
if '+' in indirect:
|
|
|
|
indirect, const = indirect.split('+', 1)
|
2021-09-04 00:42:47 +02:00
|
|
|
indirect = sanitize(indirect)
|
|
|
|
const = sanitize(const)
|
2021-09-03 06:33:25 +02:00
|
|
|
|
2021-09-03 06:35:53 +02:00
|
|
|
if const[-1] == 'h':
|
|
|
|
adjust = int(const[:-1], 16)
|
|
|
|
else:
|
2021-09-09 03:04:17 +02:00
|
|
|
adjust = int(const, 10)
|
2021-09-03 06:35:53 +02:00
|
|
|
elif '-' in indirect:
|
|
|
|
indirect, const = indirect.split('-', 1)
|
2021-09-04 00:42:47 +02:00
|
|
|
indirect = sanitize(indirect)
|
|
|
|
const = sanitize(const)
|
2021-09-03 06:33:25 +02:00
|
|
|
|
2021-09-03 06:35:53 +02:00
|
|
|
if const[-1] == 'h':
|
|
|
|
adjust = -int(const[:-1], 16)
|
|
|
|
else:
|
2021-09-09 03:04:17 +02:00
|
|
|
adjust = -int(const, 10)
|
2021-09-03 06:35:53 +02:00
|
|
|
|
|
|
|
if indirect[-1] == 'h':
|
|
|
|
return int(indirect[:-1], 16) + adjust
|
|
|
|
|
|
|
|
# Register-based indirect modes.
|
|
|
|
if indirect == "rsp":
|
|
|
|
return registers.rsp + adjust
|
|
|
|
if indirect == "esp":
|
|
|
|
return (registers.rsp & 0xFFFFFFFF) + adjust
|
|
|
|
if indirect == "sp":
|
|
|
|
return (registers.rsp & 0xFFFF) + adjust
|
|
|
|
if indirect == "spl":
|
|
|
|
return (registers.rsp & 0xFF) + adjust
|
|
|
|
if indirect == "rbp":
|
|
|
|
return registers.rbp + adjust
|
|
|
|
if indirect == "ebp":
|
|
|
|
return (registers.rbp & 0xFFFFFFFF) + adjust
|
|
|
|
if indirect == "bp":
|
|
|
|
return (registers.rbp & 0xFFFF) + adjust
|
|
|
|
if indirect == "bp":
|
|
|
|
return (registers.rbp & 0xFF) + adjust
|
|
|
|
if indirect == "rsi":
|
|
|
|
return registers.rsi + adjust
|
|
|
|
if indirect == "esi":
|
|
|
|
return (registers.rsi & 0xFFFFFFFF) + adjust
|
|
|
|
if indirect == "si":
|
|
|
|
return (registers.rsi & 0xFFFF) + adjust
|
|
|
|
if indirect == "si":
|
|
|
|
return (registers.rsi & 0xFF) + adjust
|
|
|
|
if indirect == "rdi":
|
|
|
|
return registers.rdi + adjust
|
|
|
|
if indirect == "edi":
|
|
|
|
return (registers.rdi & 0xFFFFFFFF) + adjust
|
|
|
|
if indirect == "di":
|
|
|
|
return (registers.rdi & 0xFFFF) + adjust
|
|
|
|
if indirect == "di":
|
|
|
|
return (registers.rdi & 0xFF) + adjust
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
raise Exception(f"Unsupported indirect address {indirect}!")
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def get_value(immediate: str) -> Optional[int]:
|
2021-09-03 06:33:55 +02:00
|
|
|
"""
|
|
|
|
Given an immediate value as formatted by the iced-x86 operand formatter,
|
|
|
|
resolve it to an immediate integer. If the value we receive is not
|
|
|
|
actually an immediate value, return None.
|
|
|
|
"""
|
|
|
|
|
2021-09-03 06:33:25 +02:00
|
|
|
immediate = sanitize(immediate)
|
|
|
|
|
|
|
|
if immediate[-1] == "h":
|
|
|
|
try:
|
|
|
|
return int(immediate[:-1], 16)
|
|
|
|
except Exception:
|
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
|
|
|
return int(immediate, 10)
|
|
|
|
except Exception:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2021-09-03 06:33:55 +02:00
|
|
|
def get_size(operand: str) -> Optional[int]:
|
|
|
|
"""
|
|
|
|
Given an operand as formatted by the iced-x86 operand formatter, return
|
|
|
|
the size in bytes that that operand represents in a load or store.
|
|
|
|
Supports both registers and byte/word/dword/qword specifiers in front of
|
|
|
|
immediate values and indirect memory references.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if operand in {'rax', 'rbx', 'rcx', 'rdx', 'rsp', 'rbp', 'rsi', 'rdi'}:
|
|
|
|
return 8
|
|
|
|
if operand in {'eax', 'ebx', 'ecx', 'edx', 'esp', 'ebp', 'esi', 'edi'}:
|
2021-09-03 06:33:25 +02:00
|
|
|
return 4
|
2021-09-03 06:33:55 +02:00
|
|
|
if operand in {'ax', 'bx', 'cx', 'dx', 'sp', 'bp', 'si', 'di'}:
|
2021-09-03 06:33:25 +02:00
|
|
|
return 2
|
2021-09-03 06:33:55 +02:00
|
|
|
if operand in {'ah', 'al', 'bh', 'bl', 'ch', 'cl', 'dh', 'dl', 'spl', 'bpl', 'sil', 'dil'}:
|
2021-09-03 06:33:25 +02:00
|
|
|
return 1
|
|
|
|
|
2021-09-03 06:33:55 +02:00
|
|
|
if operand[:5] == "byte ":
|
2021-09-03 06:33:25 +02:00
|
|
|
return 1
|
|
|
|
|
2021-09-03 06:33:55 +02:00
|
|
|
if operand[:5] == "word ":
|
2021-09-03 06:33:25 +02:00
|
|
|
return 2
|
|
|
|
|
2021-09-03 06:33:55 +02:00
|
|
|
if operand[:6] == "dword ":
|
2021-09-03 06:33:25 +02:00
|
|
|
return 4
|
|
|
|
|
2021-09-03 06:33:55 +02:00
|
|
|
if operand[:6] == "qword ":
|
|
|
|
return 8
|
|
|
|
|
2021-09-03 06:33:25 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def assign(registers: Registers, memory: Memory, size: int, loc: str, value: int) -> None:
|
2021-09-03 06:33:55 +02:00
|
|
|
"""
|
|
|
|
Given the registers and memory of our emulator, the size of the operation
|
|
|
|
performed, the location to assign to and the value we should assign,
|
|
|
|
compute where the assignment should happen and then execute it.
|
|
|
|
"""
|
|
|
|
|
2021-09-03 06:33:25 +02:00
|
|
|
address = get_address(registers, loc)
|
|
|
|
if address is not None:
|
|
|
|
if size == 1:
|
|
|
|
data = struct.pack("<B", value)
|
|
|
|
elif size == 2:
|
|
|
|
data = struct.pack("<H", value)
|
|
|
|
elif size == 4:
|
|
|
|
data = struct.pack("<I", value)
|
2021-09-03 07:07:15 +02:00
|
|
|
elif size == 8:
|
|
|
|
data = struct.pack("<Q", value)
|
2021-09-03 06:33:25 +02:00
|
|
|
else:
|
|
|
|
raise Exception(f"Unsupported size {size} for memory assign!")
|
|
|
|
memory.store(address, data)
|
|
|
|
return
|
|
|
|
|
2021-09-03 06:33:55 +02:00
|
|
|
if loc == "rax":
|
|
|
|
registers.rax = value
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "rbx":
|
|
|
|
registers.rbx = value
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "rcx":
|
|
|
|
registers.rcx = value
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "rdx":
|
|
|
|
registers.rdx = value
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "rsp":
|
|
|
|
registers.rsp = value
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "rbp":
|
|
|
|
registers.rbp = value
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "rsi":
|
|
|
|
registers.rsi = value
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "rdi":
|
|
|
|
registers.rdi = value
|
|
|
|
return
|
|
|
|
|
2021-09-03 06:33:25 +02:00
|
|
|
if loc == "eax":
|
2021-09-03 06:33:55 +02:00
|
|
|
registers.rax = (registers.rax & 0xFFFFFFFF00000000) | (value & 0xFFFFFFFF)
|
2021-09-03 06:33:25 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "ebx":
|
2021-09-03 06:33:55 +02:00
|
|
|
registers.rbx = (registers.rbx & 0xFFFFFFFF00000000) | (value & 0xFFFFFFFF)
|
2021-09-03 06:33:25 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "ecx":
|
2021-09-03 06:33:55 +02:00
|
|
|
registers.rcx = (registers.rcx & 0xFFFFFFFF00000000) | (value & 0xFFFFFFFF)
|
2021-09-03 06:33:25 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "edx":
|
2021-09-03 06:33:55 +02:00
|
|
|
registers.rdx = (registers.rdx & 0xFFFFFFFF00000000) | (value & 0xFFFFFFFF)
|
2021-09-03 06:33:25 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "esp":
|
2021-09-03 06:33:55 +02:00
|
|
|
registers.rsp = (registers.rsp & 0xFFFFFFFF00000000) | (value & 0xFFFFFFFF)
|
2021-09-03 06:33:25 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "ebp":
|
2021-09-03 06:33:55 +02:00
|
|
|
registers.rbp = (registers.rbp & 0xFFFFFFFF00000000) | (value & 0xFFFFFFFF)
|
2021-09-03 06:33:25 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "esi":
|
2021-09-03 06:33:55 +02:00
|
|
|
registers.rsi = (registers.rsi & 0xFFFFFFFF00000000) | (value & 0xFFFFFFFF)
|
2021-09-03 06:33:25 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "edi":
|
2021-09-03 06:33:55 +02:00
|
|
|
registers.rdi = (registers.rdi & 0xFFFFFFFF00000000) | (value & 0xFFFFFFFF)
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "ax":
|
|
|
|
registers.rax = (registers.rax & 0xFFFFFFFFFFFF0000) | (value & 0xFFFF)
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "bx":
|
|
|
|
registers.rbx = (registers.rbx & 0xFFFFFFFFFFFF0000) | (value & 0xFFFF)
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "cx":
|
|
|
|
registers.rcx = (registers.rcx & 0xFFFFFFFFFFFF0000) | (value & 0xFFFF)
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "dx":
|
|
|
|
registers.rdx = (registers.rdx & 0xFFFFFFFFFFFF0000) | (value & 0xFFFF)
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "sp":
|
|
|
|
registers.rsp = (registers.rsp & 0xFFFFFFFFFFFF0000) | (value & 0xFFFF)
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "bp":
|
|
|
|
registers.rbp = (registers.rbp & 0xFFFFFFFFFFFF0000) | (value & 0xFFFF)
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "si":
|
|
|
|
registers.rsi = (registers.rsi & 0xFFFFFFFFFFFF0000) | (value & 0xFFFF)
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "di":
|
|
|
|
registers.rdi = (registers.rdi & 0xFFFFFFFFFFFF0000) | (value & 0xFFFF)
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "ah":
|
|
|
|
registers.rax = (registers.rax & 0xFFFFFFFFFFFF00FF) | ((value & 0xFF) << 8)
|
2021-09-03 06:33:25 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "al":
|
2021-09-03 06:33:55 +02:00
|
|
|
registers.rax = (registers.rax & 0xFFFFFFFFFFFFFF00) | (value & 0xFF)
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "bh":
|
|
|
|
registers.rbx = (registers.rbx & 0xFFFFFFFFFFFF00FF) | ((value & 0xFF) << 8)
|
2021-09-03 06:33:25 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "bl":
|
2021-09-03 06:33:55 +02:00
|
|
|
registers.rbx = (registers.rbx & 0xFFFFFFFFFFFFFF00) | (value & 0xFF)
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "ch":
|
|
|
|
registers.rcx = (registers.rcx & 0xFFFFFFFFFFFF00FF) | ((value & 0xFF) << 8)
|
2021-09-03 06:33:25 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "cl":
|
2021-09-03 06:33:55 +02:00
|
|
|
registers.rcx = (registers.rcx & 0xFFFFFFFFFFFFFF00) | (value & 0xFF)
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "dh":
|
|
|
|
registers.rdx = (registers.rdx & 0xFFFFFFFFFFFF00FF) | ((value & 0xFF) << 8)
|
2021-09-03 06:33:25 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "dl":
|
2021-09-03 06:33:55 +02:00
|
|
|
registers.rdx = (registers.rdx & 0xFFFFFFFFFFFFFF00) | (value & 0xFF)
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "spl":
|
|
|
|
registers.rsp = (registers.rsp & 0xFFFFFFFFFFFFFF00) | (value & 0xFF)
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "bpl":
|
|
|
|
registers.rbp = (registers.rbp & 0xFFFFFFFFFFFFFF00) | (value & 0xFF)
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "sil":
|
|
|
|
registers.rsi = (registers.rsi & 0xFFFFFFFFFFFFFF00) | (value & 0xFF)
|
|
|
|
return
|
|
|
|
|
|
|
|
if loc == "dil":
|
|
|
|
registers.rdi = (registers.rdi & 0xFFFFFFFFFFFFFF00) | (value & 0xFF)
|
2021-09-03 06:33:25 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
raise Exception(f"Unsupported destination {loc} for assign!")
|
|
|
|
|
|
|
|
|
|
|
|
def fetch(registers: Registers, memory: Memory, size: int, loc: str) -> int:
|
2021-09-03 06:33:55 +02:00
|
|
|
"""
|
|
|
|
Given the registers and memory of our emulator, the size of the operation
|
|
|
|
performed and the location to fetch from, compute where the fetch should
|
|
|
|
happen and then execute it, returning the results of the fetch.
|
|
|
|
"""
|
|
|
|
|
2021-09-03 06:33:25 +02:00
|
|
|
address = get_address(registers, loc)
|
|
|
|
if address is not None:
|
|
|
|
if size == 1:
|
|
|
|
return struct.unpack("<B", memory.load(address, size))[0]
|
|
|
|
elif size == 2:
|
|
|
|
return struct.unpack("<H", memory.load(address, size))[0]
|
|
|
|
elif size == 4:
|
|
|
|
return struct.unpack("<I", memory.load(address, size))[0]
|
2021-09-03 07:07:15 +02:00
|
|
|
elif size == 8:
|
|
|
|
return struct.unpack("<Q", memory.load(address, size))[0]
|
2021-09-03 06:33:25 +02:00
|
|
|
else:
|
|
|
|
raise Exception(f"Unsupported size {size} for memory fetch!")
|
|
|
|
|
|
|
|
immediate = get_value(loc)
|
|
|
|
if immediate is not None:
|
|
|
|
if size == 1:
|
|
|
|
return immediate & 0xFF
|
|
|
|
if size == 2:
|
|
|
|
return immediate & 0xFFFF
|
|
|
|
if size == 4:
|
|
|
|
return immediate & 0xFFFFFFFF
|
2021-09-03 07:07:15 +02:00
|
|
|
if size == 8:
|
|
|
|
return immediate
|
2021-09-03 06:33:25 +02:00
|
|
|
raise Exception(f"Unsupported size {size} for immediate fetch!")
|
|
|
|
|
2021-09-03 06:33:55 +02:00
|
|
|
if loc == "rax":
|
|
|
|
return registers.rax
|
|
|
|
|
|
|
|
if loc == "rbx":
|
|
|
|
return registers.rbx
|
|
|
|
|
|
|
|
if loc == "rcx":
|
|
|
|
return registers.rcx
|
|
|
|
|
|
|
|
if loc == "rdx":
|
|
|
|
return registers.rdx
|
|
|
|
|
|
|
|
if loc == "rsi":
|
|
|
|
return registers.rsi
|
|
|
|
|
|
|
|
if loc == "rdi":
|
|
|
|
return registers.rdi
|
|
|
|
|
|
|
|
if loc == "rsp":
|
|
|
|
return registers.rsp
|
|
|
|
|
|
|
|
if loc == "rbp":
|
|
|
|
return registers.rbp
|
|
|
|
|
2021-09-03 06:33:25 +02:00
|
|
|
if loc == "eax":
|
2021-09-03 06:33:55 +02:00
|
|
|
return registers.rax & 0xFFFFFFFF
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
if loc == "ebx":
|
2021-09-03 06:33:55 +02:00
|
|
|
return registers.rbx & 0xFFFFFFFF
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
if loc == "ecx":
|
2021-09-03 06:33:55 +02:00
|
|
|
return registers.rcx & 0xFFFFFFFF
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
if loc == "edx":
|
2021-09-03 06:33:55 +02:00
|
|
|
return registers.rdx & 0xFFFFFFFF
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
if loc == "esi":
|
2021-09-03 06:33:55 +02:00
|
|
|
return registers.rsi & 0xFFFFFFFF
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
if loc == "edi":
|
2021-09-03 06:33:55 +02:00
|
|
|
return registers.rdi & 0xFFFFFFFF
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
if loc == "ebp":
|
2021-09-03 06:33:55 +02:00
|
|
|
return registers.rbp & 0xFFFFFFFF
|
|
|
|
|
|
|
|
if loc == "esp":
|
|
|
|
return registers.rsp & 0xFFFFFFFF
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
if loc == "ax":
|
2021-09-03 06:33:55 +02:00
|
|
|
return registers.rax & 0xFFFF
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
if loc == "bx":
|
2021-09-03 06:33:55 +02:00
|
|
|
return registers.rbx & 0xFFFF
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
if loc == "cx":
|
2021-09-03 06:33:55 +02:00
|
|
|
return registers.rcx & 0xFFFF
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
if loc == "dx":
|
2021-09-03 06:33:55 +02:00
|
|
|
return registers.rdx & 0xFFFF
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
if loc == "si":
|
2021-09-03 06:33:55 +02:00
|
|
|
return registers.rsi & 0xFFFF
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
if loc == "di":
|
2021-09-03 06:33:55 +02:00
|
|
|
return registers.rdi & 0xFFFF
|
|
|
|
|
|
|
|
if loc == "bp":
|
|
|
|
return registers.rbp & 0xFFFF
|
|
|
|
|
|
|
|
if loc == "sp":
|
|
|
|
return registers.rsp & 0xFFFF
|
|
|
|
|
|
|
|
if loc == "ah":
|
|
|
|
return (registers.rax & 0xFF00) >> 8
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
if loc == "al":
|
2021-09-03 06:33:55 +02:00
|
|
|
return registers.rax & 0xFF
|
|
|
|
|
|
|
|
if loc == "bh":
|
|
|
|
return (registers.rbx & 0xFF00) >> 8
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
if loc == "bl":
|
2021-09-03 06:33:55 +02:00
|
|
|
return registers.rbx & 0xFF
|
|
|
|
|
|
|
|
if loc == "ch":
|
|
|
|
return (registers.rcx & 0xFF00) >> 8
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
if loc == "cl":
|
2021-09-03 06:33:55 +02:00
|
|
|
return registers.rcx & 0xFF
|
|
|
|
|
|
|
|
if loc == "dh":
|
|
|
|
return (registers.rdx & 0xFF00) >> 8
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
if loc == "dl":
|
2021-09-03 06:33:55 +02:00
|
|
|
return registers.rdx & 0xFF
|
|
|
|
|
|
|
|
if loc == "spl":
|
|
|
|
return registers.rsp & 0xFF
|
|
|
|
|
|
|
|
if loc == "bpl":
|
|
|
|
return registers.rbp & 0xFF
|
|
|
|
|
|
|
|
if loc == "sil":
|
|
|
|
return registers.rsi & 0xFF
|
|
|
|
|
|
|
|
if loc == "dil":
|
|
|
|
return registers.rdi & 0xFF
|
2021-09-03 06:33:25 +02:00
|
|
|
|
|
|
|
raise Exception(f"Unsupported source {loc} for fetch!")
|