Skip to content

Commit 9308ca4

Browse files
[HARFANGLAB INCIDENTS] - Unsafe Alert Process attributes access when creating File (#3153)
1 parent aefa1bd commit 9308ca4

File tree

4 files changed

+89
-84
lines changed

4 files changed

+89
-84
lines changed

external-import/harfanglab-incidents/src/harfanglab_incidents_connector/client_api.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ def _request(self, **kwargs):
5050
)
5151
return None
5252

53-
def generate_alerts(self, since: datetime = None, threat_id: str = None):
53+
def generate_alerts(
54+
self, since: datetime | None = None, threat_id: str | None = None
55+
):
5456
"""
5557
Get lists of alerts from Harfanglab API.
5658
:param since: Minimum alerts creation datetime
@@ -91,7 +93,7 @@ def generate_alerts(self, since: datetime = None, threat_id: str = None):
9193
else:
9294
url = None
9395

94-
def generate_threats(self, since: datetime = None):
96+
def generate_threats(self, since: datetime | None = None):
9597
"""
9698
Get lists of threats from Harfanglab API.
9799
:param since: Minimum threats creation datetime

external-import/harfanglab-incidents/src/harfanglab_incidents_connector/connector.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def _set_state_last_datetime(self, value: datetime):
127127
self.helper.set_state(state)
128128

129129
def _collect_incident_intelligence(
130-
self, threat: harfanglab.Threat = None
130+
self, threat: harfanglab.Threat | None = None
131131
) -> list[opencti.BaseModel]:
132132
"""
133133
Collect intelligence from Harfanglab and convert into STIX object

external-import/harfanglab-incidents/src/harfanglab_incidents_connector/converter_to_stix.py

+35-32
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ def __init__(self, helper, config):
2828
MARKING_DEFINITIONS_BY_NAME["TLP:CLEAR"],
2929
)
3030

31-
def _create_directory(
32-
self, process: harfanglab.Process = None
33-
) -> opencti.Directory:
31+
def _create_directory(self, process: harfanglab.Process) -> opencti.Directory:
3432
"""
3533
Create a Directory (STIX2.1 observable, aka SCO) for a given alert's process.
3634
:param process: Process found in a Harfanglab alert
@@ -43,7 +41,7 @@ def _create_directory(
4341
)
4442
return octi_directory
4543

46-
def _create_domain_name(self, ioc: harfanglab.IocRule = None) -> opencti.DomainName:
44+
def _create_domain_name(self, ioc: harfanglab.IocRule) -> opencti.DomainName:
4745
"""
4846
Create a DomainName (STIX2.1 observable, aka SCO) for a given ioc.
4947
:param ioc: Indicator from Harfanglab
@@ -56,7 +54,7 @@ def _create_domain_name(self, ioc: harfanglab.IocRule = None) -> opencti.DomainN
5654
)
5755
return octi_domain_name
5856

59-
def _create_file(self, process: harfanglab.Process = None) -> opencti.File:
57+
def _create_file(self, process: harfanglab.Process) -> opencti.File:
6058
"""
6159
Create a File (STIX2.1 observable, aka SCO) for a given ioc.
6260
:param process: Process found in a Harfanglab alert
@@ -71,7 +69,7 @@ def _create_file(self, process: harfanglab.Process = None) -> opencti.File:
7169
)
7270
return octi_file
7371

74-
def _create_hostname(self, agent: harfanglab.Agent = None) -> opencti.Hostname:
72+
def _create_hostname(self, agent: harfanglab.Agent) -> opencti.Hostname:
7573
"""
7674
Create a Hostname (custom observable, extension of STIX 2.1 observables) for a given alert's agent.
7775
:param agent: Agent found in a Harfanglab alert
@@ -84,7 +82,7 @@ def _create_hostname(self, agent: harfanglab.Agent = None) -> opencti.Hostname:
8482
)
8583
return octi_hostname
8684

87-
def _create_ipv4(self, ioc: harfanglab.IocRule = None) -> opencti.IPv4:
85+
def _create_ipv4(self, ioc: harfanglab.IocRule) -> opencti.IPv4:
8886
"""
8987
Create an IPv4Address (STIX2.1 observable, aka SCO) for a given ioc.
9088
:param ioc: Indicator from Harfanglab
@@ -97,7 +95,7 @@ def _create_ipv4(self, ioc: harfanglab.IocRule = None) -> opencti.IPv4:
9795
)
9896
return octi_ipv4
9997

100-
def _create_ipv6(self, ioc: harfanglab.IocRule = None) -> opencti.IPv6:
98+
def _create_ipv6(self, ioc: harfanglab.IocRule) -> opencti.IPv6:
10199
"""
102100
Create an IPv6Address (STIX2.1 observable, aka SCO) for a given ioc.
103101
:param ioc: Indicator from Harfanglab
@@ -110,7 +108,7 @@ def _create_ipv6(self, ioc: harfanglab.IocRule = None) -> opencti.IPv6:
110108
)
111109
return octi_ipv6
112110

113-
def _create_url(self, ioc: harfanglab.IocRule = None) -> opencti.Url:
111+
def _create_url(self, ioc: harfanglab.IocRule) -> opencti.Url:
114112
"""
115113
Create a URL (STIX2.1 observable, aka SCO) for a given ioc.
116114
:param ioc: Indicator from Harfanglab
@@ -123,9 +121,7 @@ def _create_url(self, ioc: harfanglab.IocRule = None) -> opencti.Url:
123121
)
124122
return octi_url
125123

