| 1 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use v5.16.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | package Game::Tibia::Packet::Login; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # ABSTRACT: Login packet support for the MMORPG Tibia | ||||||||||||||||||||||||||||
| 8 | 3 | our $VERSION = '0.006'; # VERSION | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use File::ShareDir 'dist_file'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Crypt::OpenSSL::RSA; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Digest::Adler32; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Game::Tibia::Packet; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =pod | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =encoding utf8 | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 NAME | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | Game::Tibia::Packet::Login - Login packet support for the MMORPG Tibia | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | use Game::Tibia::Packet::Login; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | Decodes Tibia Login packets into hashes and vice versa. By default uses the OTServ RSA key, but allows different RSA keys to be supplied. Version 9.80 and above is not supported. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | my $otserv = Crypt::OpenSSL::RSA->new_private_key( | ||||||||||||||||||||||||||||
| 38 | 3 | ▶ | do { local $/; open my $rsa, dist_file('Game-Tibia-Packet', 'otserv.private') or die "Couldn't open private key $!"; <$rsa>; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 METHODS AND ARGUMENTS | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =over 4 | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =item new(version => $version, [$character => undef, packet => $packet, rsa => OTSERV]) | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | Constructs a new C<Game::Tibia::Packet::Login> instance of version C<$version>. If C<packet> is supplied, decryption using the supplied rsa private key is attempted. If no C<rsa> is supplied, the OTServ RSA key is used. If a C<$character> name is supplied, it's assumed to be a game server login packet. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub new { | ||||||||||||||||||||||||||||
| 52 | 3 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=13 SCAR=27.5
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 3 | my $self = { | |||||||||||||||||||||||||||
| 55 | packet => undef, | ||||||||||||||||||||||||||||
| 56 | rsa => $otserv, | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | @_ | ||||||||||||||||||||||||||||
| 59 | }; | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 3 | ▶ | $self->{version} //= $self->{versions}{client}{VERSION}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"version"} //= $self->{"versions"}{"client"}{"VERSION"}
MC/DC0%0/2
$self->{"version"} //= $self->{"versions"}{"client"}{"VERSION"}
$self->{"version"}
$self->{"versions"}{"client"}{"VERSION"}
Truth table33%1/3
A //= B
A= $self->{"version"}B= $self->{"versions"}{"client"}{"VERSION"}
| |||||||||||||||||||||||||||||
| 62 | 3 | ▶ | croak 'A protocol version < 9.80 must be supplied' if !defined $self->{version} || $self->{version} >= 980; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $self->{"version"} or $self->{"version"} >= 980
MC/DC0%0/2
not defined $self->{"version"} or $self->{"version"} >= 980
not defined $self->{"version"}
$self->{"version"} >= 980
Truth table33%1/3
not(A) or B
A= not defined $self->{"version"}B= $self->{"version"} >= 980
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 3 | $self->{versions}{client} = Game::Tibia::Packet::version($self->{version}); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 3 | ▶ | if ($self->{versions}{client}{rsa}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 2 | ▶ | if (defined $self->{rsa} and !blessed $self->{rsa}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->{"rsa"} and not builtin::blessed($self->{"rsa"})
MC/DC0%0/2
defined $self->{"rsa"} and not builtin::blessed($self->{"rsa"})
defined $self->{"rsa"}
not builtin::blessed($self->{"rsa"})
Truth table33%1/3
A and not(B)
A= defined $self->{"rsa"}B= not builtin::blessed($self->{"rsa"})
| |||||||||||||||||||||||||||||
| 68 | 0 | $self->{rsa} = Crypt::OpenSSL::RSA->new_private_key($self->{rsa}); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 2 | ▶ | $self->{rsa}->use_no_padding if defined $self->{rsa}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 3 | ▶ | if (defined $self->{packet}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | (my $len, $self->{os}, $self->{versions}{client}{VERSION}, $self->{versions}{spr}, $self->{versions}{dat}, $self->{versions}{pic}, my $payload) | ||||||||||||||||||||||||||||
| 75 | 2 | = unpack 'v x(S S L3)< a*', $self->{packet}; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2 | ▶ | if ($self->{versions}{client}{rsa}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 1 | $payload = $self->{rsa}->decrypt($payload); | |||||||||||||||||||||||||||
| 79 | 1 | ▶ | croak q(Decoded RSA doesn't start with zero.) if $payload !~ /^\0/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | $payload = substr $payload, 1; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2 | ▶ | if ($self->{versions}{client}{xtea}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 1 | ($self->{XTEA}, $payload) = unpack 'a16 a*', $payload; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 2 | ▶ | my $acc_data_pattern = $self->{versions}{client}{accname} ? '(S/a S/a)<' : '(V S/a)<'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | ($self->{account}, $self->{password}, $self->{hwinfo}, $self->{padding}) | ||||||||||||||||||||||||||||
| 89 | 2 | = unpack "$acc_data_pattern a47 a*", $payload; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 3 | bless $self, $class; | |||||||||||||||||||||||||||
| 93 | 3 | return $self; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =item finalize([$rsa]) | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | Finalizes the packet. encrypts with RSA and prepends header | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | sub finalize { | ||||||||||||||||||||||||||||
| 104 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
finalize: called CC=17 SCAR=31.3
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 2 | ▶ | my $rsa = shift // $self->{rsa}; | ||||||||||||||||||||||||||
|
Condition33%1/3
shift() // $self->{"rsa"}
MC/DC0%0/2
shift() // $self->{"rsa"}
shift()
$self->{"rsa"}
Truth table33%1/3
A // B
A= shift()B= $self->{"rsa"}
| |||||||||||||||||||||||||||||
| 106 | 2 | ▶ | $self->{rsa}->use_no_padding if defined $self->{rsa}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 2 | ▶ | $self->{versions}{client} = Game::Tibia::Packet::version $self->{versions}{client} unless ref $self->{versions}{client}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 2 | my $payload = ''; | |||||||||||||||||||||||||||
| 110 | 2 | ▶ | if ($self->{versions}{client}{rsa}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 1 | ▶ | $rsa = Crypt::OpenSSL::RSA->new_private_key($rsa) unless blessed $rsa; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 1 | ▶ | $rsa->size == 128 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | or croak "Protocol $self->{versions}{client}{VERSION} expects 128 bit RSA key, but ${\($rsa->size*8)} bit were provided"; | |||||||||||||||||||||||||||
| 114 | 1 | $payload .= "\0"; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 2 | ▶ | $self->{packet} = defined $self->{character} ? "\x0a" : "\x01"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 2 | $self->{packet} .= pack '(S2)<', $self->{os}, $self->{versions}{client}{VERSION}; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | $self->{packet} .= defined $self->{character} ? "\0" : | ||||||||||||||||||||||||||||
| 121 | 2 | ▶ | pack '(L3)<', $self->{versions}{spr}, $self->{versions}{dat}, $self->{versions}{pic}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 2 | ▶ | my $acc_pattern = $self->{versions}{client}{acc_name} ? '(S/a)<' : 'V'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 2 | ▶ | $payload .= $self->{XTEA} if $self->{versions}{client}{xtea}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 2 | $payload .= pack $acc_pattern, $self->{account}; | |||||||||||||||||||||||||||
| 127 | 2 | ▶ | $payload .= pack '(S/a)<', $self->{character} if defined $self->{character}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 2 | $payload .= pack '(S/a)<', $self->{password}; | |||||||||||||||||||||||||||
| 129 | 2 | ▶ | $payload .= pack 'a47', $self->{hwinfo} if defined $self->{hwinfo} && $self->{hwinfo} ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"hwinfo"} and $self->{"hwinfo"} ne ""
MC/DC50%1/2
defined $self->{"hwinfo"} and $self->{"hwinfo"} ne ""
defined $self->{"hwinfo"}
$self->{"hwinfo"} ne ""
Truth table66%2/3
A and B
A= defined $self->{"hwinfo"}B= $self->{"hwinfo"} ne ""
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 2 | ▶ | if ($self->{versions}{client}{rsa}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 1 | my $padding_len = 128 - length($payload); | |||||||||||||||||||||||||||
| 133 | 1 | ▶ | $self->{padding} //= ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"padding"} //= ""
MC/DC0%0/1
$self->{"padding"} //= ""
$self->{"padding"}
Truth table50%1/2
A
A= $self->{"padding"}
| |||||||||||||||||||||||||||||
| 134 | 1 | $payload .= pack "a$padding_len", $self->{padding}; | |||||||||||||||||||||||||||
| 135 | 1 | $payload = $self->{rsa}->encrypt($payload); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 2 | $self->{packet} .= $payload; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 2 | ▶ | if ($self->{versions}{client}{adler32}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | my $a32 = Digest::Adler32->new; | |||||||||||||||||||||||||||
| 142 | 0 | $a32->add($self->{packet}); | |||||||||||||||||||||||||||
| 143 | 0 | my $digest = unpack 'H*', pack 'N', unpack 'L', $a32->digest; | |||||||||||||||||||||||||||
| 144 | 0 | $self->{packet} = $digest.$self->{packet}; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 2 | $self->{packet} = pack("S/a", $self->{packet}); | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 2 | $self->{packet}; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 3 | 1; | |||||||||||||||||||||||||||
| 153 | __END__ | ||||||||||||||||||||||||||||