Skip to content

Commit

Permalink
Compatibility with unicorn 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sledgeh4w committed Sep 21, 2024
1 parent 148c6df commit 48fc0ce
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
7 changes: 3 additions & 4 deletions src/chomper/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
arm64_const,
arm_const,
)
from unicorn.unicorn import UC_HOOK_CODE_TYPE

from . import const
from .arch import arm_arch, arm64_arch
Expand Down Expand Up @@ -81,7 +80,7 @@ def __init__(

self.modules: List[Module] = []

self.hooks: Dict[str, UC_HOOK_CODE_TYPE] = {}
self.hooks: Dict[str, Callable] = {}
self.syscall_handlers: Dict[int, Callable] = {}

self.memory_manager = MemoryManager(
Expand Down Expand Up @@ -283,7 +282,7 @@ def backtrace(self) -> List[Tuple[int, Optional[Module]]]:
def add_hook(
self,
symbol_or_addr: Union[int, str],
callback: UC_HOOK_CODE_TYPE,
callback: Callable,
user_data: Optional[dict] = None,
) -> int:
"""Add hook to the emulator.
Expand Down Expand Up @@ -322,7 +321,7 @@ def add_hook(
def add_interceptor(
self,
symbol_or_addr: Union[int, str],
callback: UC_HOOK_CODE_TYPE,
callback: Callable,
user_data: Optional[dict] = None,
):
"""Add interceptor to the emulator."""
Expand Down
33 changes: 21 additions & 12 deletions src/chomper/instruction.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re

from unicorn.unicorn import arm64_const
from unicorn import arm64_const


class AutomicInstruction:
Expand Down Expand Up @@ -44,34 +44,43 @@ def __init__(self, emu, code: bytes):
else:
self._op_bits = 64

def read_reg(self, reg_id: int) -> int:
if reg_id in (arm64_const.UC_ARM64_REG_WZR, arm64_const.UC_ARM64_REG_XZR):
return 0

return self.emu.uc.reg_read(reg_id)

def write_reg(self, reg_id: int, value: int):
self.emu.uc.reg_write(reg_id, value)

def execute(self):
address = self.emu.uc.reg_read(self._regs[-1])
address = self.read_reg(self._regs[-1])
value = self.emu.read_int(address, self._op_bits // 8)

result = None

if self._inst[2].startswith("ldxr"):
self.emu.uc.reg_write(self._regs[0], value)
self.write_reg(self._regs[0], value)

elif self._inst[2].startswith("ldadd"):
self.emu.uc.reg_write(self._regs[1], value)
result = value + self.emu.uc.reg_read(self._regs[0])
self.write_reg(self._regs[1], value)
result = value + self.read_reg(self._regs[0])

elif self._inst[2].startswith("ldset"):
self.emu.uc.reg_write(self._regs[1], value)
result = value | self.emu.uc.reg_read(self._regs[0])
self.write_reg(self._regs[1], value)
result = value | self.read_reg(self._regs[0])

elif self._inst[2].startswith("swp"):
self.emu.uc.reg_write(self._regs[1], value)
result = self.emu.uc.reg_read(self._regs[0])
self.write_reg(self._regs[1], value)
result = self.read_reg(self._regs[0])

elif self._inst[2].startswith("cas"):
n = self.emu.uc.reg_read(self._regs[0])
n = self.read_reg(self._regs[0])

self.emu.uc.reg_write(self._regs[0], value)
self.write_reg(self._regs[0], value)

if n == value:
result = self.emu.uc.reg_read(self._regs[1])
result = self.read_reg(self._regs[1])

if result is not None:
result %= 2**self._op_bits
Expand Down
8 changes: 3 additions & 5 deletions src/chomper/os/android/hooks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import os
import random
from functools import wraps
from typing import Dict
from typing import Callable, Dict

from unicorn.unicorn import UC_HOOK_CODE_TYPE
hooks: Dict[str, Callable] = {}

hooks: Dict[str, UC_HOOK_CODE_TYPE] = {}


def get_hooks() -> Dict[str, UC_HOOK_CODE_TYPE]:
def get_hooks() -> Dict[str, Callable]:
"""Returns a dictionary of default hooks."""
return hooks.copy()

Expand Down
8 changes: 3 additions & 5 deletions src/chomper/os/ios/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
import time
import uuid
from functools import wraps
from typing import Dict

from unicorn.unicorn import UC_HOOK_CODE_TYPE
from typing import Callable, Dict

from chomper.exceptions import SymbolMissingException
from chomper.objc import ObjC
from chomper.utils import pyobj2cfobj

hooks: Dict[str, UC_HOOK_CODE_TYPE] = {}
hooks: Dict[str, Callable] = {}


def get_hooks() -> Dict[str, UC_HOOK_CODE_TYPE]:
def get_hooks() -> Dict[str, Callable]:
"""Returns a dictionary of default hooks."""
return hooks.copy()

Expand Down

0 comments on commit 48fc0ce

Please sign in to comment.