|
|
|
@ -3,6 +3,7 @@
|
|
|
|
|
import os
|
|
|
|
|
import json
|
|
|
|
|
import uuid
|
|
|
|
|
import hashlib
|
|
|
|
|
import argparse
|
|
|
|
|
import configparser
|
|
|
|
|
import urllib.parse
|
|
|
|
@ -27,23 +28,6 @@ SNAPSHOT_BASE = {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 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"] = snapshot["data"]["disks"]
|
|
|
|
|
snapshot["data"]["lshw"] = json.loads(snapshot["data"]["lshw"])
|
|
|
|
|
snapshot["data"].pop("disks")
|
|
|
|
|
snapshot.pop("erase")
|
|
|
|
|
snapshot.pop("token_hash")
|
|
|
|
|
|
|
|
|
|
## End Legacy Functions ##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Utility Functions ##
|
|
|
|
|
def logs(f):
|
|
|
|
@ -62,6 +46,7 @@ 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)
|
|
|
|
@ -71,14 +56,7 @@ def exec_cmd_erase(cmd):
|
|
|
|
|
## End Utility functions ##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def convert_to_credential(snapshot):
|
|
|
|
|
snapshot["data"] = json.dumps(snapshot["data"])
|
|
|
|
|
file_path = os.path.join(BASE_DIR, "templates", "snapshot.json")
|
|
|
|
|
with open(file_path) as f:
|
|
|
|
|
ff = f.read()
|
|
|
|
|
template = Template(ff)
|
|
|
|
|
cred = template.substitute(**snapshot)
|
|
|
|
|
return cred
|
|
|
|
|
## Legacy Functions ##
|
|
|
|
|
|
|
|
|
|
def convert_to_legacy_snapshot(snapshot):
|
|
|
|
|
snapshot["sid"] = str(uuid.uuid4()).split("-")[1]
|
|
|
|
@ -87,13 +65,13 @@ def convert_to_legacy_snapshot(snapshot):
|
|
|
|
|
snapshot["schema_api"] = "1.0.0"
|
|
|
|
|
snapshot["settings_version"] = "No Settings Version (NaN)"
|
|
|
|
|
snapshot["timestamp"] = snapshot["timestamp"].replace(" ", "T")
|
|
|
|
|
snapshot["data"]["smart"] = snapshot["data"]["smartctl"]
|
|
|
|
|
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 -xml'
|
|
|
|
|
lshw = 'sudo lshw -json'
|
|
|
|
|
hwinfo = 'sudo hwinfo --reallyall'
|
|
|
|
|
lspci = 'sudo lspci -vv'
|
|
|
|
|
|
|
|
|
@ -359,28 +337,24 @@ def send_to_sign_credential(snapshot, token, url):
|
|
|
|
|
res = json.loads(response_text)
|
|
|
|
|
if res.get("status") == "success" and res.get("data"):
|
|
|
|
|
return res["data"]
|
|
|
|
|
return snapshot
|
|
|
|
|
return json.dumps(snapshot)
|
|
|
|
|
else:
|
|
|
|
|
logger.error(_("Credential cannot signed in '%s'"), url)
|
|
|
|
|
return snapshot
|
|
|
|
|
return json.dumps(snapshot)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(_("Credential not remotely builded to URL '%s'. Do you have internet? Is your server up & running? Is the url token authorized?\n %s"), url, e)
|
|
|
|
|
return json.dumps(snapshot)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 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):
|
|
|
|
|
def send_snapshot_to_devicehub(snapshot, token, url, ev_uuid, legacy):
|
|
|
|
|
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)
|
|
|
|
|
# apt install qrencode
|
|
|
|
|
qr = "echo {} | qrencode -t ANSI".format(ev_url)
|
|
|
|
|
print(exec_cmd(qr))
|
|
|
|
|
print(ev_url)
|
|
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
|
"Authorization": f"Bearer {token}",
|
|
|
|
@ -391,7 +365,7 @@ def send_snapshot_to_devicehub(snapshot, token, url, ev_uuid):
|
|
|
|
|
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')
|
|
|
|
|
response_text = response.read().decode('utf-8')
|
|
|
|
|
|
|
|
|
|
if 200 <= status_code < 300:
|
|
|
|
|
logger.info(_("Snapshot successfully sent to '%s'"), url)
|
|
|
|
@ -414,13 +388,12 @@ def send_snapshot_to_devicehub(snapshot, token, url, ev_uuid):
|
|
|
|
|
print(exec_cmd(qr))
|
|
|
|
|
print(f"url: {ev_url}")
|
|
|
|
|
else:
|
|
|
|
|
logger.error(_("Snapshot cannot sent to '%s'"), url)
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_config(config_file="settings.ini"):
|
|
|
|
|
"""
|
|
|
|
|
Tries to load configuration from a config file.
|
|
|
|
@ -445,7 +418,7 @@ def load_config(config_file="settings.ini"):
|
|
|
|
|
else:
|
|
|
|
|
logger.error(_("Config file '%s' not found. Using default values."), config_file)
|
|
|
|
|
path = os.path.join(os.getcwd())
|
|
|
|
|
url, token, device, erase, legacy, url_wallet, wb_sign_token = None, None, None, None, None, None, None
|
|
|
|
|
url, token, device, erase, legacy, url_wallet, wb_sign_token = (None,)*7
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'path': path,
|
|
|
|
@ -509,6 +482,7 @@ def main():
|
|
|
|
|
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)
|
|
|
|
@ -519,9 +493,9 @@ def main():
|
|
|
|
|
snapshot = gen_snapshot(all_disks)
|
|
|
|
|
snap_uuid = snapshot["uuid"]
|
|
|
|
|
|
|
|
|
|
if config['erase'] and config['device'] and not config.get("legacy"):
|
|
|
|
|
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 config.get("legacy"):
|
|
|
|
|
elif config['erase'] and not legacy:
|
|
|
|
|
snapshot['erase'] = gen_erase(all_disks, config['erase'])
|
|
|
|
|
|
|
|
|
|
if legacy:
|
|
|
|
@ -543,7 +517,13 @@ def main():
|
|
|
|
|
save_snapshot_in_disk(snapshot, config['path'], snap_uuid)
|
|
|
|
|
|
|
|
|
|
if config['url']:
|
|
|
|
|
send_snapshot_to_devicehub(snapshot, config['token'], config['url'], snap_uuid)
|
|
|
|
|
send_snapshot_to_devicehub(
|
|
|
|
|
snapshot,
|
|
|
|
|
config['token'],
|
|
|
|
|
config['url'],
|
|
|
|
|
snap_uuid,
|
|
|
|
|
legacy
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
logger.info(_("END"))
|
|
|
|
|
|
|
|
|
|