126-
def _create_user_account(
127-
self, process: harfanglab.Process = None
128-
) -> opencti.UserAccount:
124+
def _create_user_account(self, process: harfanglab.Process) -> opencti.UserAccount:
129125
"""
130126
Create a UserAccount (STIX2.1 observable, aka SCO) for a given alert's process.
131127
:param process: Process found in a Harfanglab alert
@@ -149,7 +145,7 @@ def create_author(self) -> opencti.Author:
149145
)
150146
return octi_author
151147

152-
def create_attack_pattern(self, technique_tag: str = None) -> opencti.AttackPattern:
148+
def create_attack_pattern(self, technique_tag: str) -> opencti.AttackPattern:
153149
"""
154150
Create an AttackPattern (STIX 2.1 domain object, aka SDO) for a given technique.
155151
:param technique_tag: A Yara signature's technique tag
@@ -168,8 +164,8 @@ def create_attack_pattern(self, technique_tag: str = None) -> opencti.AttackPatt
168164

169165
def create_case_incident(
170166
self,
171-
threat: harfanglab.Threat = None,
172-
object_refs: list[opencti.BaseModel] = None,
167+
threat: harfanglab.Threat,
168+
object_refs: list[opencti.BaseModel] | None = None,
173169
) -> opencti.CaseIncident:
174170
incident_priority = INCIDENT_PRIORITIES_BY_LEVEL[threat.level]
175171
incident_top_agent = threat.top_agents[0]
@@ -179,7 +175,9 @@ def create_case_incident(
179175
description=f"Incident from {self.helper.connect_name}",
180176
severity=threat.level,
181177
priority=incident_priority,
182-
object_refs=[object_ref.id for object_ref in object_refs],
178+
object_refs=(
179+
[object_ref.id for object_ref in object_refs] if object_refs else []
180+
),
183181
author=self.author,
184182
created_at=threat.created_at,
185183
object_marking_refs=[self.marking_definition.id],
@@ -195,10 +193,10 @@ def create_case_incident(
195193

196194
def create_incident(
197195
self,
198-
alert: harfanglab.Alert = None,
196+
alert: harfanglab.Alert,
199197
alert_intelligence: (
200198
harfanglab.IocRule | harfanglab.SigmaRule | harfanglab.YaraSignature
201-
) = None,
199+
) | None = None,
202200
) -> opencti.Incident:
203201
"""
204202
Create an Incident (STIX 2.1 domain object, aka SDO) for a given Harfanglab alert and its corresponding ioc.
@@ -240,10 +238,10 @@ def create_incident(
240238

241239
def create_indicator(
242240
self,
243-
alert: harfanglab.Alert = None,
241+
alert: harfanglab.Alert,
244242
alert_intelligence: (
245243
harfanglab.IocRule | harfanglab.SigmaRule | harfanglab.YaraSignature
246-
) = None,
244+
) | None = None,
247245
) -> opencti.Indicator:
248246
"""
249247
Create an Indicator (STIX 2.1 domain object, aka SDO) from a Harfanglab alert and its corresponding IOC, Sigma rule or Yara signature.
@@ -290,15 +288,17 @@ def create_indicator(
290288

291289
def create_note(
292290
self,
293-
threat_note: harfanglab.ThreatNote = None,
294-
object_refs: list[opencti.BaseModel] = None,
291+
threat_note: harfanglab.ThreatNote,
292+
object_refs: list[opencti.BaseModel] | None = None,
295293
) -> opencti.Note:
296294
case_incident = object_refs[0]
297295

298296
octi_note = opencti.Note(
299297
abstract=threat_note.title,
300298
content=threat_note.content,
301-
object_refs=[object_ref.id for object_ref in object_refs],
299+
object_refs=(
300+
[object_ref.id for object_ref in object_refs] if object_refs else []
301+
),
302302
author=self.author,
303303
created_at=threat_note.created_at,
304304
updated_at=threat_note.updated_at,
@@ -309,10 +309,10 @@ def create_note(
309309

310310
def create_observables(
311311
self,
312-
alert: harfanglab.Alert = None,
312+
alert: harfanglab.Alert,
313313
alert_intelligence: (
314314
harfanglab.IocRule | harfanglab.SigmaRule | harfanglab.YaraSignature
315-
) = None,
315+
) | None = None,
316316
):
317317
"""
318318
Create STIX 2.1 observables, aka SCO, from a Harfanglab alert and its corresponding IOC, Sigma rule or Yara signature.
@@ -337,8 +337,11 @@ def create_observables(
337337
observable = self._create_domain_name(alert_intelligence)
338338
case "url":
339339
observable = self._create_url(alert_intelligence)
340-
if isinstance(
341-
alert_intelligence, (harfanglab.SigmaRule, harfanglab.YaraSignature)
340+
if (
341+
isinstance(
342+
alert_intelligence, (harfanglab.SigmaRule, harfanglab.YaraSignature)
343+
)
344+
and alert.process is not None
342345
):
343346
observable = self._create_file(alert.process)
344347

@@ -355,8 +358,8 @@ def create_observables(
355358

356359
def create_sighting(
357360
self,
358-
alert: harfanglab.Alert = None,
359-
sighted_ref: opencti.BaseModel = None,
361+
alert: harfanglab.Alert,
362+
sighted_ref: opencti.BaseModel | None = None,
360363
) -> opencti.Sighting:
361364
"""
362365
Create a Sighting (STIX 2.1 relationship object, aka SRO) for an indicator sighted in a Harfanglab alert.
@@ -383,9 +386,9 @@ def create_sighting(
383386

384387
def create_relationship(
385388
self,
386-
relationship_type: str = None,
387-
source: opencti.BaseModel = None,
388-
target: opencti.BaseModel = None,
389+
relationship_type: str,
390+
source: opencti.BaseModel,
391+
target: opencti.BaseModel,
389392
) -> opencti.Relationship:
390393
"""
391394
Create a Relationship (STIX 2.1 relationship object, aka SRO).

0 commit comments

Comments
 (0)