aboutsummaryrefslogtreecommitdiffstats
path: root/utils/asn.pl
blob: d78277459a1a7387f015ad9432f9b565cebe46d0 (plain)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/env perl

use warnings;
use strict;
use Pod::Usage;
use Getopt::Long;
use File::Fetch;
use LWP::Simple;
use PerlIO::gzip;
use File::Basename;
use URI;
use Data::Dumper;

$LWP::Simple::ua->show_progress(1);

my %config = (
    asn_sources => [
        'ftp://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latest',
        'ftp://ftp.ripe.net/ripe/stats/delegated-ripencc-latest',
        'ftp://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-latest',
        'ftp://ftp.apnic.net/pub/stats/apnic/delegated-apnic-latest',
        'ftp://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-latest'
    ],
    bgp_sources => ['http://data.ris.ripe.net/rrc00/latest-bview.gz']
);

my $download_asn    = 0;
my $download_bgp    = 0;
my $download_target = "./";
my $help            = 0;
my $man             = 0;
my $v4              = 1;
my $v6              = 1;
my $parse           = 1;
my $v4_zone         = "asn.rspamd.com";
my $v6_zone         = "asn6.rspamd.com";
my $v4_file         = "asn.zone";
my $v6_file         = "asn6.zone";
my $ns_servers      = [ "asn-ns.rspamd.com", "asn-ns2.rspamd.com" ];
my $use_bgpdump     = 0;

GetOptions(
    "download-asn" => \$download_asn,
    "download-bgp" => \$download_bgp,
    "4!"           => \$v4,
    "6!"           => \$v6,
    "parse!"       => \$parse,
    "target=s"     => \$download_target,
    "zone-v4=s"    => \$v4_zone,
    "zone-v6=s"    => \$v6_zone,
    "file-v4=s"    => \$v4_file,
    "file-v6=s"    => \$v6_file,
    "ns-server=s@" => \$ns_servers,
    "help|?"       => \$help,
    "man"          => \$man,
) or pod2usage(2);

pod2usage(1) if $help;
pod2usage( -exitval => 0, -verbose => 2 ) if $man;

if ( -x bgpdump ) {
    use_bgpdump = $1;
} else {
    warn "bgpdump is not found will try to use Net::MRT instead, results can be incomplete";
}


sub download_file {
    my ($u) = @_;

    print "Fetching $u\n";
    my $ff    = File::Fetch->new( uri => $u );
    my $where = $ff->fetch( to => $download_target ) or die $ff->error;

    return $where;
}

if ($download_asn) {
    foreach my $u ( @{ $config{'asn_sources'} } ) {
        download_file($u);
    }
}

if ($download_bgp) {
    foreach my $u ( @{ $config{'bgp_sources'} } ) {
        download_file($u);
    }
}

if ( !$parse ) {
    exit 0;
}

my $v4_fh;
my $v6_fh;

if ($v4) {
    open( $v4_fh, ">", $v4_file ) or die "Cannot open $v4_file for writing: $!";
    print $v4_fh "\$SOA 43200 $ns_servers->[0] support.rspamd.com 0 600 300 86400 300\n";
    foreach my $ns ( @{$ns_servers} ) {
        print $v4_fh "\$NS 43200 $ns\n";
    }
}
if ($v6) {
    open( $v6_fh, ">", $v6_file ) or die "Cannot open $v6_file for writing: $!";
    print $v6_fh "\$SOA 43200 $ns_servers->[0] support.rspamd.com 0 600 300 86400 300\n";
    foreach my $ns ( @{$ns_servers} ) {
        print $v6_fh "\$NS 43200 $ns\n";
    }
}

sub is_bougus_asn {
    my $as = shift;
    # 64496-64511 Reserved for use in documentation and sample code
    # 64512-65534 Designated for private use
    # 65535       Reserved
    # 65536-65551 Reserved for use in documentation and sample code
    # 65552-131071 Reserved
    return 1 if $as >= 64496 && $as <= 131071;
    # 4294967295
    return 1 if $as == 4294967295;
    # AS0 is reserved
    # AS1 is legal AS, but in most cases used by others without permission
    # of owner (probably lame admins use AS1 as private AS).
    return 1 if $as <= 1;

    return 0;
}

# Now load BGP data
my $networks = {};

