Repository navigation
Expand file tree
/
Copy pathbiomeBluetooth.py
More file actions
186 lines (152 loc) · 7.43 KB
/
Copy pathbiomeBluetooth.py
File metadata and controls
186 lines (152 loc) · 7.43 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import os
import struct
import blackboxprotobuf
from datetime import datetime, timezone
from time import mktime
from io import StringIO
from io import BytesIO
from scripts.artifact_report import ArtifactHtmlReport
from scripts.ilapfuncs import logfunc, tsv, timeline, is_platform_windows, open_sqlite_db_readonly, convert_ts_human_to_utc, convert_utc_human_to_timezone, convert_time_obj_to_utc
def utf8_in_extended_ascii(input_string, *, raise_on_unexpected=False):
"""Returns a tuple of bool (whether mis-encoded utf-8 is present) and str (the converted string)"""
output = [] # individual characters, join at the end
is_in_multibyte = False # True if we're currently inside a utf-8 multibyte character
multibytes_expected = 0
multibyte_buffer = []
mis_encoded_utf8_present = False
def handle_bad_data(index, character):
if not raise_on_unexpected: # not raising, so we dump the buffer into output and append this character
output.extend(multibyte_buffer)
multibyte_buffer.clear()
output.append(character)
nonlocal is_in_multibyte
is_in_multibyte = False
nonlocal multibytes_expected
multibytes_expected = 0
else:
raise ValueError(f"Expected multibyte continuation at index: {index}")
for idx, c in enumerate(input_string):
code_point = ord(c)
if code_point <= 0x7f or code_point > 0xf4: # ASCII Range data or higher than you get for mis-encoded utf-8:
if not is_in_multibyte:
output.append(c) # not in a multibyte, valid ascii-range data, so we append
else:
handle_bad_data(idx, c)
else: # potentially utf-8
if (code_point & 0xc0) == 0x80: # continuation byte
if is_in_multibyte:
multibyte_buffer.append(c)
else:
handle_bad_data(idx, c)
else: # start-byte
if not is_in_multibyte:
assert multibytes_expected == 0
assert len(multibyte_buffer) == 0
while (code_point & 0x80) != 0:
multibytes_expected += 1
code_point <<= 1
multibyte_buffer.append(c)
is_in_multibyte = True
else:
handle_bad_data(idx, c)
if is_in_multibyte and len(multibyte_buffer) == multibytes_expected: # output utf-8 character if complete
utf_8_character = bytes(ord(x) for x in multibyte_buffer).decode("utf-8")
output.append(utf_8_character)
multibyte_buffer.clear()
is_in_multibyte = False
multibytes_expected = 0
mis_encoded_utf8_present = True
if multibyte_buffer: # if we have left-over data
handle_bad_data(len(input_string), "")
return mis_encoded_utf8_present, "".join(output)
def timestampsconv(webkittime):
unix_timestamp = webkittime + 978307200
finaltime = datetime.fromtimestamp(unix_timestamp, tz=timezone.utc)
return(finaltime)
def get_biomeBluetooth(files_found, report_folder, seeker, wrap_text, timezone_offset):
typess = {'1': {'type': 'message', 'message_typedef': {'1': {'type': 'bytes', 'name': ''}, '2': {'type': 'message', 'message_typedef': {'1': {'type': 'int', 'name': ''}, '2': {'type': 'int', 'name': ''}}, 'name': ''}}, 'name': ''}, '2': {'type': 'double', 'name': ''}, '3': {'type': 'double', 'name': ''}, '4': {'type': 'message', 'message_typedef': {'1': {'type': 'message', 'message_typedef': {'1': {'type': 'int', 'name': ''}, '2': {'type': 'int', 'name': ''}}, 'name': ''}, '3': {'type': 'str', 'name': ''}}, 'name': ''}, '5': {'type': 'str', 'name': ''}, '8': {'type': 'fixed64', 'name': ''}, '10': {'type': 'int', 'name': ''}}
for file_found in files_found:
file_found = str(file_found)
filename = os.path.basename(file_found)
if filename.startswith('.'):
continue
if os.path.isfile(file_found):
if 'tombstone' in file_found:
continue
else:
pass
else:
continue
with open(file_found, 'rb') as file:
data = file.read()
data_list = []
headerloc = data.index(b'SEGB')
#print(headerloc)
b = data
ab = BytesIO(b)
ab.seek(headerloc)
ab.read(4) #Main header
#print('---- Start of Notifications ----')
while True:
#print('----')
sizeofnotificatoninhex = (ab.read(4))
try:
sizeofnotificaton = (struct.unpack_from("<i",sizeofnotificatoninhex)[0])
except:
break
if sizeofnotificaton == 0:
break
allocation = ab.read(4)
date1 = ab.read(8)
date1 = (struct.unpack_from("<d",date1)[0])
convertedtime1 = timestampsconv(date1)
convertedtime1 = convert_utc_human_to_timezone(convertedtime1, timezone_offset)
#print(convertedtime1)
segbtime = convertedtime1
date2 = ab.read(8)
date2 = (struct.unpack_from("<d",date2)[0])
convertedtime2 = timestampsconv(date2)
#print(convertedtime2)
ignore1 = ab.read(8)
protostuff = ab.read(sizeofnotificaton)
checkforempty = BytesIO(protostuff)
check = checkforempty.read(1)
if check == b'\x00':
pass
else:
protostuff, types = blackboxprotobuf.decode_message(protostuff)#,typess)
#pp = pprint.PrettyPrinter(indent=4)
#pp.pprint(protostuff)
#print(types)
mac = protostuff['1'].decode()
if isinstance(protostuff['2'], dict):
desc = protostuff['2']
else:
desc = protostuff['2'].decode()
data_list.append((segbtime,mac,desc))
modresult = (sizeofnotificaton % 8)
resultante = 8 - modresult
if modresult == 0:
pass
else:
ab.read(resultante)
if len(data_list) > 0:
description = ''
report = ArtifactHtmlReport(f'Biome Bluetooth')
report.start_artifact_report(report_folder, f'Biome Bluetooth - {filename}', description)
report.add_script()
data_headers = ('Timestamp','MAC','Name')
report.write_artifact_data_table(data_headers, data_list, file_found)
report.end_artifact_report()
tsvname = f'Biome Bluetooth - {filename}'
tsv(report_folder, data_headers, data_list, tsvname) # TODO: _csv.Error: need to escape, but no escapechar set
tlactivity = f'Biome Bluetooth - {filename}'
timeline(report_folder, tlactivity, data_list, data_headers)
else:
logfunc(f'No data available for Biome Bluetooth')
__artifacts__ = {
"biomeBluetooth": (
"Biome Bluetooth",
('*/Biome/streams/restricted/Device.Wireless.Bluetooth/local/*'),
get_biomeBluetooth)
}