Created
August 1, 2023 01:51
-
-
Save Cdaprod/a6ae6292b8a5a2fa8ca8c8486ba36749 to your computer and use it in GitHub Desktop.
Shodan Query Notebook and Visualization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"cells":[{"metadata":{},"cell_type":"markdown","source":"[My name is David Cannan follow me on GitHub!](https://github.com/Cdaprod)"},{"metadata":{},"cell_type":"markdown","source":"---"},{"metadata":{},"id":"733ed050","cell_type":"markdown","source":"# Shodan Query and Visualization Notebook"},{"metadata":{},"id":"17310e43","cell_type":"markdown","source":"[Click here to open this notebook in Google Colab!](https://colab.research.google.com/gist/Cdaprod/e4237318f0b8b26f29cc7f2cbe8cb468.ipynb)"},{"metadata":{},"id":"7a0d245d","cell_type":"markdown","source":"This notebook provides tools for querying Shodan, visualizing the results on a map, and saving data as JSON."},{"metadata":{},"id":"25c75537","cell_type":"markdown","source":"## Setup"},{"metadata":{},"id":"ca8b9241","cell_type":"markdown","source":"Let's install and import the necessary libraries first."},{"metadata":{"trusted":false},"id":"3925f700","cell_type":"code","source":"\n!pip install shodan pandas folium\nimport shodan\nimport pandas as pd\nimport folium\nimport json\n","execution_count":null,"outputs":[]},{"metadata":{},"id":"77370a4a","cell_type":"markdown","source":"## ShodanQuery Class Definition"},{"metadata":{},"id":"d4dd17a5","cell_type":"markdown","source":"This class facilitates querying Shodan and returning results."},{"metadata":{"trusted":false},"id":"2f876dfb","cell_type":"code","source":"\nclass ShodanQuery:\n def __init__(self, api_key):\n self.api = shodan.Shodan(api_key)\n\n def search(self, query):\n try:\n results = self.api.search(query)\n return results\n except shodan.APIError as e:\n print(f\"Error: {e}\")\n return None\n","execution_count":null,"outputs":[]},{"metadata":{},"id":"d5ec2df7","cell_type":"markdown","source":"## Query Execution"},{"metadata":{},"id":"a58fdea0","cell_type":"markdown","source":"Replace the placeholders with your actual API key and query, then run the cell."},{"metadata":{"trusted":false},"id":"333d0027","cell_type":"code","source":"\nAPI_KEY = \"YOUR_SHODAN_API_KEY\"\nquery = \"organization:TARGET_PROGRAM_NAME\"\n\nsearcher = ShodanQuery(API_KEY)\ndata = searcher.search(query)\n","execution_count":null,"outputs":[]},{"metadata":{},"id":"7a25cde9","cell_type":"markdown","source":"## Save Results as JSON"},{"metadata":{},"id":"b6e4b218","cell_type":"markdown","source":"We will structure the data into appropriate levels and save it as a JSON file."},{"metadata":{"trusted":false},"id":"b417dfd3","cell_type":"code","source":"\n# Structure data with appropriate levels\nstructured_data = {\n \"total_results\": data['total'],\n \"results\": data['matches']\n}\n\n# Save to JSON\nwith open('shodan_results.json', 'w') as f:\n json.dump(structured_data, f)\n","execution_count":null,"outputs":[]},{"metadata":{},"id":"edd95297","cell_type":"markdown","source":"## Visualization using Folium"},{"metadata":{},"id":"964bf7ed","cell_type":"markdown","source":"Let's visualize the geographical distribution of our results on a map."},{"metadata":{"trusted":false},"id":"62194098","cell_type":"code","source":"\nm = folium.Map()\n\nfor result in data['matches']:\n lat = result['location']['latitude']\n lon = result['location']['longitude']\n tooltip = result['ip_str']\n folium.Marker([lat, lon], tooltip=tooltip).add_to(m)\n\nm\n","execution_count":null,"outputs":[]},{"metadata":{},"id":"12286488","cell_type":"markdown","source":"## Save Map as an Image"},{"metadata":{},"id":"7cf51fa5","cell_type":"markdown","source":"Due to limitations with Folium, we'll utilize Selenium to capture and save the map as an image.\nNote: You need to have Selenium and a webdriver (e.g., ChromeDriver) installed."},{"metadata":{"trusted":false},"id":"99dc6889","cell_type":"code","source":"\n!pip install selenium\nfrom selenium import webdriver\n\n# Save the map as HTML first\nm.save('map.html')\n\n# Use Selenium to open the map and capture it as an image\nbrowser = webdriver.Chrome('path_to_chromedriver')\nbrowser.get('file:///path_to_directory/map.html')\nbrowser.save_screenshot('map.png')\nbrowser.quit()\n","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":5} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment