devicehub-django/evidence/parse.py

122 lines
3.6 KiB
Python
Raw Normal View History

2024-07-11 15:40:45 +00:00
import os
import json
import shutil
import hashlib
2024-07-01 10:17:23 +00:00
2024-07-11 15:40:45 +00:00
from datetime import datetime
2024-09-18 16:01:46 +00:00
from dmidecode import DMIParse
from evidence.models import Annotation
from evidence.xapian import index
2024-09-18 16:01:46 +00:00
from utils.constants import ALGOS, CHASSIS_DH
2024-07-11 15:40:45 +00:00
2024-09-23 11:44:04 +00:00
def get_network_cards(child, nets):
2024-09-26 08:10:15 +00:00
if child['id'] == 'network' and "PCI:" in child.get("businfo"):
2024-09-23 11:44:04 +00:00
nets.append(child)
if child.get('children'):
2024-09-23 12:14:30 +00:00
[get_network_cards(x, nets) for x in child['children']]
2024-09-23 11:44:04 +00:00
def get_mac(lshw):
nets = []
2024-09-25 17:04:29 +00:00
try:
get_network_cards(json.loads(lshw), nets)
except Exception as ss:
print("WARNING!! {}".format(ss))
return
2024-09-23 12:30:47 +00:00
nets_sorted = sorted(nets, key=lambda x: x['businfo'])
# This funcion get the network card integrated in motherboard
# integrate = [x for x in nets if "pci@0000:00:" in x.get('businfo', '')]
2024-09-23 11:44:04 +00:00
2024-09-23 12:30:47 +00:00
if nets_sorted:
return nets_sorted[0]['serial']
2024-09-23 11:44:04 +00:00
2024-09-20 16:05:29 +00:00
2024-07-11 15:40:45 +00:00
class Build:
2024-07-31 11:28:46 +00:00
def __init__(self, evidence_json, user, check=False):
2024-07-26 15:59:34 +00:00
self.json = evidence_json
2024-07-18 15:21:22 +00:00
self.uuid = self.json['uuid']
2024-07-11 15:40:45 +00:00
self.user = user
self.hid = None
2024-07-31 11:28:46 +00:00
self.generate_chids()
if check:
return
2024-07-11 15:40:45 +00:00
2024-07-15 14:23:14 +00:00
self.index()
2024-07-18 15:21:22 +00:00
self.create_annotations()
2024-07-11 15:40:45 +00:00
2024-07-15 14:23:14 +00:00
def index(self):
snap = json.dumps(self.json)
index(self.user.institution, self.uuid, snap)
2024-07-15 14:23:14 +00:00
2024-07-31 11:28:46 +00:00
def generate_chids(self):
self.algorithms = {
'hidalgo1': self.get_hid_14(),
}
2024-07-15 14:23:14 +00:00
def get_hid_14(self):
if self.json.get("software") == "workbench-script":
2024-09-18 16:01:46 +00:00
hid = self.get_hid(self.json)
else:
device = self.json['device']
manufacturer = device.get("manufacturer", '')
model = device.get("model", '')
chassis = device.get("chassis", '')
serial_number = device.get("serialNumber", '')
sku = device.get("sku", '')
hid = f"{manufacturer}{model}{chassis}{serial_number}{sku}"
2024-07-15 14:23:14 +00:00
return hashlib.sha3_256(hid.encode()).hexdigest()
2024-07-18 15:21:22 +00:00
def create_annotations(self):
2024-07-31 11:28:46 +00:00
for k, v in self.algorithms.items():
2024-07-18 15:21:22 +00:00
Annotation.objects.create(
uuid=self.uuid,
2024-09-18 16:01:46 +00:00
owner=self.user.institution,
user=self.user,
2024-07-18 15:21:22 +00:00
type=Annotation.Type.SYSTEM,
key=k,
value=v
)
2024-09-18 16:01:46 +00:00
def get_chassis_dh(self):
chassis = self.get_chassis()
lower_type = chassis.lower()
for k, v in CHASSIS_DH.items():
if lower_type in v:
return k
return self.default
def get_sku(self):
return self.dmi.get("System")[0].get("SKU Number", "n/a").strip()
def get_chassis(self):
return self.dmi.get("Chassis")[0].get("Type", '_virtual')
def get_hid(self, snapshot):
dmidecode_raw = snapshot["data"]["dmidecode"]
self.dmi = DMIParse(dmidecode_raw)
manufacturer = self.dmi.manufacturer().strip()
model = self.dmi.model().strip()
chassis = self.get_chassis_dh()
serial_number = self.dmi.serial_number()
sku = self.get_sku()
2024-09-23 12:14:30 +00:00
if not snapshot["data"].get('lshw'):
return f"{manufacturer}{model}{chassis}{serial_number}{sku}"
lshw = snapshot["data"]["lshw"]
# mac = get_mac2(hwinfo_raw) or ""
mac = get_mac(lshw) or ""
2024-09-20 16:22:27 +00:00
if not mac:
print(f"WARNING: Could not retrieve MAC address in snapshot {snapshot['uuid']}" )
# TODO generate system annotation for that snapshot
2024-09-20 17:48:53 +00:00
else:
print(f"{manufacturer}{model}{chassis}{serial_number}{sku}{mac}")
2024-09-18 16:01:46 +00:00
2024-09-20 16:05:29 +00:00
return f"{manufacturer}{model}{chassis}{serial_number}{sku}{mac}"