-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leverett-Wightman-cost.py
73 lines (62 loc) · 1.92 KB
/
Leverett-Wightman-cost.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#! /usr/bin/env python
# coding=UTF-8
# Filename: Leverett-Wightman-cost.py
# Template python-shodan code from:
# eireann.leverett@cantab.net
# www.concinnity-risks.com
import time, sys
from shodan import Shodan
from decimal import *
SHODAN_API_KEY = ""
# Create a connection to the Shodan API
api = Shodan(SHODAN_API_KEY)
FACETS = [
('org', 10),
('asn', 10),
# We only care about the top 5 countries, this is how we let Shodan know to return 5 instead of the
# default 10 for a facet. If you want to see more than 10, you could do ('country', 1000) for example
# to see the top 1,000 countries for a search query.
('country', 10),
]
FACET_TITLES = {
'org': 'Top 10 Organizations',
'asn': 'Top 10 Autonomous Systems',
'country': 'Top 10 Countries',
}
#Queries Shodan for a search term and then stores results in a list of dictionaries
def query_Shodan(term):
try:
#Search Shodan
results = api.count(term, facets=FACETS)
except Exception, e:
#No results found, print no 'matches'
print 'No %s\r' %e
#Returns a list of dictionary objects. Each dictionary is a result
return results
# Input validation
if len(sys.argv) == 1:
print 'Usage:%s<shodan query>' %sys.argv[0]
sys.exit(1)
query = ' '.join(sys.argv[1:])
result = query_Shodan(query)
shodancost = 19.00
ipcost = shodancost/4294967296
print 'Shodan Summary Information'
print 'Query:%s' % query
print 'Total Results: %s\n' % result['total']
if result['total'] == 0:
IPv4lwcost = float('Inf')
else:
IPv4lwcost = (Decimal(shodancost)/Decimal(result['total']))
print 'All-IPv4 L-W cost: $%.10f\n' % IPv4lwcost
# Print the summary info from the facets
for facet in result['facets']:
print FACET_TITLES[facet]
for term in result['facets'][facet]:
print term['value']+'\n'
org = term['value'].replace(" ", "+")
orgquery = query+' '+facet+':\"%s\"' % org
time.sleep(1)
vips = api.count(orgquery)
print '$%.10f' % (Decimal(ipcost*vips['total'])/Decimal(term['count']))
print ''