-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpacket-tibia.c
2764 lines (2486 loc) · 105 KB
/
packet-tibia.c
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* packet-tibia.c
* Routines for Tibia/OTServ login and game protocol dissection
*
* Copyright 2017, Ahmad Fatoum <ahmad[AT]a3f.at>
*
* A dissector for:
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* Tibia (https://tibia.com) is a Massively Multiplayer Online Role-Playing
* Game (MMORPG) by Cipsoft GmbH.
*
* Three official clients exist: The current Qt-based 11.0+ client,
* the old C++ client used from Tibia 7.0 till 10.99 and the Flash client.
* The latter two are being phased out. They use the same protocol,
* except that the session key for the Flash client is transported alongside
* the character list over HTTPS. It's possible this is done in the same manner
* as in the native client from 10.74 up. We don't support the Flash client.
*
* The dissector supports Tibia versions from 7.0 (2001) till
* 11.00 (2016-10-12). Tibia has an active open source server emulator
* community (OTServ) that still makes use of older versions and surpasses
* the official servers in popularity, therefore compatibility with older
* protocol iterations should be maintained.
*
* Transport is over TCP, with recent versions encrypting player interaction
* with XTEA. Authentication and key exchange is done with a hard-coded
* RSA public key in the client.
*
* Two protocols are dissected: The Tibia login protocol and the Tibia game
* protocol. Traditionally, login servers were stateless and only responsible
* for providing the addresses of the game servers alongside the character
* list upon successful authentication. Then a new authentication request
* (this time with character selection) is sent to the game server.
* That way, a client who knows the game server address can very well skip
* the login server entirely. Starting with 10.61, this is no longer possible,
* as the login server provides a session key that needs to be sent to the
* game server.
*
* Starting with Tibia 7.61, login server requests can't be reliably
* differentiated from game server requests. Therefore we apply some heuristics
* to classify packets.
*
* Starting with Tibia 11.01, a web service takes the role of the login server.
* Starting with Tibia 11.11, the Adler32 checksum was replaced by a 32-bit
* sequence number. The most significant bit indicates whether the packet was
* DEFLATE-compressed. These features are not yet supported.
*
* Packets from and to the game server contain commands. Commands are
* identified by the first octet and are variable in length. The dissector has
* most command names hard-coded. However, a complete implementation of the
* game protocol is unlikely.
*
* The RSA private key usually used by OTServ is hard-coded in. Server
* administrators may add their own private key in PEM or PKCS#12 format over
* an UAT. For servers where the private key is indeed private (like
* for official servers), the symmetric XTEA key (retrievable by memory
* peeking or MitM) may be provided to the dissector via UAT.
*
* Unsurprisingly, no official specification of the protocol exist, following
* resources have been written by the community:
*
* - OTServ: Community effort to replicate a Tibia Server.
* - Outcast: A Tibia client implementation of the game protocol as of 2006.
* Comes with a PDF spec written by Khaos
* - TibiaAPI: Bot framework, containing a listing of commands as of 2009
* - TFS: OTServ-Fork which is kept up-to-date with most of the official protocol
* - otclient: Open Source implementation of an up-to-date Tibia client
*
* An official slide set by Cipsoft detailing the architecture of Tibia
* from Game Developers Conference Europe 2011 is also available:
* http://www.gdcvault.com/play/1014908/Inside-Tibia-The-Technical-Infrastructure
*
* The login protocol, as implemented here, has been inferred from network
* footage and game client execution traces and was written from scratch.
* The listing of game protocol commands were taken from TibiaAPI and Khaos' spec
* No code of Cipsoft GmbH was used.
*
* Tibia is a registered trademark of Cipsoft GmbH.
*/
#include "config.h"
#include <epan/packet.h>
#include "packet-tcp.h"
#include <wsutil/adler32.h>
#include <epan/address.h>
#include <epan/to_str.h>
#include <epan/prefs.h>
#include <epan/uat.h>
#include <epan/conversation.h>
#include <epan/value_string.h>
#include <epan/expert.h>
#include <epan/address.h>
#include <wsutil/filesystem.h>
#include <wsutil/file_util.h>
#include <wsutil/wsgcrypt.h>
#include <wsutil/report_message.h>
#include <wsutil/xtea.h>
#include <wsutil/strtoi.h>
#include <wsutil/rsa.h>
#include <errno.h>
#include <wsutil/ws_printf.h>
#include <epan/ptvcursor.h>
void proto_register_tibia(void);
void proto_reg_handoff_tibia(void);
/* preferences */
static gboolean try_otserv_key = TRUE,
show_char_name = TRUE,
show_acc_info = TRUE,
show_xtea_key = FALSE,
dissect_game_commands = FALSE,
reassemble_tcp_segments = TRUE;
/* User Access Tables */
struct rsakeys_assoc {
char *ipaddr;
char *port;
char *keyfile;
char *password;
};
UAT_CSTRING_CB_DEF(rsakeylist_uats, ipaddr, struct rsakeys_assoc)
UAT_CSTRING_CB_DEF(rsakeylist_uats, port, struct rsakeys_assoc)
UAT_FILENAME_CB_DEF(rsakeylist_uats, keyfile, struct rsakeys_assoc)
UAT_CSTRING_CB_DEF(rsakeylist_uats, password, struct rsakeys_assoc)
#define XTEA_KEY_LEN 16
struct xteakeys_assoc {
guint32 framenum;
char *key;
};
static void *xteakeys_copy_cb(void *, const void *, size_t);
static void xteakeys_free_cb(void *);
static void xtea_parse_uat(void);
static gboolean xteakeys_uat_fld_key_chk_cb(void *, const char *, guint, const void *, const void *, char **);
UAT_DEC_CB_DEF(xteakeylist_uats, framenum, struct xteakeys_assoc)
UAT_CSTRING_CB_DEF(xteakeylist_uats, key, struct xteakeys_assoc)
#define COND_POISONED 0x1
#define COND_BURNING 0x2
#define COND_ELECTROCUTED 0x4
#define COND_DRUNK 0x8
#define COND_MANASHIELD 0x10
#define COND_PARALYZED 0x20
#define COND_HASTE 0x40
#define COND_BATTLE 0x80
#define COND_DROWNING 0x100
#define COND_FREEZING 0x200
#define COND_DAZZLED 0x400
#define COND_CURSED 0x800
#define COND_BUFF 0x1000
#define COND_PZBLOCK 0x2000
#define COND_PZ 0x4000
#define COND_BLEEDING 0x8000
#define COND_HUNGRY 0x10000
/* The login server has been traditionally on 7171,
* For OTServ, the game server often listens on the same IP/port,
* but occasionally on 7172. Official Tibia doesn't host login and
* game servers on the same IP address
*/
#define TIBIA_DEFAULT_TCP_PORT_RANGE "7171,7172"
static gint proto_tibia = -1;
static uat_t *rsakeys_uat = NULL, *xteakeys_uat = NULL;
static struct rsakeys_assoc *rsakeylist_uats = NULL;
static struct xteakeys_assoc *xteakeylist_uats = NULL;
static guint nrsakeys = 0, nxteakeys = 0;
static gint hf_tibia_len = -1;
static gint hf_tibia_nonce = -1;
static gint hf_tibia_adler32 = -1;
static gint hf_tibia_adler32_status = -1;
static gint hf_tibia_os = -1;
static gint hf_tibia_proto_version = -1;
static gint hf_tibia_client_version = -1;
static gint hf_tibia_file_versions = -1;
static gint hf_tibia_file_version_spr = -1;
static gint hf_tibia_file_version_dat = -1;
static gint hf_tibia_file_version_pic = -1;
static gint hf_tibia_game_preview_state = -1;
static gint hf_tibia_content_revision = -1;
static gint hf_tibia_undecoded_rsa_data = -1;
static gint hf_tibia_undecoded_xtea_data = -1;
static gint hf_tibia_unknown = -1;
static gint hf_tibia_xtea_key = -1;
static gint hf_tibia_loginflags_gm = -1;
static gint hf_tibia_acc_name = -1;
static gint hf_tibia_acc_number = -1;
static gint hf_tibia_session_key = -1;
static gint hf_tibia_char_name = -1;
static gint hf_tibia_acc_pass = -1;
static gint hf_tibia_char_name_convo = -1;
static gint hf_tibia_acc_name_convo = -1;
static gint hf_tibia_acc_pass_convo = -1;
static gint hf_tibia_session_key_convo = -1;
static gint hf_tibia_client_info = -1;
static gint hf_tibia_client_locale = -1;
static gint hf_tibia_client_locale_id = -1;
static gint hf_tibia_client_locale_name = -1;
static gint hf_tibia_client_ram = -1;
static gint hf_tibia_client_cpu = -1;
static gint hf_tibia_client_cpu_name = -1;
static gint hf_tibia_client_clock = -1;
static gint hf_tibia_client_clock2 = -1;
static gint hf_tibia_client_gpu = -1;
static gint hf_tibia_client_vram = -1;
static gint hf_tibia_client_resolution = -1;
static gint hf_tibia_client_resolution_x = -1;
static gint hf_tibia_client_resolution_y = -1;
static gint hf_tibia_client_resolution_hz = -1;
static gint hf_tibia_payload_len = -1;
static gint hf_tibia_loginserv_command = -1;
static gint hf_tibia_gameserv_command = -1;
static gint hf_tibia_client_command = -1;
static gint hf_tibia_motd = -1;
static gint hf_tibia_dlg_error = -1;
static gint hf_tibia_dlg_info = -1;
static gint hf_tibia_charlist = -1;
static gint hf_tibia_charlist_length = -1;
static gint hf_tibia_charlist_entry_name = -1;
static gint hf_tibia_charlist_entry_world = -1;
static gint hf_tibia_charlist_entry_ip = -1;
static gint hf_tibia_charlist_entry_port = -1;
static gint hf_tibia_worldlist = -1;
static gint hf_tibia_worldlist_length = -1;
static gint hf_tibia_worldlist_entry_name = -1;
static gint hf_tibia_worldlist_entry_ip = -1;
static gint hf_tibia_worldlist_entry_port = -1;
static gint hf_tibia_worldlist_entry_preview = -1;
static gint hf_tibia_worldlist_entry_id = -1;
static gint hf_tibia_pacc_days = -1;
static gint hf_tibia_channel_id = -1;
static gint hf_tibia_channel_name = -1;
static gint hf_tibia_char_cond = -1;
static gint hf_tibia_char_cond_poisoned = -1;
static gint hf_tibia_char_cond_burning = -1;
static gint hf_tibia_char_cond_electrocuted = -1;
static gint hf_tibia_char_cond_drunk = -1;
static gint hf_tibia_char_cond_manashield = -1;
static gint hf_tibia_char_cond_paralyzed = -1;
static gint hf_tibia_char_cond_haste = -1;
static gint hf_tibia_char_cond_battle = -1;
static gint hf_tibia_char_cond_drowning = -1;
static gint hf_tibia_char_cond_freezing = -1;
static gint hf_tibia_char_cond_dazzled = -1;
static gint hf_tibia_char_cond_cursed = -1;
static gint hf_tibia_char_cond_buff = -1;
static gint hf_tibia_char_cond_pzblock = -1;
static gint hf_tibia_char_cond_pz = -1;
static gint hf_tibia_char_cond_bleeding = -1;
static gint hf_tibia_char_cond_hungry = -1;
static const int *char_conds[] = {
&hf_tibia_char_cond_poisoned,
&hf_tibia_char_cond_burning,
&hf_tibia_char_cond_electrocuted,
&hf_tibia_char_cond_drunk,
&hf_tibia_char_cond_manashield,
&hf_tibia_char_cond_paralyzed,
&hf_tibia_char_cond_haste,
&hf_tibia_char_cond_battle,
&hf_tibia_char_cond_drowning,
&hf_tibia_char_cond_freezing,
&hf_tibia_char_cond_dazzled,
&hf_tibia_char_cond_cursed,
&hf_tibia_char_cond_buff,
&hf_tibia_char_cond_pzblock,
&hf_tibia_char_cond_pz,
&hf_tibia_char_cond_bleeding,
&hf_tibia_char_cond_hungry,
NULL
};
static gint hf_tibia_chat_msg = -1;
static gint hf_tibia_speech_type = -1;
static gint hf_tibia_coords_x = -1;
static gint hf_tibia_coords_y = -1;
static gint hf_tibia_coords_z = -1;
static gint hf_tibia_coords = -1;
static gint hf_tibia_stackpos = -1;
#if 0
static gint hf_tibia_item = -1;
#endif
static gint hf_tibia_container = -1;
static gint hf_tibia_container_icon = -1;
static gint hf_tibia_container_slot = -1;
static gint hf_tibia_container_slots = -1;
static gint hf_tibia_inventory = -1;
static gint hf_tibia_vip = -1;
static gint hf_tibia_vip_online = -1;
static gint hf_tibia_player = -1;
static gint hf_tibia_creature = -1;
static gint hf_tibia_creature_health = -1;
static gint hf_tibia_window = -1;
static gint hf_tibia_window_icon = -1;
static gint hf_tibia_window_textlen = -1;
static gint hf_tibia_window_text = -1;
static gint hf_tibia_light_level = -1;
static gint hf_tibia_light_color = -1;
static gint hf_tibia_magic_effect_id = -1;
static gint hf_tibia_animated_text_color = -1;
static gint hf_tibia_animated_text = -1;
static gint hf_tibia_projectile = -1;
static gint hf_tibia_squarecolor = -1;
static gint hf_tibia_textmsg_class = -1;
static gint hf_tibia_textmsg = -1;
static gint hf_tibia_walk_dir = -1;
static gint ett_tibia = -1;
static gint ett_command = -1;
static gint ett_file_versions = -1;
static gint ett_client_info = -1;
static gint ett_locale = -1;
static gint ett_cpu = -1;
static gint ett_resolution = -1;
static gint ett_charlist = -1;
static gint ett_worldlist = -1;
static gint ett_char = -1;
static gint ett_world = -1;
static gint ett_coords = -1;
static gint ett_char_cond = -1;
static expert_field ei_xtea_len_toobig = EI_INIT;
static expert_field ei_adler32_checksum_bad = EI_INIT;
static expert_field ei_rsa_plaintext_no_leading_zero = EI_INIT;
static expert_field ei_rsa_ciphertext_too_short = EI_INIT;
static expert_field ei_rsa_decrypt_failed = EI_INIT;
struct rsakey {
address addr;
guint16 port;
gcry_sexp_t privkey;
};
GHashTable *rsakeys, *xteakeys;
struct proto_traits {
guint32 adler32:1, rsa:1, compression:1, xtea:1, login_webservice:1, acc_name:1, nonce:1,
extra_gpu_info:1, gmbyte:1, hwinfo:1;
guint32 outfit_addons:1, stamina:1, lvl_on_msg:1;
guint32 ping:1, client_version:1, game_preview:1, auth_token:1, session_key:1;
guint32 game_content_revision:1, worldlist_in_charlist:1;
guint string_enc;
};
struct tibia_convo {
guint32 xtea_key[XTEA_KEY_LEN / sizeof (guint32)];
guint32 xtea_framenum;
const guint8 *acc, *pass, *char_name, *session_key;
struct proto_traits has;
guint16 proto_version;
guint8 loginserv_is_peer :1;
guint16 clientport;
guint16 servport;
gcry_sexp_t privkey;
};
static struct proto_traits
get_version_traits(guint16 version)
{
struct proto_traits has;
memset(&has, 0, sizeof has);
has.gmbyte = TRUE; /* Not sure when the GM byte first appeared */
has.string_enc = ENC_ISO_8859_1;
if (version >= 761) /* 761 was a test client. 770 was the first release */
has.xtea = has.rsa = TRUE;
if (version >= 780)
has.outfit_addons = has.stamina = has.lvl_on_msg = TRUE;
if (version >= 830)
has.adler32 = has.acc_name = TRUE;
if (version >= 841)
has.hwinfo = has.nonce = TRUE;
if (version >= 953)
has.ping = TRUE;
if (version >= 980)
has.client_version = has.game_preview = TRUE;
if (version >= 1010)
has.worldlist_in_charlist = TRUE;
if (version >= 1061)
has.extra_gpu_info = TRUE;
if (version >= 1071)
has.game_content_revision = TRUE;
if (version >= 1072)
has.auth_token = TRUE;
if (version >= 1074)
has.session_key = TRUE;
if (version >= 1101)
has.login_webservice = TRUE;
if (version >= 1111) {
has.compression = TRUE; /* with DEFLATE */
has.adler32 = FALSE;
}
#if 0 /* With the legacy client being phased out, maybe Unicode support incoming? */
if (version >= 11xy)
has.string_enc = ENC_UTF_8;
#endif
return has;
}
static guint16
get_version_get_charlist_packet_size(struct proto_traits *has)
{
guint16 size = 2;
if (has->adler32 || has->compression)
size += 4;
size += 17;
if (has->extra_gpu_info)
size += 222;
if (has->rsa)
size += 128;
return size;
}
static guint16
get_version_char_login_packet_size(struct proto_traits *has)
{
guint16 size = 2;
if (has->adler32 || has->compression)
size += 4;
size += 5;
if (has->client_version)
size += 4;
if (has->game_content_revision)
size += 2;
if (has->game_preview)
size += 1;
if (has->rsa)
size += 128;
return size;
}
#define XTEA_FROM_UAT 0
#define XTEA_UNKNOWN 0xFFFFFFFF
static struct tibia_convo *
tibia_get_convo(packet_info *pinfo)
{
conversation_t *epan_conversation = find_or_create_conversation(pinfo);
struct tibia_convo *convo = (struct tibia_convo*)conversation_get_proto_data(epan_conversation, proto_tibia);
if (!convo) {
struct rsakey rsa_key;
convo = wmem_new0(wmem_file_scope(), struct tibia_convo);
/* FIXME there gotta be a cleaner way... */
if (pinfo->srcport >= 0xC000) {
convo->clientport = pinfo->srcport;
convo->servport = pinfo->destport;
rsa_key.addr = pinfo->dst;
} else {
convo->clientport = pinfo->destport;
convo->servport = pinfo->srcport;
rsa_key.addr = pinfo->src;
}
rsa_key.port = convo->servport;
convo->privkey = (gcry_sexp_t)g_hash_table_lookup(rsakeys, &rsa_key);
convo->xtea_framenum = XTEA_UNKNOWN;
conversation_add_proto_data(epan_conversation, proto_tibia, (void *)convo);
}
if (convo->xtea_framenum == XTEA_UNKNOWN) {
guint8 *xtea_key = (guint8*)g_hash_table_lookup(xteakeys, GUINT_TO_POINTER(pinfo->num));
if (xtea_key) {
memcpy(convo->xtea_key, xtea_key, XTEA_KEY_LEN);
convo->xtea_framenum = XTEA_FROM_UAT;
}
}
return convo;
}
static guint32
ipv4tonl(const char *str)
{
guint32 ipaddr = 0;
for (int octet = 0; octet < 4; octet++) {
ws_strtou8(str, &str, &((guint8*)&ipaddr)[octet]);
str++;
}
return ipaddr;
}
static void rsakey_free(void *_rsakey);
static void
register_gameserv_addr(struct tibia_convo *convo, guint32 ipaddr, guint16 port)
{
/* Game servers in the list inherit the same RSA key as the login server */
if (convo->has.rsa) {
struct rsakey *entry = g_new(struct rsakey, 1);
alloc_address_wmem(NULL, &entry->addr, AT_IPv4, sizeof ipaddr, &ipaddr);
entry->port = port;
entry->privkey = NULL;
if (!g_hash_table_contains(rsakeys, entry)) {
entry->privkey = convo->privkey;
g_hash_table_insert(rsakeys, entry, entry->privkey);
} else {
rsakey_free(entry);
}
}
/* TODO Mark all communication with the IP/Port pair above
* as Tibia communication. How?
*/
}
static gcry_sexp_t otserv_key;
static gcry_sexp_t
convo_get_privkey(struct tibia_convo *convo)
{
return convo->privkey ? convo->privkey
: try_otserv_key ? otserv_key
: NULL;
}
enum client_cmd {
/* from TibiaAPI */
C_GET_CHARLIST = 0x01,
C_LOGIN_CHAR = 0x0A,
C_LOGOUT = 0x14, /* I think this is a 7.7+ thing */
C_PONG = 0x1E,
C_AUTO_WALK = 0x64,
C_GO_NORTH = 0x65,
C_GO_EAST = 0x66,
C_GO_SOUTH = 0x67,
C_GO_WEST = 0x68,
C_AUTO_WALK_CANCEL = 0x69,
C_GO_NE = 0x6A,
C_GO_SE = 0x6B,
C_GO_SW = 0x6C,
C_GO_NW = 0x6D,
C_TURN_NORTH = 0x6F,
C_TURN_EAST = 0x70,
C_TURN_SOUTH = 0x71,
C_TURN_WEST = 0x72,
C_MOVE_ITEM = 0x78,
C_SHOP_BUY = 0x7A,
C_SHOP_SELL = 0x7B,
C_SHOP_CLOSE = 0x7C,
C_ITEM_USE = 0x82,
C_ITEM_USE_ON = 0x83,
C_ITEM_USE_BATTLELIST = 0x84,
C_ITEM_ROTATE = 0x85,
C_CONTAINER_CLOSE = 0x87,
C_CONTAINER_OPEN_PARENT = 0x88,
C_LOOK_AT = 0x8C,
C_PLAYER_SPEECH = 0x96,
C_CHANNEL_LIST = 0x97,
C_CHANNEL_OPEN = 0x98,
C_CHANNEL_CLOSE = 0x99,
C_PRIVATE_CHANNEL_OPEN = 0x9A,
C_NPC_CHANNEL_CLOSE = 0x9E,
C_FIGHT_MODES = 0xA0,
C_ATTACK = 0xA1,
C_FOLLOW = 0xA2,
C_CANCEL_GO = 0xBE,
C_TILE_UPDATE = 0xC9,
C_CONTAINER_UPDATE = 0xCA,
C_SET_OUTFIT = 0xD3,
C_VIP_ADD = 0xDC,
C_VIP_REMOVE = 0xDD
};
static const value_string from_client_packet_types[] = {
{ C_GET_CHARLIST, "Charlist request" },
{ C_LOGIN_CHAR, "Character login" },
{ C_LOGOUT, "Logout" },
{ C_PONG, "Pong" },
{ C_AUTO_WALK, "Map walk" },
{ C_GO_NORTH, "Go north"},
{ C_GO_EAST, "Go east"},
{ C_GO_SOUTH, "Go south"},
{ C_GO_WEST, "Go west"},
{ C_AUTO_WALK_CANCEL, "Map walk cancel" },
{ C_GO_NE, "Go north-east"},
{ C_GO_SE, "Go south-east"},
{ C_GO_SW, "Go south-west"},
{ C_GO_NW, "Go north-west"},
{ C_TURN_NORTH, "Turn north" },
{ C_TURN_EAST, "Turn east" },
{ C_TURN_SOUTH, "Turn south" },
{ C_TURN_WEST, "Turn west" },
{ C_MOVE_ITEM, "Move item" },
{ C_SHOP_BUY, "Buy in shop" },
{ C_SHOP_SELL, "Sell in shop" },
{ C_SHOP_CLOSE, "Close shop" },
{ C_ITEM_USE, "Use item" },
{ C_ITEM_USE_ON, "Use item on" },
{ C_ITEM_USE_BATTLELIST, "Use item on battle list" },
{ C_ITEM_ROTATE, "Rotate item" },
{ C_CONTAINER_CLOSE, "Close container" },
{ C_CONTAINER_OPEN_PARENT, "Open parent container" },
{ C_LOOK_AT, "Look at" },
{ C_PLAYER_SPEECH, "Speech" },
{ C_CHANNEL_LIST, "List channels" },
{ C_CHANNEL_OPEN, "Open public channel" },
{ C_CHANNEL_CLOSE, "close channel" },
{ C_PRIVATE_CHANNEL_OPEN, "Open private channel" },
{ C_NPC_CHANNEL_CLOSE, "Open NPC channel" },
{ C_FIGHT_MODES, "Set fight modes" },
{ C_ATTACK, "Attack" },
{ C_FOLLOW, "Follow" },
{ C_CANCEL_GO, "Cancel go" },
{ C_TILE_UPDATE, "Update tile" },
{ C_CONTAINER_UPDATE, "Update container" },
{ C_SET_OUTFIT, "Set outfit" },
{ C_VIP_ADD, "Add VIP" },
{ C_VIP_REMOVE, "Remove VIP" },
{ 0, NULL }
};
static value_string_ext from_client_packet_types_ext = VALUE_STRING_EXT_INIT(from_client_packet_types);
enum loginserv_cmd {
LOGINSERV_DLG_ERROR = 0x0A,
LOGINSERV_DLG_ERROR2 = 0x0B,
LOGINSERV_DLG_MOTD = 0x14,
LOGINSERV_SESSION_KEY = 0x28,
LOGINSERV_DLG_CHARLIST = 0x64
};
static const value_string from_loginserv_packet_types[] = {
{ LOGINSERV_DLG_ERROR, "Error" },
{ LOGINSERV_DLG_ERROR2, "Error" },
{ LOGINSERV_DLG_MOTD, "MOTD" },
{ LOGINSERV_SESSION_KEY, "Session key" },
{ LOGINSERV_DLG_CHARLIST, "Charlist" },
{ 0, NULL }
};
enum gameserv_cmd {
/* Credit to Khaos (OBJECT Networks). Values and comments extracted from PDF table */
S_MAPINIT = 0x0A, /* Long playerCreatureId Int unknownU16 (Byte reportBugs?) */
S_GMACTIONS = 0x0B, /* Used to be 32 unknown bytes, but with GMs removed it might not be in use anymore */
S_DLG_ERROR = 0x14, /* String errorMessage */
S_DLG_INFO = 0x15,
S_DLG_TOOMANYPLAYERS = 0x16,
S_PING = 0x1E,
S_NONCE = 0x1F,
S_PLAYERLOC = 0x64, /* Coord pos */
S_GO_NORTH = 0x65, /* MapDescription (18,1) */
S_GO_EAST = 0x66, /* MapDescription (1,14) */
S_GO_SOUTH = 0x67, /* MapDescription (18,1) */
S_GO_WEST = 0x68, /* MapDescription (1,14) */
S_TILEUPDATE = 0x69, /* Coord pos TileDescription td */
S_ADDITEM = 0x6a, /* Coord pos ThingDescription thing */
S_REPLACEITEM = 0x6b, /* Coord pos Byte stackpos ThingDescription thing */
S_REMOVEITEM = 0x6c, /* Coord pos Byte stackpos */
S_MOVE_THING = 0x6d,
S_CONTAINER = 0x6e, /* Byte index Short containerIcon Byte slotCount ThingDescription item */
S_CONTAINERCLOSE = 0x6f, /* Byte index */
S_ADDITEMCONTAINER = 0x70, /* Byte index ThingDescription itm */
S_TRANSFORMITEMCONTAINER = 0x71, /* Byte index Byte slot */
S_REMOVEITEMCONTAINER = 0x72, /* Byte index Byte slot */
S_INVENTORYEMPTY = 0x78, /* Byte invSlot */
S_INVENTORYITEM = 0x79, /* Byte invSlot ThingDescription itm */
S_TRADEREQ = 0x7d, /* String otherperson Byte slotCount ThingDescription itm */
S_TRADEACK = 0x7e, /* String otherperson Byte slotCount ThingDescription itm */
S_TRADECLOSE = 0x7f,
S_LIGHTLEVEL = 0x82, /* Byte lightlevel Byte lightcolor */
S_MAGIC_EFFECT = 0x83,
S_ANIMATEDTEXT = 0x84, /* Coord pos Byte color String message */
S_DISTANCESHOT = 0x85, /* Coord pos1 Byte stackposition Coord pos2 */
S_CREATURESQUARE = 0x86, /* Long creatureid Byte squarecolor */
S_CREATURE_HEALTH = 0x8C,
S_CREATURELIGHT = 0x8d, /* Long creatureid Byte ? Byte ? */
S_SETOUTFIT = 0x8e, /* Long creatureid Byte lookType Byte headType Byte bodyType Byte legsType Byte feetType // can extended look go here too? */
S_CREATURESPEED = 0x8f, /* YIKES! I didnt handle this! */
S_TEXTWINDOW = 0x96, /* Long windowId Byte icon Byte maxlength String message */
S_STATUSMSG = 0xA0, /* Status status */
S_SKILLS = 0xA1, /* Skills skills */
S_PLAYER_CONDITION = 0xA2,
S_CANCELATTACK = 0xA3,
S_SPEAK = 0xAA,
S_CHANNELSDIALOG = 0xAB, /* Byte channelCount (Int channelId String channelName) */
S_CHANNEL_OPEN = 0xAC,
S_OPENPRIV = 0xAD, /* String playerName */
S_TEXTMESSAGE = 0xB4, /* Byte msgClass String string */
S_CANCELWALK = 0xB5, /* Byte direction */
S_FLOORUP = 0xBE, /* Advanced topic; read separate text */
S_FLOORDOWN = 0xBF, /* Advanced topic; read separate text */
S_OUTFITLIST = 0xC8, /* Byte lookType Byte headType Byte bodyType Byte legsType Byte feetType Byte firstModel Byte lastModel */
S_VIPADD = 0xD2, /* long guid string name byte isonline */
S_VIPLOGIN = 0xD3, /* long guid */
S_VIPLOGOUT = 0xD4 /* long guid*/
};
static const value_string from_gameserv_packet_types[] = {
{ S_MAPINIT, "Initialize map" },
{ S_GMACTIONS, "GM actions" },
{ S_DLG_ERROR, "Error" },
{ S_DLG_INFO, "Info" },
{ S_DLG_TOOMANYPLAYERS, "Too many players" },
{ S_PING, "Ping" },
{ S_NONCE, "Nonce" },
{ S_PLAYERLOC, "Set player location" },
{ S_GO_NORTH, "Go north" },
{ S_GO_EAST, "Go east" },
{ S_GO_SOUTH, "Go south" },
{ S_GO_WEST, "Go west" },
{ S_TILEUPDATE, "Update tile" },
{ S_ADDITEM, "Add item" },
{ S_REPLACEITEM, "Replace item" },
{ S_REMOVEITEM, "Remove item" },
{ S_MOVE_THING, "Move thing" },
{ S_CONTAINER, "Open container" },
{ S_CONTAINERCLOSE, "Close container" },
{ S_ADDITEMCONTAINER, "Add item in container" },
{ S_TRANSFORMITEMCONTAINER, "Transform item in container" },
{ S_REMOVEITEMCONTAINER, "Remove item in container" },
{ S_INVENTORYEMPTY, "Inventory empty" },
{ S_INVENTORYITEM, "Inventory item" },
{ S_TRADEREQ, "Trade request" },
{ S_TRADEACK, "Trade acknowledge" },
{ S_TRADECLOSE, "Trade over" },
{ S_LIGHTLEVEL, "Light level" },
{ S_MAGIC_EFFECT, "Magic effect" },
{ S_ANIMATEDTEXT, "Animated text" },
{ S_DISTANCESHOT, "Distance shot" },
{ S_CREATURESQUARE, "Creature square" },
{ S_CREATURE_HEALTH, "Creature health" },
{ S_CREATURELIGHT, "Creature light" },
{ S_SETOUTFIT, "Set outfit" },
{ S_CREATURESPEED, "Set creature speed" },
{ S_TEXTWINDOW, "Text window" },
{ S_STATUSMSG, "Status message" },
{ S_SKILLS, "Skills" },
{ S_PLAYER_CONDITION, "Player condition" },
{ S_CANCELATTACK, "Cancel attack" },
{ S_SPEAK, "Creature speech" },
{ S_CHANNELSDIALOG, "Channels dialog" },
{ S_CHANNEL_OPEN, "Channel open" },
{ S_OPENPRIV, "Private channel open" },
{ S_TEXTMESSAGE, "Text message" },
{ S_CANCELWALK, "Cancel walk" },
{ S_FLOORUP, "Floor +1" },
{ S_FLOORDOWN, "Floor -1" },
{ S_OUTFITLIST, "Outfit list" },
{ S_VIPADD, "Add VIP" },
{ S_VIPLOGIN, "VIP login" },
{ S_VIPLOGOUT, "VIP logout" },
{ 0, NULL }
};
static value_string_ext from_gameserv_packet_types_ext = VALUE_STRING_EXT_INIT(from_gameserv_packet_types);
static const unit_name_string mb_unit = {"MB", NULL};
static int
dissect_loginserv_packet(struct tibia_convo *convo, tvbuff_t *tvb, int offset, int len, packet_info *pinfo, proto_tree *tree, gboolean first_fragment )
{
ptvcursor_t *ptvc = ptvcursor_new(tree, tvb, offset);
col_append_str(pinfo->cinfo, COL_INFO, first_fragment ? " commands:" : ",");
len += offset;
if (ptvcursor_current_offset(ptvc) < len) {
for (;;) {
int cmd = tvb_get_guint8(tvb, ptvcursor_current_offset(ptvc));
ptvcursor_add_with_subtree(ptvc, hf_tibia_loginserv_command, 1, convo->has.string_enc, ett_command);
ptvcursor_advance(ptvc, 1);
switch ((enum loginserv_cmd)cmd) {
case LOGINSERV_DLG_ERROR:
case LOGINSERV_DLG_ERROR2:
ptvcursor_add(ptvc, hf_tibia_dlg_error, 2, ENC_LITTLE_ENDIAN | convo->has.string_enc);
break;
case LOGINSERV_DLG_MOTD:
ptvcursor_add(ptvc, hf_tibia_motd, 2, ENC_LITTLE_ENDIAN | convo->has.string_enc);
break;
case LOGINSERV_SESSION_KEY:
ptvcursor_add(ptvc, hf_tibia_session_key, 2, ENC_LITTLE_ENDIAN | convo->has.string_enc);
break;
case LOGINSERV_DLG_CHARLIST:
if (convo->has.worldlist_in_charlist) {
guint8 world_count = tvb_get_guint8(tvb, ptvcursor_current_offset(ptvc));
ptvcursor_add(ptvc, hf_tibia_worldlist_length, 1, ENC_NA);
/* Empty character list? */
if (world_count) {
ptvcursor_add_with_subtree(ptvc, hf_tibia_worldlist, SUBTREE_UNDEFINED_LENGTH, ENC_NA, ett_worldlist);
while (world_count--) {
proto_item *it = ptvcursor_add(ptvc, hf_tibia_worldlist_entry_id, 1, ENC_NA);
ptvcursor_push_subtree(ptvc, it, ett_world);
ptvcursor_add(ptvc, hf_tibia_worldlist_entry_name, 2, convo->has.string_enc | ENC_LITTLE_ENDIAN);
guint ipv4addr_len = tvb_get_letohs(tvb, ptvcursor_current_offset(ptvc));
char *ipv4addr_str = (char*)tvb_get_string_enc(wmem_packet_scope(), tvb, ptvcursor_current_offset(ptvc) + 2, ipv4addr_len, ENC_LITTLE_ENDIAN | convo->has.string_enc);
guint32 ipv4addr = ipv4tonl(ipv4addr_str);
ptvcursor_add(ptvc, hf_tibia_worldlist_entry_ip, 2, ENC_LITTLE_ENDIAN | convo->has.string_enc);
guint16 port = tvb_get_letohs(tvb, ptvcursor_current_offset(ptvc));
ptvcursor_add(ptvc, hf_tibia_worldlist_entry_port, 2, ENC_LITTLE_ENDIAN);
ptvcursor_add(ptvc, hf_tibia_worldlist_entry_preview, 1, ENC_NA);
ptvcursor_pop_subtree(ptvc);
register_gameserv_addr(convo, ipv4addr, port);
}
ptvcursor_pop_subtree(ptvc);
}
guint8 char_count = tvb_get_guint8(tvb, ptvcursor_current_offset(ptvc));
ptvcursor_add(ptvc, hf_tibia_charlist_length, 1, ENC_NA);
if (char_count) {
ptvcursor_add_with_subtree(ptvc, hf_tibia_charlist, SUBTREE_UNDEFINED_LENGTH, ENC_NA, ett_charlist);
while (char_count--) {
proto_item *it = ptvcursor_add(ptvc, hf_tibia_worldlist_entry_id, 1, ENC_NA);
ptvcursor_push_subtree(ptvc, it, ett_char);
ptvcursor_add(ptvc, hf_tibia_charlist_entry_name, 2, convo->has.string_enc | ENC_LITTLE_ENDIAN);
ptvcursor_pop_subtree(ptvc);
}
ptvcursor_pop_subtree(ptvc);
}
} else {
guint8 char_count = tvb_get_guint8(tvb, ptvcursor_current_offset(ptvc));
ptvcursor_add(ptvc, hf_tibia_charlist_length, 1, ENC_NA);
if (char_count) {
ptvcursor_add_with_subtree(ptvc, hf_tibia_charlist, SUBTREE_UNDEFINED_LENGTH, ENC_NA, ett_charlist);
while (char_count--) {
proto_item *it = ptvcursor_add(ptvc, hf_tibia_charlist_entry_name, 2, convo->has.string_enc | ENC_LITTLE_ENDIAN);
ptvcursor_push_subtree(ptvc, it, ett_char);
ptvcursor_add(ptvc, hf_tibia_charlist_entry_world, 2, ENC_LITTLE_ENDIAN | convo->has.string_enc);
guint32 ipv4addr = tvb_get_ipv4(tvb, ptvcursor_current_offset(ptvc));
ptvcursor_add(ptvc, hf_tibia_charlist_entry_ip, 4, ENC_BIG_ENDIAN);
guint16 port = tvb_get_letohs(tvb, ptvcursor_current_offset(ptvc));
ptvcursor_add(ptvc, hf_tibia_charlist_entry_port, 2, ENC_BIG_ENDIAN);
ptvcursor_pop_subtree(ptvc);
register_gameserv_addr(convo, ipv4addr, port);
}
ptvcursor_pop_subtree(ptvc);
}
ptvcursor_add(ptvc, hf_tibia_pacc_days, 2, ENC_LITTLE_ENDIAN);
}
break;
default:
offset = ptvcursor_current_offset(ptvc);
call_data_dissector(tvb_new_subset_length(tvb, offset, len - offset), pinfo, ptvcursor_tree(ptvc));
ptvcursor_advance(ptvc, len - offset);
}
ptvcursor_pop_subtree(ptvc);
col_append_fstr(pinfo->cinfo, COL_INFO, " %s (0x%x)",
val_to_str(cmd, from_loginserv_packet_types, "Unknown"), cmd);
if (ptvcursor_current_offset(ptvc) >= len)
break;
col_append_str(pinfo->cinfo, COL_INFO, ",");
}
}
offset = ptvcursor_current_offset(ptvc);
ptvcursor_free(ptvc);
return offset;
}
static void
dissect_coord(ptvcursor_t *ptvc, gboolean with_stackpos)
{
tvbuff_t *tvb;
proto_tree *tree;
int offset;
guint32 x, y, z, stackpos;
proto_item *coords_tuple = ptvcursor_add_with_subtree(ptvc, hf_tibia_coords, SUBTREE_UNDEFINED_LENGTH, ENC_NA, ett_coords);
{
tvb = ptvcursor_tvbuff(ptvc);
tree = ptvcursor_tree(ptvc);
offset = ptvcursor_current_offset(ptvc);
proto_tree_add_item_ret_uint(tree, hf_tibia_coords_x, tvb, offset, 2, ENC_LITTLE_ENDIAN, &x);
offset += 2;
proto_tree_add_item_ret_uint(tree, hf_tibia_coords_y, tvb, offset, 2, ENC_LITTLE_ENDIAN, &y);
offset += 2;
proto_tree_add_item_ret_uint(tree, hf_tibia_coords_z, tvb, offset, 1, ENC_NA, &z);
offset += 1;
ptvcursor_advance(ptvc, 5);
}
if (with_stackpos) {
proto_tree_add_item_ret_uint(tree, hf_tibia_stackpos, tvb, offset, 1, ENC_NA, &stackpos);
proto_item_set_text(coords_tuple, "Coordinates: (%u, %u, %u)[%u]", x, y, z, stackpos);
ptvcursor_advance(ptvc, 1);
} else {
proto_item_set_text(coords_tuple, "Coordinates: (%u, %u, %u)", x, y, z);
}
ptvcursor_pop_subtree(ptvc);
}
static int
dissect_gameserv_packet(struct tibia_convo *convo, tvbuff_t *tvb, int offset, int len, packet_info *pinfo, proto_tree *tree, gboolean first_fragment)
{
ptvcursor_t *ptvc = ptvcursor_new(tree, tvb, offset);
col_append_str(pinfo->cinfo, COL_INFO, first_fragment ? " commands:" : ",");
len += offset;
if (ptvcursor_current_offset(ptvc) < len) {
for (;;) {
int cmd = tvb_get_guint8(tvb, ptvcursor_current_offset(ptvc));
ptvcursor_add_with_subtree(ptvc, hf_tibia_gameserv_command, 1, convo->has.string_enc, ett_command);
ptvcursor_advance(ptvc, 1);
switch ((enum gameserv_cmd)cmd) {
case S_DLG_INFO:
case S_DLG_ERROR:
case S_DLG_TOOMANYPLAYERS:
ptvcursor_add(ptvc, cmd == S_DLG_ERROR ? hf_tibia_dlg_error : hf_tibia_dlg_info, 2, ENC_LITTLE_ENDIAN | convo->has.string_enc);
break;
case S_GMACTIONS: /* 0x0B, Used to be 32 unknown bytes, but with GMs removed it might not be in use anymore */
ptvcursor_add(ptvc, hf_tibia_unknown, 32, ENC_NA);
break;
case S_PLAYERLOC: /* 0x64,Coord pos */
dissect_coord(ptvc, FALSE);
break;
case S_TILEUPDATE: /* 0x69,Coord pos TileDescription td */
dissect_coord(ptvc, FALSE);
ptvcursor_add(ptvc, hf_tibia_unknown, len - ptvcursor_current_offset(ptvc), ENC_NA);
break;
case S_ADDITEM: /* 0x6a,Coord pos ThingDescription thing */
dissect_coord(ptvc, FALSE);
ptvcursor_add(ptvc, hf_tibia_unknown, len - ptvcursor_current_offset(ptvc), ENC_NA);
break;
case S_REPLACEITEM: /* 0x6b,Coord pos Byte stackpos ThingDescription thing */
dissect_coord(ptvc, TRUE);
ptvcursor_add(ptvc, hf_tibia_unknown, len - ptvcursor_current_offset(ptvc), ENC_NA);