Created
June 14, 2022 17:16
-
-
Save Xf4kt0r/bff51d71c1fcc377a81f7047088dc9c8 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
import sys | |
from urllib.request import urlopen | |
import json | |
import csv | |
from time import sleep | |
from pprint import pprint | |
#vars | |
api_key = sys.argv[1] #read your API key as the first arg in the commandline | |
ip_limit = 100 #limit number of hosts | |
ip_list = [] #set up the ip list shell | |
def get_shodan_3389_webservers(api_key,ip_limit,ip_list): | |
init_url = 'https://api.shodan.io/shodan/host/count?key=' + api_key + '&query=country:US+port:3389+server:+WebServer&facets=ip:' + str(ip_limit) | |
#we are gathering a list of ip addresses, this was necessary since you must have a paid account to do actual queries | |
#fortunately, we can poll the system for content | |
with urlopen(init_url) as init_resp: | |
init_content = init_resp.read() | |
init_response = json.loads(init_content) | |
for ip_index in range(ip_limit): #here we add the individual ips to a list | |
ip_list.append(init_response['facets']['ip'][ip_index]['value']) | |
#opening up the csv file and setting the headers | |
with open('shodan_data.csv', 'w', newline='') as csv_file: | |
fieldnames = ['IP Address','Operating System'] | |
write_out = csv.DictWriter(csv_file, fieldnames=fieldnames) | |
write_out.writeheader() | |
#iterate through the ip list and pull the data for each host | |
for ip in ip_list: | |
req_url = 'https://api.shodan.io/shodan/host/' + ip + '?key=' + api_key | |
with urlopen(req_url) as final_resp: | |
final_content = final_resp.read() | |
sleep(2) #making sure it doesn't walk on itself or make too many requests too quickly | |
final_response = json.loads(final_content) | |
#grab the ip and os for each host, writing it to the csv file | |
ip_str = final_response['ip_str'] | |
op_sys = final_response['os'] | |
write_out.writerow({'IP Address':ip_str,'Operating System':op_sys}) | |
get_shodan_3389_webservers(api_key,ip_limit,ip_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment