Created
January 25, 2021 08:31
-
-
Save syang-ng/e62621db313fafa6b966db24a8413a86 to your computer and use it in GitHub Desktop.
repair dex by fart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import os | |
import re | |
import sys | |
def parse_ins_bin(ins_path): | |
with open(ins_path, 'r') as f: | |
content = f.read() | |
ins_array=re.findall(r"{name:(.*?),method_idx:(.*?),offset:(.*?),code_item_len:(.*?),ins:(.*?)}", content) #(.*?)最短匹配 | |
code_items = [] | |
for ins in ins_array: | |
offset=(int)(ins[2]) | |
code_item = base64.b64decode(ins[4]) | |
code_items.append((offset, code_item)) | |
return code_items | |
def parse_all_ins_bin(ins_dir, keyword): | |
ins_bins = os.listdir(ins_dir) | |
ins_bins = [i for i in ins_bins if '.bin' in i and keyword in i] | |
code_items = [] | |
for ins_bin in ins_bins: | |
code_items += parse_ins_bin('{}/{}'.format(ins_dir, ins_bin)) | |
return code_items | |
if __name__ == "__main__": | |
dex_path = sys.argv[1] | |
ins_dir = sys.argv[2] | |
keyword = dex_path.split('/')[-1].split('_')[0] | |
with open(dex_path, 'rb') as f: | |
dex = bytearray(f.read()) | |
code_items = parse_all_ins_bin(ins_dir, keyword) | |
for offset, code_item in code_items: | |
size = len(code_item) | |
dex[offset:offset+size] = code_item | |
output_path = dex_path.replace('.dex', '_repair.dex') | |
with open(output_path, 'wb') as f: | |
f.write(dex) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment