-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathshodandb.py
148 lines (83 loc) · 2.88 KB
/
shodandb.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#Import the Shodan Library
from shodan import WebAPI
import time
import pickle
class ShodanDB_Search:
"""Searches the shodan database for exploits that are in shodan, msf and exploitd"""
def __init__(self,search):
#Attributes
self.configFile = "config.dat"
self.dataList = []
self.resultsList = None
self.API_KEY = ""
self.loadData()
self.API_KEY = self.dataList[0]
self.search = search
self.api = WebAPI(self.API_KEY)
## def main(self):
##
##
def loadData(self):
try:
with open(self.configFile, mode="rb") as myFile:
self.dataList = pickle.load(myFile)
except:
self.getAPI_Key()
def getAPI_Key(self):
accepted = False
while accepted == False:
if self.API_KEY == "":
print("API Key not given - enter below!")
print("If you have not got an API Key then visit - www.shodanhq.com")
try:
getKey = input("Enter API Key: ")
self.API_KEY = getKey
self.dataList.append(self.API_KEY)
#write the data to config file for persistence
with open(self.configFile, mode="wb") as myFile:
pickle.dump(self.dataList, myFile)
accepted = True
except:
print("Please enter a correct API Key")
if accepted == True:
self.API_KEY = getKey
def searchShodan(self):
search = self.search
print()
print("Please wait this may take a while..")
searchResults = self.api.exploits.search(search)
keyDataList = []
dataResults = searchResults['matches']
#Common Variables
totalResults = int(searchResults['total'])
print()
print("[+] {0} exploits found!".format(str(totalResults)))
time.sleep(2)
keyDataList.append(totalResults)
#Too much data to output at once format into pages
resultsPagesList = []
if totalResults > 10:
totalPages = int(totalResults // 10)
if (totalResults % 10) > 0:
totalPages += 1
for page in range(1,totalPages + 1):
resultsList = []
#Counter Variables for creating lists for each page
endNum = (page * 10)
startNum = (page * 10) - 10
#If last page use the total pages variable as the end number
if endNum > totalResults:
endNum = totalResults
#Create lists
resultsList = dataResults[startNum:endNum]
resultsPagesList.append(resultsList)
#10 or less results = one page
else:
endNum = totalResults
startNum = 0
#Create lists
resultsList = dataResults[startNum:endNum]
resultsPagesList.append(resultsList)
return resultsPagesList, keyDataList
if __name__ == "__main__":
new = ShodanDB_Search("hello")