-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
121 lines (94 loc) · 3 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
#!/usr/bin/env python3
import json
import ipaddress
import paramiko
import socket
import csv
import sys
import threading
from pprint import pprint
import multiprocessing
from concurrent.futures import ThreadPoolExecutor
import random
import datetime
csv_writer_lock = threading.Lock()
total = 0
results_writer = None
def get_prefixes():
prefixes = []
with open('ip-ranges.json', 'r') as json_file:
data = json.load(json_file)
for ip_block in data['prefixes']:
if ip_block['service'] != 'EC2':
continue
prefix = ip_block['ip_prefix']
prefixes.append(ipaddress.ip_network(prefix))
return prefixes
def get_hosts_from_prefixes(prefixes):
hosts = []
for prefix in prefixes:
hosts += list(prefix.hosts())
return hosts
def test_host(host):
publickey = 0
password = 0
keyboard_interactive = 0
other = []
host = host.exploded
try:
s = socket.socket()
s.settimeout(3)
s.connect((host, 22))
t = paramiko.Transport(s)
t.connect()
except paramiko.ssh_exception.SSHException as e:
return [host, 0, str(e), 0, 0, 0, ""]
except Exception as e:
return [host, 0, str(e), 0, 0, 0, ""]
try:
t.auth_none('')
except paramiko.BadAuthenticationType as err:
if "publickey" in err.allowed_types:
publickey = 1
if "password" in err.allowed_types:
password = 1
if "keyboard-interactive" in err.allowed_types:
keyboard_interactive = 1
other = list(set(err.allowed_types) - {"publickey", "password", "keyboard-interactive"})
t.close()
except Exception as e:
try:
t.close()
except Exception as f:
pass
return [host, 1, str(e), 0, 0, 0]
return [host, 1, "", publickey, password, keyboard_interactive, ":".join(other)]
def host_run(host):
global csv_writer_lock, total, results_writer
result = test_host(host)
with csv_writer_lock:
total += 1
pprint(str(result))
results_writer.writerow(result)
def main():
global total, results_writer
prefixes = get_prefixes()
hosts = get_hosts_from_prefixes(prefixes)
pprint("Number of hosts: " + str(len(hosts)))
hosts = hosts[:11440973]
pprint("Number of hosts selected" + str(len(hosts)))
time = datetime.datetime.now()
time = time.strftime("%Y_%m_%d_%H_%M_%S")
with open('results_' + time + '.csv', 'w') as outfile:
results_writer = csv.writer(outfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
#with ThreadPoolExecutor(max_workers=10) as executor:
#args = ((results_writer, host) for host in hosts)
#for _ in executor.map(host_run, args):
# pass
# executor.shutdown(wait=True)
pool = multiprocessing.Pool()
pool.map(host_run, hosts)
print("End")
print("Total" + str(total))
if __name__ == '__main__':
main()