Skip to content

Commit

Permalink
Added input file option
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisTruncer committed May 12, 2014
1 parent 8af09c3 commit 0bc40ae
Showing 1 changed file with 52 additions and 38 deletions.
90 changes: 52 additions & 38 deletions ShodanSearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def cli_parser():
parser.add_argument(
"-search", metavar="Apache server", default=False,
help="Use this when searching Shodan for a string.")
parser.add_argument(
"-f", metavar="ips.txt", default=None,
help="File containing IPs to search shodan for.")
parser.add_argument(
"-ip", metavar='192.168.1.1', default=False,
help="Used to return results from Shodan about a specific IP.")
Expand All @@ -40,7 +43,7 @@ def cli_parser():
parser.print_help()
sys.exit()

return args.search, args.ip, args.cidr, args.hostnameonly, args.page
return args.search, args.ip, args.cidr, args.hostnameonly, args.page, args.f


def create_shodan_object():
Expand All @@ -52,52 +55,63 @@ def create_shodan_object():
return shodan_object


def shodan_cidr_search(shodan_search_object, shodan_search_cidr):
def shodan_cidr_search(shodan_search_object, shodan_search_cidr, input_file_ips):

title()

if not validate_cidr(shodan_search_cidr):
print "[*] ERROR: Please provide valid CIDR notation!"
sys.exit()
if shodan_search_cidr is not False:

else:
if not validate_cidr(shodan_search_cidr):
print "[*] ERROR: Please provide valid CIDR notation!"
sys.exit()

print "[*] Searching Shodan for info about " + shodan_search_cidr
else:

# Create cidr notated list
network = IPNetwork(shodan_search_cidr)
print "[*] Searching Shodan for info about " + shodan_search_cidr

# search shodan for each IP
for ip in network:
# Create cidr notated list
network = IPNetwork(shodan_search_cidr)

print "\n[*] Searching specifically for: " + str(ip)
elif input_file_ips is not False:
try:
with open(input_file_ips, 'r') as ips_provided:
network = ips_provided.readlines()
except IOError:
print "[*] ERROR: You didn't provide a valid input file."
print "[*] ERROR: Please re-run and provide a valid file."
sys.exit()

try:
# Search Shodan
result = shodan_search_object.host(ip)
# search shodan for each IP
for ip in network:

# Display basic info of result
print "\n*** RESULT ***"
print "IP: " + result['ip']
print "Country: " + result['country_name']
if result['city'] is not None:
print "City: " + result['city']
print "\n"
print "\n[*] Searching specifically for: " + str(ip)

# Loop through other info
for item in result['data']:
print "Port: " + str(item['port'])
print "Banner: " + item['banner']
try:
# Search Shodan
result = shodan_search_object.host(ip)

except Exception, e:
if str(e).strip() == "API access denied":
print "You provided an invalid API Key!"
print "Please provide a valid API Key and re-run!"
sys.exit()
elif str(e).strip() == "No information available for that IP.":
print "No information is available for " + str(ip)
else:
print "[*]Unknown Error: " + str(e)
# Display basic info of result
print "\n*** RESULT ***"
print "IP: " + result['ip']
print "Country: " + result['country_name']
if result['city'] is not None:
print "City: " + result['city']
print "\n"

# Loop through other info
for item in result['data']:
print "Port: " + str(item['port'])
print "Banner: " + item['banner']

except Exception, e:
if str(e).strip() == "API access denied":
print "You provided an invalid API Key!"
print "Please provide a valid API Key and re-run!"
sys.exit()
elif str(e).strip() == "No information available for that IP.":
print "No information is available for " + str(ip)
else:
print "[*]Unknown Error: " + str(e)


def shodan_ip_search(shodan_search_object, shodan_search_ip):
Expand Down Expand Up @@ -241,7 +255,7 @@ def validate_ip(val_ip):

# Parse command line options
search_string, search_ip, search_cidr, search_hostnameonly,\
search_page_number = cli_parser()
search_page_number, search_file = cli_parser()

# Create object used to search Shodan
shodan_api_object = create_shodan_object()
Expand All @@ -254,8 +268,8 @@ def validate_ip(val_ip):
elif search_ip is not False:
shodan_ip_search(shodan_api_object, search_ip)

elif search_cidr is not False:
shodan_cidr_search(shodan_api_object, search_cidr)
elif search_cidr is not False or search_file is not None:
shodan_cidr_search(shodan_api_object, search_cidr, search_file)

else:
print "You didn't provide a valid option!"

0 comments on commit 0bc40ae

Please sign in to comment.