2024-07-11 15:40:45 +00:00
|
|
|
import json
|
|
|
|
import hashlib
|
2024-10-25 15:36:13 +00:00
|
|
|
import logging
|
2024-07-01 10:17:23 +00:00
|
|
|
|
2024-09-18 16:01:46 +00:00
|
|
|
from dmidecode import DMIParse
|
2024-10-21 16:39:31 +00:00
|
|
|
|
2024-09-25 10:51:08 +00:00
|
|
|
from evidence.models import Annotation
|
2024-10-04 15:32:53 +00:00
|
|
|
from evidence.xapian import index
|
2024-10-25 15:36:13 +00:00
|
|
|
from utils.constants import CHASSIS_DH
|
2024-11-15 11:47:08 +00:00
|
|
|
from evidence.parse_details import get_inxi_key, get_inxi
|
2024-10-25 15:36:13 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger('django')
|
2024-07-11 15:40:45 +00:00
|
|
|
|
2024-11-11 06:41:08 +00:00
|
|
|
|
2024-11-15 11:47:08 +00:00
|
|
|
def get_mac(inxi):
|
|
|
|
nets = get_inxi_key(inxi, "Network")
|
|
|
|
networks = [(nets[i], nets[i + 1]) for i in range(0, len(nets) - 1, 2)]
|
2024-11-09 19:52:26 +00:00
|
|
|
|
2024-11-15 11:47:08 +00:00
|
|
|
for n, iface in networks:
|
|
|
|
if get_inxi(n, "port"):
|
|
|
|
return get_inxi(iface, 'mac')
|
2024-07-11 15:40:45 +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)
|
2024-10-04 15:32:53 +00:00
|
|
|
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):
|
2024-10-21 09:24:09 +00:00
|
|
|
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-10-25 15:36:13 +00:00
|
|
|
|
|
|
|
|
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-10-25 15:36:13 +00:00
|
|
|
annotation = Annotation.objects.filter(
|
|
|
|
uuid=self.uuid,
|
|
|
|
owner=self.user.institution,
|
|
|
|
type=Annotation.Type.SYSTEM,
|
|
|
|
)
|
|
|
|
|
|
|
|
if annotation:
|
2024-10-31 13:24:16 +00:00
|
|
|
txt = "Warning: Snapshot %s already registered (annotation exists)"
|
2024-10-31 09:14:02 +00:00
|
|
|
logger.warning(txt, self.uuid)
|
2024-10-25 15:36:13 +00:00
|
|
|
return
|
2024-07-18 15:21:22 +00:00
|
|
|
|
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-10-04 15:32:53 +00:00
|
|
|
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_hid(self, snapshot):
|
2024-11-15 11:47:08 +00:00
|
|
|
try:
|
2024-11-20 17:41:59 +00:00
|
|
|
self.inxi = json.loads(self.json["data"]["inxi"])
|
2024-11-15 11:47:08 +00:00
|
|
|
except Exception:
|
|
|
|
logger.error("No inxi in snapshot %s", self.uuid)
|
|
|
|
return ""
|
|
|
|
|
|
|
|
machine = get_inxi_key(self.inxi, 'Machine')
|
|
|
|
for m in machine:
|
|
|
|
system = get_inxi(m, "System")
|
|
|
|
if system:
|
|
|
|
manufacturer = system
|
|
|
|
model = get_inxi(m, "product")
|
|
|
|
serial_number = get_inxi(m, "serial")
|
|
|
|
chassis = get_inxi(m, "Type")
|
|
|
|
else:
|
|
|
|
sku = get_inxi(m, "part-nu")
|
|
|
|
|
|
|
|
mac = get_mac(self.inxi) or ""
|
2024-09-20 16:22:27 +00:00
|
|
|
if not mac:
|
2024-10-31 09:40:53 +00:00
|
|
|
txt = "Could not retrieve MAC address in snapshot %s"
|
|
|
|
logger.warning(txt, snapshot['uuid'])
|
2024-11-15 11:47:08 +00:00
|
|
|
return f"{manufacturer}{model}{chassis}{serial_number}{sku}"
|
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}"
|