-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
259 lines (181 loc) · 6.11 KB
/
main.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
#!/usr/bin/env python3.8
from shodan import Shodan
import argparse
import logging
import json
import time
import re
def valid_API_Key(arg_values):
""" to valid argument value for metadata
"""
regex = "^([A-Za-z0-9]{32})$"
if not re.match(regex, arg_values):
msg = "The api key is invalid"
raise argparse.ArgumentTypeError(msg)
else:
return arg_values
def loadKeyFromFile(filename):
try:
fichier = open(filename, "r")
lines = fichier.readlines()
fichier.close()
except:
print(f"fichier {filename} non trouvé...")
quit()
for line in lines:
if valid_API_Key(line.strip()):
return line.strip()
quit()
def parse_args():
"""function to parse arguments from the CLI
"""
epilog = """
Exemples:
blablabla
Enjoy!
"""
parser = argparse.ArgumentParser(epilog=epilog, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("-l", "--loglevel", type=str, help="The log level for the program",
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], default="DISABLE")
subparser = parser.add_subparsers(dest='command')
search = subparser.add_parser('search')
search.description = "search for servers on shodan"
search.add_argument("-k", "--api-key", type=valid_API_Key, required=False, help="api key for shodan")
search.add_argument("-q", "--query", type=str, required=True, help="query for shodan search")
search.add_argument("-i", "--ip", type=str, required=False, help="Test if the ip is find in the result", nargs="*")
search.add_argument("-d", "--dns", type=str, required=False, help="dns name to find in result", nargs="*")
count = subparser.add_parser('count')
count.description = "count servers on shodan"
count.add_argument("-k", "--api-key", type=valid_API_Key, required=False, help="api key for shodan")
count.add_argument("-q", "--query", type=str, required=True, help="query for shodan search")
count.add_argument("-n", "--number", type=int, required=True, help="teorical number of result")
key = subparser.add_parser('api-key')
key.add_argument("-k", "--key-value", type=valid_API_Key, required=False, help="the api key for shodan")
key.add_argument("-d", "--delete", required=False, action="store_true")
key.add_argument("-c", "--confirm", required=False, action="store_true")
return parser.parse_args()
def search(api_key, query, ip, dns):
if not ip and not dns:
quit()
try:
api = Shodan(api_key)
except Exception as e:
print(e)
quit()
try:
results = api.search(query)
except Exception as e:
print(e)
quit()
filename = "shodan" + time.strftime("%Y%m%d-%H%M%S") + ".json"
fichier = open(filename, "w")
fichier.write(json.dumps(results))
fichier.close()
to_return = []
j = 1
for matche in results['matches']:
for dn in dns:
if dn in matche['domains']:
to_return.append(dn)
for i in ip:
if i == matche['ip_str']:
to_return.append(i)
j += 1
found = ','.join(to_return)
j = str(j)
print(f"In {j} element I found following element : {found}")
return to_return
def count(api_key, query, number):
try:
api = Shodan(api_key)
except Exception as e:
print(e)
quit()
try:
results = api.count(query)
except Exception as e:
print(e)
quit()
find = int(results['total'])
number = int(number)
if find == number:
print(f"Pas de nouvelle entrée dans Shodan, {number} serveurs trouvé")
else:
print(f"Nouvelle entrée dans shodan détecté. {number} espected and {find} found")
def key(key, delete=False, confirm=False):
if confirm == False and delete:
choice = ""
while choice != "y" and choice != "n":
choice = input("(y)es or (n)o : ")
if choice == "y":
confirm = True
else:
print("Not okay, no key deleted...")
quit(0)
if delete:
if confirm:
fichier = open("api-key.txt", "w")
fichier.write("")
fichier.close()
print("Key file deleted")
if key:
fichier = open("api-key.txt", "w")
fichier.write(key)
fichier.close()
print("Key added successfully")
return 0
def main():
"""main function
"""
# ARGPARSE
args = parse_args()
print(args)
# LOGGING
disable_logging = False
if args.loglevel == "DEBUG":
p_level = logging.DEBUG
elif args.loglevel == "INFO":
p_level = logging.INFO
elif args.loglevel == "WARNING":
p_level = logging.WARNING
elif args.loglevel == "ERROR":
p_level = logging.ERROR
elif args.loglevel == "CRITICAL":
p_level = logging.CRITICAL
elif args.loglevel == "DISABLE":
p_level = logging.CRITICAL
disable_logging = True
else:
p_level = logging.WARNING
logging.basicConfig(level=p_level, format='%(asctime)s - %(levelname)s - %(message)s')
if disable_logging:
logging.disable(level=logging.CRITICAL)
# ACTION
if args.command == "search":
print("search !")
api_key = loadKeyFromFile("api-key.txt") if args.api_key == None else args.api_key
query = args.query
ip = args.ip
dns = args.dns
search(api_key, query, ip, dns)
elif args.command == "count":
print("count !")
api_key = loadKeyFromFile("api-key.txt") if args.api_key == None else args.api_key
query = args.query
number = args.number
count(api_key, query, number)
elif args.command == "api-key":
key(args.key_value, args.delete, args.confirm)
else:
print("nothing to do.")
if __name__ == "__main__":
"""Entrypoint of the script
"""
main()
class Class():
"""Class template
"""
def __init__(self):
pass
def function(self):
pass