|
|
@ -6,7 +6,6 @@ import uuid
|
|
|
|
import hashlib
|
|
|
|
import hashlib
|
|
|
|
import argparse
|
|
|
|
import argparse
|
|
|
|
import configparser
|
|
|
|
import configparser
|
|
|
|
import urllib.parse
|
|
|
|
|
|
|
|
import urllib.request
|
|
|
|
import urllib.request
|
|
|
|
|
|
|
|
|
|
|
|
import gettext
|
|
|
|
import gettext
|
|
|
@ -17,7 +16,6 @@ from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Legacy Functions ##
|
|
|
|
## Legacy Functions ##
|
|
|
|
|
|
|
|
|
|
|
|
def convert_to_legacy_snapshot(snapshot):
|
|
|
|
def convert_to_legacy_snapshot(snapshot):
|
|
|
|
snapshot["sid"] = str(uuid.uuid4()).split("-")[0]
|
|
|
|
snapshot["sid"] = str(uuid.uuid4()).split("-")[0]
|
|
|
|
snapshot["software"] = "workbench-script"
|
|
|
|
snapshot["software"] = "workbench-script"
|
|
|
@ -27,8 +25,9 @@ def convert_to_legacy_snapshot(snapshot):
|
|
|
|
snapshot["timestamp"] = snapshot["timestamp"].replace(" ", "T")
|
|
|
|
snapshot["timestamp"] = snapshot["timestamp"].replace(" ", "T")
|
|
|
|
snapshot["data"]["smart"] = snapshot["data"]["disks"]
|
|
|
|
snapshot["data"]["smart"] = snapshot["data"]["disks"]
|
|
|
|
snapshot["data"].pop("disks")
|
|
|
|
snapshot["data"].pop("disks")
|
|
|
|
|
|
|
|
snapshot.pop("code")
|
|
|
|
snapshot.pop("erase")
|
|
|
|
snapshot.pop("erase")
|
|
|
|
|
|
|
|
|
|
|
|
## End Legacy Functions ##
|
|
|
|
## End Legacy Functions ##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -55,6 +54,11 @@ def exec_cmd_erase(cmd):
|
|
|
|
return ''
|
|
|
|
return ''
|
|
|
|
# return os.popen(cmd).read()
|
|
|
|
# return os.popen(cmd).read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def gen_code():
|
|
|
|
|
|
|
|
uid = str(uuid.uuid4()).encode('utf-8')
|
|
|
|
|
|
|
|
return hashlib.shake_256(uid).hexdigest(3)
|
|
|
|
|
|
|
|
|
|
|
|
## End Utility functions ##
|
|
|
|
## End Utility functions ##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -62,6 +66,7 @@ SNAPSHOT_BASE = {
|
|
|
|
'timestamp': str(datetime.now()),
|
|
|
|
'timestamp': str(datetime.now()),
|
|
|
|
'type': 'Snapshot',
|
|
|
|
'type': 'Snapshot',
|
|
|
|
'uuid': str(uuid.uuid4()),
|
|
|
|
'uuid': str(uuid.uuid4()),
|
|
|
|
|
|
|
|
'code': gen_code(),
|
|
|
|
'software': "workbench-script",
|
|
|
|
'software': "workbench-script",
|
|
|
|
'version': "0.0.1",
|
|
|
|
'version': "0.0.1",
|
|
|
|
'data': {},
|
|
|
|
'data': {},
|
|
|
@ -296,15 +301,6 @@ def save_snapshot_in_disk(snapshot, path):
|
|
|
|
# TODO sanitize url, if url is like this, it fails
|
|
|
|
# TODO sanitize url, if url is like this, it fails
|
|
|
|
# url = 'http://127.0.0.1:8000/api/snapshot/'
|
|
|
|
# url = 'http://127.0.0.1:8000/api/snapshot/'
|
|
|
|
def send_snapshot_to_devicehub(snapshot, token, url):
|
|
|
|
def send_snapshot_to_devicehub(snapshot, token, url):
|
|
|
|
url_components = urllib.parse.urlparse(url)
|
|
|
|
|
|
|
|
ev_path = "evidence/{}".format(snapshot["uuid"])
|
|
|
|
|
|
|
|
components = (url_components.schema, 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 = {
|
|
|
|
headers = {
|
|
|
|
"Authorization": f"Bearer {token}",
|
|
|
|
"Authorization": f"Bearer {token}",
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
"Content-Type": "application/json"
|
|
|
@ -314,17 +310,26 @@ def send_snapshot_to_devicehub(snapshot, token, url):
|
|
|
|
request = urllib.request.Request(url, data=data, headers=headers)
|
|
|
|
request = urllib.request.Request(url, data=data, headers=headers)
|
|
|
|
with urllib.request.urlopen(request) as response:
|
|
|
|
with urllib.request.urlopen(request) as response:
|
|
|
|
status_code = response.getcode()
|
|
|
|
status_code = response.getcode()
|
|
|
|
#response_text = response.read().decode('utf-8')
|
|
|
|
response_text = response.read().decode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
if 200 <= status_code < 300:
|
|
|
|
if 200 <= status_code < 300:
|
|
|
|
logger.info(_("Snapshot successfully sent to '%s'"), url)
|
|
|
|
logger.info(_("Snapshot successfully sent to '%s'"), url)
|
|
|
|
else:
|
|
|
|
|
|
|
|
logger.error(_("Snapshot cannot sent to '%s'"), url)
|
|
|
|
try:
|
|
|
|
|
|
|
|
response = json.loads(response_text)
|
|
|
|
|
|
|
|
if response.get('url'):
|
|
|
|
|
|
|
|
# apt install qrencode
|
|
|
|
|
|
|
|
qr = "echo {} | qrencode -t ANSI".format(response['url'])
|
|
|
|
|
|
|
|
print(exec_cmd(qr))
|
|
|
|
|
|
|
|
print("url: {}".format(response['url']))
|
|
|
|
|
|
|
|
if response.get("dhid"):
|
|
|
|
|
|
|
|
print("dhid: {}".format(response['dhid']))
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
|
|
logger.error(response_text)
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
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)
|
|
|
|
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"):
|
|
|
|
def load_config(config_file="settings.ini"):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Tries to load configuration from a config file.
|
|
|
|
Tries to load configuration from a config file.
|
|
|
|