-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworkbench-script.py
More file actions
590 lines (474 loc) · 18 KB
/
workbench-script.py
File metadata and controls
590 lines (474 loc) · 18 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# -*- coding: utf-8 -*-
# Copyright (c) 2024 pangea.org Associació Pangea - Coordinadora Comunicació per a la Cooperació
# SPDX-License-Identifier: AGPL-3.0-or-later
import os
import json
import uuid
import hashlib
import argparse
import configparser
import urllib.parse
import urllib.request
import urllib.error
import gettext
import locale
import logging
from datetime import datetime
import time
SNAPSHOT_BASE = {
'timestamp': str(datetime.now()),
'type': 'Snapshot',
'uuid': str(uuid.uuid4()),
'software': "workbench-script",
'version': "0.0.1",
'operator_id': "",
'data': {},
'erase': []
}
## Utility Functions ##
def logs(f):
def wrapper(*args, **kwargs):
try:
return f(*args, **kwargs)
except Exception as err:
logger.error(err)
return ''
return wrapper
@logs
def exec_cmd(cmd):
logger.info(_('Running command `%s`'), cmd)
return os.popen(cmd).read()
@logs
def exec_cmd_erase(cmd):
logger.info(_('Running command `%s`'), cmd)
return ''
# return os.popen(cmd).read()
## End Utility functions ##
## Legacy Functions ##
def convert_to_legacy_snapshot(snapshot):
snapshot["sid"] = str(uuid.uuid4()).split("-")[1]
snapshot["software"] = "workbench-script"
snapshot["version"] = "dev"
snapshot["schema_api"] = "1.0.0"
snapshot["settings_version"] = "No Settings Version (NaN)"
snapshot["timestamp"] = snapshot["timestamp"].replace(" ", "T")
snapshot["data"]["smart"] = json.loads(snapshot["data"]["smartctl"])
snapshot["data"].pop("smartctl")
snapshot["data"].pop("inxi")
snapshot.pop("operator_id")
snapshot.pop("erase")
lshw = 'sudo lshw -json'
hwinfo = 'sudo hwinfo --reallyall'
lspci = 'sudo lspci -vv'
data = {
'lshw': exec_cmd(lshw) or "{}",
'hwinfo': exec_cmd(hwinfo),
'lspci': exec_cmd(lspci)
}
snapshot['data'].update(data)
## End Legacy Functions ##
## Command Functions ##
## Erase Functions ##
## Xavier Functions ##
def erase_basic(disk):
"""
Basic Erasure
https://tsapps.nist.gov/publication/get_pdf.cfm?pub_id=917935
Settings for basic data erasure using shred Linux command.
A software-based fast non-100%-secured way of erasing data storage.
Performs 1 pass overwriting one round using all zeros.
Compliant with NIST SP-800-8y8.
In settings appear:
WB_ERASE = EraseBasic
WB_ERASE_STEPS = 1
WB_ERASE_LEADING_ZEROS = False
"""
cmd = f'shred -vn 1 /dev/{disk}'
return [exec_cmd_erase(cmd)]
def erase_baseline(disk):
"""
Baseline Secure Erasure
Settings for advanced data erasure using badblocks Linux software.
A secured-way of erasing data storages, erase hidden areas,
checking the erase sector by sector.
Performs 1 pass overwriting each sector with zeros and a final verification.
Compliant with HMG Infosec Standard 5 Baseline.
In settings appear:
WB_ERASE = EraseSectors
WB_ERASE_STEPS = 1
WB_ERASE_LEADING_ZEROS = True
WB_ERASE_1_METHOD = EraseBasic
WB_ERASE_1_STEP_TYPE = 0
WB_ERASE_2_METHOD = EraseSectors
WB_ERASE_2_STEP_TYPE = 1
"""
result = []
cmd = f'shred -zvn 0 /dev/{disk}'
result.append(exec_cmd_erase(cmd))
cmd = f'badblocks -st random -w /dev/{disk}'
result.append(exec_cmd_erase(cmd))
return result
def erase_enhanced(disk):
"""
Enhanced Secure Erasure
Settings for advanced data erasure using badblocks Linux software.
A secured-way of erasing data storages, erase hidden areas,
checking the erase sector by sector.
Performs 3 passes overwriting every sector with zeros and ones,
and final verification. Compliant with HMG Infosec Standard 5 Enhanced.
In settings appear:
WB_ERASE = EraseSectors
WB_ERASE_LEADING_ZEROS = True
WB_ERASE_1_METHOD = EraseBasic
WB_ERASE_1_STEP_TYPE = 1
WB_ERASE_2_METHOD = EraseBasic
WB_ERASE_2_STEP_TYPE = 0
WB_ERASE_3_METHOD = EraseSectors
WB_ERASE_3_STEP_TYPE = 1
"""
result = []
cmd = f'shred -vn 1 /dev/{disk}'
result.append(exec_cmd_erase(cmd))
cmd = f'shred -zvn 0 /dev/{disk}'
result.append(exec_cmd_erase(cmd))
## creo que realmente seria asi (3 pases y una extra poniendo a ceros):
# shred -zvn 3 /def/{disk}
# tampoco estoy seguro que el badblocks haga un proceso de verificacion.
cmd = f'badblocks -st random -w /dev/{disk}'
result.append(exec_cmd_erase(cmd))
return result
## End Xavier Functions ##
def ata_secure_erase_null(disk):
cmd_baseline = f'hdparm --user-master u --security-erase NULL /dev/{disk}'
return [exec_cmd_erase(cmd_baseline)]
def ata_secure_erase_enhanced(disk):
cmd_enhanced = f'hdparm --user-master u --security-erase-enhanced /dev/{disk}'
return [exec_cmd_erase(cmd_enhanced)]
def nvme_secure_erase(disk):
cmd_encrypted = f'nvme format /dev/{disk} --ses=1'
return [exec_cmd_erase(cmd_encrypted)]
## End Erase Functions ##
@logs
def get_disks():
disks = json.loads(
exec_cmd('lsblk -Jdo NAME,TYPE,MOUNTPOINTS,ROTA,TRAN')
)
return disks.get('blockdevices', [])
@logs
def gen_erase(all_disks, type_erase, user_disk=None):
erase = []
for disk in all_disks:
if user_disk and disk['name'] not in user_disk:
continue
if disk['type'] != 'disk':
continue
if 'boot' in disk['mountpoints']:
continue
if not disk['rota']:
# if soport nvme erase
erase.append(nvme_secure_erase(disk['name']))
elif disk['tran'] in ['ata', 'sata']:
# if soport ata erase
if type_erase == 'basic':
erase.append(ata_secure_erase_null(disk['name']))
elif type_erase == 'baseline':
erase.append(ata_secure_erase_null(disk['name']))
elif type_erase == 'enhanced':
erase.append(ata_secure_erase_enhanced(disk['name']))
else:
# For old disks
if type_erase == 'basic':
erase.append(erase_basic(disk['name']))
elif type_erase == 'baseline':
erase.append(erase_baseline(disk['name']))
elif type_erase == 'enhanced':
erase.append(erase_enhanced(disk['name']))
return erase
@logs
def exec_smart(disk):
cmd = f'sudo smartctl -x --json=cosviu /dev/{disk}'
return json.loads(exec_cmd(cmd))
@logs
def smartctl(all_disks, disk=None):
if disk:
return [exec_smart(disk)]
data_list = []
for disk in all_disks:
if disk['type'] == 'disk':
data = exec_smart(disk['name'])
data_list.append(data)
return json.dumps(data_list)
## End Command Functions ##
# TODO permitir selección
# TODO permitir que vaya más rápido
def get_data(all_disks):
dmidecode = 'sudo dmidecode'
inxi = "sudo inxi -afmnGEMABD -x 3 --edid --output json --output-file print"
data = {
'smartctl': smartctl(all_disks),
'dmidecode': exec_cmd(dmidecode),
'inxi': exec_cmd(inxi)
}
return data
def gen_snapshot(all_disks):
snapshot = SNAPSHOT_BASE.copy()
snapshot['data'] = get_data(all_disks)
return snapshot
def save_snapshot_in_disk(snapshot, path, snap_uuid):
snapshot_path = os.path.join(path, 'snapshots')
filename = "{}/{}_{}.json".format(
snapshot_path,
datetime.now().strftime("%F_%H-%M-%S"),
snap_uuid)
try:
if not os.path.exists(snapshot_path):
os.makedirs(snapshot_path)
logger.info(_("Created snapshots directory at '%s'"), snapshot_path)
with open(filename, "w") as f:
f.write(snapshot)
logger.info(_("Snapshot written in path '%s'"), filename)
except Exception as e:
try:
logger.warning(_("Attempting to save file in actual path. Reason: Failed to write in snapshots directory:\n %s."), e)
fallback_filename = "{}/{}_{}.json".format(
path,
datetime.now().strftime("%Y%m%d-%H_%M_%S"),
snap_uuid)
with open(fallback_filename, "w") as f:
f.write(snapshot)
logger.warning(_("Snapshot written in fallback path '%s'"), fallback_filename)
except Exception as e:
logger.error(_("Could not save snapshot locally. Reason: Failed to write in fallback path:\n %s"), e)
def http_post(url, data, headers, redirect_depth=0):
"""Send a POST request with given headers and JSON data."""
MAX_REDIRECTS = 5
if redirect_depth > MAX_REDIRECTS:
err_msg = "Error: Too many redirects (limit {})".format(MAX_REDIRECTS)
logger.error(err_msg)
return 0, err_msg
try:
request = urllib.request.Request(url, data=data, headers=headers)
with urllib.request.urlopen(request) as response:
status_code = response.getcode()
response_text = response.read().decode('utf-8')
except urllib.error.HTTPError as e:
if e.code in (301, 302, 307, 308):
new_location = e.headers.get('Location')
if new_location:
new_url = urllib.parse.urljoin(url, new_location)
logger.info(_("Redirect %s detected. Retrying POST to: %s"), e.code, new_url)
return http_post(new_url, data, headers, redirect_depth + 1)
except urllib.error.HTTPError as e:
status_code = e.code
response_text = e.read().decode('utf-8')
logger.error("HTTPError %s: %s", status_code, response_text)
return status_code, response_text
def send_snapshot_to_idhub(snapshot, token, url):
"""Send snapshot to be signed as a credential"""
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
try:
cred = {
"type": "DeviceSnapshotV1",
"save": False,
"data": snapshot
}
data = json.dumps(cred).encode('utf-8')
## uncomment to save precredential
#with open('/tmp/pre-vc-test.json', "wb") as f:
# f.write(data)
status_code, response_text = http_post(url, data, headers)
if 200 <= status_code < 300:
logger.info(_("Credential successfully signed"))
res = json.loads(response_text)
if res.get("status") == "success" and res.get("data"):
return res["data"]
return json.dumps(snapshot)
else:
logger.error(_("Failed to sign credential. URL: '%s'"), url)
return json.dumps(snapshot)
except Exception as e:
logger.error(_("Credential not remotely signed by IdHub URL '%s'. Do you have internet? Is your server up & running? Is the url token authorized?\n %s"), url, e)
return json.dumps(snapshot)
# apt install qrencode
def generate_qr_code(url, disable_qr):
"""Generate and print QR code for the given URL."""
if disable_qr:
return
qr_command = "echo {} | qrencode -t ANSI".format(url)
print(exec_cmd(qr_command))
# TODO sanitize url, if url is like this, it fails
# url = 'http://127.0.0.1:8000/api/snapshot/'
def send_snapshot_to_devicehub(snapshot, token, url, ev_uuid, legacy, disable_qr, http_max_retries=5, http_retry_delay=5):
"""Send snapshot to be stored in devicehub inventory service"""
url_components = urllib.parse.urlparse(url)
ev_path = f"evidence/{ev_uuid}"
components = (url_components.scheme, url_components.netloc, ev_path, '', '', '')
ev_url = urllib.parse.urlunparse(components)
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
retries = 1
while retries <= http_max_retries:
try:
data = snapshot.encode('utf-8')
status_code, response_text = http_post(url, data, headers)
if 200 <= status_code < 300:
logger.info(_("Snapshot successfully sent to '%s'"), url)
if legacy:
try:
response = json.loads(response_text)
public_url = response.get('public_url')
dhid = response.get('dhid')
if public_url:
generate_qr_code(public_url, disable_qr)
print("url: {}".format(public_url))
if dhid:
print("dhid: {}".format(dhid))
except Exception:
logger.error(response_text)
else:
generate_qr_code(ev_url, disable_qr)
print("url: {}".format(ev_url))
return
else:
logger.error(
_("Snapshot %s not remotely sent to URL '%s'. Server responded with error:\n %s"), ev_uuid, url, response_text)
except Exception as e:
logger.error(
_("Snapshot not remotely sent to URL '%s'. Do you have internet? Is your server up & running? Is the url token authorized?\n %s"), url, e)
retries += 1
if retries <= http_max_retries:
logger.info(_("Retrying... (%d/%d)"), retries, http_max_retries)
time.sleep(http_retry_delay)
logger.error(
_("Failed to send snapshot to URL '%s' after %d attempts"), url, http_max_retries)
def load_config(config_file="settings.ini"):
"""
Tries to load configuration from a config file.
"""
# Define all keys and their default values in one place
defaults = {
'path': os.getcwd(),
'url': None,
'token': None,
'device': None,
'erase': None,
'legacy': None,
'url_wallet': None,
'wb_sign_token': None,
'disable_qr': None,
'http_max_retries': 5,
'http_retry_delay': 5
}
if not os.path.exists(config_file):
logger.error(_("Config file '%s' not found. Using default values."), config_file)
return defaults
logger.info(_("Found config file in path: %s."), config_file)
config = configparser.ConfigParser()
config.read(config_file)
result = {}
# Iterate through defaults to extract values dynamically
for key, default_val in defaults.items():
if config.has_option('settings', key):
# Auto-cast to int if the default value is an integer
if isinstance(default_val, int):
result[key] = config.getint('settings', key)
else:
result[key] = config.get('settings', key)
else:
result[key] = default_val
# Validate URL
if result.get('url') and not result['url'].startswith(('http://', 'https://')):
logger.error(_("Configured URL does not start with http:// or https://"))
exit(1)
return result
def parse_args():
"""
Parse config argument, if available
"""
parser = argparse.ArgumentParser(
usage=_("workbench-script.py [-h] [--config CONFIG]"),
description=_("Optional config loader for workbench."))
parser.add_argument(
'--config',
help=_("path to the config file. Defaults to 'settings.ini' in the current directory."),
default="settings.ini" # Fallback to 'settings.ini' by default
)
return parser.parse_args()
def prepare_lang():
locale_path = os.path.join(os.path.dirname(__file__), 'locale')
domain = 'messages'
gettext.bindtextdomain(domain, locale_path)
gettext.textdomain(domain)
global _
# with LANG=es_ES.UTF-8, it detects spanish
_ = gettext.gettext
# # this would force it to spanish
# lang = gettext.translation(domain, localedir=locale_path, languages=['es'])
# lang.install()
# _ = lang.gettext
def prepare_logger():
global logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
formatter = logging.Formatter('[%(asctime)s] workbench: %(levelname)s: %(message)s')
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
def main():
prepare_lang()
prepare_logger()
logger.info(_("START"))
# Parse the command-line arguments
args = parse_args()
# Load the config file, either specified via --config or the default 'settings.ini'
config_file = args.config
config = load_config(config_file)
legacy = config.get("legacy")
# TODO show warning if non root, means data is not complete
# if annotate as potentially invalid snapshot (pending the new API to be done)
if os.geteuid() != 0:
logger.warning(_("This script must be run as root. Collected data will be incomplete or unusable"))
all_disks = get_disks()
snapshot = gen_snapshot(all_disks)
snap_uuid = snapshot["uuid"]
if config['erase'] and config['device'] and not legacy:
snapshot['erase'] = gen_erase(all_disks, config['erase'], user_disk=config['device'])
elif config['erase'] and not legacy:
snapshot['erase'] = gen_erase(all_disks, config['erase'])
token = config.get("token")
if token:
snapshot["token_hash"] = hashlib.sha3_256(token.encode("utf8")).hexdigest()
if legacy:
convert_to_legacy_snapshot(snapshot)
snapshot = json.dumps(snapshot)
else:
url_wallet = config.get("url_wallet")
wb_sign_token = config.get("wb_sign_token")
if wb_sign_token:
tk = wb_sign_token.encode("utf8")
snapshot["operator_id"] = hashlib.sha3_256(tk).hexdigest()
if url_wallet and wb_sign_token:
snapshot = send_snapshot_to_idhub(snapshot, wb_sign_token, url_wallet)
else:
snapshot = json.dumps(snapshot)
save_snapshot_in_disk(snapshot, config['path'], snap_uuid)
if config['url']:
send_snapshot_to_devicehub(
snapshot,
config['token'],
config['url'],
snap_uuid,
legacy,
config['disable_qr'],
config['http_max_retries'],
config['http_retry_delay']
)
logger.info(_("END"))
if __name__ == '__main__':
main()