Skip to content

Commit 2926c7a

Browse files
federicofantinipranjalg1331
authored andcommitted
Added timeout parameter malware bazaar end threatfox (intelowlproject#2691)
* added limit parameter * fixed reverse migrate
1 parent af952d4 commit 2926c7a

File tree

2 files changed

+89
-8
lines changed

2 files changed

+89
-8
lines changed

api_app/ingestors_manager/ingestors/malware_bazaar.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class MalwareBazaar(AbuseCHMixin, Ingestor):
2323
hours: int
2424
# Download samples from chosen signatures (aka malware families)
2525
signatures: str
26+
# Max number of results you want to display
27+
limit: int
2628

2729
@classmethod
2830
def update(cls) -> bool:
@@ -32,7 +34,7 @@ def update(cls) -> bool:
3234
def get_signature_information(self, signature):
3335
result = requests.post(
3436
self.url,
35-
data={"query": "get_siginfo", "signature": signature, "limit": 100},
37+
data={"query": "get_siginfo", "signature": signature, "limit": self.limit},
3638
headers=self.authentication_header,
3739
timeout=30,
3840
)
@@ -53,20 +55,19 @@ def get_recent_samples(self):
5355
current_time = timezone.now()
5456
for signature in self.signatures:
5557
data = self.get_signature_information(signature)
58+
hours_str = "hour" if self.hours == 1 else "hours"
59+
if len(data) > self.limit:
60+
logger.info(
61+
f"{signature}: in the last {hours_str} there are "
62+
f"more results than the limit {len(data)}/{self.limit}"
63+
)
5664
for elem in data:
5765
first_seen = timezone.make_aware(
5866
timezone.datetime.strptime(elem["first_seen"], "%Y-%m-%d %H:%M:%S")
5967
)
6068
diff = int((current_time - first_seen).total_seconds()) // 3600
6169
if elem["signature"] == signature and diff <= self.hours:
6270
hashes.add(elem["sha256_hash"])
63-
64-
last_hours_str = (
65-
"Last hour" if self.hours == 1 else f"Last {self.hours} hours"
66-
)
67-
logger.info(
68-
f"{last_hours_str} {signature} samples: {len(hashes)}/{len(data)}"
69-
)
7071
return hashes
7172

7273
def download_sample(self, h):
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from django.db import migrations
2+
3+
4+
def migrate(apps, schema_editor):
5+
Parameter = apps.get_model("api_app", "Parameter")
6+
PythonModule = apps.get_model("api_app", "PythonModule")
7+
IngestorConfig = apps.get_model("ingestors_manager", "IngestorConfig")
8+
PluginConfig = apps.get_model("api_app", "PluginConfig")
9+
10+
ingestors = [
11+
"malware_bazaar.MalwareBazaar",
12+
"threatfox.ThreatFox",
13+
]
14+
for ingestor in ingestors:
15+
module = PythonModule.objects.get(
16+
module=ingestor,
17+
base_path="api_app.ingestors_manager.ingestors",
18+
)
19+
p = Parameter.objects.create(
20+
name="limit",
21+
type="int",
22+
description="Max number of results.",
23+
is_secret=False,
24+
required=True,
25+
python_module=module,
26+
)
27+
p.full_clean()
28+
p.save()
29+
30+
ic = IngestorConfig.objects.get(name=ingestor.split(".")[1])
31+
pc = PluginConfig(
32+
value=20,
33+
ingestor_config=ic,
34+
for_organization=False,
35+
owner=None,
36+
parameter=p,
37+
)
38+
pc.full_clean()
39+
pc.save()
40+
41+
42+
def reverse_migrate(apps, schema_editor):
43+
Parameter = apps.get_model("api_app", "Parameter")
44+
PythonModule = apps.get_model("api_app", "PythonModule")
45+
IngestorConfig = apps.get_model("ingestors_manager", "IngestorConfig")
46+
PluginConfig = apps.get_model("api_app", "PluginConfig")
47+
48+
ingestors = [
49+
"malware_bazaar.MalwareBazaar",
50+
"threatfox.ThreatFox",
51+
]
52+
for ingestor in ingestors:
53+
module = PythonModule.objects.get(
54+
module=ingestor,
55+
base_path="api_app.ingestors_manager.ingestors",
56+
)
57+
ic = IngestorConfig.objects.get(name=ingestor.split(".")[1])
58+
p = Parameter.objects.get(
59+
name="limit",
60+
type="int",
61+
description="Max number of results.",
62+
is_secret=False,
63+
required=True,
64+
python_module=module,
65+
)
66+
PluginConfig.objects.get(
67+
parameter=p,
68+
ingestor_config=ic,
69+
).delete()
70+
p.delete()
71+
72+
73+
class Migration(migrations.Migration):
74+
atomic = False
75+
dependencies = [
76+
("api_app", "0065_job_mpnodesearch"),
77+
("ingestors_manager", "0026_alter_ingestor_config_malware_bazaar_threatfox"),
78+
]
79+
80+
operations = [migrations.RunPython(migrate, reverse_migrate)]

0 commit comments

Comments
 (0)