|
| 1 | +from django.db import migrations |
| 2 | +from django.db.models.fields.related_descriptors import ( |
| 3 | + ForwardManyToOneDescriptor, |
| 4 | + ForwardOneToOneDescriptor, |
| 5 | + ManyToManyDescriptor, |
| 6 | + ReverseManyToOneDescriptor, |
| 7 | + ReverseOneToOneDescriptor, |
| 8 | +) |
| 9 | + |
| 10 | +plugin = { |
| 11 | + "python_module": { |
| 12 | + "health_check_schedule": None, |
| 13 | + "update_schedule": None, |
| 14 | + "module": "ipqsfile.IPQSFileScan", |
| 15 | + "base_path": "api_app.analyzers_manager.file_analyzers", |
| 16 | + }, |
| 17 | + "name": "IPQS_Malware_File_Scanner", |
| 18 | + "description": "Scan files for malware, viruses, and malicious payloads in real-time using [IPQualityScore](https://www.ipqualityscore.com/)'s advanced file scanning engine.", |
| 19 | + "disabled": False, |
| 20 | + "soft_time_limit": 140, |
| 21 | + "routing_key": "default", |
| 22 | + "health_check_status": True, |
| 23 | + "type": "file", |
| 24 | + "docker_based": False, |
| 25 | + "maximum_tlp": "AMBER", |
| 26 | + "observable_supported": [], |
| 27 | + "supported_filetypes": [ |
| 28 | + "application/w-script-file", |
| 29 | + "application/javascript", |
| 30 | + "application/x-javascript", |
| 31 | + "text/javascript", |
| 32 | + "application/x-vbscript", |
| 33 | + "text/x-ms-iqy", |
| 34 | + "application/vnd.android.package-archive", |
| 35 | + "application/x-dex", |
| 36 | + "application/onenote", |
| 37 | + "application/zip", |
| 38 | + "multipart/x-zip", |
| 39 | + "application/java-archive", |
| 40 | + "text/rtf", |
| 41 | + "application/rtf", |
| 42 | + "application/x-sharedlib", |
| 43 | + "application/vnd.microsoft.portable-executable", |
| 44 | + "application/x-elf", |
| 45 | + "application/octet-stream", |
| 46 | + "application/vnd.tcpdump.pcap", |
| 47 | + "application/pdf", |
| 48 | + "text/html", |
| 49 | + "application/x-mspublisher", |
| 50 | + "application/vnd.ms-excel.addin.macroEnabled", |
| 51 | + "application/vnd.ms-excel.sheet.macroEnabled.12", |
| 52 | + "application/vnd.ms-excel", |
| 53 | + "application/excel", |
| 54 | + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", |
| 55 | + "application/xml", |
| 56 | + "text/xml", |
| 57 | + "application/encrypted", |
| 58 | + "text/plain", |
| 59 | + "text/csv", |
| 60 | + "application/vnd.openxmlformats-officedocument.presentationml.presentation", |
| 61 | + "application/msword", |
| 62 | + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
| 63 | + "application/vnd.ms-powerpoint", |
| 64 | + "application/vnd.ms-office", |
| 65 | + "application/x-binary", |
| 66 | + "application/x-macbinary", |
| 67 | + "application/mac-binary", |
| 68 | + "application/x-mach-binary", |
| 69 | + "application/x-zip-compressed", |
| 70 | + "application/x-compressed", |
| 71 | + "application/vnd.ms-outlook", |
| 72 | + "message/rfc822", |
| 73 | + "application/pkcs7-signature", |
| 74 | + "application/x-pkcs7-signature", |
| 75 | + "multipart/mixed", |
| 76 | + "text/x-shellscript", |
| 77 | + "application/x-chrome-extension", |
| 78 | + "application/json", |
| 79 | + "application/x-executable", |
| 80 | + "text/x-java", |
| 81 | + "text/x-kotlin", |
| 82 | + "text/x-swift", |
| 83 | + "text/x-objective-c", |
| 84 | + "application/x-ms-shortcut", |
| 85 | + "application/gzip", |
| 86 | + ], |
| 87 | + "run_hash": False, |
| 88 | + "run_hash_type": "", |
| 89 | + "not_supported_filetypes": [], |
| 90 | + "mapping_data_model": {}, |
| 91 | + "model": "analyzers_manager.AnalyzerConfig", |
| 92 | +} |
| 93 | + |
| 94 | +params = [ |
| 95 | + { |
| 96 | + "python_module": { |
| 97 | + "module": "ipqsfile.IPQSFileScan", |
| 98 | + "base_path": "api_app.analyzers_manager.file_analyzers", |
| 99 | + }, |
| 100 | + "name": "ipqs_api_key", |
| 101 | + "type": "str", |
| 102 | + "description": "Please provide the IPQS API key.", |
| 103 | + "is_secret": True, |
| 104 | + "required": True, |
| 105 | + }, |
| 106 | + { |
| 107 | + "python_module": { |
| 108 | + "module": "ipqsfile.IPQSFileScan", |
| 109 | + "base_path": "api_app.analyzers_manager.file_analyzers", |
| 110 | + }, |
| 111 | + "name": "polling_interval", |
| 112 | + "type": "int", |
| 113 | + "description": "Recommended polling interval: 10 seconds.", |
| 114 | + "is_secret": False, |
| 115 | + "required": True, |
| 116 | + }, |
| 117 | + { |
| 118 | + "python_module": { |
| 119 | + "module": "ipqsfile.IPQSFileScan", |
| 120 | + "base_path": "api_app.analyzers_manager.file_analyzers", |
| 121 | + }, |
| 122 | + "name": "max_retries", |
| 123 | + "type": "int", |
| 124 | + "description": "Recommended max retries: 8.", |
| 125 | + "is_secret": False, |
| 126 | + "required": True, |
| 127 | + }, |
| 128 | +] |
| 129 | + |
| 130 | +values = [] |
| 131 | + |
| 132 | + |
| 133 | +def _get_real_obj(Model, field, value): |
| 134 | + def _get_obj(Model, other_model, value): |
| 135 | + if isinstance(value, dict): |
| 136 | + real_vals = {} |
| 137 | + for key, real_val in value.items(): |
| 138 | + real_vals[key] = _get_real_obj(other_model, key, real_val) |
| 139 | + value = other_model.objects.get_or_create(**real_vals)[0] |
| 140 | + # it is just the primary key serialized |
| 141 | + else: |
| 142 | + if isinstance(value, int): |
| 143 | + if Model.__name__ == "PluginConfig": |
| 144 | + value = other_model.objects.get(name=plugin["name"]) |
| 145 | + else: |
| 146 | + value = other_model.objects.get(pk=value) |
| 147 | + else: |
| 148 | + value = other_model.objects.get(name=value) |
| 149 | + return value |
| 150 | + |
| 151 | + if ( |
| 152 | + type(getattr(Model, field)) |
| 153 | + in [ |
| 154 | + ForwardManyToOneDescriptor, |
| 155 | + ReverseManyToOneDescriptor, |
| 156 | + ReverseOneToOneDescriptor, |
| 157 | + ForwardOneToOneDescriptor, |
| 158 | + ] |
| 159 | + and value |
| 160 | + ): |
| 161 | + other_model = getattr(Model, field).get_queryset().model |
| 162 | + value = _get_obj(Model, other_model, value) |
| 163 | + elif type(getattr(Model, field)) in [ManyToManyDescriptor] and value: |
| 164 | + other_model = getattr(Model, field).rel.model |
| 165 | + value = [_get_obj(Model, other_model, val) for val in value] |
| 166 | + return value |
| 167 | + |
| 168 | + |
| 169 | +def _create_object(Model, data): |
| 170 | + mtm, no_mtm = {}, {} |
| 171 | + for field, value in data.items(): |
| 172 | + value = _get_real_obj(Model, field, value) |
| 173 | + if type(getattr(Model, field)) is ManyToManyDescriptor: |
| 174 | + mtm[field] = value |
| 175 | + else: |
| 176 | + no_mtm[field] = value |
| 177 | + try: |
| 178 | + o = Model.objects.get(**no_mtm) |
| 179 | + except Model.DoesNotExist: |
| 180 | + o = Model(**no_mtm) |
| 181 | + o.full_clean() |
| 182 | + o.save() |
| 183 | + for field, value in mtm.items(): |
| 184 | + attribute = getattr(o, field) |
| 185 | + if value is not None: |
| 186 | + attribute.set(value) |
| 187 | + return False |
| 188 | + return True |
| 189 | + |
| 190 | + |
| 191 | +def migrate(apps, schema_editor): |
| 192 | + Parameter = apps.get_model("api_app", "Parameter") |
| 193 | + PluginConfig = apps.get_model("api_app", "PluginConfig") |
| 194 | + python_path = plugin.pop("model") |
| 195 | + Model = apps.get_model(*python_path.split(".")) |
| 196 | + if not Model.objects.filter(name=plugin["name"]).exists(): |
| 197 | + exists = _create_object(Model, plugin) |
| 198 | + if not exists: |
| 199 | + for param in params: |
| 200 | + _create_object(Parameter, param) |
| 201 | + for value in values: |
| 202 | + _create_object(PluginConfig, value) |
| 203 | + |
| 204 | + |
| 205 | +def reverse_migrate(apps, schema_editor): |
| 206 | + python_path = plugin.pop("model") |
| 207 | + Model = apps.get_model(*python_path.split(".")) |
| 208 | + Model.objects.get(name=plugin["name"]).delete() |
| 209 | + |
| 210 | + |
| 211 | +class Migration(migrations.Migration): |
| 212 | + atomic = False |
| 213 | + dependencies = [ |
| 214 | + ("api_app", "0073_alter_updatecheckstatus_last_checked_at_and_more"), |
| 215 | + ("analyzers_manager", "0190_remove_greynoise_labs_analyzer"), |
| 216 | + ] |
| 217 | + |
| 218 | + operations = [migrations.RunPython(migrate, reverse_migrate)] |
0 commit comments