mirror of
https://github.com/elseif/MikroTikPatch
synced 2026-07-17 18:49:41 +00:00
modified: patch.py
This commit is contained in:
parent
0a270a1108
commit
ed3dcd0ab1
113
patch.py
113
patch.py
@ -321,46 +321,79 @@ def patch_loader(loader_file):
|
|||||||
print("loader module import failed. cannot run patch_loader.py")
|
print("loader module import failed. cannot run patch_loader.py")
|
||||||
|
|
||||||
def patch_squashfs(path,key_dict):
|
def patch_squashfs(path,key_dict):
|
||||||
for root, dirs, files in os.walk(path):
|
url_replacements = {
|
||||||
for _file in files:
|
os.environ.get('MIKRO_LICENCE_URL', '').encode(): os.environ.get('CUSTOM_LICENCE_URL', '').encode(),
|
||||||
file = os.path.join(root,_file)
|
os.environ.get('MIKRO_UPGRADE_URL', '').encode(): os.environ.get('CUSTOM_UPGRADE_URL', '').encode(),
|
||||||
if os.path.isfile(file):
|
os.environ.get('MIKRO_CLOUD_URL', '').encode(): os.environ.get('CUSTOM_CLOUD_URL', '').encode(),
|
||||||
if _file =='loader':
|
os.environ.get('MIKRO_CLOUD_PUBLIC_KEY', '').encode(): os.environ.get('CUSTOM_CLOUD_PUBLIC_KEY', '').encode(),
|
||||||
patch_loader(file)
|
}
|
||||||
continue
|
url_replacements = {k: v for k, v in url_replacements.items() if k and v}
|
||||||
if _file =='BOOTX64.EFI':
|
renew_replacements = {
|
||||||
print(f'patch {file} ...')
|
os.environ.get('MIKRO_RENEW_URL', '').encode(): os.environ.get('CUSTOM_RENEW_URL', '').encode(),
|
||||||
data = open(file,'rb').read()
|
}
|
||||||
data = patch_kernel(data,key_dict)
|
renew_replacements = {k: v for k, v in renew_replacements.items() if k and v}
|
||||||
open(file,'wb').write(data)
|
|
||||||
continue
|
|
||||||
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,file)
|
|
||||||
if _data != data:
|
|
||||||
open(file,'wb').write(_data)
|
|
||||||
url_dict = {
|
|
||||||
os.environ['MIKRO_LICENCE_URL'].encode():os.environ['CUSTOM_LICENCE_URL'].encode(),
|
|
||||||
os.environ['MIKRO_UPGRADE_URL'].encode():os.environ['CUSTOM_UPGRADE_URL'].encode(),
|
|
||||||
os.environ['MIKRO_CLOUD_URL'].encode():os.environ['CUSTOM_CLOUD_URL'].encode(),
|
|
||||||
os.environ['MIKRO_CLOUD_PUBLIC_KEY'].encode():os.environ['CUSTOM_CLOUD_PUBLIC_KEY'].encode(),
|
|
||||||
}
|
|
||||||
data = open(file,'rb').read()
|
|
||||||
for old_url,new_url in url_dict.items():
|
|
||||||
if old_url in data:
|
|
||||||
print(f'{file} url patched {old_url.decode()[:7]}...')
|
|
||||||
data = data.replace(old_url,new_url)
|
|
||||||
open(file,'wb').write(data)
|
|
||||||
|
|
||||||
if os.path.split(file)[1] == 'licupgr':
|
for root, dirs, files in os.walk(path):
|
||||||
url_dict = {
|
if 'mode' in files and 'keyman' in files:
|
||||||
os.environ['MIKRO_RENEW_URL'].encode():os.environ['CUSTOM_RENEW_URL'].encode(),
|
for file_path in [os.path.join(root, 'mode'),os.path.join(root, 'keyman')]:
|
||||||
}
|
with open(file_path, 'rb') as f:
|
||||||
for old_url,new_url in url_dict.items():
|
data = f.read()
|
||||||
if old_url in data:
|
modified = False
|
||||||
print(f'{file} url patched {old_url.decode()[:7]}...')
|
for old_public_key,new_public_key in key_dict.items():
|
||||||
data = data.replace(old_url,new_url)
|
new_data = replace_key(old_public_key,new_public_key,data,file_path)
|
||||||
open(file,'wb').write(data)
|
if new_data != data:
|
||||||
|
data = new_data
|
||||||
|
modified = True
|
||||||
|
assert modified, f'{file_path} key not patched'
|
||||||
|
with open(f'{file_path}_', 'wb') as f:
|
||||||
|
f.write(data)
|
||||||
|
if 'loader' in files and os.path.isfile(os.path.join(root, 'loader')):
|
||||||
|
loader_file = os.path.join(root, 'loader')
|
||||||
|
patch_loader(loader_file)
|
||||||
|
|
||||||
|
if 'BOOTX64.EFI' in files and os.path.isfile(os.path.join(root, 'BOOTX64.EFI')):
|
||||||
|
efi_file = os.path.join(root, 'BOOTX64.EFI')
|
||||||
|
with open(efi_file, 'rb') as f:
|
||||||
|
data = f.read()
|
||||||
|
new_data = patch_kernel(data,key_dict)
|
||||||
|
assert new_data != data, f'{file_path} key not patched'
|
||||||
|
with open(efi_file, 'wb') as f:
|
||||||
|
f.write(new_data)
|
||||||
|
|
||||||
|
|
||||||
|
for filename in files:
|
||||||
|
if filename in ['mode','keyman','loader','BOOTX64.EFI']:
|
||||||
|
continue
|
||||||
|
file_path = os.path.join(root,filename)
|
||||||
|
if not os.path.isfile(file_path):
|
||||||
|
continue
|
||||||
|
|
||||||
|
modified = False
|
||||||
|
with open(file_path, 'rb') as f:
|
||||||
|
data = f.read()
|
||||||
|
|
||||||
|
for old_public_key,new_public_key in key_dict.items():
|
||||||
|
new_data = replace_key(old_public_key,new_public_key,data,file_path)
|
||||||
|
if new_data != data:
|
||||||
|
data = new_data
|
||||||
|
modified = True
|
||||||
|
|
||||||
|
for old_url,new_url in url_replacements.items():
|
||||||
|
if old_url in data:
|
||||||
|
print(f'{file_path} url patched {old_url.decode()[:7]}...')
|
||||||
|
data = data.replace(old_url,new_url)
|
||||||
|
modified = True
|
||||||
|
|
||||||
|
if filename == 'licupgr':
|
||||||
|
for old_url,new_url in renew_replacements.items():
|
||||||
|
if old_url in data:
|
||||||
|
print(f'{file_path} url patched {old_url.decode()[:7]}...')
|
||||||
|
data = data.replace(old_url,new_url)
|
||||||
|
modified = True
|
||||||
|
|
||||||
|
if modified:
|
||||||
|
with open(file_path, 'wb') as f:
|
||||||
|
f.write(data)
|
||||||
|
|
||||||
def run_shell_command(command):
|
def run_shell_command(command):
|
||||||
process = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
process = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
@ -379,7 +412,7 @@ def patch_npk_package(package,key_dict):
|
|||||||
open(squashfs_file,'wb').write(package[NpkPartID.SQUASHFS].data)
|
open(squashfs_file,'wb').write(package[NpkPartID.SQUASHFS].data)
|
||||||
print(f"extract {squashfs_file} ...")
|
print(f"extract {squashfs_file} ...")
|
||||||
run_shell_command(f"unsquashfs -d {extract_dir} {squashfs_file}")
|
run_shell_command(f"unsquashfs -d {extract_dir} {squashfs_file}")
|
||||||
patch_squashfs(extract_dir,key_dict)
|
patch_squashfs(os.path.join(extract_dir,'nova','bin'),key_dict)
|
||||||
logo = os.path.join(extract_dir,"nova/lib/console/logo.txt")
|
logo = os.path.join(extract_dir,"nova/lib/console/logo.txt")
|
||||||
run_shell_command(f"sudo sed -i '1d' {logo}")
|
run_shell_command(f"sudo sed -i '1d' {logo}")
|
||||||
run_shell_command(f"sudo sed -i '8s#.*# elseif@live.cn https://github.com/elseif/MikroTikPatch#' {logo}")
|
run_shell_command(f"sudo sed -i '8s#.*# elseif@live.cn https://github.com/elseif/MikroTikPatch#' {logo}")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user