diff --git a/.gitignore b/.gitignore index 1c5005e..e2f2801 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ __pycache__/ +.vscode/ venv/ app/ test_*.py \ No newline at end of file diff --git a/npk.py b/npk.py index abf790b..62532d1 100644 --- a/npk.py +++ b/npk.py @@ -102,7 +102,6 @@ class NpkInfo: def build_time(self,value:datetime): self._build_time = int(value.timestamp()) - class NpkNameInfo(NpkInfo): _format = '<16s4sI12s' def __init__(self,name:str,version:str,build_time=datetime.now(),unknow=b'\x00'*12): @@ -171,7 +170,6 @@ class Package: def __iter__(self): for part in self._parts: yield part - def __getitem__(self, id:NpkPartID): for part in self._parts: if part.id == id: @@ -209,7 +207,7 @@ class NovaPackage(Package): self._parts.append(NpkPartItem(NpkPartID(part_id),NpkInfo.unserialize_from(part_data))) else: self._parts.append(NpkPartItem(NpkPartID(part_id),part_data)) - + def get_digest(self,hash_fnc,package:Package=None)->bytes: parts = package._parts if package else self._parts for part in parts: @@ -312,9 +310,7 @@ class NovaPackage(Package): assert int.from_bytes(data[:4],'little') == NovaPackage.NPK_MAGIC, 'Invalid Nova Package Magic' assert int.from_bytes(data[4:8],'little') == len(data) - 8, 'Invalid Nova Package Size' return NovaPackage(data[8:]) - - if __name__=='__main__': parser = argparse.ArgumentParser(description='nova package creator and editor') subparsers = parser.add_subparsers(dest="command") @@ -335,7 +331,6 @@ if __name__=='__main__': kcdsa_public_key = bytes.fromhex(os.environ['CUSTOM_LICENSE_PUBLIC_KEY']) eddsa_public_key = bytes.fromhex(os.environ['CUSTOM_NPK_SIGN_PUBLIC_KEY']) - if args.command =='sign': print(f'Signing {args.input}') npk = NovaPackage.load(args.input) diff --git a/patch.py b/patch.py index 8ab1f3e..65a0b4a 100644 --- a/patch.py +++ b/patch.py @@ -108,7 +108,6 @@ def patch_elf(data: bytes,key_dict:dict): initrd_xz = find_7zXZ_data(data) return patch_initrd_xz(initrd_xz,key_dict) - def patch_pe(data: bytes,key_dict:dict): vmlinux_xz = find_7zXZ_data(data) vmlinux = lzma.decompress(vmlinux_xz) @@ -266,8 +265,7 @@ def run_shell_command(command): process = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return process.stdout, process.stderr - -def patch_npk_package(package,key_dict,kcdsa_private_key,eddsa_private_key): +def patch_npk_package(package,key_dict): if package[NpkPartID.NAME_INFO].data.name == 'system': file_container = NpkFileContainer.unserialize_from(package[NpkPartID.FILE_CONTAINER].data) for item in file_container: @@ -280,8 +278,6 @@ def patch_npk_package(package,key_dict,kcdsa_private_key,eddsa_private_key): elif item.name == b'boot/initrd.rgz': print(f'patch {item.name} ...') item.data = patch_kernel(item.data,key_dict) - open('initrd.rgz','wb').write(item.data) - package[NpkPartID.FILE_CONTAINER].data = file_container.serialize() try: squashfs_file = 'squashfs-root.sfs' @@ -298,6 +294,7 @@ def patch_npk_package(package,key_dict,kcdsa_private_key,eddsa_private_key): elif 'ARCH' in os.environ and os.environ['ARCH'] == '-arm64': run_shell_command(f"sudo cp keygen/keygen_aarch64 {keygen}") run_shell_command(f"sudo chmod a+x {keygen}") + run_shell_command(f'echo -e "\n https://github.com/elseif/MikroTikPatch" | sudo tee -a {os.path.join(extract_dir,"nova/lib/console/logo.txt")}') print(f"pack {extract_dir} ...") run_shell_command(f"rm -f {squashfs_file}") _, stderr = run_shell_command(f"mksquashfs {extract_dir} {squashfs_file} -quiet -comp xz -no-xattrs -b 256k") @@ -313,10 +310,9 @@ def patch_npk_file(key_dict,kcdsa_private_key,eddsa_private_key,input_file,outpu npk = NovaPackage.load(input_file) if len(npk._packages) > 0: for package in npk._packages: - patch_npk_package(package,key_dict,kcdsa_private_key,eddsa_private_key) + patch_npk_package(package,key_dict) else: - patch_npk_package(npk,key_dict,kcdsa_private_key,eddsa_private_key) - + patch_npk_package(npk,key_dict) npk.sign(kcdsa_private_key,eddsa_private_key) npk.save(output_file or input_file)