-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathasn
executable file
·217 lines (192 loc) · 5.38 KB
/
asn
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
#!/usr/bin/env ruby
require 'json'
require 'mechanize'
require 'nokogiri'
require 'fileutils'
require 'colorize'
@agent = Mechanize.new
country = "us"
def msg(msg)
puts "[*] #{msg}".blue
end
def countries()
countries = []
html = @agent.get("https://ipinfo.io/countries/").body()
Nokogiri::HTML(html).css("tr").each do |tr|
td = tr.css("td")
if td.length > 0
name = td[0].text
url = td[0].css("a")[0]["href"].split("/")[2]
counts = td[1].text
country = {"name"=>name, "cc"=>url, "counts"=>counts}
countries.push(country)
end
end
return countries
end
def asn(country_code)
asns = []
html = @agent.get("https://ipinfo.io/countries/#{country_code}").body()
Nokogiri::HTML(html).css(".main-content").css("table").css("tr").each do |tr|
td = tr.css('td')
if td[0] != ""
if td != nil and td.length > 0
asn = td[0].css("a")[0]["href"].split("/")[1]
name = td[1].text
count = td[2].text
asn = {"name"=>name, "asn"=>asn, "count"=>count}
asns.push(asn)
end
end
end
return asns
end
def ranges_from_asn(asn)
ranges = []
html = @agent.get("https://ipinfo.io/#{asn}").body
Nokogiri::HTML(html).css("#blocks").css("table").css("tbody").css("tr").each do |tr|
td = tr.css("td")
range = td.css("a").text.gsub("\n", "").gsub(" ", "")
ranges.push(range)
end
return ranges
end
companies = {
"amazon"=>{
"glob"=>"amazon",
"asn"=>{}
},
"microsoft"=>{
"glob"=>"microsoft",
"asn"=>{}
},
"digitalocean"=>{
"glob"=>"digitalocean",
"asn"=>{}
},
"godaddy"=>{
"glob"=>"godaddy",
"asn"=>{}
},
"google"=>{
"glob"=>"google",
"asn"=>{}
},
"linode"=>{
"glob"=>"linode",
"asn"=>{}
},
"ovh"=>{
"glob"=>"ovh",
"asn"=>{}
},
"rackspace"=>{
"glob"=>"rackspace",
"asn"=>{}
},
"vultr"=>{
"glob"=>"choopa",
"asn"=>{}
},
"dedicated.com"=>{
"glob"=>"Dedicated.com",
"asn"=>{}
},
"hetzner"=>{
"glob"=>"hetzner",
"asn"=>{}
},
"at&t"=>{
"glob"=>"at&t",
"asn"=>{}
},
"telefonica"=>{
"glob"=>"telefonica",
"asn"=>{}
},
"bt"=>{
"glob"=>"british telecommunications",
"asn"=>{}
}
}
FileUtils.mkdir_p 'index'
FileUtils.mkdir_p 'companies'
countries = ["us", "gb", "fr", "de", "ru", "au", "pl", "ca", "cn", "nl", "id", "in", "ua", "nz", "mx", "cz", "il", "my", "ie", "tw", "be", "ky"]
# countries = ["us", "gb", "ie", "fr", "au", "ru", "ca"]
### Iterate and pull company names with ASN numbers and write to JSON files.
countries.each do |country|
if ! File.exist? "index/#{country}.json"
msg "Pulling ASN data for #{country}.."
country_data = asn(country)
File.open("index/#{country}.json", "w") {|file|file.write(JSON.pretty_generate(country_data))}
end
country_data = JSON.parse(File.open("index/#{country}.json", "r").read())
# Iterate through each company in country JSON
country_data.each do |company|
companyname = company["name"]
companies.keys.each do |company_f|
# If ASN name includes the "glob" push it to the ASN array
if companyname.downcase.include? companies[company_f]["glob"]
asn = company["asn"]
companies[company_f]["asn"][asn] = []
end
end
end
msg "Waiting 10 seconds..." if ! File.exist? "index/#{country}.json"
sleep 10 if ! File.exist? "index/#{country}.json"
end
global_ranges = ""
companies.keys.each do |company|
company_data = {}
if File.exist? "companies/#{company}/#{company}.json"
# msg "File exists for #{company}"
company_data = JSON.parse(File.open("companies/#{company}/#{company}.json", "r").read())
if ! File.exist? "companies/#{company}/ranges.txt"
ranges = File.open("companies/#{company}/ranges.txt", "w")
if company_data.has_key? "asn" and company_data["asn"].length > 0
company_data["asn"].keys.each do |asn|
company_data["asn"][asn].each do |asn|
asn.each do |range|
ranges.write("#{range}\n")
global_ranges += "#{range}\n"
end
end
end
end
end
if company_data.has_key? "asn"
company_data["asn"].each do |asn, range_data|
range_data.each do |ranges|
ranges.each do |range|
global_ranges += "#{range}\n"
end
end
end
end
end
companies[company].delete("glob")
companies[company]["asn"].keys.each do |asn|
if company_data.has_key? "asn" and company_data["asn"].has_key? asn
if company_data["asn"][asn].length < 1
msg "Pulling #{asn} #{company}"
range_data = ranges_from_asn(asn)
if range_data != nil
company_data["asn"][asn].push(range_data)
end
FileUtils.mkdir_p "companies/#{company}/"
File.open("companies/#{company}/#{company}.json", "w") { |file| file.write(JSON.pretty_generate(company_data)) }
end
else
company_data = companies[company]
msg "Pulling #{asn} #{company}"
range_data = ranges_from_asn(asn)
if range_data != nil
company_data["asn"][asn].push(range_data)
end
FileUtils.mkdir_p "companies/#{company}/"
File.open("companies/#{company}/#{company}.json", "w") { |file| file.write(JSON.pretty_generate(company_data)) }
end
end
end
FileUtils.mkdir_p "all"
File.open("all/ranges.txt", "w") {|file| file.write(global_ranges) }