Files

341 lines
10 KiB
Python

"""Persistence models for IoT PreTester 4.6 Demo."""
import json
from django.db import models
class Manufacturer(models.Model):
name = models.CharField(max_length=50, unique=True)
created = models.DateTimeField(auto_now_add=True)
def to_json(self):
return {"id": self.id, "name": self.name, "created": self.created.isoformat()}
class Meta:
db_table = "iot_manufacturer"
ordering = ["name"]
class DeviceType(models.Model):
device_type = models.CharField(max_length=50, unique=True)
created = models.DateTimeField(auto_now_add=True)
def to_json(self):
return {
"id": self.id,
"device_type": self.device_type,
"created": self.created.isoformat(),
}
class Meta:
db_table = "iot_device_type"
ordering = ["device_type"]
class Product(models.Model):
manufacturer = models.ForeignKey(Manufacturer, on_delete=models.PROTECT)
device_type = models.ForeignKey(DeviceType, on_delete=models.PROTECT)
product_name = models.CharField(max_length=100)
created = models.DateTimeField(auto_now_add=True)
def to_json(self):
return {
"id": self.id,
"manufacturer": self.manufacturer.to_json(),
"product_name": self.product_name,
"device_type": self.device_type.to_json(),
"created": self.created.isoformat(),
}
class Meta:
db_table = "iot_product"
ordering = ["product_name"]
constraints = [
models.UniqueConstraint(
fields=["manufacturer", "device_type", "product_name"],
name="unique_demo_product",
)
]
class Device(models.Model):
product = models.ForeignKey(Product, on_delete=models.PROTECT)
serial = models.CharField(max_length=100)
comment = models.CharField(max_length=255, blank=True, default="")
created = models.DateTimeField(auto_now_add=True)
def to_json(self):
return {
"id": self.id,
"product": self.product.to_json(),
"serial": self.serial,
"comment": self.comment,
"created": self.created.isoformat(),
}
class Meta:
db_table = "iot_device"
constraints = [
models.UniqueConstraint(
fields=["product", "serial"], name="unique_demo_device"
)
]
class Firmware(models.Model):
"""Firmware metadata associated with a capture; binaries are not analyzed."""
version = models.CharField(max_length=100)
product = models.ForeignKey(Product, on_delete=models.PROTECT)
devices = models.ManyToManyField(Device, through="FirmwareDevices")
created = models.DateTimeField(auto_now_add=True)
def to_json(self):
return {
"id": self.id,
"version": self.version,
"product": self.product_id,
"created": self.created.isoformat(),
}
class Meta:
db_table = "iot_firmware"
constraints = [
models.UniqueConstraint(
fields=["product", "version"], name="unique_product_firmware"
)
]
class FirmwareDevices(models.Model):
firmware = models.ForeignKey(Firmware, on_delete=models.CASCADE)
device = models.ForeignKey(Device, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = "iot_firmware_devices"
constraints = [
models.UniqueConstraint(
fields=["firmware", "device"], name="unique_device_firmware"
)
]
class File(models.Model):
firmware = models.ForeignKey(Firmware, on_delete=models.PROTECT)
device = models.ForeignKey(Device, on_delete=models.CASCADE)
filename = models.CharField(max_length=255)
ip_address = models.GenericIPAddressField()
status = models.CharField(max_length=50, default="pending")
# Last analysis failure, shown in the UI so nobody has to dig through logs first.
error_message = models.TextField(null=True, blank=True)
upload_time = models.DateTimeField(auto_now_add=True)
def to_json(self):
return {
"id": self.id,
"filename": self.filename,
"ip_address": self.ip_address,
"firmware_version": self.firmware.version,
"status": self.status,
"error_message": self.error_message,
"upload_time": self.upload_time.isoformat(),
}
class Meta:
db_table = "iot_files"
ordering = ["-upload_time"]
constraints = [
models.UniqueConstraint(
fields=["device", "firmware", "filename", "ip_address"],
name="unique_device_capture",
)
]
class RawDataset(models.Model):
file = models.OneToOneField(File, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = "iot_raw_dataset"
class HashRaw(models.Model):
raw_dataset = models.ForeignKey(RawDataset, on_delete=models.CASCADE)
hash_value = models.TextField()
created = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = "iot_raw_hash"
constraints = [
models.UniqueConstraint(
fields=["raw_dataset", "hash_value"], name="unique_raw_hash"
)
]
class UrlRaw(models.Model):
raw_dataset = models.ForeignKey(RawDataset, on_delete=models.CASCADE)
url = models.CharField(max_length=500)
created = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = "iot_raw_url"
constraints = [
models.UniqueConstraint(
fields=["raw_dataset", "url"], name="unique_raw_url"
)
]
class Tool(models.Model):
tool_name = models.CharField(max_length=100, unique=True)
version = models.CharField(max_length=50)
created = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = "iot_tool"
class Port(models.Model):
port = models.PositiveIntegerField(unique=True)
created = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = "iot_ports"
class TlsVersion(models.Model):
tls_version = models.CharField(max_length=50, unique=True)
created = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = "iot_tls_versions"
class CipherSuite(models.Model):
cipher_suite = models.CharField(max_length=100, unique=True)
created = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = "iot_cipher_suites"
class HashType(models.Model):
hash_type = models.CharField(max_length=100, unique=True)
created = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = "iot_hash_types"
class AnalyzedDataset(models.Model):
file = models.OneToOneField(File, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = "iot_analyzed_dataset"
class UrlAnalyzed(models.Model):
analyzed_dataset = models.ForeignKey(AnalyzedDataset, on_delete=models.CASCADE)
url = models.CharField(max_length=500)
tool = models.ForeignKey(Tool, on_delete=models.PROTECT)
ports = models.ManyToManyField(Port)
tls_versions = models.ManyToManyField(TlsVersion)
cipher_suites = models.ManyToManyField(CipherSuite)
created = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = "iot_analyzed_url"
constraints = [
models.UniqueConstraint(
fields=["analyzed_dataset", "url"], name="unique_analyzed_url"
)
]
class HashAnalyzed(models.Model):
analyzed_dataset = models.ForeignKey(AnalyzedDataset, on_delete=models.CASCADE)
hash_type = models.ManyToManyField(HashType)
hash_value = models.TextField()
cracked = models.BooleanField(default=False)
cracked_value = models.CharField(max_length=255, blank=True, null=True)
crack_time = models.CharField(max_length=50, blank=True, null=True)
tool = models.ForeignKey(Tool, on_delete=models.PROTECT)
created = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = "iot_analyzed_hash"
constraints = [
models.UniqueConstraint(
fields=["analyzed_dataset", "hash_value"], name="unique_analyzed_hash"
)
]
class FindingReport(models.Model):
file = models.OneToOneField(File, on_delete=models.CASCADE)
result_json = models.TextField()
finding_count = models.IntegerField(default=0)
max_severity = models.CharField(max_length=20, default="info")
created = models.DateTimeField(auto_now_add=True)
def to_json(self):
return {
"id": self.id,
"file_id": self.file_id,
"finding_count": self.finding_count,
"max_severity": self.max_severity,
"created": self.created.isoformat(),
"result": json.loads(self.result_json),
}
class Meta:
db_table = "iot_finding_report"
ordering = ["-created"]
class DeepAnalysisJob(models.Model):
STATE_CHOICES = [
("pending", "Pending"),
("running", "Running"),
("done", "Done"),
("error", "Error"),
]
file = models.OneToOneField(File, on_delete=models.CASCADE)
state = models.CharField(max_length=20, choices=STATE_CHOICES, default="pending")
url_total = models.IntegerField(default=0)
url_done = models.IntegerField(default=0)
hash_total = models.IntegerField(default=0)
hash_done = models.IntegerField(default=0)
cracked = models.IntegerField(default=0)
message = models.TextField(blank=True, default="")
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def to_json(self):
done = self.url_done + self.hash_done
total = self.url_total + self.hash_total
return {
"file_id": self.file_id,
"state": self.state,
"url_total": self.url_total,
"url_done": self.url_done,
"hash_total": self.hash_total,
"hash_done": self.hash_done,
"cracked": self.cracked,
"progress_done": done,
"progress_total": total,
"percent": int(done * 100 / total) if total else (100 if self.state == "done" else 0),
"message": self.message,
"updated": self.updated.isoformat(),
}
class Meta:
db_table = "iot_deep_analysis_job"
ordering = ["-created"]