Skip to content

Instantly share code, notes, and snippets.

@kynex7510
Created January 18, 2025 16:00
Show Gist options
  • Save kynex7510/4d950b0e7b69996825d1c7d99c20ea1c to your computer and use it in GitHub Desktop.
Save kynex7510/4d950b0e7b69996825d1c7d99c20ea1c to your computer and use it in GitHub Desktop.
EXEFS_SECTIONS = [
".text",
".ro",
".rw"
]
def align_block(v: int) -> int:
return v + 15 & ~(15)
def make_name(data: bytes) -> str:
index = 0
for b in data:
if b == 0:
break
index += 1
return data.decode()[:index]
def extract_code(app_path: str):
input = open(app_path, "rb")
input.seek(0x1A0, 0)
exefs_offset = int.from_bytes(input.read(4), byteorder='little')
input.seek(exefs_offset, 0)
exefs_header = input.read(0x30)
code_size = 0
for i in range(len(EXEFS_SECTIONS)):
index = i * 0x10
name = make_name(exefs_header[index:index + 0x08])
if name != EXEFS_SECTIONS[i]:
raise Exception(f"Expected {EXEFS_SECTIONS[i]}, found {name}")
index += 0x0C
size = int.from_bytes(exefs_header[index:index + 0x04], byteorder='little')
code_size += align_block(size)
input.seek(0xC00, 0)
with open("code.bin", "wb") as out:
out.write(input.read(code_size))
input.close()
extract_code("HID3.app")
print("Done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment