-
Notifications
You must be signed in to change notification settings - Fork 3
/
patch-vbmeta.py
executable file
·55 lines (40 loc) · 1.4 KB
/
patch-vbmeta.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
import os
import sys
import argparse
# Magic for the vbmeta image header
AVB_MAGIC = b"AVB0"
AVB_MAGIC_LEN = 4
# Information about the verification flags
FLAGS_OFFSET = 123
FLAGS_TO_SET = b"\x03"
def patch_vbmeta(file):
# try reading the file with read/write to make sure it exists
try:
fd = os.open(file, os.O_RDWR)
except OSError:
sys.exit(f"Error reading file: {file}\nFile not modified. Exiting...")
# making sure it's a vbmeta image by reading the magic bytes at the start of the file
magic = os.read(fd, AVB_MAGIC_LEN)
if magic != AVB_MAGIC:
fd.close()
sys.exit(
"Error: The provided image is not a valid vbmeta image.\nFile not modified. Exiting..."
)
# set the disable-verity and disable-verification flags at offset 123
try:
os.lseek(fd, FLAGS_OFFSET, os.SEEK_SET)
os.write(fd, FLAGS_TO_SET)
except OSError:
fd.close()
sys.exit("Error: Failed when patching the vbmeta image.\nExiting...")
# end of program
os.close(fd)
print("Patching successful.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Python script to patch Android vbmeta image file to disable verification flags"
)
parser.add_argument("filename", help="vbmeta image file to patch")
args = parser.parse_args()
patch_vbmeta(args.filename)