Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apk: Support replacing existing debuggable attribute #166

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions frida_tools/apk.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,25 @@ def insert_debuggable(self, name: int, resource_map: ResourceMap) -> None:

# Some parts of Android expect this to be sorted by resource ID.
attr_offset = None
replace = False
for insert_pos in range(self.attribute_count + 1):
attr_offset = 0x24 + 20 * insert_pos
attr_offset = 0x24 + insert_pos * struct.calcsize(self.ATTRIBUTE_FORMAT)
idx = int.from_bytes(chunk_data[attr_offset + 4 : attr_offset + 8], "little")
if resource_map.get_resource(idx) > ResourceMap.DEBUGGING_RESOURCE:
res = resource_map.get_resource(idx)
if res >= ResourceMap.DEBUGGING_RESOURCE:
replace = res == ResourceMap.DEBUGGING_RESOURCE
break
chunk_data[attr_offset:attr_offset] = debuggable

self.header.size = len(chunk_data)
chunk_data[4 : 4 + 4] = struct.pack("<I", self.header.size)
if replace:
chunk_data[attr_offset : attr_offset + struct.calcsize(self.ATTRIBUTE_FORMAT)] = debuggable
else:
chunk_data[attr_offset:attr_offset] = debuggable

self.header.size = len(chunk_data)
chunk_data[4 : 4 + 4] = struct.pack("<I", self.header.size)

self.attribute_count += 1
chunk_data[28 : 28 + 2] = struct.pack("<H", self.attribute_count)
self.attribute_count += 1
chunk_data[28 : 28 + 2] = struct.pack("<H", self.attribute_count)

self.header.chunk_data = bytes(chunk_data)

Expand Down
Loading