forked from NullArray/Shogun
/
shogun.py
319 lines (243 loc) · 9.36 KB
/
shogun.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env Python2.7
import sys
import os, os.path
import shodan
import pickle
import socket
from datetime import datetime
from blessings import Terminal
t = Terminal()
ts = datetime.now()
reset = False
logging = False
api = ""
print t.cyan("""
.d88888b dP
88. "' 88
`Y88888b. 88d888b. .d8888b. .d8888b. dP dP 88d888b.
`8b 88' `88 88' `88 88' `88 88 88 88' `88
d8' .8P 88 88 88. .88 88. .88 88. .88 88 88
Y88888P dP dP `88888P' `8888P88 `88888P' dP dP
.88
d8888P """)
print t.cyan("\n SHOGUN - Shodan Command Line Interface\n")
def key():
global api
if reset == True:
if os.path.isfile( os.path.expanduser(".config/api.p") ):
path = os.path.expanduser(".config/api.p")
try:
os.remove(path)
except:
print "\n[" + t.red("!") + "]Critical. Deleting old API key file failed"
print "[" + t.magenta("?") + "]Continue with old APY key?\n"
query = raw_input("[Y]es/[N]o: ")
if query == "y":
main()
elif query == "n":
print "\n[" + t.red("!") + "]Aborted."
sys.exit(1)
else:
print "\n[" + t.red("!") + "]Unhandled option. Defaulting to main menu."
main()
if not os.path.isfile(os.path.expanduser(".config/api.p")):
print "[" + t.green("+") + "]Please provide your Shodan.io API key."
SHODAN_API_KEY = raw_input("API key: ")
pickle.dump(SHODAN_API_KEY, open( os.path.expanduser(".config/api.p"), "wb" ))
print "\n[" + t.green("+") + "]Your API key has been saved to 'api.p' in '~/.conifg'.\n"
print "[" + t.green("+") + "]Welcome to Shogun."
print "[" + t.green("+") + "]This program serves as a simple CLI to the Shodan.io search engine."
print "[" + t.green("+") + "]Type the command 'help' to show which options are available to you.\n\n"
else:
path = os.path.expanduser(".config/api.p")
SHODAN_API_KEY = pickle.load(open( os.path.expanduser(".config/api.p"), "rb" ))
print "\n[" + t.green("+") + "]Your API key was loaded from " + path
print "\n[" + t.green("+") + "]Welcome to Shogun."
print "[" + t.green("+") + "]This program serves as a simple CLI to the Shodan.io search engine."
print "[" + t.green("+") + "]Type the command 'help' to show which options are available to you.\n\n"
try:
api = shodan.Shodan(SHODAN_API_KEY)
except Exception as e:
print "\n[" + t.red("!") + "]Critical. API setup failed.\n"
print e
sys.exit(0)
main()
def resolve():
global logging
print "\n[" + t.green("+") + "]Please provide the domain you wish to resolve. I.E. 'google.com'."
query = raw_input("\n<" + t.cyan("RESOLVE") + ">$ ")
try:
data = socket.gethostbyname_ex(query)
print "\n[" + t.green("+") + "]Domain resolves to: \n" + repr(data) + "\n"
except socket.gaierror as e:
print "[" + t.red("!") + "]Critical. A GAIerror was raised with the following error message."
print e + "\n"
print "[" + t.green("+") + "]Consider typing the domain without the protocol, I.E. 'google.com, instead of http://google.com"
print "[" + t.green("+") + "]Defaulting to main menu."
if logging == True:
print "[" + t.green("+") + "]Results saved to shogun.log in the current directory"
with open('shogun.log', 'ab') as log:
log.write("\nTime: %s\n" % ts)
log.write("Shogun Resolver Results Log. Query -> %s\n" % query)
for items in data[2]:
log.write("%s\n" % items)
main()
def ports():
global api
global logging
print "\n[" + t.green("+") + "]Please provide a host IP."
query = raw_input("\n<" + t.cyan("PORTS") + ">$ ")
try:
result = api.host(query)
print
print "[" + t.green("+") + "]Detail on %s:" % result['ip_str']
for service in result['data']:
print "\t- Running %s on port %d" % (service.get('product', 'unknown service'), service['port'])
except Exception as e:
print "[" + t.red("!") + "]Critical. An error was raised with the following error message.\n"
print e
print "\n[" + t.green("+") + "]Defaulting to main menu."
if logging == True:
print "[" + t.green("+") + "]Results saved to shogun.log in the current directory"
with open('shogun.log', 'ab') as log:
log.write("\nTime: %s\n" % ts)
log.write("Shogun Ports and Services Results Log. Query -> %s\n" % query)
for service in result['data']:
log.write("\t- Running %s on port %d" % (service.get('product', 'unknown service'), service['port']))
log.write("\n")
main()
def platform():
global api
global logging
print "\n[" + t.green("+") + "]Please provide your search query."
query = raw_input("\n<" + t.cyan("PLATFORM") + ">$ ")
try:
result = api.search(query)
print
for service in result['matches']:
print service['ip_str']
print
except Exception as e:
print "[" + t.red("!") + "]Critical. An error was raised with the following error message.\n"
print e
print "\n[" + t.green("+") + "]Defaulting to main menu."
if logging == True:
print "[" + t.green("+") + "]Results saved to shogun.log in the current directory"
with open('shogun.log', 'ab') as log:
log.write("\nTime: %s\n" % ts)
log.write("Shogun Platform Results Log. Query -> %s\n" % query)
for service in result['matches']:
log.write(service['ip_str'])
log.write("\n")
main()
def summary():
global api
global logging
print "\n[" + t.magenta("?") + "]How many results would you like to display?"
print "[" + t.green("+") + "]I.E. '10' shows top 10 results for search item."
amount = raw_input("\n<" + t.cyan("SUMMARY") + ">$ ")
if amount == "0":
print "[" + t.red("!") + "]Amount must be greater than 0."
print "\n[" + t.green("+") + "]Defaulting to main menu."
main()
print "\n[" + t.green("+") + "]Please enter your search query"
query = raw_input("\n<" + t.cyan("SUMMARY") + ">$" )
FACETS = [
('org', amount),
('domain', amount),
('port', amount),
('asn', amount),
('country', amount),
]
FACET_TITLES = {
'org': "Top" +repr(amount) + "Organizations",
'domain': "Top" +repr(amount) + "Domains",
'port': "Top" +repr(amount) + "Ports",
'asn': "Top" +repr(amount) + "Autonomous Systems",
'country': "Top" +repr(amount) + "Countries",
}
try:
result = api.count(query, facets=FACETS)
print "\n[" + t.green("+") + "]Summary information on item: [ %s ]" % (query)
print "[" + t.green("+") + "]Total Results: %s\n" % result['total']
print
for facet in result['facets']:
print FACET_TITLES[facet]
for term in result['facets'][facet]:
print "%s: %s" % (term['value'], term['count'])
print
except Exception as e:
print "[" + t.red("!") + "]Critical. An error was raised with the following error message.\n"
print e
print "\n[" + t.green("+") + "]Defaulting to main menu."
main()
if logging == True:
print "[" + t.green("+") + "]Results saved to shogun.log in the current directory"
with open('shogun.log', 'ab') as log:
log.write("\nTime: %s\n" % ts)
log.write("Shogun Summary Results Log. Query -> %s\n" % query)
for facet in result['facets']:
log.write(FACET_TITLES[facet])
log.write("\n")
for term in result['facets'][facet]:
log.write("%s: %s" % (term['value'], term['count']))
log.write("\n")
main()
def commands():
print
print "[" + t.magenta("~") + "]Help - Print usage information"
print "[" + t.magenta("~") + "]Resolve - Query DNS to retrieve a domain's associated IP address"
print "[" + t.magenta("~") + "]Ports - Retrieve ports/services associated with provided host IP"
print "[" + t.magenta("~") + "]Platform- Retrieve IPs associated with platform. I.E. 'Routers' returns a list of IPs that are classified as part of such"
print "[" + t.magenta("~") + "]Summary - Provide a summary of information on provided search item. I.E. 'IIS' will provide top results for 'IIS'"
print "[" + t.magenta("~") + "]Logging - Enable or disable search result logging(Disabled by default)"
print "[" + t.magenta("~") + "]Api - Display API key info and/or change API key"
print "[" + t.magenta("~") + "]Quit/Q - Exit Shogun"
def main():
global api
global reset
global logging
while True:
query = raw_input("\n<" + t.cyan("SHOGUN") + ">$ ")
if query == "help":
commands()
elif query == "resolve":
resolve()
elif query == "ports":
ports()
elif query == "platform":
platform()
elif query == "summary":
summary()
elif query == "logging":
print "[" + t.magenta("?") + "]Enable logging?\n"
logs = raw_input("[Y]es/[N]o: ")
if logs == "y":
print "[" + t.green("+") + "]Logging enabled"
logging = True
elif logs == "n":
print "[" + t.green("+") + "]Logging disabled"
logging = False
else:
print "[" + t.red("!") + "]Unhandled option"
logging = False
elif query == "api":
SHODAN_API_KEY = pickle.load(open( os.path.expanduser(".config/api.p"), "rb" ))
print "\n[" + t.green("+") + "]Current API key: %s" % (SHODAN_API_KEY)
print "\n[" + t.magenta("?") + "]Would you like to change your API key?"
change = raw_input("[Y]es/[N]o: ")
if change == "y":
reset = True
key()
elif change == "n":
print "\n[" + t.green("+") + "]Aborted"
reset = False
else:
print "[" + t.red("!") + "]Unhandled option"
reset = False
elif query == "quit" or "q":
sys.exit(0)
else:
print "[" + t.red("!") + "]Unhandled option"
if __name__=="__main__":
key()