|
1 | 1 | import uuid |
2 | 2 |
|
| 3 | +import logging |
3 | 4 | from app.database import Base |
4 | 5 | from app.models.event import DistributionLevel |
5 | 6 | from sqlalchemy import BigInteger, Boolean, Column, Enum, ForeignKey, Integer, String |
6 | 7 | from sqlalchemy.dialects.postgresql import UUID |
7 | 8 | from sqlalchemy.orm import Mapped, mapped_column, relationship |
| 9 | +from app.services.minio import get_minio_client |
| 10 | +from app.settings import Settings, get_settings |
| 11 | + |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
8 | 14 |
|
9 | 15 |
|
10 | 16 | class Attribute(Base): |
@@ -37,3 +43,45 @@ class Attribute(Base): |
37 | 43 | last_seen = Column(BigInteger(), index=True) |
38 | 44 |
|
39 | 45 | tags = relationship("Tag", secondary="attribute_tags", lazy="subquery") |
| 46 | + |
| 47 | + def to_misp_format( |
| 48 | + self, |
| 49 | + settings: Settings = get_settings(), |
| 50 | + ): |
| 51 | + """Convert the Attribute to a MISP-compatible dictionary representation.""" |
| 52 | + |
| 53 | + attr_json = { |
| 54 | + "id": self.id, |
| 55 | + "event_id": self.event_id, |
| 56 | + "object_id": self.object_id, |
| 57 | + "object_relation": self.object_relation, |
| 58 | + "category": self.category, |
| 59 | + "type": self.type, |
| 60 | + "value": self.value, |
| 61 | + "to_ids": self.to_ids, |
| 62 | + "uuid": str(self.uuid), |
| 63 | + "timestamp": self.timestamp, |
| 64 | + "distribution": self.distribution.name if self.distribution else None, |
| 65 | + "sharing_group_id": self.sharing_group_id, |
| 66 | + "comment": self.comment, |
| 67 | + "deleted": self.deleted, |
| 68 | + "disable_correlation": self.disable_correlation, |
| 69 | + "first_seen": self.first_seen, |
| 70 | + "last_seen": self.last_seen, |
| 71 | + "Tags": [tag.to_misp_format() for tag in self.tags], |
| 72 | + } |
| 73 | + |
| 74 | + # if its a file attribute, we need to handle it differently |
| 75 | + if self.type in ["malware-sample", "attachment"]: |
| 76 | + try: |
| 77 | + MinioClient = get_minio_client() |
| 78 | + data = MinioClient.get_object(settings.Storage.minio.bucket, self.uuid) |
| 79 | + file_content = data.read() |
| 80 | + file_b64 = file_content.encode("base64") |
| 81 | + attr_json["data"] = file_b64 |
| 82 | + except Exception as e: |
| 83 | + file_b64 = None |
| 84 | + logger.error(f"Error storing attachment: {str(e)}") |
| 85 | + print(f"Error fetching file from storage: {str(e)}") |
| 86 | + |
| 87 | + return attr_json |
0 commit comments