|
| 1 | +import io |
| 2 | +import base64 |
| 3 | +import zipfile |
| 4 | + |
| 5 | +from anyrun import RunTimeException |
| 6 | +from anyrun.connectors import SandboxConnector |
| 7 | +from anyrun.connectors.sandbox.operation_systems import WindowsConnector, LinuxConnector, AndroidConnector |
| 8 | + |
| 9 | +from anyrun_sandbox.config import Config |
| 10 | + |
| 11 | + |
| 12 | +class AnyRunSubmitter: |
| 13 | + """ |
| 14 | + Implements functionality for sending MISP entities to the ANY.RUN sandbox |
| 15 | + """ |
| 16 | + def __init__(self, request: dict) -> None: |
| 17 | + self._request = request |
| 18 | + self._config: dict = request.get("config") |
| 19 | + |
| 20 | + def submit(self) -> str: |
| 21 | + """ |
| 22 | + Configures ANY.RUN Sandbox environment and executes the analysis |
| 23 | +
|
| 24 | + :return: ANY.RUN Sandbox analysis uuid |
| 25 | + """ |
| 26 | + self._parse_config() |
| 27 | + self._sanitize_config() |
| 28 | + |
| 29 | + if self._os_type == "windows": |
| 30 | + with SandboxConnector.windows(self._token, Config.INTEGRATION) as connector: |
| 31 | + analysis_uuid = self._process_analysis(connector) |
| 32 | + elif self._os_type in ("ubuntu", "debian"): |
| 33 | + with SandboxConnector.linux(self._token, Config.INTEGRATION) as connector: |
| 34 | + analysis_uuid = self._process_analysis(connector) |
| 35 | + elif self._os_type == "android": |
| 36 | + with SandboxConnector.android(self._token, Config.INTEGRATION) as connector: |
| 37 | + analysis_uuid = self._process_analysis(connector) |
| 38 | + else: |
| 39 | + raise RunTimeException( |
| 40 | + f"Received invalid OS type: {self._os_type}. Supports: windows, ubuntu, debian, android" |
| 41 | + ) |
| 42 | + |
| 43 | + return analysis_uuid |
| 44 | + |
| 45 | + def _parse_config(self) -> None: |
| 46 | + """ |
| 47 | + Configures analysis options according to the chosen environment |
| 48 | +
|
| 49 | + """ |
| 50 | + self._token = self._config.pop("api_key", "") |
| 51 | + self._os_type = self._config.pop("os_type", "") |
| 52 | + |
| 53 | + if not any((self._token, self._os_type)): |
| 54 | + raise RunTimeException(f"ANYRUN Sandbox API-KEY and OS type must be specified.") |
| 55 | + |
| 56 | + if "url" in self._request: |
| 57 | + self._prepare_url_params() |
| 58 | + self._config["obj_url"] = self._request.get("url") |
| 59 | + elif "malware-sample" in self._request: |
| 60 | + self._prepare_file_params() |
| 61 | + self._prepare_file_content("malware-sample") |
| 62 | + elif "attachment" in self._request: |
| 63 | + self._prepare_file_params() |
| 64 | + self._prepare_file_content("attachment") |
| 65 | + else: |
| 66 | + raise RunTimeException("Received invalid Object. Supports: url, attachment, malware-sample.") |
| 67 | + |
| 68 | + def _process_analysis(self, connector: WindowsConnector | LinuxConnector | AndroidConnector) -> str: |
| 69 | + """ |
| 70 | + Executes analysis |
| 71 | +
|
| 72 | + :param connector: Instance of the ANY.RUN connector |
| 73 | + :return: ANY.RUN Sandbox analysis uuid |
| 74 | + """ |
| 75 | + connector.check_authorization() |
| 76 | + |
| 77 | + if "obj_url" in self._config: |
| 78 | + analysis_uuid = connector.run_url_analysis(**self._config) |
| 79 | + else: |
| 80 | + analysis_uuid = connector.run_file_analysis(**self._config) |
| 81 | + return analysis_uuid |
| 82 | + |
| 83 | + def _prepare_url_params(self) -> None: |
| 84 | + """ |
| 85 | + Prepares analysis configuration for the url submission |
| 86 | + """ |
| 87 | + if self._os_type != "windows": |
| 88 | + self._config.pop("env_version", "") |
| 89 | + self._config.pop("env_bitness", "") |
| 90 | + self._config.pop("env_type", "") |
| 91 | + |
| 92 | + self._config.pop("obj_ext_extension", "") |
| 93 | + self._config.pop("obj_ext_startfolder", "") |
| 94 | + self._config.pop("obj_ext_cmd", "") |
| 95 | + self._config.pop("obj_force_elevation", "") |
| 96 | + self._config.pop("run_as_root", "") |
| 97 | + |
| 98 | + |
| 99 | + def _prepare_file_params(self) -> None: |
| 100 | + """ |
| 101 | + Prepares analysis configuration for the file submission |
| 102 | + """ |
| 103 | + self._config.pop("obj_ext_browser", "") |
| 104 | + |
| 105 | + if self._os_type == "windows": |
| 106 | + self._config.pop("run_as_root", "") |
| 107 | + elif self._os_type in ("ubuntu", "debian"): |
| 108 | + self._config.pop("env_version", "") |
| 109 | + self._config.pop("env_bitness", "") |
| 110 | + self._config.pop("env_type", "") |
| 111 | + self._config.pop("obj_force_elevation", "") |
| 112 | + self._config["env_os"] = self._os_type |
| 113 | + elif self._os_type == "android": |
| 114 | + self._config.pop("env_version", "") |
| 115 | + self._config.pop("env_bitness", "") |
| 116 | + self._config.pop("env_type", "") |
| 117 | + self._config.pop("obj_force_elevation", "") |
| 118 | + self._config.pop("obj_ext_startfolder", "") |
| 119 | + self._config.pop("run_as_root", "") |
| 120 | + |
| 121 | + def _prepare_file_content(self, sample_type: str) -> None: |
| 122 | + """ |
| 123 | + Prepares file content to the analysis |
| 124 | +
|
| 125 | + :param sample_type: Attachment or malware-sample MISP entity |
| 126 | + """ |
| 127 | + if sample_type == "attachment": |
| 128 | + filename = self._request.get("attachment") |
| 129 | + file_content = self._extract_file_content(self._request.get("data")) |
| 130 | + else: |
| 131 | + filename = self._request.get("malware-sample").split("|", 1)[0] |
| 132 | + file_content = self._extract_file_content(self._request.get("data"), is_encoded=True) |
| 133 | + |
| 134 | + self._config["filename"] = filename |
| 135 | + self._config["file_content"] = file_content |
| 136 | + |
| 137 | + def _sanitize_config(self) -> None: |
| 138 | + """ |
| 139 | + Removes empty parameters from the analysis configuration |
| 140 | + """ |
| 141 | + temp_config = dict() |
| 142 | + |
| 143 | + for key, value in self._config.items(): |
| 144 | + if value is not None: |
| 145 | + temp_config[key] = value |
| 146 | + |
| 147 | + self._config = temp_config |
| 148 | + |
| 149 | + @staticmethod |
| 150 | + def _extract_file_content(file_content: str, is_encoded: bool = False) -> bytes: |
| 151 | + """ |
| 152 | + Extracts file content from the **malware-sample** MISP entity |
| 153 | +
|
| 154 | + :param file_content: Base64 file content |
| 155 | + :param is_encoded: Marks if **malware-sample** or **attachment** entity received |
| 156 | + :return: File bytes payload |
| 157 | + """ |
| 158 | + data = base64.b64decode(file_content) |
| 159 | + |
| 160 | + if is_encoded: |
| 161 | + with zipfile.ZipFile(io.BytesIO(data)) as file: |
| 162 | + data = file.read(file.namelist()[0], pwd=b"infected") |
| 163 | + |
| 164 | + return data |
0 commit comments