| 1 | package VPNDetection::Bogon; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Socket qw(AF_INET AF_INET6 inet_pton); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use VPNDetection::Bogons; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use VPNDetection::Result; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 7 | our $VERSION = '3.14.0'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # Whether an address is private, loopback, link-local, documentation, multicast | ||||||||||||||||||||||||||||
| 14 | # or otherwise not routable on the public internet, including the IPv6 | ||||||||||||||||||||||||||||
| 15 | # equivalents and the 6to4 and Teredo ranges that wrap them. | ||||||||||||||||||||||||||||
| 16 | # | ||||||||||||||||||||||||||||
| 17 | # These can never be VPN or proxy infrastructure, so the client answers them | ||||||||||||||||||||||||||||
| 18 | # itself and they never cost a request. | ||||||||||||||||||||||||||||
| 19 | 28040 | ▶ | sub is_bogon { | ||||||||||||||||||||||||||
|
Subroutine
is_bogon: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 20 | 28040 | my ($ip) = @_; | |||||||||||||||||||||||||||
| 21 | 28040 | ▶ | return 0 unless defined $ip && length $ip; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ip and length $ip
MC/DC50%1/2
defined $ip and length $ip
defined $ip
length $ip
Truth table66%2/3
A and B
A= defined $ipB= length $ip
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 28039 | $ip = unmapped($ip); | |||||||||||||||||||||||||||
| 24 | 28039 | my $v6 = index($ip, ':') >= 0; | |||||||||||||||||||||||||||
| 25 | 28039 | ▶ | my $addr = _pton($v6 ? AF_INET6 : AF_INET, $ip); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 28039 | ▶ | return 0 unless defined $addr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 28036 | ▶ | for my $range (@{ $v6 ? _v6() : _v4() }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 449068 | ▶ | return 1 if ($addr & $range->[1]) eq $range->[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 27958 | return 0; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # The IPv4 address an IPv4-mapped IPv6 address (::ffff:a.b.c.d, in any spelling) | ||||||||||||||||||||||||||||
| 35 | # carries, dotted, and any other string as given. A server listening on :: sees | ||||||||||||||||||||||||||||
| 36 | # every IPv4 visitor in that form, which read whole is inside ::ffff:0:0/96, so | ||||||||||||||||||||||||||||
| 37 | # judging it whole would answer every such visitor locally as a bogon. ::a.b.c.d | ||||||||||||||||||||||||||||
| 38 | # is IPv4-compatible rather than mapped, and stays IPv6. | ||||||||||||||||||||||||||||
| 39 | 83523 | ▶ | sub unmapped { | ||||||||||||||||||||||||||
|
Subroutine
unmapped: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 83523 | my ($ip) = @_; | |||||||||||||||||||||||||||
| 41 | 83523 | ▶ | return $ip unless defined $ip && index($ip, ':') >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ip and index($ip, ":") != -1
MC/DC50%1/2
defined $ip and index($ip, ":") != -1
defined $ip
index($ip, ":") != -1
Truth table66%2/3
A and B
A= defined $ipB= index($ip, ":") != -1
| |||||||||||||||||||||||||||||
| 42 | 74 | my $addr = _pton(AF_INET6, $ip); | |||||||||||||||||||||||||||
| 43 | 74 | ▶ | return $ip unless defined $addr && substr($addr, 0, 12) eq ("\0" x 10) . "\xff\xff"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $addr and substr($addr, 0, 12) eq "\0\0\0\0\0\0\0\0\0\0\377\377"
MC/DC50%1/2
defined $addr and substr($addr, 0, 12) eq "\0\0\0\0\0\0\0\0\0\0\377\377"
defined $addr
substr($addr, 0, 12) eq "\0\0\0\0\0\0\0\0\0\0\377\377"
Truth table66%2/3
A and B
A= defined $addrB= substr($addr, 0, 12) eq "\0\0\0\0\0\0\0\0\0\0\377\377"
| |||||||||||||||||||||||||||||
| 44 | 21 | return join '.', unpack 'C4', substr($addr, 12); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # The answer a bogon gets, in the full shape the API serves at its widest plan: | ||||||||||||||||||||||||||||
| 48 | # every flag present and false, every detail object present and empty, and a | ||||||||||||||||||||||||||||
| 49 | # location and AS of empty strings and AS 0. | ||||||||||||||||||||||||||||
| 50 | 6 | ▶ | sub bogon_result { | ||||||||||||||||||||||||||
|
Subroutine
bogon_result: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | 6 | my ($ip) = @_; | |||||||||||||||||||||||||||
| 52 | 6 | my %answer = (ip => $ip, is_bogon => 1); | |||||||||||||||||||||||||||
| 53 | 6 | $answer{$_} = 0 for @VPNDetection::Result::FLAGS; | |||||||||||||||||||||||||||
| 54 | 6 | $answer{$_} = {} for @VPNDetection::Result::DETAILS; | |||||||||||||||||||||||||||
| 55 | 6 | $answer{location} = { map { $_ => '' } @VPNDetection::Result::LOCATION }; | |||||||||||||||||||||||||||
| 56 | 6 | ▶ | $answer{asn} = { map { $_ => $_ eq 'asn' ? 0 : '' } @VPNDetection::Result::ASN }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 6 | return VPNDetection::Result->_new(\%answer, {}); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # Parsed on first use rather than at load: a program that never looks an address | ||||||||||||||||||||||||||||
| 61 | # up should not pay for the table. | ||||||||||||||||||||||||||||
| 62 | 7 | my ($v4, $v6); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 27985 | ▶ | sub _v4 { | ||||||||||||||||||||||||||
|
Subroutine
_v4: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 65 | 27985 | ▶ | $v4 ||= [map { _range(AF_INET, $_, 4) } @VPNDetection::Bogons::V4]; | ||||||||||||||||||||||||||
|
Condition100%2/2
$v4 ||= [map({_range(2, $_, 4);} @VPNDetection::Bogons::V4)]
MC/DC100%1/1
$v4 ||= [map({_range(2, $_, 4);} @VPNDetection::Bogons::V4)]
$v4
Truth table100%2/2
A
A= $v4
| |||||||||||||||||||||||||||||
| 66 | 27985 | return $v4; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 51 | ▶ | sub _v6 { | ||||||||||||||||||||||||||
|
Subroutine
_v6: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 70 | 51 | ▶ | $v6 ||= [map { _range(AF_INET6, $_, 16) } @VPNDetection::Bogons::V6]; | ||||||||||||||||||||||||||
|
Condition100%2/2
$v6 ||= [map({_range(10, $_, 16);} @VPNDetection::Bogons::V6)]
MC/DC100%1/1
$v6 ||= [map({_range(10, $_, 16);} @VPNDetection::Bogons::V6)]
$v6
Truth table100%2/2
A
A= $v6
| |||||||||||||||||||||||||||||
| 71 | 51 | return $v6; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # A range is [masked network bytes, mask bytes], both packed strings, so | ||||||||||||||||||||||||||||
| 75 | # membership is one bitwise string AND and one string comparison. Perl has no | ||||||||||||||||||||||||||||
| 76 | # 128 bit integer, and it does not need one: `&` on two byte strings is a | ||||||||||||||||||||||||||||
| 77 | # per-byte AND, so a /36 IPv6 mask is just a 16 byte string. | ||||||||||||||||||||||||||||
| 78 | # | ||||||||||||||||||||||||||||
| 79 | # That behavior is why no file in this distribution says `use v5.28` or later: | ||||||||||||||||||||||||||||
| 80 | # those enable the `bitwise` feature, under which `&` becomes numeric-only, both | ||||||||||||||||||||||||||||
| 81 | # operands numify to 0, and EVERY address would match EVERY range. The isBogon | ||||||||||||||||||||||||||||
| 82 | # corpus is what catches it. | ||||||||||||||||||||||||||||
| 83 | 240 | ▶ | sub _range { | ||||||||||||||||||||||||||
|
Subroutine
_range: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 84 | 240 | my ($family, $cidr, $width) = @_; | |||||||||||||||||||||||||||
| 85 | 240 | my ($net, $bits) = split m{/}, $cidr, 2; | |||||||||||||||||||||||||||
| 86 | 240 | my $mask = _mask($bits, $width); | |||||||||||||||||||||||||||
| 87 | 240 | return [inet_pton($family, $net) & $mask, $mask]; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 240 | ▶ | sub _mask { | ||||||||||||||||||||||||||
|
Subroutine
_mask: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 91 | 240 | my ($bits, $width) = @_; | |||||||||||||||||||||||||||
| 92 | 240 | my $mask = "\xff" x int($bits / 8); | |||||||||||||||||||||||||||
| 93 | 240 | my $partial = $bits % 8; | |||||||||||||||||||||||||||
| 94 | 240 | ▶ | $mask .= chr((0xff << (8 - $partial)) & 0xff) if $partial; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 240 | return $mask . "\x00" x ($width - length $mask); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # inet_pton is strict: it rejects a prefix, a shortened dotted quad and junk, so | ||||||||||||||||||||||||||||
| 99 | # no separate validation is needed. It dies rather than returning undef on some | ||||||||||||||||||||||||||||
| 100 | # platforms, hence the eval. | ||||||||||||||||||||||||||||
| 101 | 28113 | ▶ | sub _pton { | ||||||||||||||||||||||||||
|
Subroutine
_pton: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 102 | 28113 | my ($family, $ip) = @_; | |||||||||||||||||||||||||||
| 103 | 28113 | my $packed = eval { inet_pton($family, $ip) }; | |||||||||||||||||||||||||||
| 104 | 28113 | ▶ | return undef unless defined $packed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 28110 | return $packed; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 7 | 1; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||||||||||