|
| 1 | +""" |
| 2 | +author: Mislav Sever |
| 3 | +
|
| 4 | +File Inspection Engine (FIE) |
| 5 | +A Python module for the ReversingLabs File Inspection Engine REST API. |
| 6 | +""" |
| 7 | + |
| 8 | +import requests |
| 9 | +from io import BytesIO |
| 10 | + |
| 11 | +from ReversingLabs.SDK.helper import DEFAULT_USER_AGENT, RESPONSE_CODE_ERROR_MAP, WrongInputError |
| 12 | + |
| 13 | + |
| 14 | +class FileInspectionEngine(object): |
| 15 | + |
| 16 | + __SCAN_ENDPOINT = "/scan" |
| 17 | + __REPORT_ENDPOINT = "/report" |
| 18 | + |
| 19 | + def __init__(self, host, verify=True, proxies=None, user_agent=DEFAULT_USER_AGENT): |
| 20 | + self._host = self.__validate_host(host) |
| 21 | + self._url = "{host}{{endpoint}}".format(host=self._host) |
| 22 | + self._verify = verify |
| 23 | + |
| 24 | + self._headers = {"User-Agent": user_agent} |
| 25 | + |
| 26 | + if proxies: |
| 27 | + if not isinstance(proxies, dict): |
| 28 | + raise WrongInputError("proxies parameter must be a dictionary.") |
| 29 | + if len(proxies) == 0: |
| 30 | + raise WrongInputError("proxies parameter can not be an empty dictionary.") |
| 31 | + self._proxies = proxies |
| 32 | + |
| 33 | + @staticmethod |
| 34 | + def __validate_host(host): |
| 35 | + """Returns a formatted host URL including the protocol prefix. |
| 36 | + :param host: URL string |
| 37 | + :type host: str |
| 38 | + :returns: formatted URL string |
| 39 | + :rtype: str |
| 40 | + """ |
| 41 | + if not isinstance(host, str): |
| 42 | + raise WrongInputError("host parameter must be string.") |
| 43 | + |
| 44 | + if not host.startswith(("http://", "https://")): |
| 45 | + raise WrongInputError("host parameter must contain a protocol definition at the beginning.") |
| 46 | + |
| 47 | + host = host.rstrip("/") |
| 48 | + |
| 49 | + return host |
| 50 | + |
| 51 | + def test_connection(self): |
| 52 | + """Creates a lightweight request towards the FIE scan API to test the connection. |
| 53 | + """ |
| 54 | + fake_file = BytesIO(b'this is a sample text') |
| 55 | + |
| 56 | + response = self.scan_using_open_file( |
| 57 | + file_source=fake_file |
| 58 | + ) |
| 59 | + |
| 60 | + self.__raise_on_error(response) |
| 61 | + |
| 62 | + return |
| 63 | + |
| 64 | + def scan_using_file_path(self, file_path): |
| 65 | + """Sends a file to the FIE for inspection and returns a simple verdict in the submit response. |
| 66 | + Uses a file path string as input. |
| 67 | + :param file_path: local path to the file |
| 68 | + :type file_path: str |
| 69 | + :return: response |
| 70 | + :rtype: requests.Response |
| 71 | + """ |
| 72 | + if not isinstance(file_path, str): |
| 73 | + raise WrongInputError("file_path must be a string.") |
| 74 | + |
| 75 | + try: |
| 76 | + file_handle = open(file_path, "rb") |
| 77 | + except IOError as error: |
| 78 | + raise WrongInputError("Error while opening file in 'rb' mode - {error}".format(error=str(error))) |
| 79 | + |
| 80 | + response = self.__upload_file( |
| 81 | + file_source=file_handle, |
| 82 | + endpoint=self.__SCAN_ENDPOINT |
| 83 | + ) |
| 84 | + |
| 85 | + return response |
| 86 | + |
| 87 | + def scan_using_open_file(self, file_source): |
| 88 | + """Sends a file to the FIE for inspection and returns a simple verdict in the submit response. |
| 89 | + Uses an open file handle as input. |
| 90 | + :param file_source: open file in rb mode |
| 91 | + :type file_source: file or BinaryIO |
| 92 | + :return: response |
| 93 | + :rtype: requests.Response |
| 94 | + """ |
| 95 | + response = self.__upload_file( |
| 96 | + file_source=file_source, |
| 97 | + endpoint=self.__SCAN_ENDPOINT |
| 98 | + ) |
| 99 | + |
| 100 | + return response |
| 101 | + |
| 102 | + def report_using_file_path(self, file_path): |
| 103 | + """Sends a file to the FIE for inspection and returns a more complex analysis report in the submit response. |
| 104 | + Uses a file path string as input. |
| 105 | + :param file_path: local path to the file |
| 106 | + :type file_path: str |
| 107 | + :return: response |
| 108 | + :rtype: requests.Response |
| 109 | + """ |
| 110 | + if not isinstance(file_path, str): |
| 111 | + raise WrongInputError("file_path must be a string.") |
| 112 | + |
| 113 | + try: |
| 114 | + file_handle = open(file_path, "rb") |
| 115 | + except IOError as error: |
| 116 | + raise WrongInputError("Error while opening file in 'rb' mode - {error}".format(error=str(error))) |
| 117 | + |
| 118 | + response = self.__upload_file( |
| 119 | + file_source=file_handle, |
| 120 | + endpoint=self.__REPORT_ENDPOINT |
| 121 | + ) |
| 122 | + |
| 123 | + return response |
| 124 | + |
| 125 | + def report_using_open_file(self, file_source): |
| 126 | + """Sends a file to the FIE for inspection and returns a more complex analysis report in the submit response. |
| 127 | + Uses an open file handle as input. |
| 128 | + :param file_source: open file in rb mode |
| 129 | + :type file_source: file or BinaryIO |
| 130 | + :return: response |
| 131 | + :rtype: requests.Response |
| 132 | + """ |
| 133 | + response = self.__upload_file( |
| 134 | + file_source=file_source, |
| 135 | + endpoint=self.__REPORT_ENDPOINT |
| 136 | + ) |
| 137 | + |
| 138 | + return response |
| 139 | + |
| 140 | + def __upload_file(self, file_source, endpoint): |
| 141 | + """Internal method for utilizing the FIE endpoints. |
| 142 | + :param file_source: open file in rb mode |
| 143 | + :type file_source: file or BinaryIO |
| 144 | + :param endpoint: endpoint string |
| 145 | + :type endpoint: str |
| 146 | + :return: response |
| 147 | + :rtype: requests.Response |
| 148 | + """ |
| 149 | + if not hasattr(file_source, "read"): |
| 150 | + raise WrongInputError("file_source parameter must be a file open in 'rb' mode.") |
| 151 | + |
| 152 | + url = self._url.format(endpoint=endpoint) |
| 153 | + |
| 154 | + response = requests.post( |
| 155 | + url=url, |
| 156 | + data=file_source, |
| 157 | + verify=self._verify, |
| 158 | + proxies=self._proxies, |
| 159 | + headers=self._headers |
| 160 | + ) |
| 161 | + |
| 162 | + self.__raise_on_error(response) |
| 163 | + |
| 164 | + return response |
| 165 | + |
| 166 | + @staticmethod |
| 167 | + def __raise_on_error(response): |
| 168 | + """Accepts a response object for validation and raises an exception if an error status code is received. |
| 169 | + :return: response |
| 170 | + :rtype: requests.Response |
| 171 | + """ |
| 172 | + exception = RESPONSE_CODE_ERROR_MAP.get(response.status_code, None) |
| 173 | + if not exception: |
| 174 | + return |
| 175 | + raise exception(response_object=response) |
0 commit comments