forked from alephsecurity/abootool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaboot.py
110 lines (90 loc) · 2.87 KB
/
aboot.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""
Author: Roee Hay / Aleph Research / HCL Technologies
"""
import string
import json
import hashlib
import os
from serializable import Serializable
from log import *
from config import Config
bootloaders_by_device = {}
bootloaders_by_oem = {}
bootloaders = None
def get_bootloaders(path=Config.data_path):
global bootloaders
if bootloaders:
return bootloaders
bootloaders = []
n = 0
for f in os.listdir(path):
if f.endswith(".json"):
bl = ABOOT.create_from_json(os.path.join(path, f))
bootloaders.append(bl)
if not bootloaders_by_oem.has_key(bl.oem):
bootloaders_by_oem[bl.oem] = []
bootloaders_by_oem[bl.oem].append(bl)
if not bootloaders_by_device.has_key(bl.device):
bootloaders_by_device[bl.device] = []
bootloaders_by_device[bl.device].append(bl)
n+=1
D("loaded %d bootloaders (%d devices, %d OEMs)", n, len(bootloaders_by_device), len(bootloaders_by_oem))
return bootloaders
def by_oem(oem = None):
all()
if not oem:
return bootloaders_by_oem
try:
D("bootloader.by_oem[%s]: %s", oem, bootloaders_by_oem[oem])
return bootloaders_by_oem[oem]
except KeyError:
return []
def by_device(device = None):
all()
if not device:
return bootloaders_by_device
try:
D("bootloader.by_device[%s]: %s", device, bootloaders_by_device[device])
return bootloaders_by_device[device]
except KeyError:
return []
def all():
if not bootloaders:
get_bootloaders()
return bootloaders
class ABOOT(Serializable):
@classmethod
def create_from_json(cls, path):
data = json.load(file(path, "rb"))
return ABOOT().set_data(data)
@classmethod
def create_from_bootloader_image(cls, fp, oem, device, build, src, name, strprefix=""):
data = fp.read()
sha256 = hashlib.sha256(data).hexdigest()
D("SHA256 = %s", sha256)
s = ""
printable = set(string.printable)
strings = set()
i = 0
for c in data:
if 0 == i % 2**20:
T("%d", i >> 20)
if c in printable:
s += c
else:
if "" != s:
if s.startswith(strprefix):
strings.add(s)
s = ""
i += 1
strings = list(strings)
strings.sort()
return ABOOT().set_data({'src': src,
'name': name,
'sha256': sha256,
'strings': strings,
'oem': oem,
'device': device,
'build': build})
def __repr__(self):
return "%s/%s/%s" % (self.oem, self.device, self.build)