├── .env ├── .github └── workflows │ └── pylint.yml ├── LICENSE ├── README.md ├── extract.py └── requirements.txt /.env: -------------------------------------------------------------------------------- 1 | OPENAI_API_KEY="REPLACEME" 2 | -------------------------------------------------------------------------------- /.github/workflows/pylint.yml: -------------------------------------------------------------------------------- 1 | name: Pylint 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | strategy: 9 | matrix: 10 | python-version: ["3.8", "3.9", "3.10"] 11 | steps: 12 | - uses: actions/checkout@v3 13 | - name: Set up Python ${{ matrix.python-version }} 14 | uses: actions/setup-python@v3 15 | with: 16 | python-version: ${{ matrix.python-version }} 17 | - name: Install dependencies 18 | run: | 19 | python -m pip install --upgrade pip 20 | pip install pylint 21 | - name: Analysing the code with pylint 22 | run: | 23 | pylint $(git ls-files '*.py') 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # aiocrioc 2 | An LLM and OCR based Indicator of Compromise Extraction Tool. 3 | Built as a POC to compare against straight regex and OCR see: [***ioc-ocr-extractor***](https://github.com/referefref/ioc-ocr-extractor) 4 | The LLM version does significantly better at understanding the context of indicators like domains and file extensions which are often confused with plain regex (such as .com) 5 | - As featured on [***GreyNoise Intelligence Storm Watch***](https://www.youtube.com/watch?v=sQpFLLZy4Yw), [***Risky Biz***](https://news.risky.biz/risky-biz-news-intellexa-pulls-new-predator-spyware-infra-after-thorough-undressing/), [***National Cyber Security Centre Newsletter***](https://ctoatncsc.substack.com/p/cto-at-ncsc-summary-week-ending-march-7ee) 6 | 7 | ## Contributions 8 | * Thanks to [***Marco Ramilli***](https://github.com/marcoramilli) for the addition of local file:// and embedded image parsing 9 | * Additional contributions are welcomed 10 | 11 | ## Setup (tested on Ubuntu 22.04 with python3-venv) 12 | ```bash 13 | # Download and install requirements 14 | apt install tesseract-ocr python3 python3-venv git -y 15 | # Clone git repo 16 | git clone https://github.com/referefref/aiocrioc.git 17 | cd aiocrioc 18 | # Setup python3 virtual environment 19 | python3 -m venv env 20 | source env/bin/activate 21 | # Install python requirements with pip 22 | pip install -r requirements.txt 23 | # Set your openai key in the .env file 24 | sed -i 's/REPLACEME/put your openai key here or edit the .env file/g' .env 25 | ``` 26 | 27 | ## Usage 28 | ```bash 29 | ./extractor.py --url "url" --output "outputfile.json" 30 | ``` 31 | 32 | ## Example 33 | ```bash 34 | extract.py --url "https://thedfirreport.com/2024/01/29/buzzing-on-christmas-eve-trigona-ransomware-in-3-hours/" --output "test.json" 35 | ``` 36 | ## Output 37 | ```json 38 | [ 39 | { 40 | "Indicator Type": "indicator type", 41 | "Indicator": "indicator", 42 | "Context": "context" 43 | }, 44 | { 45 | "Indicator Type": "IP address", 46 | "Indicator": "77.83.36.6", 47 | "Context": "initial RDP connection from Ukraine" 48 | }, 49 | { 50 | "Indicator Type": "IP address", 51 | "Indicator": "193.106.31.98", 52 | "Context": "second RDP connection" 53 | }, 54 | { 55 | "Indicator Type": "domain name", 56 | "Indicator": "Mega.io", 57 | "Context": "used for data exfiltration" 58 | }, 59 | { 60 | "Indicator Type": "file name", 61 | "Indicator": "build_redacted.exe", 62 | "Context": "Trigona Ransomware executable" 63 | }, 64 | { 65 | "Indicator Type": "SHA-256 hash", 66 | "Indicator": "d743daa22fdf4313a10da027b034c603eda255be037cb45b28faea23114d3b8a", 67 | "Context": "hash for build_redacted.exe" 68 | }, 69 | { 70 | "Indicator Type": "MD5 hash", 71 | "Indicator": "1852be15aa8dcf664291b3849bd348e4", 72 | "Context": "MD5 hash for build_redacted.exe" 73 | }, 74 | { 75 | "Indicator Type": "SHA-1 hash", 76 | "Indicator": "eea811d2a304101cc0b0edebe6590ea0f3da0a27", 77 | "Context": "SHA-1 hash for build_redacted.exe" 78 | }, 79 | { 80 | "Indicator Type": "file name", 81 | "Indicator": "DefenderOFF.bat", 82 | "Context": "batch script to disable Windows Defender" 83 | }, 84 | { 85 | "Indicator Type": "SHA-256 hash", 86 | "Indicator": "d6d8302d8db7f17aaa45059b60eb8de33166c95d1d833ca4d5061201e4737009", 87 | "Context": "hash for DefenderOFF.bat" 88 | }, 89 | { 90 | "Indicator Type": "MD5 hash", 91 | "Indicator": "c5d7ce243c1d735d9ca419cc916b87ec", 92 | "Context": "MD5 hash for DefenderOFF.bat" 93 | }, 94 | { 95 | "Indicator Type": "SHA-1 hash", 96 | "Indicator": "21b7460aa5f7eb7a064d2a7a6837da57719f9c2e", 97 | "Context": "SHA-1 hash for DefenderOFF.bat" 98 | }, 99 | { 100 | "Indicator Type": "file name", 101 | "Indicator": "newuser.bat", 102 | "Context": "script to create a new user named sys with password Taken1918" 103 | }, 104 | { 105 | "Indicator Type": "username", 106 | "Indicator": "sys", 107 | "Context": "created by newuser.bat" 108 | }, 109 | { 110 | "Indicator Type": "password", 111 | "Indicator": "Taken1918", 112 | "Context": "password for user sys created by newuser.bat" 113 | }, 114 | { 115 | "Indicator Type": "file name", 116 | "Indicator": "newnewuser.bat", 117 | "Context": "script to create a user named Support with password Kawa72ws" 118 | }, 119 | { 120 | "Indicator Type": "username", 121 | "Indicator": "Support", 122 | "Context": "created by newnewuser.bat" 123 | }, 124 | { 125 | "Indicator Type": "password", 126 | "Indicator": "Kawa72ws", 127 | "Context": "password for user Support created by newnewuser.bat" 128 | }, 129 | { 130 | "Indicator Type": "file name", 131 | "Indicator": "netscan.exe", 132 | "Context": "SoftPerfect\u2019s Netscan tool used for network discovery" 133 | }, 134 | { 135 | "Indicator Type": "SHA-256 hash", 136 | "Indicator": "18f0898d595ec054d13b02915fb7d3636f65b8e53c0c66b3c7ee3b6fc37d3566", 137 | "Context": "hash for netscan.exe" 138 | }, 139 | { 140 | "Indicator Type": "MD5 hash", 141 | "Indicator": "27f7186499bc8d10e51d17d3d6697bc5", 142 | "Context": "MD5 hash for netscan.exe" 143 | }, 144 | { 145 | "Indicator Type": "SHA-1 hash", 146 | "Indicator": "52332ce16ee0c393b8eea6e71863ad41e3caeafd", 147 | "Context": "SHA-1 hash for netscan.exe" 148 | }, 149 | { 150 | "Indicator Type": "file name", 151 | "Indicator": "rdp.exe", 152 | "Context": "Remote Desktop Plus tool dropped but not used" 153 | }, 154 | { 155 | "Indicator Type": "SHA-256 hash", 156 | "Indicator": "8cf27e05e639fcc273d3cceadf68e69573b58e74b4bfce8460a418366a782fbd", 157 | "Context": "hash for rdp.exe" 158 | }, 159 | { 160 | "Indicator Type": "MD5 hash", 161 | "Indicator": "037d9a5307e32252a3556bbe038a0722", 162 | "Context": "MD5 hash for rdp.exe" 163 | }, 164 | { 165 | "Indicator Type": "SHA-1 hash", 166 | "Indicator": "641b7cf77286bd86eb144147bbf073bbd2c9c261", 167 | "Context": "SHA-1 hash for rdp.exe" 168 | }, 169 | { 170 | "Indicator Type": "file name", 171 | "Indicator": "start \u2014 \u043a\u043e\u043f\u0438\u044f.bat", 172 | "Context": "batch script to execute rclone.exe for data exfiltration" 173 | }, 174 | { 175 | "Indicator Type": "SHA-256 hash", 176 | "Indicator": "8b5fdb358b26c09a01c56de4de69841c67051f64ac8afcdd56dfddee06fdaa7b", 177 | "Context": "hash for start \u2014 \u043a\u043e\u043f\u0438\u044f.bat" 178 | }, 179 | { 180 | "Indicator Type": "MD5 hash", 181 | "Indicator": "76faaf2e85045fcd1a404b7cb921d7c1", 182 | "Context": "MD5 hash for start \u2014 \u043a\u043e\u043f\u0438\u044f.bat" 183 | }, 184 | { 185 | "Indicator Type": "SHA-1 hash", 186 | "Indicator": "4484887c6857a26e40f4337d64ac0df7c391ba83", 187 | "Context": "SHA-1 hash for start \u2014 \u043a\u043e\u043f\u0438\u044f.bat" 188 | } 189 | ] 190 | ``` 191 | -------------------------------------------------------------------------------- /extract.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import json 3 | import os 4 | from urllib.parse import urljoin 5 | from bs4 import BeautifulSoup 6 | import binascii as ba 7 | import requests 8 | from PIL import Image 9 | import pytesseract 10 | from dotenv import load_dotenv 11 | import openai 12 | from colorama import Fore, Style, init 13 | 14 | init(autoreset=True) 15 | 16 | load_dotenv() 17 | 18 | openai.api_key = os.getenv("OPENAI_API_KEY") 19 | 20 | 21 | def download_page(url, user_agent): 22 | print(Fore.BLUE + "Downloading webpage content...") 23 | headers = {'User-Agent': user_agent} 24 | response = requests.get(url, headers=headers) 25 | response.raise_for_status() 26 | soup = BeautifulSoup(response.text, 'html.parser') 27 | lines = [line.strip() for line in soup.get_text( 28 | separator='\n').split('\n') if line.strip()] 29 | noemptylines = "\n".join(lines) 30 | 31 | return noemptylines 32 | 33 | 34 | def download_images(soup, base_url): 35 | print(Fore.BLUE + "Downloading images...") 36 | images = soup.find_all('img') 37 | # This list will store the paths of downloaded images. 38 | downloaded_image_paths = [] 39 | i = 0 40 | for img in images: 41 | i += 1 42 | img_url = urljoin(base_url, img.get('src')) 43 | if not img_url: 44 | continue # Skip if img['src'] is None 45 | 46 | if 'data:image/' in img_url: #embedded images 47 | ext = img_url.partition('data:image/')[2].split(';')[0] 48 | img_path = os.path.join(os.getcwd(), 'newim'+str(i)+'.'+ext) 49 | with open(img_path,'wb') as f: 50 | f.write(ba.a2b_base64(img_url.partition('base64,')[2])) 51 | downloaded_image_paths.append(img_path) 52 | 53 | else: 54 | img_response = requests.get(img_url) 55 | img_name = os.path.basename(img_url) 56 | img_path = os.path.join(os.getcwd(), img_name) 57 | with open(img_path, 'wb') as f: 58 | f.write(img_response.content) 59 | downloaded_image_paths.append(img_path) # Add the path to the list 60 | 61 | return downloaded_image_paths # Return the list of downloaded image paths 62 | 63 | 64 | def ocr_image(image_path): 65 | print(Fore.BLUE + f"Performing OCR on {image_path}...") 66 | return pytesseract.image_to_string(Image.open(image_path)) 67 | 68 | 69 | def parse_openai_response_to_iocs(text): 70 | """Parses the OpenAI text response into a list of IOC dictionaries.""" 71 | iocs = [] 72 | lines = [line for line in text.split('\n') if line.strip()] 73 | for line in text.split("\n"): 74 | parts = line.split(" : ") 75 | if len(parts) == 3: 76 | iocs.append({ 77 | "Indicator Type": parts[0].strip(), 78 | "Indicator": parts[1].strip(), 79 | "Context": parts[2].strip(), 80 | }) 81 | # else: 82 | # print(Fore.RED + "Response format incorrect, resubmitting request...") 83 | # return None 84 | return iocs 85 | 86 | 87 | def extract_iocs_with_openai(content, context_identifier, retry_count=0, max_retries=1): 88 | if retry_count > max_retries: 89 | print( 90 | Fore.RED + "Maximum retry limit reached. Moving on without additional retries.") 91 | return [] 92 | 93 | system_prompt = "Return only as requested, without comments or code blocks, only as plain text. If nothing is found return: 'No IOCs found in the provided text'" 94 | prompt = ( 95 | f"Extract all IOCs (IP addresses, domain names, email addresses, email subject, file name, useragent strings, urls, usernames, passwords, SHA and MD5 hashes, and so on) from the following text and format each as 'indicator type : indicator : context'. Context should be information that surrounds the IOC, such as the filename assocaited to the hash, the type of infrastructure that eixsts on an IPv4 address, and so on. {content}") 96 | message = [{"role": "assistant", "content": system_prompt}, 97 | {"role": "user", "content": prompt}] 98 | 99 | try: 100 | response = openai.chat.completions.create( 101 | model="gpt-4-turbo-preview", 102 | messages=message, 103 | temperature=0.3, 104 | max_tokens=4096, 105 | top_p=1.0, 106 | frequency_penalty=0.0, 107 | presence_penalty=0.0 108 | ) 109 | iocs_text = response.choices[0].message.content 110 | print(iocs_text) 111 | iocs = parse_openai_response_to_iocs(iocs_text) 112 | if iocs is None: 113 | print( 114 | Fore.YELLOW + f"Retry {retry_count+1}/{max_retries} due to format mismatch...") 115 | return extract_iocs_with_openai(content, context_identifier, retry_count+1, max_retries) 116 | return iocs 117 | except Exception as e: 118 | print(Fore.RED + f"An error occurred while querying OpenAI: {e}") 119 | return [] 120 | 121 | 122 | def cleanup_files(file_paths): 123 | for path in file_paths: 124 | try: 125 | os.remove(path) 126 | print(Fore.GREEN + f"Successfully removed {path}.") 127 | except Exception as e: 128 | print(Fore.RED + f"Error removing file {path}: {e}") 129 | 130 | 131 | def main(url, output_file, user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3", retry_limit=1): 132 | print(Fore.GREEN + "Starting IOC extraction process...") 133 | 134 | if "file://" in url: #reading html content from local files 135 | from pathlib import Path 136 | page_content = Path(url.split("file://")[1]).read_text() 137 | else: 138 | page_content = download_page(url, user_agent) 139 | 140 | soup = BeautifulSoup(page_content, 'html.parser') 141 | text_content = soup.get_text() 142 | downloaded_images = download_images(soup, url) 143 | 144 | text_iocs = extract_iocs_with_openai( 145 | text_content, "text_content", 0, retry_limit) 146 | 147 | all_iocs = text_iocs[:] 148 | for img_path in downloaded_images: 149 | img_text = ocr_image(img_path) 150 | img_iocs = extract_iocs_with_openai(img_text, img_path, 0, retry_limit) 151 | all_iocs.extend(img_iocs) 152 | 153 | cleanup_files(downloaded_images) 154 | 155 | with open(output_file, 'w') as f: 156 | json.dump(all_iocs, f, indent=4) 157 | print(Fore.GREEN + "IOC extraction process completed.") 158 | 159 | 160 | if __name__ == "__main__": 161 | parser = argparse.ArgumentParser( 162 | description='Extract IOCs from a webpage.') 163 | parser.add_argument('--url', required=True, 164 | help='The URL of the webpage to analyze') 165 | parser.add_argument('--output', required=True, 166 | help='The JSON file to output') 167 | parser.add_argument('--retry-limit', type=int, default=3, 168 | help='The maximum number of retries for OpenAI requests') 169 | args = parser.parse_args() 170 | 171 | main(args.url, args.output) 172 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | beautifulsoup4==4.11.1 2 | colorama==0.4.6 3 | openai==1.13.3 4 | Pillow==10.2.0 5 | pytesseract==0.3.10 6 | python-dotenv==1.0.1 7 | Requests==2.31.0 8 | --------------------------------------------------------------------------------