You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

asn.pl 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #!/usr/bin/env perl
  2. #
  3. use warnings;
  4. use strict;
  5. use autodie;
  6. use File::Basename;
  7. use File::Fetch;
  8. use Getopt::Long;
  9. use Pod::Usage;
  10. use FindBin;
  11. use lib "$FindBin::Bin/extlib/lib/perl5";
  12. use URI;
  13. my %config = (
  14. asn_sources => [
  15. 'ftp://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latest',
  16. 'ftp://ftp.ripe.net/ripe/stats/delegated-ripencc-latest',
  17. 'http://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-latest',
  18. 'ftp://ftp.apnic.net/pub/stats/apnic/delegated-apnic-latest',
  19. 'ftp://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-latest'
  20. ],
  21. bgp_sources => ['http://data.ris.ripe.net/rrc00/latest-bview.gz']
  22. );
  23. my $download_asn = 0;
  24. my $download_bgp = 0;
  25. my $download_target = "./";
  26. my $help = 0;
  27. my $man = 0;
  28. my $v4 = 1;
  29. my $v6 = 1;
  30. my $parse = 1;
  31. my $v4_zone = "asn.rspamd.com";
  32. my $v6_zone = "asn6.rspamd.com";
  33. my $v4_file = "asn.zone";
  34. my $v6_file = "asn6.zone";
  35. my $ns_servers = [ "asn-ns.rspamd.com", "asn-ns2.rspamd.com" ];
  36. my $unknown_placeholder = "--";
  37. GetOptions(
  38. "download-asn" => \$download_asn,
  39. "download-bgp" => \$download_bgp,
  40. "4!" => \$v4,
  41. "6!" => \$v6,
  42. "parse!" => \$parse,
  43. "target=s" => \$download_target,
  44. "zone-v4=s" => \$v4_zone,
  45. "zone-v6=s" => \$v6_zone,
  46. "file-v4=s" => \$v4_file,
  47. "file-v6=s" => \$v6_file,
  48. "ns-server=s@" => \$ns_servers,
  49. "help|?" => \$help,
  50. "man" => \$man,
  51. "unknown-placeholder" => \$unknown_placeholder,
  52. ) or
  53. pod2usage(2);
  54. pod2usage(1) if $help;
  55. pod2usage(-exitval => 0, -verbose => 2) if $man;
  56. if ($download_asn) {
  57. foreach my $u (@{ $config{'asn_sources'} }) {
  58. download_file($u);
  59. }
  60. }
  61. if ($download_bgp) {
  62. foreach my $u (@{ $config{'bgp_sources'} }) {
  63. download_file($u);
  64. }
  65. }
  66. if (!$parse) {
  67. exit 0;
  68. }
  69. # Prefix to ASN map
  70. my $networks = { 4 => {}, 6 => {} };
  71. foreach my $u (@{ $config{'bgp_sources'} }) {
  72. my $parsed = URI->new($u);
  73. my $fname = $download_target . '/' . basename($parsed->path);
  74. use constant {
  75. F_MARKER => 0,
  76. F_TIMESTAMP => 1,
  77. F_PEER_IP => 3,
  78. F_PEER_AS => 4,
  79. F_PREFIX => 5,
  80. F_AS_PATH => 6,
  81. F_ORIGIN => 7,
  82. };
  83. open(my $bgpd, '-|', "bgpdump -v -M $fname") or die "can't start bgpdump: $!";
  84. while (<$bgpd>) {
  85. chomp;
  86. my @e = split /\|/;
  87. if ($e[F_MARKER] ne 'TABLE_DUMP2') {
  88. warn "bad line: $_\n";
  89. next;
  90. }
  91. my $origin_as;
  92. my $prefix = $e[F_PREFIX];
  93. my $ip_ver = 6;
  94. if ($prefix =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d{1,2}$/) {
  95. $ip_ver = 4;
  96. }
  97. if ($e[F_AS_PATH]) {
  98. # not empty AS_PATH
  99. my @as_path = split /\s/, $e[F_AS_PATH];
  100. $origin_as = pop @as_path;
  101. if (substr($origin_as, 0, 1) eq '{') {
  102. # route is aggregated
  103. if ($origin_as =~ /^{(\d+)}$/) {
  104. # single AS aggregated, just remove { } around
  105. $origin_as = $1;
  106. } else {
  107. # use previous AS from AS_PATH
  108. $origin_as = pop @as_path;
  109. }
  110. }
  111. # strip bogus AS
  112. while (is_bougus_asn($origin_as)) {
  113. $origin_as = pop @as_path;
  114. last if scalar @as_path == 0;
  115. }
  116. }
  117. # empty AS_PATH or all AS_PATH elements was stripped as bogus - use
  118. # PEER_AS as origin AS
  119. $origin_as //= $e[F_PEER_AS];
  120. $networks->{$ip_ver}{$prefix} = int($origin_as);
  121. }
  122. }
  123. # Remove default routes
  124. delete $networks->{4}{'0.0.0.0/0'};
  125. delete $networks->{6}{'::/0'};
  126. # Now roughly detect countries
  127. my $as_info = {};
  128. # RIR statistics exchange format
  129. # https://www.apnic.net/publications/media-library/documents/resource-guidelines/rir-statistics-exchange-format
  130. # https://www.arin.net/knowledge/statistics/nro_extended_stats_format.pdf
  131. # first 7 fields for this two formats are same
  132. use constant {
  133. F_REGISTRY => 0, # {afrinic,apnic,arin,iana,lacnic,ripencc}
  134. F_CC => 1, # ISO 3166 2-letter country code
  135. F_TYPE => 2, # {asn,ipv4,ipv6}
  136. F_START => 3,
  137. F_VALUE => 4,
  138. F_DATE => 5,
  139. F_STATUS => 6,
  140. };
  141. foreach my $u (@{ $config{'asn_sources'} }) {
  142. my $parsed = URI->new($u);
  143. my $fname = $download_target . '/' . basename($parsed->path);
  144. open(my $fh, "<", $fname) or die "Cannot open $fname: $!";
  145. while (<$fh>) {
  146. next if /^\#/;
  147. chomp;
  148. my @elts = split /\|/;
  149. if ($elts[F_TYPE] eq 'asn' && $elts[F_START] ne '*') {
  150. my $as_start = int($elts[F_START]);
  151. my $as_end = $as_start + int($elts[F_VALUE]) - 1;
  152. for my $as ($as_start .. $as_end) {
  153. $as_info->{$as}{'country'} = $elts[F_CC];
  154. $as_info->{$as}{'rir'} = $elts[F_REGISTRY];
  155. }
  156. }
  157. }
  158. }
  159. # Write zone files
  160. my $ns_list = join ' ', @{$ns_servers};
  161. my $zone_header = << "EOH";
  162. \$SOA 43200 $ns_servers->[0] support.rspamd.com 0 600 300 86400 300
  163. \$NS 43200 $ns_list
  164. EOH
  165. if ($v4) {
  166. # create temp file in the same dir so we can be sure that mv is atomic
  167. my $out_dir = dirname($v4_file);
  168. my $out_file = basename($v4_file);
  169. my $temp_file = "$out_dir/.$out_file.tmp";
  170. open my $v4_fh, '>', $temp_file;
  171. print $v4_fh $zone_header;
  172. while (my ($net, $asn) = each %{ $networks->{4} }) {
  173. my $country = $as_info->{$asn}{'country'} || $unknown_placeholder;
  174. my $rir = $as_info->{$asn}{'rir'} || $unknown_placeholder;
  175. # "8.8.8.0/24 15169|8.8.8.0/24|US|arin|" for 8.8.8.8
  176. printf $v4_fh "%s %s|%s|%s|%s|\n", $net, $asn, $net, $country, $rir;
  177. }
  178. close $v4_fh;
  179. rename $temp_file, $v4_file;
  180. }
  181. if ($v6) {
  182. my $out_dir = dirname($v6_file);
  183. my $out_file = basename($v6_file);
  184. my $temp_file = "$out_dir/.$out_file.tmp";
  185. open my $v6_fh, '>', $temp_file;
  186. print $v6_fh $zone_header;
  187. while (my ($net, $asn) = each %{ $networks->{6} }) {
  188. my $country = $as_info->{$asn}{'country'} || $unknown_placeholder;
  189. my $rir = $as_info->{$asn}{'rir'} || $unknown_placeholder;
  190. # "2606:4700:4700::/48 13335|2606:4700:4700::/48|US|arin|" for 2606:4700:4700::1111
  191. printf $v6_fh "%s %s|%s|%s|%s|\n", $net, $asn, $net, $country, $rir;
  192. }
  193. close $v6_fh;
  194. rename $temp_file, $v6_file;
  195. }
  196. exit 0;
  197. ########################################################################
  198. sub download_file {
  199. my ($url) = @_;
  200. local $File::Fetch::WARN = 0;
  201. local $File::Fetch::TIMEOUT = 180; # connectivity to ftp.lacnic.net is bad
  202. my $ff = File::Fetch->new(uri => $url);
  203. my $where = $ff->fetch(to => $download_target) or
  204. die "$url: ", $ff->error;
  205. return $where;
  206. }
  207. # Returns true if AS number is bogus
  208. # e. g. a private AS.
  209. # List of allocated and reserved AS:
  210. # https://www.iana.org/assignments/as-numbers/as-numbers.txt
  211. sub is_bougus_asn {
  212. my $as = shift;
  213. # 64496-64511 Reserved for use in documentation and sample code
  214. # 64512-65534 Designated for private use
  215. # 65535 Reserved
  216. # 65536-65551 Reserved for use in documentation and sample code
  217. # 65552-131071 Reserved
  218. return 1 if $as >= 64496 && $as <= 131071;
  219. # Reserved (RFC6996, RFC7300, RFC7607)
  220. return 1 if $as == 0 || $as >= 4200000000;
  221. return 0;
  222. }
  223. __END__
  224. =head1 NAME
  225. asn.pl - download and parse ASN data for Rspamd
  226. =head1 SYNOPSIS
  227. asn.pl [options]
  228. Options:
  229. --download-asn Download ASN data from RIRs
  230. --download-bgp Download BGP full view dump from RIPE RIS
  231. --target Where to download files (default: current dir)
  232. --zone-v4 IPv4 zone (default: asn.rspamd.com)
  233. --zone-v6 IPv6 zone (default: asn6.rspamd.com)
  234. --file-v4 IPv4 zone file (default: ./asn.zone)
  235. --file-v6 IPv6 zone (default: ./asn6.zone)
  236. --unknown-placeholder Placeholder for unknown elements (default: --)
  237. --help Brief help message
  238. --man Full documentation
  239. =head1 OPTIONS
  240. =over 8
  241. =item B<--download-asn>
  242. Download ASN data from RIR.
  243. =item B<--download-bgp>
  244. Download GeoIP data from Ripe
  245. =item B<--target>
  246. Specifies where to download files.
  247. =item B<--help>
  248. Print a brief help message and exits.
  249. =item B<--man>
  250. Prints the manual page and exits.
  251. =back
  252. =head1 DESCRIPTION
  253. B<asn.pl> is intended to download ASN data and GeoIP data and create a rbldnsd zone.
  254. =cut
  255. # vim: et:ts=4:sw=4