foreach my $u ( @{ $config{'bgp_sources'} } ) {
    my $parsed = URI->new($u);
    my $fname  = $download_target . '/' . basename( $parsed->path );

    if ($use_bgpdump) {
        use constant {
          F_MARKER => 0,
          F_TIMESTAMP => 1,
          F_PEER_IP => 3,
          F_PEER_AS => 4,
          F_PREFIX => 5,
          F_AS_PATH => 6,
          F_ORIGIN => 7,
        };

        open(my $bgpd, '-|', "bgpdump -v -M $fname") or die "can't start bgpdump: $!";

        while (<$bgpd>) {
            chomp;
            my @e = split /\|/;
            if ($e[F_MARKER] ne 'TABLE_DUMP2') {
                warn "bad line: $_\n";
                next;
            }

            my $origin_as;
            my $prefix = $e[F_PREFIX];
            my $ipv6 = 0;

            if ($prefix !~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d{1,2}$/) {
                $ipv6 = 1;
            }

            if ($e[F_AS_PATH]) {
                # not empty AS_PATH
                my @as_path = split /\s/, $e[F_AS_PATH];
                $origin_as = pop @as_path;

                if (substr($origin_as, 0, 1) eq '{') {
                    # route is aggregated
                    if ($origin_as =~ /^{(\d+)}$/) {
                        # single AS aggregated, just remove { } around
                        $origin_as = $1;
                    } else {
                        # use previous AS from AS_PATH
                        $origin_as = pop @as_path;
                    }
                }
                # strip bogus AS
                while (is_bougus_asn($origin_as)) {
                    $origin_as = pop @as_path;
                    last if scalar @as_path == 0;
                }
            }
            # empty AS_PATH or all AS_PATH elements is stripped as bogus - use PEER_AS is origin AS
            $origin_as //= $e[F_PEER_AS];

            if (!$networks->{$origin_as}) {
                if (!$ipv6) {
                    $networks->{$origin_as} = { nets_v4 => [ $prefix ], nets_v6 => [] };
                }
                else {
                    $networks->{$origin_as} = { nets_v6 => [ $prefix ], nets_v4 => [] };
                }
            }
            else {
                if (!$ipv6) {
                    push @{$networks->{$origin_as}->{'nets_v4'}}, $prefix;
                }
                else {
                    push @{$networks->{$origin_as}->{'nets_v6'}}, $prefix;
                }
            }
        }
    }
    else {
        require Net::MRT;
        $Net::MRT::USE_RFC4760 = -1;

        open( my $fh, "<:gzip", $fname )
          or die "Cannot open $fname: $!";
        while (my $dd = eval {Net::MRT::mrt_read_next($fh)}) {
            if ($dd->{'prefix'} && $dd->{'bits'}) {
                next if $dd->{'subtype'} == 2 and !$v4;
                next if $dd->{'subtype'} == 4 and !$v6;
                my $entry = $dd->{'entries'}->[0];
                my $net = $dd->{'prefix'} . '/' . $dd->{'bits'};
                if ($entry && $entry->{'AS_PATH'}) {
                    my $as = $entry->{'AS_PATH'}->[-1];
                    if (ref($as) eq "ARRAY") {
                        $as = @{$as}[0];
                    }

                    next if (is_bougus_asn($as));

                    if (!$networks->{$as}) {
                        if ($dd->{'subtype'} == 2) {
                            $networks->{$as} = { nets_v4 => [ $net ], nets_v6 => [] };
                        }
                        else {
                            $networks->{$as} = { nets_v6 => [ $net ], nets_v4 => [] };
                        }
                    }
                    else {

                        if ($dd->{'subtype'} == 2) {
                            push @{$networks->{$as}->{'nets_v4'}}, $net;
                        }
                        else {
                            push @{$networks->{$as}->{'nets_v6'}}, $net;
                        }
                    }
                }
            }
        }
    }
}

# Now roughly detect countries
foreach my $u ( @{ $config{'asn_sources'} } ) {
    my $parsed = URI->new($u);
    my $fname  = $download_target . '/' . basename( $parsed->path );
    open( my $fh, "<", $fname ) or die "Cannot open $fname: $!";

    while (<$fh>) {
        next if /^\#/;
        chomp;
        my @elts = split /\|/;

        if ( $elts[2] eq 'asn' && $elts[3] ne '*' ) {
            my $as_start = int( $elts[3] );
            my $as_end   = $as_start + int( $elts[4] );

            for ( my $as = $as_start ; $as < $as_end ; $as++ ) {
                my $real_as = $as;

                if ( ref($as) eq "ARRAY" ) {
                    $real_as = @{$as}[0];
                }

                if ( $networks->{"$real_as"} ) {
                    $networks->{"$real_as"}->{'country'} = $elts[1];
                    $networks->{"$real_as"}->{'rir'}     = $elts[0];
                }
            }
        }
    }
}

while ( my ( $k, $v ) = each( %{$networks} ) ) {
    if ($v4) {
        foreach my $n ( @{ $v->{'nets_v4'} } ) {

            # "15169 | 8.8.8.0/24 | US | arin |" for 8.8.8.8
            if ( $v->{'country'} ) {
                printf $v4_fh "%s %s|%s|%s|%s|\n", $n, $k, $n, $v->{'country'}, $v->{'rir'};
            }
            else {
                printf $v4_fh "%s %s|%s|%s|%s|\n", $n, $k, $n, 'UN', 'UN';
            }
        }
    }
    if ($v6) {
        foreach my $n ( @{ $v->{'nets_v6'} } ) {

            # "15169 | 8.8.8.0/24 | US | arin |" for 8.8.8.8
            if ( $v->{'country'} ) {
                printf $v6_fh "%s %s|%s|%s|%s|\n", $n, $k, $n, $v->{'country'}, $v->{'rir'};
            }
            else {
                printf $v6_fh "%s %s|%s|%s|%s|\n", $n, $k, $n, 'UN', 'UN';
            }
        }
    }
}

__END__

=head1 NAME

asn.pl - download and parse ASN data for Rspamd

=head1 SYNOPSIS

asn.pl [options]

 Options:
   --download-asn         Download ASN data from RIR
   --download-bgp         Download GeoIP data from Maxmind
   --target               Where to download files (default: current dir)
   --zone-v4              IPv4 zone (default: asn.rspamd.com)
   --zone-v6              IPv6 zone (default: asn6.rspamd.com)
   --file-v4              IPv4 zone file (default: ./asn.zone)
   --file-v6              IPv6 zone (default: ./asn6.zone)
   --help                 Brief help message
   --man                  Full documentation

=head1 OPTIONS

=over 8

=item B<--download-asn>

Download ASN data from RIR.

=item B<--download-bgp>

Download GeoIP data from Ripe

=item B<--target>

Specifies where to download files.

=item B<--help>

Print a brief help message and exits.

=item B<--man>

Prints the manual page and exits.

=back

=head1 DESCRIPTION

B<asn.pl> is intended to download ASN data and GeoIP data and create a rbldnsd zone.

=cut