-
Notifications
You must be signed in to change notification settings - Fork 0
/
cyberscan.py
184 lines (153 loc) · 5.58 KB
/
cyberscan.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
import os
import sys
import termcolor
from pprint import pprint
import cybertools
import myregex
import pandas
from threading import Thread
from queue import Queue
def get_targets_from_file(file:str):
complete_filepath = os.path.join(os.getcwd(), file)
lines = []
with open(complete_filepath, "r") as f:
for l in f.readlines():
if l.split():
lines.append(l.strip())
lines = list(set(lines))
return lines
def get_target_information(target:str, whois:bool=False, abuseipdb:bool=False, virustotal:bool=False, shodan:bool=False, q:Queue=None):
target_type = myregex.get_type(target)
if not target_type['type']:
# Try to remove obfuscations.
target = cybertools.deobfuscate(target)
target_type = myregex.get_type(target)
if not target_type['type']:
print(termcolor.colored(f"{target} : type not found.", "red"), "\n")
return dict()
result = dict()
result["target"] = target
result.update(target_type)
if whois:
if target_type['type'] == 'ip':
r = cybertools.whois_ip(target)
for k,v in r.items():
result[f"whois_{k}"] = v
elif target_type['type'] == 'domain':
r = cybertools.whois_domain(target)
for k,v in r.items():
result[f"whois_{k}"] = v
if abuseipdb and target_type['type'] == 'ip':
r = cybertools.abuseipdb_check(target)
for k,v in r.items():
result[f"abuseipdb_{k}"] = v
if virustotal:
r = cybertools.virustotal_check(target)
for k,v in r.items():
result[f"virustotal_{k}"] = v
if shodan and target_type['type'] == 'ip':
r = cybertools.shodan_check(target)
for k,v in r.items():
result[f"shodan_{k}"] = v
if q:
q.put(result)
return result
def write_csv(filename:str, ioc_list:list):
dataframe = pandas.DataFrame(ioc_list)
dataframe.to_csv(filename, index=False, encoding='utf-8')
def scan_with_multithread(targets_list:list, selected_whois:bool=False, selected_abuseipdb:bool=False, selected_virustotal:bool=False, selected_shodan:bool=False):
threads = []
queue_results = Queue()
for target in targets_list:
threads.append(Thread(target=get_target_information, args=(target, selected_whois, selected_abuseipdb, selected_virustotal, selected_shodan, queue_results,)))
for t in threads:
t.start()
for t in threads:
t.join()
queue_results.put(None)
scan_results_by_type = dict()
while True:
q = queue_results.get()
queue_results.task_done()
if q is None:
break
else:
print(termcolor.colored(q["target"], "cyan"))
pprint(q)
print()
if not scan_results_by_type.get(q["type"]):
scan_results_by_type[q["type"]] = list()
scan_results_by_type[q["type"]].append(q)
for k,v in scan_results_by_type.items():
write_csv(f"output/output_{k}.csv", v)
print(termcolor.colored(f"created file output_{k}.csv", "green"))
def scan_multiple_targets(targets_list:list, selected_whois:bool=False, selected_abuseipdb:bool=False, selected_virustotal:bool=False, selected_shodan:bool=False):
scan_results_by_type = dict()
for target in targets_list:
target_info = get_target_information(target, selected_whois, selected_abuseipdb, selected_virustotal, selected_shodan)
if not target_info:
continue
if target_info.get("abuseipdb_status") and (target_info["abuseipdb_status"] == 401 or target_info["abuseipdb_status"] == 429):
print(termcolor.colored("abuseipdb error: " + str(target_info["abuseipdb_status"]), "red"))
selected_abuseipdb = False
if target_info.get("virustotal_status") and (target_info["virustotal_status"] == 401 or target_info["virustotal_status"] == 429):
print(termcolor.colored("virustotal error: " + str(target_info["virustotal_status"]), "red"))
selected_virustotal = False
print(termcolor.colored(target, "cyan"))
pprint(target_info)
print()
if not scan_results_by_type.get(target_info["type"]):
scan_results_by_type[target_info["type"]] = list()
scan_results_by_type[target_info["type"]].append(target_info)
for k,v in scan_results_by_type.items():
write_csv(f"output/output_{k}.csv", v)
def main():
# termcolor check
try:
os.system('color')
except:
pass
argv = []
if len(sys.argv) == 1:
argv = input("Input a ioc to scan or a file with '-f example.txt':\n").split()
else:
argv = sys.argv[1:]
if len(argv) == 1:
result = get_target_information(argv[0], True, True, True, True)
pprint(result)
elif len(argv) == 2 and argv[0] == '-f':
print("Select service you want to use: \n1) WhoIs. \n2) AbuseIPDB analysis. \n3) VirusTotal analysis.\n4) Shodan. \n5) Everything.")
inp = input()
try:
inp = int(inp)
except ValueError:
print(termcolor.colored("Invalid input.", "red"))
return
selected_whois = False
selected_abuseipdb = False
selected_virustotal = False
selected_shodan = False
if inp == 1:
selected_whois = True
elif inp == 2:
selected_abuseipdb = True
elif inp == 3:
selected_virustotal = True
elif inp == 4:
selected_shodan = True
elif inp == 5:
selected_whois = True
selected_abuseipdb = True
selected_virustotal = True
selected_shodan = True
else:
print(termcolor.colored("Invalid input.", "red"))
return
targets_list = get_targets_from_file(argv[1])
print(f"targets to scan: {len(targets_list)}")
# scan_multiple_targets(targets_list, selected_whois, selected_abuseipdb, selected_virustotal, selected_shodan)
scan_with_multithread(targets_list, selected_whois, selected_abuseipdb, selected_virustotal, selected_shodan)
else:
print(termcolor.colored("Invalid arguments.", "red"))
if __name__ == '__main__':
main()