Skip to content

Commit 751a970

Browse files
committed
update sha1 before updating checksum
1 parent a6e717e commit 751a970

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

DexRepair.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,13 @@ def repair_dex_magic(dex_data: bytearray):
7272
return dex_data
7373

7474

75-
def update_dex_hashes(dex_data: bytearray):
75+
def update_dex_hashes(dex_data: bytearray, repair_sha1: bool = False):
7676
"""
7777
This function updates the checksum and signature in the DEX header of a given bytearray containing dex data.
7878
7979
Parameters:
8080
dex_data (bytearray): The bytearray containing the dex data.
81+
repair_sha1 (bool): If True, the SHA-1 signature is updated. If False, the SHA-1 signature is not updated.
8182
8283
Returns:
8384
bytearray: The bytearray containing the updated dex data.
@@ -88,25 +89,29 @@ def update_dex_hashes(dex_data: bytearray):
8889
The updated checksum is then packed into a 4-byte little-endian integer and written back into the dex data, starting from the 9th byte.
8990
The updated signature is then written back into the dex data, starting from the 13th byte.
9091
"""
92+
if repair_sha1:
93+
signature = hashlib.sha1(dex_data[32:]).digest()
94+
print(f"Signature: {signature.hex()}")
95+
dex_data[12:32] = signature
96+
9197
checksum = zlib.adler32(dex_data[12:])
92-
signature = hashlib.sha1(dex_data[32:]).digest()
9398
print(f"Checksum: {checksum:#x}")
94-
print(f"Signature: {signature.hex()}")
95-
9699
dex_data[8:12] = struct.pack("<I", checksum)
97-
dex_data[12:32] = signature
100+
98101
return dex_data
99102

100103

101104
def repair_dex(
102105
dex_path: str,
106+
repair_sha1: bool = False,
103107
output_dex_path: str = None,
104108
):
105109
"""
106110
This function repairs dex files in the given path. If the path is a directory, it will repair all dex files within that directory. If the path is a file, it will repair that specific dex file.
107111
108112
Parameters:
109113
dex_path (str): The path to the dex file or directory containing dex files.
114+
repair_sha1 (bool, optional): If True, the SHA-1 signature is updated. If False, the SHA-1 signature is not updated. Default is False.
110115
output_dex_path (str, optional): The output path for the repaired dex files. If not provided, the repaired dex files will be overwritten in the original location.
111116
112117
Returns:
@@ -125,19 +130,22 @@ def repair_dex(
125130
if not os.path.isdir(output_dex_path):
126131
raise DexRepairError(f"{output_dex_path} not a directory!")
127132
print(f"Repairing {file_path}...")
128-
repair_dex_file(file_path, output_file_path)
133+
repair_dex_file(file_path, output_file_path, repair_sha1)
129134
elif os.path.isfile(dex_path):
130135
repair_dex_file(dex_path, output_dex_path)
131136
else:
132137
raise DexRepairError(f"Path not found: {dex_path}")
133138

134139

135-
def repair_dex_file(dex_file_path: str, output_dex_path: str = None):
140+
def repair_dex_file(
141+
dex_file_path: str, repair_sha1: bool = False, output_dex_path: str = None
142+
):
136143
"""
137144
This function repairs a single dex file by fixing the DEX magic number and updating the checksum and signature in the DEX header. The repaired dex file is then written to the output path if it is provided, or to the original path if it is not.
138145
139146
Parameters:
140147
dex_file_path (str): The path to the dex file to be repaired.
148+
repair_sha1 (bool, optional): If True, the SHA-1 signature is updated. If False, the SHA-1 signature is not updated. Default is False.
141149
output_dex_path (str, optional): The output path for the repaired dex file. If not provided, the repaired dex file will be overwritten in the original location.
142150
143151
Returns:
@@ -153,7 +161,7 @@ def repair_dex_file(dex_file_path: str, output_dex_path: str = None):
153161
raise DexRepairError(f"DEX file not found: {dex_file_path}")
154162

155163
dex_data = repair_dex_magic(dex_data)
156-
dex_data = update_dex_hashes(dex_data)
164+
dex_data = update_dex_hashes(dex_data, repair_sha1)
157165

158166
if output_dex_path:
159167
with open(output_dex_path, "wb") as f:
@@ -168,6 +176,9 @@ def main():
168176
parser = argparse.ArgumentParser(description="DEX Repair Tool", epilog=epilog)
169177
parser.add_argument("dex_file", help="Path to the DEX file")
170178
parser.add_argument("-o", "--output", help="Path to the output DEX file (optional)")
179+
parser.add_argument(
180+
"-s", "--sha", action="store_true", help="Repair SHA1 hash (optional)"
181+
)
171182

172183
args = parser.parse_args()
173184

@@ -177,7 +188,7 @@ def main():
177188
output = args.dex_file.replace(".dex", "_repaired.dex")
178189

179190
try:
180-
repair_dex(args.dex_file, output)
191+
repair_dex(args.dex_file, output, args.sha)
181192
print("DEX repair completed successfully.")
182193

183194
except DexRepairError as e:

0 commit comments

Comments
 (0)