Skip to content

Instantly share code, notes, and snippets.

@chenxiaolong
Created December 10, 2023 23:34
Show Gist options
  • Save chenxiaolong/da42491c0b7d4fb8262b6fc3c2850777 to your computer and use it in GitHub Desktop.
Save chenxiaolong/da42491c0b7d4fb8262b6fc3c2850777 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import argparse
import io
import mmap
import zipfile
def build_empty_zip():
stream = io.BytesIO()
with zipfile.ZipFile(stream, 'w'):
pass
return stream.getvalue()
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('system', help='system.img to modify')
parser.add_argument('otacerts', help='Recovery\'s otacerts.zip file')
return parser.parse_args()
def main():
args = parse_args()
empty_zip_data = build_empty_zip()
with open(args.otacerts, 'rb') as f:
otacerts_data = f.read()
assert len(empty_zip_data) < len(otacerts_data)
with open(args.system, 'r+b') as f:
system_data = mmap.mmap(f.fileno(), 0)
offset = system_data.find(otacerts_data)
assert offset >= 0
# zero-pad in front of zip
padding_size = len(otacerts_data) - len(empty_zip_data)
system_data[offset:offset + padding_size] = b'\0' * padding_size
system_data[offset + padding_size:offset + len(otacerts_data)] = \
empty_zip_data
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment