Nav apraksta

alerts.py 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # IRIS Source Code
  2. # Copyright (C) 2024 - DFIR-IRIS
  3. # contact@dfir-iris.org
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 3 of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program; if not, write to the Free Software Foundation,
  17. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. from datetime import datetime
  19. import uuid
  20. from sqlalchemy.dialects.postgresql import JSON
  21. from sqlalchemy import BigInteger
  22. from sqlalchemy import String
  23. from sqlalchemy import Column
  24. from sqlalchemy import DateTime
  25. from sqlalchemy import ForeignKey
  26. from sqlalchemy import Integer
  27. from sqlalchemy import Text
  28. from sqlalchemy import text
  29. from sqlalchemy.dialects.postgresql import UUID
  30. from sqlalchemy.orm import relationship
  31. from app import db
  32. from app.models.models import alert_assets_association
  33. from app.models.models import alert_iocs_association
  34. class AlertCaseAssociation(db.Model):
  35. __tablename__ = 'alert_case_association'
  36. alert_id = Column(ForeignKey('alerts.alert_id'), primary_key=True, nullable=False)
  37. case_id = Column(ForeignKey('cases.case_id'), primary_key=True, nullable=False, index=True)
  38. class Alert(db.Model):
  39. __tablename__ = 'alerts'
  40. alert_id = Column(BigInteger, primary_key=True)
  41. alert_uuid = Column(UUID(as_uuid=True), default=uuid.uuid4, nullable=False,
  42. server_default=text('gen_random_uuid()'), unique=True)
  43. alert_title = Column(Text, nullable=False)
  44. alert_description = Column(Text)
  45. alert_source = Column(Text)
  46. alert_source_ref = Column(Text)
  47. alert_source_link = Column(Text)
  48. alert_source_content = Column(JSON)
  49. alert_severity_id = Column(ForeignKey('severities.severity_id'), nullable=False)
  50. alert_status_id = Column(ForeignKey('alert_status.status_id'), nullable=False)
  51. alert_context = Column(JSON)
  52. alert_source_event_time = Column(DateTime, nullable=False, server_default=text("now()"))
  53. alert_creation_time = Column(DateTime, nullable=False, server_default=text("now()"))
  54. alert_note = Column(Text)
  55. alert_tags = Column(Text)
  56. alert_owner_id = Column(ForeignKey('user.id'))
  57. modification_history = Column(JSON)
  58. alert_customer_id = Column(ForeignKey('client.client_id'), nullable=False)
  59. alert_classification_id = Column(ForeignKey('case_classification.id'))
  60. alert_resolution_status_id = Column(ForeignKey('alert_resolution_status.resolution_status_id'), nullable=True)
  61. owner = relationship('User', foreign_keys=[alert_owner_id])
  62. severity = relationship('Severity')
  63. status = relationship('AlertStatus')
  64. customer = relationship('Client')
  65. classification = relationship('CaseClassification')
  66. resolution_status = relationship('AlertResolutionStatus')
  67. cases = relationship('Cases', secondary="alert_case_association", back_populates='alerts')
  68. comments = relationship('Comments', back_populates='alert', cascade='all, delete-orphan')
  69. assets = relationship('CaseAssets', secondary=alert_assets_association, back_populates='alerts')
  70. iocs = relationship('Ioc', secondary=alert_iocs_association, back_populates='alerts')
  71. class Severity(db.Model):
  72. __tablename__ = 'severities'
  73. severity_id = Column(Integer, primary_key=True)
  74. severity_name = Column(Text, nullable=False, unique=True)
  75. severity_description = Column(Text)
  76. class AlertStatus(db.Model):
  77. __tablename__ = 'alert_status'
  78. status_id = Column(Integer, primary_key=True)
  79. status_name = Column(Text, nullable=False, unique=True)
  80. status_description = Column(Text)
  81. class AlertResolutionStatus(db.Model):
  82. __tablename__ = 'alert_resolution_status'
  83. resolution_status_id = Column(Integer, primary_key=True)
  84. resolution_status_name = Column(Text, nullable=False, unique=True)
  85. resolution_status_description = Column(Text)
  86. class SimilarAlertsCache(db.Model):
  87. __tablename__ = 'similar_alerts_cache'
  88. id = Column(BigInteger, primary_key=True)
  89. customer_id = Column(BigInteger, ForeignKey('client.client_id'), nullable=False)
  90. asset_name = Column(Text, nullable=True)
  91. ioc_value = Column(Text, nullable=True)
  92. alert_id = Column(BigInteger, ForeignKey('alerts.alert_id'), nullable=False)
  93. created_at = Column(DateTime, nullable=False, server_default=text("now()"))
  94. asset_type_id = Column(Integer, ForeignKey('assets_type.asset_id'), nullable=True)
  95. ioc_type_id = Column(Integer, ForeignKey('ioc_type.type_id'), nullable=True)
  96. alert = relationship('Alert')
  97. customer = relationship('Client')
  98. asset_type = relationship('AssetsType')
  99. ioc_type = relationship('IocType')
  100. def __init__(self, customer_id, alert_id, asset_name=None, ioc_value=None, asset_type_id=None, ioc_type_id=None,
  101. created_at=None):
  102. self.customer_id = customer_id
  103. self.asset_name = asset_name
  104. self.ioc_value = ioc_value
  105. self.alert_id = alert_id
  106. self.asset_type_id = asset_type_id
  107. self.ioc_type_id = ioc_type_id
  108. self.created_at = created_at if created_at else datetime.utcnow()
  109. class AlertSimilarity(db.Model):
  110. __tablename__ = 'alert_similarity'
  111. id = Column(BigInteger, primary_key=True)
  112. alert_id = Column(BigInteger, ForeignKey('alerts.alert_id'), nullable=False)
  113. similar_alert_id = Column(BigInteger, ForeignKey('alerts.alert_id'), nullable=False)
  114. similarity_type = Column(String(255), nullable=True)
  115. matching_asset_id = Column(BigInteger, ForeignKey('case_assets.asset_id'), nullable=True)
  116. matching_ioc_id = Column(BigInteger, ForeignKey('ioc.ioc_id'), nullable=True)
  117. alert = relationship("Alert", foreign_keys=[alert_id])
  118. similar_alert = relationship("Alert", foreign_keys=[similar_alert_id])
  119. matching_asset = relationship("CaseAssets")
  120. matching_ioc = relationship("Ioc")