mirror of
https://huihui.cat/mirrors/MikroTikPatch.git
synced 2025-12-10 03:24:37 +01:00
Update patch.py
Signed-off-by: elseif <elseif@live.cn>
This commit is contained in:
parent
f1ed27cb52
commit
1c9207d498
1 changed files with 6 additions and 5 deletions
11
patch.py
11
patch.py
|
|
@ -2,7 +2,7 @@ import subprocess,lzma
|
|||
import struct,os,re
|
||||
from npk import NovaPackage,NpkPartID,NpkFileContainer
|
||||
|
||||
def replace_key(old,new,data):
|
||||
def replace_key(old,new,data,name=''):
|
||||
old_chunks = [old[i:i+4] for i in range(0, len(old), 4)]
|
||||
new_chunks = [new[i:i+4] for i in range(0, len(new), 4)]
|
||||
pattern_parts = [re.escape(chunk) + b'(.{0,6})' for chunk in old_chunks[:-1]]
|
||||
|
|
@ -10,7 +10,8 @@ def replace_key(old,new,data):
|
|||
pattern_bytes = b''.join(pattern_parts)
|
||||
pattern = re.compile(pattern_bytes, flags=re.DOTALL)
|
||||
def replace_match(match):
|
||||
print(f'public key patched {old[:16].hex().upper()}...')
|
||||
print(f'{name} found public key {match.group()[:30].hex().upper()}')
|
||||
print(f'{name} public key patched {old[:16].hex().upper()}...')
|
||||
replaced = b''.join([new_chunks[i] + match.group(i+1) for i in range(len(new_chunks) - 1)])
|
||||
replaced += new_chunks[-1]
|
||||
return replaced
|
||||
|
|
@ -37,7 +38,7 @@ def patch_bzimage(data:bytes,key_dict:dict):
|
|||
initramfs = initramfs[:cpio_offset2]
|
||||
new_initramfs = initramfs
|
||||
for old_public_key,new_public_key in key_dict.items():
|
||||
new_initramfs = replace_key(old_public_key,new_public_key,new_initramfs)
|
||||
new_initramfs = replace_key(old_public_key,new_public_key,new_initramfs,'initramfs')
|
||||
new_vmlinux = vmlinux.replace(initramfs,new_initramfs)
|
||||
new_vmlinux_xz = lzma.compress(new_vmlinux,check=lzma.CHECK_CRC32,filters=[
|
||||
{"id": lzma.FILTER_X86},
|
||||
|
|
@ -96,7 +97,7 @@ def patch_initrd_xz(initrd_xz:bytes,key_dict:dict,ljust=True):
|
|||
initrd = lzma.decompress(initrd_xz)
|
||||
new_initrd = initrd
|
||||
for old_public_key,new_public_key in key_dict.items():
|
||||
new_initrd = replace_key(old_public_key,new_public_key,new_initrd)
|
||||
new_initrd = replace_key(old_public_key,new_public_key,new_initrd,'initrd')
|
||||
preset = 6
|
||||
new_initrd_xz = lzma.compress(new_initrd,check=lzma.CHECK_CRC32,filters=[{"id": lzma.FILTER_LZMA2, "preset": preset }] )
|
||||
while len(new_initrd_xz) > len(initrd_xz) and preset < 9:
|
||||
|
|
@ -269,7 +270,7 @@ def patch_squashfs(path,key_dict):
|
|||
if os.path.isfile(file):
|
||||
data = open(file,'rb').read()
|
||||
for old_public_key,new_public_key in key_dict.items():
|
||||
_data = replace_key(old_public_key,new_public_key,data)
|
||||
_data = replace_key(old_public_key,new_public_key,data,file)
|
||||
if _data != data:
|
||||
open(file,'wb').write(_data)
|
||||
url_dict = {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue