Skip to content

Commit

Permalink
We actually need to fully reload the image or things break
Browse files Browse the repository at this point in the history
  • Loading branch information
KritantaDev authored and KritantaDev committed Dec 5, 2021
1 parent 8f95b00 commit 0642853
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
14 changes: 7 additions & 7 deletions bin/ktool
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ from ktool import (
ignore,
log,
LogLevel,
Table
Table, Image
)

from ktool.exceptions import *
Expand Down Expand Up @@ -382,9 +382,9 @@ Global Flags:
print(help_prompt.__doc__)


def verify_patches(image):
def process_patches(image) -> 'Image':
try:
ktool.macho_verify(image)
return ktool.reload_image(image)
except MalformedMachOException:
exit_with_error(KToolError.ProcessingError, "Reloading MachO after patch failed. This is an issue with "
"my patch code. Please file an issue on https://github.com/kritantadev/ktool.")
Expand Down Expand Up @@ -553,7 +553,7 @@ def insert(args):
image.insert_lc_with_suf(lc, [dylib_item.raw], args.payload, last_dylib_command_index)

log.info("Reloading MachO Slice to verify integrity")
verify_patches(macho_slice)
image = process_patches(image)
patched_libraries.append(image)

with open(args.out, 'wb') as fd:
Expand Down Expand Up @@ -592,7 +592,7 @@ def edit(args):
for macho_slice in macho_file.slices:
image = ktool.load_image(macho_slice)
id_dylib_index = -1

for i, cmd in enumerate(image.macho_header.load_commands):
if cmd.cmd == 0xD:
id_dylib_index = i
Expand All @@ -601,11 +601,11 @@ def edit(args):
dylib_item = Struct.create_with_values(dylib, [0x18, 2, 0, 0])
image.rm_load_command(id_dylib_index)

verify_patches(macho_slice)
image = process_patches(image)

image.insert_lc_with_suf(LOAD_COMMAND.ID_DYLIB, [dylib_item.raw], new_iname, id_dylib_index)

verify_patches(macho_slice)
image = process_patches(image)

patched_libraries.append(image)

Expand Down
2 changes: 1 addition & 1 deletion src/ktool/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .ktool import load_image, load_objc_metadata, generate_headers, generate_text_based_stub, load_macho_file, macho_verify
from .ktool import load_image, load_objc_metadata, generate_headers, generate_text_based_stub, load_macho_file, macho_verify, reload_image

from .objc import ObjCImage
from .dyld import Dyld, Image
Expand Down
12 changes: 12 additions & 0 deletions src/ktool/ktool.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ def load_macho_file(fp: BinaryIO, use_mmaped_io=True) -> MachOFile:
return MachOFile(fp, use_mmaped_io=use_mmaped_io)


def reload_image(image: Image) -> Image:
"""
Reload an image (properly updates internal representations after patches)
:param image:
:return:
"""
# This is going to be horribly slow. Dyld class needs refactored to have a better way to do this or ideally just
# not fuck things up and require a reload every time we make a patch.
return load_image(image.slice)


def load_image(fp: Union[BinaryIO, MachOFile, Slice], slice_index=0, load_symtab=True, load_imports=True,
load_exports=True, use_mmaped_io=True) -> Image:
"""
Expand Down

0 comments on commit 0642853

Please sign in to comment.