-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_asn_info
executable file
·241 lines (204 loc) · 7.81 KB
/
update_asn_info
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
#!/usr/bin/perl
# -----------------------------------------------------------------------------
#
# RBL Updater Suite - a suite to update realtime blocklists for rspamd
# Copyright (C) 2022 - John Bradley (userjack6880)
#
# update_asn_info
# used to update SQL tables prior to Version 0 Alpha 1.3
#
# Available at: https://github.com/userjack6880/rbl_updater
#
# -----------------------------------------------------------------------------
#
# This file is part of the RBL Updater Suite for use with rspamd
#
# The RBL Updater Suite is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>.
#
# -----------------------------------------------------------------------------
use strict;
use warnings;
use Getopt::Std;
use File::Tail;
use File::Basename;
use JSON;
use LWP::UserAgent;
use LWP::Protocol::https;
use DBI;
# version number
my $version = '1 Feature Complete';
# basic settings --------------------------------------------------------------
# usage sub -------------------------------------------------------------------
sub usage {
print "\n".
"\tUsage:\n".
"\tThis script attempts to recitfy updated database configs where ASN and Provider are NULL\n".
"\n";
}
# load conf file --------------------------------------------------------------
our ($asnlist,$iplist,$dbname,$dbhost,$dbport,$dbuser,$dbpass,$log);
my $conffile = 'config.conf';
if (substr($conffile,0,1) ne '/' and substr($conffile,0,1) ne '.') { $conffile = "./$conffile"; }
if (-e File::Basename::dirname($0)."/$conffile") { $conffile = File::Basename::dirname($0)."/$conffile"; }
unless (-e $conffile) { usage(); die "Error! Could not read $conffile\n"; }
my $conftest = do $conffile;
die "$conffile could not be parsed: $@" if $@;
die "could not do $conffile: $!" if !defined $conftest;
# declare "global" variables --------------------------------------------------
my $dbh;
# BGP View Queries ------------------------------------------------------------
sub query_ip {
my $ip = shift;
my $result;
my $ua = LWP::UserAgent->new(send_te => 0);
$ua->agent("Mozilla/8.0"); # because cloudflare is a bitch
my $req = HTTP::Request->new(
GET => "https://api.bgpview.io/ip/$ip",
[
'Content-Type' => "application/json"
]
);
my $res = $ua->request($req);
if (substr($res->decoded_content,0,1) eq '<') {
$result->{error} = 1;
return $result;
}
my $data = decode_json($res->decoded_content);
$result->{prefix} = $data->{data}{rir_allocation}{prefix};
$result->{prefix} = $data->{data}{prefixes}[0]{prefix} if not defined($result->{prefix});
$result->{asn} = $data->{data}{prefixes}[0]{asn}{asn};
$result->{provider} = $data->{data}{prefixes}[0]{asn}{description};
$result->{provider} = $data->{data}{prefixes}[0]{description} if not defined($result->{provider});
$result->{provider} = 'Unknown' if not defined($result->{prefix});
return $result;
}
sub query_range {
my $range = shift;
my $result;
print "querying for $range\n";
my $ua = LWP::UserAgent->new(send_te => 0);
$ua->agent("Mozilla/8.0"); # because cloudflare is a bitch
my $req = HTTP::Request->new(
GET => "https://api.bgpview.io/prefix/$range",
[
'Content-Type' => "application/json"
]
);
my $res = $ua->request($req);
if (substr($res->decoded_content,0,1) eq '<') {
print "found nothing\n";
$result->{error} = 1;
return $result;
}
my $data = decode_json($res->decoded_content);
$result->{asn} = $data->{data}{asns}[0]{asn};
$result->{provider} = $data->{data}{asns}[0]{description};
$result->{provider} = $data->{data}{description_short} if not defined($result->{provider});
$result->{provider} = $data->{data}{description_full} if not defined($result->{provider});
$result->{provider} = 'Unknown' if not defined($result->{provider});
return $result;
}
sub query_asn {
my $asn = shift;
my $result;
my $ua = LWP::UserAgent->new(send_te => 0);
$ua->agent("Mozilla/8.0"); # because cloudflare is a bitch
my $req = HTTP::Request->new(
GET => "https://api.bgpview.io/asn/$asn",
[
'Content-Type' => "application/json"
]
);
my $res = $ua->request($req);
if (substr($res->decoded_content,0,1) eq '<') {
$result->{error} = 1;
return $result;
}
my $data = decode_json($res->decoded_content);
$result->{provider} = $data->{data}{description_short};
$result->{provider} = $data->{data}{description_full} if not defined($result->{provider});
$result->{provider} = $data->{data}{name} if not defined($result->{provider});
$result->{provider} = 'Unknown' if not defined($result->{provider});
return $result;
}
# Begin Main Code -------------------------------------------------------------
print "\nRBL Updater Suite v.$version\n".
"\nASN Updater\n\n";
# Open DB connection
$dbh = DBI->connect(
"DBI:mysql:database=$dbname;host=$dbhost;port=$dbport",
$dbuser,
$dbpass
) or die "Could not connect to database!\n";
my ($ip,$range,$asn,$provider);
# IP cascade
while (my @row = $dbh->selectrow_array(q{ SELECT INET_NTOA(ip4),ip4_net FROM ip_blocklist WHERE asn IS NULL AND provider IS NULL LIMIT 1; })) {
$ip = $row[0];
my $old_range = $row[1];
my $result = query_ip($ip);
if (defined $result->{error}) {
print "JSON error\n";
my $sth = $dbh->prepare(q{ UPDATE ip_blocklist SET provider = 'Unknown' WHERE ip4 = INET_ATON(?); });
$sth->execute($ip);
print "updated $ip with unknown\n";
}
else {
$range = $result->{prefix};
$asn = $result->{asn};
$provider = $result->{provider};
my $sth = $dbh->prepare(q{ UPDATE ip_blocklist SET ip4_net = ?, asn = ?, provider = ? where ip4_net = ?; });
$sth->execute($range,$asn,$provider,$old_range);
print "updated $range belonging to AS$asn, $provider - based on $ip\n";
}
$ip = '';
$range = '';
$asn = '';
$provider = '';
}
# focus on networks now
while ($range = $dbh->selectrow_array(q{ SELECT ip4_net FROM ipnet_blocklist WHERE provider IS NULL LIMIT 1; })) {
my $result = query_range($range);
if (defined $result->{error}) {
print "JSON error\n";
my $sth = $dbh->prepare(q{ UPDATE ipnet_blocklist SET provider = 'Unknown' WHERE ip4_net = ? });
$sth->execute($range);
print "updated $range unknown\n";
}
else {
$asn = $result->{asn};
$provider = $result->{provider};
my $sth = $dbh->prepare(q{ UPDATE ipnet_blocklist SET asn = ?, provider = ? WHERE ip4_net = ? });
$sth->execute($asn,$provider,$range);
print "updated $range belonging to AS$asn, $provider\n";
}
$range = '';
}
# finally update the ASNs
while ($asn = $dbh->selectrow_array(q{ SELECT asn FROM asn_blocklist WHERE provider IS NULL LIMIT 1; })) {
my $result = query_asn($asn);
if (defined $result->{error}) {
print "JSON error\n";
my $sth = $dbh->prepare(q{ UPDATE asn_blocklist SET provider = 'Unknown' WHERE asn = ? });
$sth->execute($asn);
print "updated $asn unknown\n";
}
else {
$provider = $result->{provider};
my $sth = $dbh->prepare(q{ UPDATE asn_blocklist SET provider = ? WHERE asn = ? });
$sth->execute($provider,$asn);
print "updated AS$asn, $provider\n";
}
$asn = '';
}
$dbh->disconnect;