-
Notifications
You must be signed in to change notification settings - Fork 0
/
port_scan.rb
89 lines (71 loc) · 1.84 KB
/
port_scan.rb
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
require 'json'
require 'nmap/program'
require 'nmap/xml'
# such a file can be found at
# http://[316:c51a:62a3:8b9::2]/result.json
# or
# http://[300:7232:2b0e:d6e9:216:3eff:feb6:65a3]/nodeinfo.json
filename = "yggnodes.json"
threads_n = 16
$ports = [80, 443, 8008, 8448]
puts "Do a port scan on the yggdrasil network. Scan ports [#{$ports.join ', '}]. Reading nodes from #{filename}. Using #{threads_n} for scanning."
puts "Read #{filename}"
json = File.read 'yggnodes.json'
puts "Parse json "
data = JSON.parse json
puts "Collecting addresses"
addrs = []
data['yggnodes'].each_value { |node|
addrs << node['address']
}
#
# yggnodes
# public keys
# address
# coords
# dht
# nodeinfo
# peers
# time
per_thread = addrs.length / threads_n
rest = addrs.length % threads_n
puts "Split addresses for threads"
parts = []
for i in 0...threads_n
parts << addrs[i * per_thread ... (i+1) * per_thread]
end
rest_part = addrs[-rest..-1]
threads = []
def generate_code(number)
charset = Array('A'..'Z') + Array('a'..'z')
Array.new(number) { charset.sample }.join
end
def work_on_address addr, ident
Nmap::Program.scan do |nmap|
nmap.service_scan = true
nmap.xml = "scan_#{ident}-#{generate_code(5)}.xml"
nmap.ipv6 = true
nmap.ports = $ports
nmap.targets = addr
end
end
threads << Thread.new {
puts "Start extra thread"
rest_part.each_with_index { |addr, index|
work_on_address addr, "r_#{index}"
}
puts "Extra thread complete"
}
for i in 0...threads_n
threads << Thread.new(i) { |num|
puts "Start thread #{num + 1}"
parts[num].each_with_index { |addr, index|
work_on_address addr, "#{num}_#{index}"
}
puts "Stop thread #{num + 1}"
}
end
puts "Waiting for threads to finish"
threads.each { |th|
th.join
}