|
| 1 | +import logging |
| 2 | +from datetime import timedelta |
| 3 | +from typing import ClassVar |
| 4 | + |
| 5 | +from core import taskmanager |
| 6 | +from core.schemas import entity, task |
| 7 | + |
| 8 | + |
| 9 | +class MalpediaMalware(task.FeedTask): |
| 10 | + _defaults = { |
| 11 | + "frequency": timedelta(days=1), |
| 12 | + "name": "Malpedia Malware", |
| 13 | + "description": "Gets list of Malpedia malware", |
| 14 | + "source": "https://malpedia.caad.fkie.fraunhofer.de/", |
| 15 | + } |
| 16 | + |
| 17 | + _SOURCE: ClassVar["str"] = ( |
| 18 | + "https://malpedia.caad.fkie.fraunhofer.de/api/get/families" |
| 19 | + ) |
| 20 | + |
| 21 | + def run(self): |
| 22 | + response = self._make_request(self._SOURCE) |
| 23 | + if not response: |
| 24 | + return |
| 25 | + families_json = response.json() |
| 26 | + for malware_name, entry in families_json.items(): |
| 27 | + self.analyze_entry(malware_name, entry) |
| 28 | + |
| 29 | + def analyze_entry(self, malware_name: str, entry: dict): |
| 30 | + """Analyzes an entry as specified in the malpedia json.""" |
| 31 | + |
| 32 | + if not entry.get("common_name"): |
| 33 | + return |
| 34 | + |
| 35 | + m = entity.Malware.find(name=entry["common_name"]) |
| 36 | + if not m: |
| 37 | + m = entity.Malware(name=entry["common_name"]) |
| 38 | + |
| 39 | + m.aliases = entry.get("aliases", []) |
| 40 | + refs = entry.get("urls", []) |
| 41 | + context = { |
| 42 | + "source": "Malpedia", |
| 43 | + "description": entry.get("description", ""), |
| 44 | + "external_references": "\n* " + "\n* ".join(refs), |
| 45 | + } |
| 46 | + m.family = entry.get("type", "") |
| 47 | + m = m.save() |
| 48 | + m.add_context(context["source"], context) |
| 49 | + attributions = entry.get("attribution", []) |
| 50 | + for attribution in attributions: |
| 51 | + intrusion_set = entity.IntrusionSet.find(name=attribution) |
| 52 | + if not intrusion_set: |
| 53 | + intrusion_set = entity.IntrusionSet(name=attribution).save() |
| 54 | + intrusion_set.link_to(m, "uses", "Malpedia") |
| 55 | + |
| 56 | + tags = [] |
| 57 | + if m.aliases: |
| 58 | + tags += m.aliases |
| 59 | + tags.append(m.name) |
| 60 | + tags.append(malware_name) |
| 61 | + m.tag(tags) |
| 62 | + |
| 63 | + |
| 64 | +class MalpediaActors(task.FeedTask): |
| 65 | + _defaults = { |
| 66 | + "frequency": timedelta(days=1), |
| 67 | + "name": "Malpedia Actors", |
| 68 | + "description": "Gets list of Malpedia actors", |
| 69 | + "source": "https://malpedia.caad.fkie.fraunhofer.de/", |
| 70 | + } |
| 71 | + |
| 72 | + _SOURCE: ClassVar["str"] = "https://malpedia.caad.fkie.fraunhofer.de/api/get/actors" |
| 73 | + |
| 74 | + def run(self): |
| 75 | + response = self._make_request(self._SOURCE) |
| 76 | + if not response: |
| 77 | + return |
| 78 | + actors_json = response.json() |
| 79 | + for actor_name, entry in actors_json.items(): |
| 80 | + self.analyze_entry(actor_name, entry) |
| 81 | + |
| 82 | + def analyze_entry(self, actor_name: str, entry: dict): |
| 83 | + intrusion_set = entity.IntrusionSet.find(name=entry["value"]) |
| 84 | + if not intrusion_set: |
| 85 | + intrusion_set = entity.IntrusionSet(name=entry["value"]) |
| 86 | + |
| 87 | + refs = entry.get("meta", {}).get("refs", []) |
| 88 | + context = { |
| 89 | + "source": "Malpedia", |
| 90 | + "description": entry.get("description", ""), |
| 91 | + "external_references": "\n* " + "\n* ".join(refs), |
| 92 | + } |
| 93 | + |
| 94 | + synonyms = entry.get("meta", {}).get("synonyms", []) |
| 95 | + |
| 96 | + if synonyms: |
| 97 | + intrusion_set.aliases = synonyms |
| 98 | + |
| 99 | + intrusion_set = intrusion_set.save() |
| 100 | + intrusion_set.add_context(context["source"], context) |
| 101 | + tags = [] |
| 102 | + |
| 103 | + if intrusion_set.aliases: |
| 104 | + tags += intrusion_set.aliases |
| 105 | + tags.append(intrusion_set.name) |
| 106 | + tags.append(actor_name) |
| 107 | + try: |
| 108 | + intrusion_set.tag(tags) |
| 109 | + except Exception as e: |
| 110 | + logging.error(f"Error tagging IntrusionSet {intrusion_set.name}: {e}") |
| 111 | + |
| 112 | + |
| 113 | +taskmanager.TaskManager.register_task(MalpediaActors) |
| 114 | +taskmanager.TaskManager.register_task(MalpediaMalware) |
0 commit comments