-
Notifications
You must be signed in to change notification settings - Fork 0
/
shodan-gci.py
executable file
·145 lines (114 loc) · 3.33 KB
/
shodan-gci.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
#!/usr/bin/env python3
import os
import sys
#cleandoc
from inspect import cleandoc
#looking up ip from icanhazip
import requests
#coloring output
from termcolor import colored
#subprocess to execute less command
import pydoc
#shodan library
import shodan
with open('api_key.txt') as api_key:
SHODAN_API_KEY = api_key.readline().rstrip()
api = shodan.Shodan(SHODAN_API_KEY)
def options():
OPTIONS_DICT = {
'1':icanhazip,
'2':shodan_query,
'3':shodan_lookup_host,
'0':cool_exit
}
while True:
option = input('\n'+cleandoc(
'''
1.What's my ip?
2.Query Shodan.
3.Lookup shodan host.
0.Exit
Shodan>
'''
))
if option in OPTIONS_DICT.keys():
return OPTIONS_DICT[option]
else:
print('\nOption not found. Try again.')
def icanhazip():
os.system('clear')
try :
myip = requests.get('https://icanhazip.com').text.rstrip()
except requests.exceptions.RequestException as e:
print(e)
return
print('Your IP is: \n')
print(myip)
#print('<My Ip-Addr>')
def shodan_query():
os.system('clear')
shodan_query = input('Enter Query : ')
# load results
try:
results = api.search(shodan_query)
except shodan.APIError as e:
print(e)
return
#load the data in the form of
#ip port organization hostnames...
data = ''
for result in results['matches']:
result_ip_str = result['ip_str'].ljust(15)
result_port = str(result['port']).ljust(6)
result_org = result['org']
result_host = result['hostnames'][0] if len(result['hostnames'])> 0 else ''
data+= \
colored(result_ip_str,'green')+' '+ \
colored(result_port,'red') +' '+ \
colored(result_org,'cyan') +' '+ \
colored(result_host,'magenta') +'\n'
#pydoc pager does not color
try:
pydoc.pipepager(data,cmd='less -R')
except KeyboardInterrupt:
return
def shodan_lookup_host():
#clear the console
os.system('clear')
print('')
#get input and lookup host
host = input('Input host IP address : ')
try:
result = api.host(host)
except shodan.APIError as e:
print(e)
return
#print IP in green
print(colored(result['ip_str'],'green'))
#print hostname data
for (index ,hostname) in enumerate(result['hostnames']):
if index == 0:
print('Hostnames:'.ljust(25),end='')
else:
print(''.ljust(25),end='')
print(str(hostname))
#print City
print('City : '.ljust(25)+str(result['city']))
#print Country
print('Country : '.ljust(25)+str(result['country_name']))
#print number of open ports
print('Number of open ports : '.ljust(25)+str(len(result['ports']))+'\n' )
#print data for each port
print('Ports : ')
for port in result['data']:
if 'port' in port:
print(' ' +colored(port['port'],'cyan'),end='')
if 'product' in port:
print(' ' + port['product'],end='')
print('')
print('')
def cool_exit():
print('Exiting.')
sys.exit(0)
while True:
options()()