devicehub-django/evidence/parse.py

121 lines
3.4 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
2024-07-26 15:59:34 +00:00
from evidence.xapian import search, index
from evidence.models import Evidence, Annotation
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-20 16:05:29 +00:00
def get_mac(hwinfo):
low_ix = None
2024-09-20 17:47:04 +00:00
lnets = []
2024-09-20 16:05:29 +00:00
nets = [x.split("\n") for x in hwinfo.split("\n\n")
if "network interface" in x and "Attached to" in x]
for n in nets:
ix = None
2024-09-20 17:47:04 +00:00
mac = None
for l in n:
if "Attached to:" in l:
for v in l.split(" "):
if "#" in v:
ix = int(v.strip("#"))
if not low_ix:
low_ix = ix
2024-09-20 16:05:29 +00:00
2024-09-20 17:47:04 +00:00
if "HW Address:" in l:
mac = l.split(" ")[-1]
if ix and mac:
lnets.append((ix, mac))
if lnets:
lnets.sort()
mac = lnets[0][1]
print(f"MAC: {mac}")
return mac
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)
2024-07-18 15:21:22 +00:00
index(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):
2024-09-18 16:01:46 +00:00
if self.json.get("software") == "EreuseWorkbench":
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,
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-20 16:05:29 +00:00
hwinfo_raw = snapshot["data"]["hwinfo"]
mac = get_mac(hwinfo_raw) or ""
2024-09-20 16:22:27 +00:00
if not mac:
print("WARNING!! No there are MAC address")
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}"