| 1 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | 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 | 4 | our $VERSION = '0.007'; # VERSION | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use File::ShareDir 'dist_file'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Crypt::OpenSSL::RSA; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Digest::Adler32; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Game::Tibia::Packet; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use constant GET_CHARLIST => 0x01; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use constant LOGIN_CHAR => 0x0A; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =pod | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =encoding utf8 | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 NAME | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | Game::Tibia::Packet::Login - Login packet support for the MMORPG Tibia | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | use Game::Tibia::Packet::Login; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 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. | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 4 | our %params; | |||||||||||||||||||||||||||
| 41 | 3 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||
| 42 | 3 | (undef, %params) = (shift, %params, @_); | |||||||||||||||||||||||||||
| 43 | 3 | ▶ | die "Malformed Tibia version\n" if exists $params{tibia} && $params{tibia} !~ /^\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $Game::Tibia::Packet::Login::params{"tibia"} and not $Game::Tibia::Packet::Login::params{"tibia"} =~ /^\d+$/u
MC/DC0%0/2
exists $Game::Tibia::Packet::Login::params{"tibia"} and not $Game::Tibia::Packet::Login::params{"tibia"} =~ /^\d+$/u
exists $Game::Tibia::Packet::Login::params{"tibia"}
not $Game::Tibia::Packet::Login::params{"tibia"} =~ /^\d+$/u
Truth table66%2/3
A and not(B)
A= exists $Game::Tibia::Packet::Login::params{"tibia"}B= not $Game::Tibia::Packet::Login::params{"tibia"} =~ /^\d+$/u
| |||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | my $otserv = Crypt::OpenSSL::RSA->new_private_key( | ||||||||||||||||||||||||||||
| 47 | 4 | ▶ | do { local $/; open my $rsa, '<', dist_file('Game-Tibia-Packet', 'otserv.private') or die "Couldn't open private key $!"; <$rsa>; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head1 METHODS AND ARGUMENTS | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =over 4 | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =item new(version => $version, [$character => undef, packet => $packet, rsa => OTSERV]) | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 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. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=20 SCAR=34.8
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 3 | my $class = shift; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 3 | my $self = { | |||||||||||||||||||||||||||
| 64 | packet => undef, | ||||||||||||||||||||||||||||
| 65 | rsa => $otserv, | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | @_ | ||||||||||||||||||||||||||||
| 68 | }; | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 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"}
| |||||||||||||||||||||||||||||
| 71 | 3 | ▶ | $self->{version} //= $params{tibia}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"version"} //= $Game::Tibia::Packet::Login::params{"tibia"}
MC/DC0%0/2
$self->{"version"} //= $Game::Tibia::Packet::Login::params{"tibia"}
$self->{"version"}
$Game::Tibia::Packet::Login::params{"tibia"}
Truth table33%1/3
A //= B
A= $self->{"version"}B= $Game::Tibia::Packet::Login::params{"tibia"}
| |||||||||||||||||||||||||||||
| 72 | 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
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 3 | $self->{versions}{client} = Game::Tibia::Packet::version($self->{version}); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 3 | ▶ | if ($self->{versions}{client}{rsa}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 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"})
| |||||||||||||||||||||||||||||
| 78 | 0 | $self->{rsa} = Crypt::OpenSSL::RSA->new_private_key($self->{rsa}); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 2 | ▶ | $self->{rsa}->use_no_padding if defined $self->{rsa}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 3 | ▶ | if (defined $self->{packet}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | (my $len, my $cmd, $self->{os}, $self->{versions}{client}{VERSION}, my $payload) | ||||||||||||||||||||||||||||
| 85 | 2 | = unpack 'v C (S S)< a*', $self->{packet}; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 2 | ▶ | croak "Expected GET_CHARLIST (0x01) or LOGIN_CHAR (0x0A) packet type, but got $cmd" if $cmd ne GET_CHARLIST and $cmd ne LOGIN_CHAR; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$cmd ne 1 and $cmd ne 10
MC/DC0%0/2
$cmd ne 1 and $cmd ne 10
$cmd ne 1
$cmd ne 10
Truth table33%1/3
A and B
A= $cmd ne 1B= $cmd ne 10
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 2 | ▶ | if ($cmd == GET_CHARLIST) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 2 | ($self->{versions}{spr}, $self->{versions}{dat}, $self->{versions}{pic}, $payload) | |||||||||||||||||||||||||||
| 91 | = unpack('(L3)< a*', $payload); | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 2 | ▶ | if ($self->{versions}{client}{rsa}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 1 | $payload = $self->{rsa}->decrypt($payload); | |||||||||||||||||||||||||||
| 96 | 1 | ▶ | croak q(Decoded RSA doesn't start with zero.) if $payload !~ /^\0/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 1 | $payload = substr $payload, 1; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 2 | ▶ | if ($self->{versions}{client}{xtea}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 1 | ($self->{xtea}, $payload) = unpack 'a16 a*', $payload; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 2 | ▶ | if ($cmd == LOGIN_CHAR) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | ($self->{gmflag}, $payload) = unpack "C a*", $payload; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 2 | ▶ | my $acc_data_pattern = $self->{versions}{client}{acc_name} ? '(S/a)<' : 'V'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 2 | ($self->{account}, $payload) = unpack "$acc_data_pattern a*", $payload; | |||||||||||||||||||||||||||
| 110 | 2 | ▶ | if ($cmd == LOGIN_CHAR) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | ($self->{character}, $payload) = unpack "(S/a)< a*", $payload; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 2 | ($self->{password}, $payload) = unpack "(S/a)< a*", $payload; | |||||||||||||||||||||||||||
| 114 | 2 | ▶ | if ($cmd == LOGIN_CHAR) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | ($self->{nonce}, $payload) = unpack "(a5) a*", $payload; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 2 | $self->{undecoded} = unpack "a*", $payload; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 3 | bless $self, $class; | |||||||||||||||||||||||||||
| 121 | 3 | return $self; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =item finalize([$rsa]) | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Finalizes the packet. encrypts with RSA and prepends header | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =cut | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 2 | ▶ | sub finalize { | ||||||||||||||||||||||||||
|
Subroutine
finalize: called CC=18 SCAR=32.1
Pod
documented
| |||||||||||||||||||||||||||||
| 132 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 133 | 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"}
| |||||||||||||||||||||||||||||
| 134 | 2 | ▶ | $self->{rsa}->use_no_padding if defined $self->{rsa}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 2 | ▶ | $self->{versions}{client} = Game::Tibia::Packet::version $self->{versions}{client} unless ref $self->{versions}{client}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 2 | my $payload = ''; | |||||||||||||||||||||||||||
| 138 | 2 | ▶ | if ($self->{versions}{client}{rsa}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 1 | ▶ | $rsa = Crypt::OpenSSL::RSA->new_private_key($rsa) unless blessed $rsa; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 1 | ▶ | $rsa->size == 128 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | or croak "Protocol $self->{versions}{client}{VERSION} expects 128 bit RSA key, but ${\($rsa->size*8)} bit were provided"; | |||||||||||||||||||||||||||
| 142 | 1 | $payload .= "\0"; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 2 | ▶ | $self->{packet} = defined $self->{character} ? "\x0a" : "\x01"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 2 | $self->{packet} .= pack '(S2)<', $self->{os}, $self->{versions}{client}{VERSION}; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | $self->{packet} .= defined $self->{character} ? "\0" : | ||||||||||||||||||||||||||||
| 149 | 2 | ▶ | pack '(L3)<', $self->{versions}{spr}, $self->{versions}{dat}, $self->{versions}{pic}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 2 | ▶ | my $acc_pattern = $self->{versions}{client}{acc_name} ? '(S/a)<' : 'V'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 2 | ▶ | $payload .= $self->{xtea} if $self->{versions}{client}{xtea}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 2 | ▶ | $payload .= pack "C", $self->{gmflag} if defined $self->{gmflag}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 2 | $payload .= pack $acc_pattern, $self->{account}; | |||||||||||||||||||||||||||
| 156 | 2 | ▶ | $payload .= pack '(S/a)<', $self->{character} if defined $self->{character}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 2 | $payload .= pack '(S/a)<', $self->{password}; | |||||||||||||||||||||||||||
| 158 | 2 | ▶ | $payload .= pack 'a5', $self->{nonce} if defined $self->{nonce}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 2 | ▶ | $payload .= pack 'a*', $self->{undecoded} if defined $self->{undecoded} && $self->{undecoded} ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"undecoded"} and $self->{"undecoded"} ne ""
MC/DC50%1/2
defined $self->{"undecoded"} and $self->{"undecoded"} ne ""
defined $self->{"undecoded"}
$self->{"undecoded"} ne ""
Truth table66%2/3
A and B
A= defined $self->{"undecoded"}B= $self->{"undecoded"} ne ""
| |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 2 | ▶ | if ($self->{versions}{client}{rsa}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 1 | my $padding_len = 128 - length($payload); | |||||||||||||||||||||||||||
| 163 | 1 | $payload .= pack "a$padding_len", ''; | |||||||||||||||||||||||||||
| 164 | 1 | $payload = $self->{rsa}->encrypt($payload); | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 2 | $self->{packet} .= $payload; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 2 | ▶ | if ($self->{versions}{client}{adler32}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 0 | my $a32 = Digest::Adler32->new; | |||||||||||||||||||||||||||
| 171 | 0 | $a32->add($self->{packet}); | |||||||||||||||||||||||||||
| 172 | 0 | my $digest = pack "N", unpack "L", $a32->digest; | |||||||||||||||||||||||||||
| 173 | 0 | $self->{packet} = $digest.$self->{packet}; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 2 | $self->{packet} = pack("(S/a)<", $self->{packet}); | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 2 | $self->{packet}; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 4 | 1; | |||||||||||||||||||||||||||
| 182 | __END__ | ||||||||||||||||||||||||||||