mtkclient/Tools/patch_legacy.py

91 lines
2.6 KiB
Python
Raw Normal View History

2023-12-14 08:41:42 -05:00
#!/usr/bin/env python3
import sys
import hashlib
2023-12-29 16:12:09 -05:00
from mtkclient.Library.utils import find_binary
patches = [
("B3F5807F01D1", "B3F5807F01D14FF000004FF000007047"), # rsa_verify / usbdl_vfy_da
("B3F5807F04BF4FF4807305F011B84FF0FF307047", "B3F5807F04BF4FF480734FF000004FF000007047"),
# rsa_verify / usbdl_vfy_da
("2DE9F746802B", "4FF000007047"), # rsa_verify / usbdl_vfy_da
("802B2DE9", "4FF000007047"),
("8023BDE8", "4FF000007047"), # DA verify fail
("800053E3F344", "0000A0E31EFF2FE1")
2023-12-14 08:41:42 -05:00
]
2023-12-29 16:12:09 -05:00
2023-12-14 08:41:42 -05:00
def patch_preloader_security(data):
2023-12-29 16:12:09 -05:00
if data[:4] != b"\x4D\x4D\x4D\x01":
2023-12-14 08:41:42 -05:00
return data
2023-12-29 16:12:09 -05:00
patched = False
2023-12-14 08:41:42 -05:00
for patchval in patches:
2023-12-29 16:12:09 -05:00
pattern = bytes.fromhex(patchval[0])
2023-12-14 08:41:42 -05:00
idx = data.find(pattern)
2023-12-29 16:12:09 -05:00
if idx != -1:
patch = bytes.fromhex(patchval[1])
data[idx:idx + len(patch)] = patch
patched = True
2023-12-14 08:41:42 -05:00
break
if patched:
2023-12-29 16:12:09 -05:00
# with open(sys.argv[1]+".patched","wb") as wf:
2023-12-14 08:41:42 -05:00
# wf.write(data)
# print("Patched !")
2023-12-29 16:12:09 -05:00
print("Patched preloader security")
2023-12-14 08:41:42 -05:00
else:
print(f"Failed to patch preloader security: {sys.argv[1]}")
return data
def patch_da2_legacy(da2):
# open("da2.bin","wb").write(da2)
da2patched = bytearray(da2)
# Patch security
check_addr = find_binary(da2, b"\x08\xB5\x4F\xF4\x50\x42\xA0\xF1\x81\x53")
if check_addr is not None:
2023-12-29 16:12:09 -05:00
da2patched[check_addr:check_addr + 4] = b"\x08\xB5\x08\xBD"
print("Patched preloader security")
2023-12-14 08:41:42 -05:00
else:
print(f"Failed to patch preloader security: {sys.argv[1]}")
return da2patched
2023-12-29 16:12:09 -05:00
2023-12-14 08:41:42 -05:00
def fix_hash(da1, da2, hashpos, hashmode):
da1 = bytearray(da1)
dahash = None
if hashmode == 1:
dahash = hashlib.sha1(da2).digest()
elif hashmode == 2:
dahash = hashlib.sha256(da2).digest()
da1[hashpos:hashpos + len(dahash)] = dahash
return da1
2023-12-29 16:12:09 -05:00
2023-12-14 08:41:42 -05:00
def compute_hash_pos(da1, da2):
hashdigest = hashlib.sha1(da2).digest()
hashdigest256 = hashlib.sha256(da2).digest()
idx = da1.find(hashdigest)
hashmode = 1
if idx == -1:
idx = da1.find(hashdigest256)
hashmode = 2
if idx != -1:
return idx, hashmode
return None, None
2023-12-29 16:12:09 -05:00
2023-12-14 08:41:42 -05:00
def main():
"""
with open(sys.argv[1],"rb") as rf:
data=bytearray(rf.read())
data=patch_preloader_security(data)
"""
2023-12-29 16:12:09 -05:00
da1 = open("loaders/6735_200000MTK_AllInOne_DA_5.2136.bin", "rb").read()
da2 = open("loaders/6735_40000000MTK_AllInOne_DA_5.2136.bin", "rb").read()
hp, hm = compute_hash_pos(da1, da2[:-0x100])
da2 = patch_da2_legacy(da2)
fix_hash(da1, da2, hp, hm)
2023-12-14 08:41:42 -05:00
2023-12-29 16:12:09 -05:00
if __name__ == "__main__":
main()