mirror of
https://github.com/elseif/MikroTikPatch
synced 2026-07-17 10:39:40 +00:00
Update patch.py
Signed-off-by: elseif <elseif@live.cn>
This commit is contained in:
parent
f1ed27cb52
commit
1c9207d498
11
patch.py
11
patch.py
@ -2,7 +2,7 @@ import subprocess,lzma
|
|||||||
import struct,os,re
|
import struct,os,re
|
||||||
from npk import NovaPackage,NpkPartID,NpkFileContainer
|
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)]
|
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)]
|
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]]
|
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_bytes = b''.join(pattern_parts)
|
||||||
pattern = re.compile(pattern_bytes, flags=re.DOTALL)
|
pattern = re.compile(pattern_bytes, flags=re.DOTALL)
|
||||||
def replace_match(match):
|
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 = b''.join([new_chunks[i] + match.group(i+1) for i in range(len(new_chunks) - 1)])
|
||||||
replaced += new_chunks[-1]
|
replaced += new_chunks[-1]
|
||||||
return replaced
|
return replaced
|
||||||
@ -37,7 +38,7 @@ def patch_bzimage(data:bytes,key_dict:dict):
|
|||||||
initramfs = initramfs[:cpio_offset2]
|
initramfs = initramfs[:cpio_offset2]
|
||||||
new_initramfs = initramfs
|
new_initramfs = initramfs
|
||||||
for old_public_key,new_public_key in key_dict.items():
|
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 = vmlinux.replace(initramfs,new_initramfs)
|
||||||
new_vmlinux_xz = lzma.compress(new_vmlinux,check=lzma.CHECK_CRC32,filters=[
|
new_vmlinux_xz = lzma.compress(new_vmlinux,check=lzma.CHECK_CRC32,filters=[
|
||||||
{"id": lzma.FILTER_X86},
|
{"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)
|
initrd = lzma.decompress(initrd_xz)
|
||||||
new_initrd = initrd
|
new_initrd = initrd
|
||||||
for old_public_key,new_public_key in key_dict.items():
|
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
|
preset = 6
|
||||||
new_initrd_xz = lzma.compress(new_initrd,check=lzma.CHECK_CRC32,filters=[{"id": lzma.FILTER_LZMA2, "preset": preset }] )
|
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:
|
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):
|
if os.path.isfile(file):
|
||||||
data = open(file,'rb').read()
|
data = open(file,'rb').read()
|
||||||
for old_public_key,new_public_key in key_dict.items():
|
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:
|
if _data != data:
|
||||||
open(file,'wb').write(_data)
|
open(file,'wb').write(_data)
|
||||||
url_dict = {
|
url_dict = {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user