2 * Routines for socks versions 4 &5 packet dissection
3 * Copyright 2000, Jeffrey C. Foster <jfoste@woodward.com>
4 * Copyright 2008, Jelmer Vernooij <jelmer@samba.org>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
13 * The Version 4 decode is based on SOCKS4.protocol and SOCKS4A.protocol.
14 * The Version 5 decoder is based upon rfc-1928
15 * The Version 5 User/Password authentication is based on rfc-1929.
18 * http://www.openssh.org/txt/socks4.protocol
19 * http://www.openssh.org/txt/socks4a.protocol
21 * for information on SOCKS version 4 and 4a.
25 * 2003-09-18 JCFoster Fixed problem with socks tunnel in socks tunnel
26 * causing heap overflow because of an infinite loop
27 * where the socks dissect was call over and over.
29 * Also remove some old code marked with __JUNK__
31 * 2001-01-08 JCFoster Fixed problem with NULL pointer for hash data.
32 * Now test and exit if hash_info is null.
35 /* Possible enhancements -
37 * Add GSS-API authentication per rfc-1961
38 * Add CHAP authentication
39 * Decode FLAG bits per
40 * https://tools.ietf.org/html/draft-ietf-aft-socks-pro-v5-04
41 * In call_next_dissector, could load the destination address into
42 * pinfo->src or pinfo->dst structure before calling next dissector.
50 #include <epan/packet.h>
51 #include <epan/exceptions.h>
52 #include <epan/proto_data.h>
54 #include "packet-tcp.h"
55 #include "packet-udp.h"
56 #include "packet-tls.h"
58 #include <epan/strutil.h>
60 #define TCP_PORT_SOCKS 1080
63 /**************** Socks commands ******************/
65 #define CONNECT_COMMAND 1
66 #define BIND_COMMAND 2
67 #define UDP_ASSOCIATE_COMMAND 3
68 #define PING_COMMAND 0x80
69 #define TRACERT_COMMAND 0x81
72 /********** V5 Authentication methods *************/
74 #define NO_AUTHENTICATION 0
75 #define GSS_API_AUTHENTICATION 1
76 #define USER_NAME_AUTHENTICATION 2
77 #define CHAP_AUTHENTICATION 3
78 #define AUTHENTICATION_FAILED 0xff
80 void proto_register_socks(void);
81 void proto_reg_handoff_socks(void);
83 /*********** Header field identifiers *************/
85 static int proto_socks
;
88 static int ett_socks_auth
;
89 static int ett_socks_name
;
91 static int hf_socks_ver
;
92 static int hf_socks_ip_dst
;
93 static int hf_socks_ip6_dst
;
94 static int hf_gssapi_payload
;
95 static int hf_gssapi_command
;
96 static int hf_gssapi_length
;
97 static int hf_v4a_dns_name
;
98 static int hf_socks_dstport
;
99 static int hf_socks_cmd
;
100 static int hf_socks_results_4
;
101 static int hf_socks_results_5
;
102 static int hf_client_auth_method_count
;
103 static int hf_client_auth_method
;
104 static int hf_socks_reserved
;
105 static int hf_socks_reserved2
;
106 static int hf_client_port
;
107 static int hf_server_accepted_auth_method
;
108 static int hf_server_auth_status
;
109 static int hf_server_remote_host_port
;
110 static int hf_socks_subnegotiation_version
;
111 static int hf_socks_username
;
112 static int hf_socks_password
;
113 static int hf_socks_remote_name
;
114 static int hf_socks_address_type
;
115 static int hf_socks_fragment_number
;
116 static int hf_socks_ping_end_command
;
117 static int hf_socks_ping_results
;
118 static int hf_socks_traceroute_end_command
;
119 static int hf_socks_traceroute_results
;
121 /************* Dissector handles ***********/
123 static dissector_handle_t socks_handle
;
124 static dissector_handle_t socks_handle_tls
;
125 static dissector_handle_t socks_udp_handle
;
127 /************* State Machine names ***********/
132 clientWaitForAuthReply
,
134 clientUserNameRequest
,
135 clientGssApiAuthRequest
,
153 int in_socks_dissector_flag
;
154 enum ClientState client
;
155 enum ServerState server
;
159 enum ClientState clientState
;
160 enum ServerState serverState
;
163 int authentication_method
;
164 uint32_t server_port
;
167 uint32_t udp_remote_port
;
170 uint32_t start_done_frame
;
174 static const value_string address_type_table
[] = {
181 /* String table for the V4 reply status messages */
183 static const value_string reply_table_v4
[] = {
185 {91, "Rejected or Failed"},
186 {92, "Rejected because SOCKS server cannot connect to identd on the client"},
187 {93, "Rejected because the client program and identd report different user-ids"},
191 /* String table for the V5 reply status messages */
193 static const value_string reply_table_v5
[] = {
195 {1, "General SOCKS server failure"},
196 {2, "Connection not allowed by ruleset"},
197 {3, "Network unreachable"},
198 {4, "Host unreachable"},
199 {5, "Connection refused"},
201 {7, "Command not supported"},
202 {8, "Address type not supported"},
206 static const value_string cmd_strings
[] = {
207 {CONNECT_COMMAND
, "Connect"},
208 {BIND_COMMAND
, "Bind"},
209 {UDP_ASSOCIATE_COMMAND
, "UdpAssociate"},
210 {PING_COMMAND
, "Ping"},
211 {TRACERT_COMMAND
, "Traceroute"},
215 static const value_string gssapi_command_table
[] = {
216 { 1, "Authentication" },
222 /************************* Support routines ***************************/
224 static const char *get_auth_method_name( unsigned Number
){
226 /* return the name of the authentication method */
228 if ( Number
== 0) return "No authentication";
229 if ( Number
== 1) return "GSSAPI";
230 if ( Number
== 2) return "Username/Password";
231 if ( Number
== 3) return "Chap";
232 if (( Number
>= 4) && ( Number
<= 0x7f))return "IANA assigned";
233 if (( Number
>= 0x80) && ( Number
<= 0xfe)) return "private method";
234 if ( Number
== 0xff) return "no acceptable method";
236 /* shouldn't reach here */
238 return "Bad method number (not 0-0xff)";
241 static int display_address(packet_info
*pinfo
, tvbuff_t
*tvb
, int offset
, proto_tree
*tree
) {
243 /* decode and display the v5 address, return offset of next byte */
245 int a_type
= tvb_get_uint8(tvb
, offset
);
247 proto_tree_add_item( tree
, hf_socks_address_type
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
252 case 1: /* IPv4 address */
253 proto_tree_add_item( tree
, hf_socks_ip_dst
, tvb
, offset
, 4, ENC_BIG_ENDIAN
);
256 case 3: /* domain name address */
261 len
= tvb_get_uint8(tvb
, offset
);
262 str
= tvb_get_string_enc(pinfo
->pool
, tvb
, offset
+1, len
, ENC_ASCII
);
263 proto_tree_add_string(tree
, hf_socks_remote_name
, tvb
, offset
, len
+1, str
);
267 case 4: /* IPv6 address */
268 proto_tree_add_item( tree
, hf_socks_ip6_dst
, tvb
, offset
, 16, ENC_NA
);
277 static int get_address_v5(tvbuff_t
*tvb
, int offset
,
278 socks_hash_entry_t
*hash_info
) {
280 /* decode the v5 address and return offset of next byte */
284 a_type
= tvb_get_uint8(tvb
, offset
);
289 case 1: /* IPv4 address */
291 set_address_tvb(&addr
, AT_IPv4
, 4, tvb
, offset
);
292 copy_address_wmem(wmem_file_scope(), &hash_info
->dst_addr
, &addr
);
297 case 4: /* IPv6 address */
299 set_address_tvb(&addr
, AT_IPv6
, 16, tvb
, offset
);
300 copy_address_wmem(wmem_file_scope(), &hash_info
->dst_addr
, &addr
);
305 case 3: /* domain name address */
306 offset
+= tvb_get_uint8(tvb
, offset
) + 1;
314 /********************* V5 UDP Associate handlers ***********************/
317 socks_udp_dissector(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void* data _U_
) {
319 /* Conversation dissector called from UDP dissector. Decode and display */
320 /* the socks header, the pass the rest of the data to the udp port */
321 /* decode routine to handle the payload. */
325 socks_hash_entry_t
*hash_info
;
326 conversation_t
*conversation
;
327 proto_tree
*socks_tree
;
330 conversation
= find_conversation_pinfo( pinfo
, 0);
332 DISSECTOR_ASSERT( conversation
); /* should always find a conversation */
334 hash_info
= (socks_hash_entry_t
*)conversation_get_proto_data(conversation
, proto_socks
);
336 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "Socks");
337 col_set_str(pinfo
->cinfo
, COL_INFO
, "Version: 5, UDP Associated packet");
340 ti
= proto_tree_add_protocol_format( tree
, proto_socks
, tvb
, offset
, -1, "Socks" );
342 socks_tree
= proto_item_add_subtree(ti
, ett_socks
);
344 proto_tree_add_item(socks_tree
, hf_socks_reserved2
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
347 proto_tree_add_item(socks_tree
, hf_socks_fragment_number
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
350 offset
= display_address(pinfo
, tvb
, offset
, socks_tree
);
351 hash_info
->udp_remote_port
= tvb_get_ntohs(tvb
, offset
);
353 proto_tree_add_uint( socks_tree
, hf_socks_dstport
, tvb
,
354 offset
, 2, hash_info
->udp_remote_port
);
358 else { /* no tree, skip past the socks header */
360 offset
= get_address_v5( tvb
, offset
, 0) + 2;
363 /* set pi src/dst port and call the udp sub-dissector lookup */
365 if ( pinfo
->srcport
== hash_info
->port
)
366 ptr
= &pinfo
->destport
;
368 ptr
= &pinfo
->srcport
;
370 *ptr
= hash_info
->udp_remote_port
;
372 decode_udp_ports( tvb
, offset
, pinfo
, tree
, pinfo
->srcport
, pinfo
->destport
, -1);
374 *ptr
= hash_info
->udp_port
;
375 return tvb_captured_length(tvb
);
380 new_udp_conversation( socks_hash_entry_t
*hash_info
, packet_info
*pinfo
){
382 conversation_t
*conversation
= conversation_new( pinfo
->num
, &pinfo
->src
, &pinfo
->dst
, CONVERSATION_UDP
,
383 hash_info
->udp_port
, hash_info
->port
, 0);
385 DISSECTOR_ASSERT( conversation
);
387 conversation_add_proto_data(conversation
, proto_socks
, hash_info
);
388 conversation_set_dissector(conversation
, socks_udp_handle
);
392 save_client_state(packet_info
*pinfo
, enum ClientState state
)
394 sock_state_t
* state_info
= (sock_state_t
*)p_get_proto_data(wmem_file_scope(), pinfo
, proto_socks
, 0);
395 if ((state_info
!= NULL
) && (state_info
->client
== clientNoInit
)) {
396 state_info
->client
= state
;
401 save_server_state(packet_info
*pinfo
, enum ServerState state
)
403 sock_state_t
* state_info
= (sock_state_t
*)p_get_proto_data(wmem_file_scope(), pinfo
, proto_socks
, 0);
404 if ((state_info
!= NULL
) && (state_info
->server
== serverNoInit
)) {
405 state_info
->server
= state
;
410 /**************** Protocol Tree Display routines ******************/
413 display_socks_v4(tvbuff_t
*tvb
, int offset
, packet_info
*pinfo
,
414 proto_tree
*tree
, socks_hash_entry_t
*hash_info
, sock_state_t
* state_info
) {
417 /* Display the protocol tree for the V4 version. This routine uses the */
418 /* stored frame information to decide what to do with the row. */
420 unsigned char ipaddr
[4];
423 /* Either there is an error, or we're done with the state machine
424 (so there's nothing to display) */
425 if (state_info
== NULL
)
428 if (hash_info
->server_port
== pinfo
->destport
) {
430 switch (state_info
->client
)
433 proto_tree_add_item( tree
, hf_socks_ver
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
436 proto_tree_add_item( tree
, hf_socks_cmd
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
440 proto_tree_add_item( tree
, hf_socks_dstport
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
443 /* Do destination address */
444 tvb_memcpy(tvb
, ipaddr
, offset
, 4);
445 proto_tree_add_item( tree
, hf_socks_ip_dst
, tvb
, offset
, 4, ENC_BIG_ENDIAN
);
448 /* display user name */
449 str_len
= tvb_strsize(tvb
, offset
);
450 proto_tree_add_item( tree
, hf_socks_username
, tvb
, offset
, str_len
, ENC_ASCII
);
453 if ( ipaddr
[0] == 0 && ipaddr
[1] == 0 &&
454 ipaddr
[2] == 0 && ipaddr
[3] != 0) {
455 /* 0.0.0.x , where x!=0 means v4a support */
456 str_len
= tvb_strsize(tvb
, offset
);
457 proto_tree_add_item( tree
, hf_v4a_dns_name
, tvb
, offset
, str_len
, ENC_ASCII
);
465 switch (state_info
->server
)
468 proto_tree_add_item( tree
, hf_socks_ver
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
470 /* Do results code */
471 proto_tree_add_item( tree
, hf_socks_results_4
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
475 proto_tree_add_item( tree
, hf_socks_dstport
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
477 /* Do remote address */
478 proto_tree_add_item( tree
, hf_socks_ip_dst
, tvb
, offset
, 4, ENC_BIG_ENDIAN
);
487 // NOLINTNEXTLINE(misc-no-recursion)
488 client_display_socks_v5(tvbuff_t
*tvb
, int offset
, packet_info
*pinfo
,
489 proto_tree
*tree
, socks_hash_entry_t
*hash_info
, sock_state_t
* state_info
) {
491 /* Display the protocol tree for the version. This routine uses the */
492 /* stored conversation information to decide what to do with the row. */
493 /* Per packet information would have been better to do this, but we */
494 /* didn't have that when I wrote this. And I didn't expect this to get */
498 const char *AuthMethodStr
;
499 sock_state_t new_state_info
;
502 /* Either there is an error, or we're done with the state machine
503 (so there's nothing to display) */
504 if (state_info
== NULL
)
507 if (state_info
->client
== clientStart
)
509 proto_tree
*AuthTree
;
510 uint8_t num_auth_methods
, auth
;
512 col_append_str(pinfo
->cinfo
, COL_INFO
, " Connect to server request");
514 proto_tree_add_item( tree
, hf_socks_ver
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
517 AuthTree
= proto_tree_add_subtree( tree
, tvb
, offset
, -1, ett_socks_auth
, &ti
, "Client Authentication Methods");
519 num_auth_methods
= tvb_get_uint8(tvb
, offset
);
520 proto_item_set_len(ti
, num_auth_methods
+1);
522 proto_tree_add_item( AuthTree
, hf_client_auth_method_count
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
525 for( i
= 0; i
< num_auth_methods
; ++i
) {
526 auth
= tvb_get_uint8( tvb
, offset
);
527 AuthMethodStr
= get_auth_method_name(auth
);
529 proto_tree_add_uint_format(AuthTree
, hf_client_auth_method
, tvb
, offset
, 1, auth
,
530 "Method[%u]: %u (%s)", i
, auth
, AuthMethodStr
);
534 if ((num_auth_methods
== 1) &&
535 (tvb_bytes_exist(tvb
, offset
+ 2, 1)) &&
536 (tvb_get_uint8(tvb
, offset
+ 2) == 0) &&
537 (tvb_reported_length_remaining(tvb
, offset
+ 2 + num_auth_methods
) > 0)) {
538 new_state_info
.client
= clientV5Command
;
539 increment_dissection_depth(pinfo
);
540 client_display_socks_v5(tvb
, offset
, pinfo
, tree
, hash_info
, &new_state_info
);
541 decrement_dissection_depth(pinfo
);
544 else if (state_info
->client
== clientV5Command
) {
545 col_append_fstr(pinfo
->cinfo
, COL_INFO
, " Command Request - %s",
546 val_to_str_const(hash_info
->command
, cmd_strings
, "Unknown"));
548 proto_tree_add_item( tree
, hf_socks_ver
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
551 proto_tree_add_item( tree
, hf_socks_cmd
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
554 proto_tree_add_item( tree
, hf_socks_reserved
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
557 offset
= display_address(pinfo
, tvb
, offset
, tree
);
558 proto_tree_add_item( tree
, hf_client_port
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
560 else if ((state_info
->client
== clientWaitForAuthReply
) &&
561 (state_info
->server
== serverInitReply
)) {
565 ti
= proto_tree_add_uint( tree
, hf_socks_ver
, tvb
, offset
, 0, 5);
566 proto_item_set_generated(ti
);
568 proto_tree_add_item( tree
, hf_socks_subnegotiation_version
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
571 switch(hash_info
->authentication_method
)
573 case NO_AUTHENTICATION
:
575 case USER_NAME_AUTHENTICATION
:
576 col_append_str(pinfo
->cinfo
, COL_INFO
, " User authentication request");
578 /* process user name */
579 len
= tvb_get_uint8(tvb
, offset
);
580 str
= tvb_get_string_enc(pinfo
->pool
, tvb
, offset
+1, len
, ENC_ASCII
);
581 proto_tree_add_string(tree
, hf_socks_username
, tvb
, offset
, len
+1, str
);
584 len
= tvb_get_uint8(tvb
, offset
);
585 str
= tvb_get_string_enc(pinfo
->pool
, tvb
, offset
+1, len
, ENC_ASCII
);
586 proto_tree_add_string(tree
, hf_socks_password
, tvb
, offset
, len
+1, str
);
587 /* offset += (len+1); */
589 case GSS_API_AUTHENTICATION
:
590 col_append_str(pinfo
->cinfo
, COL_INFO
, " GSSAPI authentication request");
592 proto_tree_add_item( tree
, hf_gssapi_command
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
593 proto_tree_add_item( tree
, hf_gssapi_length
, tvb
, offset
+1, 2, ENC_BIG_ENDIAN
);
594 len
= tvb_get_ntohs(tvb
, offset
+1);
596 proto_tree_add_item( tree
, hf_gssapi_payload
, tvb
, offset
+3, len
, ENC_NA
);
603 if (hash_info
->port
!= 0)
604 col_append_fstr(pinfo
->cinfo
, COL_INFO
, ", Remote Port: %u",
610 server_display_socks_v5(tvbuff_t
*tvb
, int offset
, packet_info
*pinfo
,
611 proto_tree
*tree
, socks_hash_entry_t
*hash_info _U_
, sock_state_t
* state_info
) {
613 /* Display the protocol tree for the version. This routine uses the */
614 /* stored conversation information to decide what to do with the row. */
615 /* Per packet information would have been better to do this, but we */
616 /* didn't have that when I wrote this. And I didn't expect this to get */
619 const char *AuthMethodStr
;
620 uint8_t auth
, auth_status
;
623 /* Either there is an error, or we're done with the state machine
624 (so there's nothing to display) */
625 if (state_info
== NULL
)
628 switch(state_info
->server
)
631 col_append_str(pinfo
->cinfo
, COL_INFO
, " Connect to server response");
633 proto_tree_add_item( tree
, hf_socks_ver
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
636 auth
= tvb_get_uint8( tvb
, offset
);
637 AuthMethodStr
= get_auth_method_name(auth
);
639 proto_tree_add_uint_format_value(tree
, hf_server_accepted_auth_method
, tvb
, offset
, 1, auth
,
640 "0x%0x (%s)", auth
, AuthMethodStr
);
643 case serverUserReply
:
644 col_append_str(pinfo
->cinfo
, COL_INFO
, " User authentication reply");
646 ti
= proto_tree_add_uint( tree
, hf_socks_ver
, tvb
, offset
, 0, 5);
647 proto_item_set_generated(ti
);
649 proto_tree_add_item( tree
, hf_socks_subnegotiation_version
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
652 auth_status
= tvb_get_uint8(tvb
, offset
);
653 ti
= proto_tree_add_item(tree
, hf_server_auth_status
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
655 proto_item_append_text(ti
, " (failure)");
657 proto_item_append_text(ti
, " (success)");
660 case serverGssApiReply
:
661 col_append_str(pinfo
->cinfo
, COL_INFO
, " GSSAPI authentication reply");
663 ti
= proto_tree_add_uint( tree
, hf_socks_ver
, tvb
, offset
, 0, 5);
664 proto_item_set_generated(ti
);
666 proto_tree_add_item( tree
, hf_socks_subnegotiation_version
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
669 auth_status
= tvb_get_uint8(tvb
, offset
);
670 proto_tree_add_item( tree
, hf_gssapi_command
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
671 if (auth_status
!= 0xFF) {
674 proto_tree_add_item( tree
, hf_gssapi_length
, tvb
, offset
+1, 2, ENC_BIG_ENDIAN
);
675 len
= tvb_get_ntohs(tvb
, offset
+1);
677 proto_tree_add_item( tree
, hf_gssapi_payload
, tvb
, offset
+3, len
, ENC_NA
);
681 case serverCommandReply
:
682 col_append_fstr(pinfo
->cinfo
, COL_INFO
, " Command Response - %s",
683 val_to_str_const(hash_info
->command
, cmd_strings
, "Unknown"));
685 proto_tree_add_item( tree
, hf_socks_ver
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
688 proto_tree_add_item( tree
, hf_socks_results_5
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
691 proto_tree_add_item( tree
, hf_socks_reserved
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
694 offset
= display_address(pinfo
, tvb
, offset
, tree
);
695 proto_tree_add_item( tree
, hf_client_port
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
698 case serverBindReply
:
699 col_append_str(pinfo
->cinfo
, COL_INFO
, " Command Response: Bind remote host info");
701 proto_tree_add_item( tree
, hf_socks_ver
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
704 proto_tree_add_item( tree
, hf_socks_results_5
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
707 proto_tree_add_item( tree
, hf_socks_reserved
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
710 offset
= display_address(pinfo
, tvb
, offset
, tree
);
711 proto_tree_add_item( tree
, hf_server_remote_host_port
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
715 if ( hash_info
->port
!= 0)
716 col_append_fstr(pinfo
->cinfo
, COL_INFO
, ", Remote Port: %u",
724 /**************** Decoder State Machines ******************/
728 state_machine_v4( socks_hash_entry_t
*hash_info
, tvbuff_t
*tvb
,
729 int offset
, packet_info
*pinfo
) {
731 /* Decode V4 protocol. This is done on the first pass through the */
732 /* list. Based upon the current state, decode the packet and determine */
733 /* what the next state should be. */
736 if (hash_info
->clientState
!= clientDone
)
737 save_client_state(pinfo
, hash_info
->clientState
);
739 if (hash_info
->serverState
!= serverDone
)
740 save_server_state(pinfo
, hash_info
->serverState
);
742 if (hash_info
->server_port
== pinfo
->destport
) {
743 /* Client side, only a single request */
744 col_append_str(pinfo
->cinfo
, COL_INFO
, " Connect to server request");
746 hash_info
->command
= tvb_get_uint8(tvb
, offset
+ 1);
748 /* get remote port */
749 if ( hash_info
->command
== CONNECT_COMMAND
)
750 hash_info
->port
= tvb_get_ntohs(tvb
, offset
+ 2);
752 /* get remote address */
753 set_address_tvb(&addr
, AT_IPv4
, 4, tvb
, offset
);
754 copy_address_wmem(wmem_file_scope(), &hash_info
->dst_addr
, &addr
);
756 hash_info
->clientState
= clientDone
;
759 col_append_str(pinfo
->cinfo
, COL_INFO
, " Connect Response");
761 if (tvb_get_uint8(tvb
, offset
+ 1) == 90)
762 hash_info
->serverState
= serverDone
;
764 hash_info
->serverState
= serverError
;
769 // NOLINTNEXTLINE(misc-no-recursion)
770 client_state_machine_v5( socks_hash_entry_t
*hash_info
, tvbuff_t
*tvb
,
771 int offset
, packet_info
*pinfo
, bool start_of_frame
) {
773 /* Decode client side of V5 protocol. This is done on the first pass through the */
774 /* list. Based upon the current state, decode the packet and determine */
775 /* what the next state should be. */
777 if (start_of_frame
) {
778 save_client_state(pinfo
, hash_info
->clientState
);
779 save_server_state(pinfo
, hash_info
->serverState
);
782 if (hash_info
->clientState
== clientStart
)
784 uint8_t num_auth_methods
;
786 num_auth_methods
= tvb_get_uint8(tvb
, offset
+ 1);
787 /* skip past auth methods */
789 if ((num_auth_methods
== 0) ||
790 ((num_auth_methods
== 1) &&
791 (tvb_get_uint8(tvb
, offset
+ 2) == 0))) {
792 /* No authentication needed */
793 hash_info
->clientState
= clientV5Command
;
794 if (tvb_reported_length_remaining(tvb
, offset
+ 2 + num_auth_methods
) > 0) {
795 increment_dissection_depth(pinfo
);
796 client_state_machine_v5(hash_info
, tvb
, offset
+ 2 + num_auth_methods
, pinfo
, false);
797 decrement_dissection_depth(pinfo
);
800 hash_info
->clientState
= clientWaitForAuthReply
;
802 } else if ((hash_info
->clientState
== clientWaitForAuthReply
) &&
803 (hash_info
->serverState
== serverInitReply
)) {
805 switch(hash_info
->authentication_method
)
807 case NO_AUTHENTICATION
:
808 hash_info
->clientState
= clientV5Command
;
809 hash_info
->serverState
= serverCommandReply
;
811 case USER_NAME_AUTHENTICATION
:
812 hash_info
->clientState
= clientV5Command
;
813 hash_info
->serverState
= serverUserReply
;
815 case GSS_API_AUTHENTICATION
:
816 hash_info
->clientState
= clientV5Command
;
817 hash_info
->serverState
= serverGssApiReply
;
820 hash_info
->clientState
= clientError
; /*Auth failed or error*/
823 } else if (hash_info
->clientState
== clientV5Command
) {
824 hash_info
->command
= tvb_get_uint8(tvb
, offset
+ 1); /* get command */
826 offset
+= 3; /* skip to address type */
828 offset
= get_address_v5(tvb
, offset
, hash_info
);
830 /** temp = tvb_get_uint8(tvb, offset); XX: what was this for ? **/
832 if (( hash_info
->command
== CONNECT_COMMAND
) ||
833 ( hash_info
->command
== UDP_ASSOCIATE_COMMAND
))
834 /* get remote port */
835 hash_info
->port
= tvb_get_ntohs(tvb
, offset
);
837 hash_info
->clientState
= clientDone
;
842 server_state_machine_v5( socks_hash_entry_t
*hash_info
, tvbuff_t
*tvb
,
843 int offset
, packet_info
*pinfo
, bool start_of_frame
) {
845 /* Decode server side of V5 protocol. This is done on the first pass through the */
846 /* list. Based upon the current state, decode the packet and determine */
847 /* what the next state should be. */
850 save_server_state(pinfo
, hash_info
->serverState
);
852 switch (hash_info
->serverState
) {
854 hash_info
->authentication_method
= tvb_get_uint8(tvb
, offset
+ 1);
855 switch (hash_info
->authentication_method
)
857 case NO_AUTHENTICATION
:
858 /* If there is no authentication, client should expect command immediately */
859 hash_info
->serverState
= serverCommandReply
;
860 hash_info
->clientState
= clientV5Command
;
862 case USER_NAME_AUTHENTICATION
:
863 hash_info
->serverState
= serverInitReply
;
865 case GSS_API_AUTHENTICATION
:
866 hash_info
->serverState
= serverInitReply
;
869 hash_info
->serverState
= serverError
;
873 case serverUserReply
:
874 hash_info
->serverState
= serverCommandReply
;
876 case serverGssApiReply
:
877 if (tvb_get_uint8(tvb
, offset
+1) == 0xFF) {
878 hash_info
->serverState
= serverError
;
880 if (tvb_get_ntohs(tvb
, offset
+2) == 0)
881 hash_info
->serverState
= serverCommandReply
;
884 case serverCommandReply
:
885 switch(hash_info
->command
)
887 case CONNECT_COMMAND
:
889 case TRACERT_COMMAND
:
890 hash_info
->serverState
= serverDone
;
894 hash_info
->serverState
= serverBindReply
;
895 if ((tvb_get_uint8(tvb
, offset
+ 2) == 0) &&
896 (tvb_reported_length_remaining(tvb
, offset
) > 5)) {
897 offset
= display_address(pinfo
, tvb
, offset
, NULL
);
898 client_state_machine_v5(hash_info
, tvb
, offset
, pinfo
, false);
902 case UDP_ASSOCIATE_COMMAND
:
903 offset
+= 3; /* skip to address type */
904 offset
= get_address_v5(tvb
, offset
, hash_info
);
906 /* save server udp port and create udp conversation */
907 hash_info
->udp_port
= tvb_get_ntohs(tvb
, offset
);
909 if (!pinfo
->fd
->visited
)
910 new_udp_conversation( hash_info
, pinfo
);
915 case serverBindReply
:
924 display_ping_and_tracert(tvbuff_t
*tvb
, int offset
, packet_info
*pinfo
, proto_tree
*tree
, socks_hash_entry_t
*hash_info
) {
926 /* Display the ping/trace_route conversation */
928 const unsigned char *data
, *dataend
;
929 const unsigned char *lineend
, *eol
;
932 /* handle the end command */
933 if ( pinfo
->destport
== TCP_PORT_SOCKS
){
934 col_append_str(pinfo
->cinfo
, COL_INFO
, ", Terminate Request");
936 proto_tree_add_item(tree
, (hash_info
->command
== PING_COMMAND
) ? hf_socks_ping_end_command
: hf_socks_traceroute_end_command
, tvb
, offset
, 1, ENC_NA
);
938 else { /* display the PING or Traceroute results */
939 col_append_str(pinfo
->cinfo
, COL_INFO
, ", Results");
942 proto_tree_add_item(tree
, (hash_info
->command
== PING_COMMAND
) ? hf_socks_ping_results
: hf_socks_traceroute_results
, tvb
, offset
, -1, ENC_NA
);
944 data
= tvb_get_ptr(tvb
, offset
, -1);
945 dataend
= data
+ tvb_captured_length_remaining(tvb
, offset
);
947 while (data
< dataend
) {
949 lineend
= find_line_end(data
, dataend
, &eol
);
950 linelen
= (int)(lineend
- data
);
952 proto_tree_add_format_text( tree
, tvb
, offset
, linelen
);
960 static void clear_in_socks_dissector_flag(void *s
)
962 sock_state_t
* state_info
= (sock_state_t
*)s
;
963 state_info
->in_socks_dissector_flag
= 0; /* avoid recursive overflow */
966 static void call_next_dissector(tvbuff_t
*tvb
, int offset
, packet_info
*pinfo
,
967 proto_tree
*tree
, proto_tree
*socks_tree
,
968 socks_hash_entry_t
*hash_info
, sock_state_t
* state_info
, struct tcpinfo
*tcpinfo
)
971 /* Display the results for PING and TRACERT extensions or */
972 /* Call TCP dissector for the port that was passed during the */
973 /* connect process */
974 /* Load pointer to pinfo->XXXport depending upon the direction, */
975 /* change pinfo port to the remote port, call next dissector to decode */
976 /* the payload, and restore the pinfo port after that is done. */
979 uint16_t save_can_desegment
;
980 struct tcp_analysis
*tcpd
=NULL
;
983 if (( hash_info
->command
== PING_COMMAND
) ||
984 ( hash_info
->command
== TRACERT_COMMAND
))
986 display_ping_and_tracert(tvb
, offset
, pinfo
, tree
, hash_info
);
988 else { /* call the tcp port decoder to handle the payload */
990 /*XXX may want to load dest address here */
992 if (pinfo
->destport
== TCP_PORT_SOCKS
) {
993 ptr
= &pinfo
->destport
;
995 ptr
= &pinfo
->srcport
;
998 *ptr
= hash_info
->port
;
1000 tcpd
= get_tcp_conversation_data(NULL
, pinfo
);
1001 /* 2003-09-18 JCFoster Fixed problem with socks tunnel in socks tunnel */
1003 state_info
->in_socks_dissector_flag
= 1; /* avoid recursive overflow */
1004 CLEANUP_PUSH(clear_in_socks_dissector_flag
, state_info
);
1006 save_can_desegment
= pinfo
->can_desegment
;
1007 pinfo
->can_desegment
= pinfo
->saved_can_desegment
;
1008 dissect_tcp_payload(tvb
, pinfo
, offset
, tcpinfo
->seq
,
1009 tcpinfo
->nxtseq
, pinfo
->srcport
, pinfo
->destport
,
1010 tree
, socks_tree
, tcpd
, tcpinfo
);
1011 pinfo
->can_desegment
= save_can_desegment
;
1013 CLEANUP_CALL_AND_POP
;
1015 *ptr
= TCP_PORT_SOCKS
;
1022 dissect_socks(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void *data
) {
1025 proto_tree
*socks_tree
= NULL
;
1027 socks_hash_entry_t
*hash_info
;
1028 conversation_t
*conversation
;
1029 sock_state_t
* state_info
;
1031 struct tcpinfo
*tcpinfo
= (struct tcpinfo
*)data
;
1033 state_info
= (sock_state_t
*)p_get_proto_data(wmem_file_scope(), pinfo
, proto_socks
, 0);
1034 if (state_info
== NULL
) {
1035 state_info
= wmem_new(wmem_file_scope(), sock_state_t
);
1036 state_info
->in_socks_dissector_flag
= 0;
1037 state_info
->client
= clientNoInit
;
1038 state_info
->server
= serverNoInit
;
1040 p_add_proto_data(wmem_file_scope(), pinfo
, proto_socks
, 0, state_info
);
1043 /* avoid recursive overflow */
1044 if (state_info
->in_socks_dissector_flag
)
1047 conversation
= find_conversation_pinfo(pinfo
, 0);
1048 if (conversation
== NULL
) {
1049 /* If we don't already have a conversation, make sure the first
1050 byte is a valid version number */
1051 version
= tvb_get_uint8(tvb
, offset
);
1052 if ((version
!= 4) && (version
!= 5))
1055 conversation
= conversation_new(pinfo
->num
, &pinfo
->src
, &pinfo
->dst
,
1056 conversation_pt_to_conversation_type(pinfo
->ptype
), pinfo
->srcport
, pinfo
->destport
, 0);
1059 hash_info
= (socks_hash_entry_t
*)conversation_get_proto_data(conversation
,proto_socks
);
1060 if (hash_info
== NULL
){
1061 hash_info
= wmem_new0(wmem_file_scope(), socks_hash_entry_t
);
1062 hash_info
->start_done_frame
= INT_MAX
;
1063 hash_info
->clientState
= clientStart
;
1064 hash_info
->serverState
= serverStart
;
1066 hash_info
->server_port
= pinfo
->destport
;
1067 hash_info
->port
= 0;
1068 hash_info
->version
= tvb_get_uint8(tvb
, offset
); /* get version*/
1070 conversation_add_proto_data(conversation
, proto_socks
, hash_info
);
1072 /* set dissector for now */
1073 if (conversation_get_dissector(conversation
, pinfo
->num
) != NULL
) {
1074 conversation_set_dissector(conversation
, socks_handle
);
1078 /* display summary window information */
1079 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "Socks");
1081 if (( hash_info
->version
== 4) || ( hash_info
->version
== 5)){
1082 col_add_fstr(pinfo
->cinfo
, COL_INFO
, "Version: %d",
1083 hash_info
->version
);
1085 else /* unknown version display error */
1086 col_set_str(pinfo
->cinfo
, COL_INFO
, "Unknown");
1089 if ( hash_info
->command
== PING_COMMAND
)
1090 col_append_str(pinfo
->cinfo
, COL_INFO
, ", Ping Req");
1091 if ( hash_info
->command
== TRACERT_COMMAND
)
1092 col_append_str(pinfo
->cinfo
, COL_INFO
, ", Traceroute Req");
1094 /* run state machine if needed */
1095 if ((!pinfo
->fd
->visited
) &&
1096 (!((hash_info
->clientState
== clientDone
) &&
1097 (hash_info
->serverState
== serverDone
)))) {
1099 if (hash_info
->server_port
== pinfo
->destport
) {
1100 if ((hash_info
->clientState
!= clientError
) &&
1101 (hash_info
->clientState
!= clientDone
))
1103 if ( hash_info
->version
== 4) {
1104 state_machine_v4( hash_info
, tvb
, offset
, pinfo
);
1105 } else if ( hash_info
->version
== 5) {
1106 client_state_machine_v5( hash_info
, tvb
, offset
, pinfo
, true);
1110 if ((hash_info
->serverState
!= serverError
) &&
1111 (hash_info
->serverState
!= serverDone
)) {
1112 if ( hash_info
->version
== 4) {
1113 state_machine_v4( hash_info
, tvb
, offset
, pinfo
);
1114 } else if ( hash_info
->version
== 5) {
1115 server_state_machine_v5( hash_info
, tvb
, offset
, pinfo
, true);
1120 if ((hash_info
->clientState
== clientDone
) &&
1121 (hash_info
->serverState
== serverDone
)) { /* if done now */
1122 hash_info
->start_done_frame
= pinfo
->num
;
1126 /* if proto tree, decode and display */
1128 ti
= proto_tree_add_item( tree
, proto_socks
, tvb
, offset
, -1, ENC_NA
);
1129 socks_tree
= proto_item_add_subtree(ti
, ett_socks
);
1131 /* if past startup, add the faked stuff */
1132 if ( pinfo
->num
> hash_info
->start_done_frame
){
1133 /* add info to tree */
1134 ti
= proto_tree_add_uint( socks_tree
, hf_socks_ver
, tvb
, offset
, 0, hash_info
->version
);
1135 proto_item_set_generated(ti
);
1137 ti
= proto_tree_add_uint( socks_tree
, hf_socks_cmd
, tvb
, offset
, 0, hash_info
->command
);
1138 proto_item_set_generated(ti
);
1140 if (hash_info
->dst_addr
.type
== AT_IPv4
) {
1141 ti
= proto_tree_add_ipv4( socks_tree
, hf_socks_ip_dst
, tvb
,
1142 offset
, 0, *((const uint32_t*)hash_info
->dst_addr
.data
));
1143 proto_item_set_generated(ti
);
1144 } else if (hash_info
->dst_addr
.type
== AT_IPv6
) {
1145 ti
= proto_tree_add_ipv6( socks_tree
, hf_socks_ip6_dst
, tvb
,
1146 offset
, 0, (const ws_in6_addr
*)hash_info
->dst_addr
.data
);
1147 proto_item_set_generated(ti
);
1150 /* no fake address for ping & traceroute */
1152 if (( hash_info
->command
!= PING_COMMAND
) &&
1153 ( hash_info
->command
!= TRACERT_COMMAND
)){
1154 ti
= proto_tree_add_uint( socks_tree
, hf_socks_dstport
, tvb
, offset
, 0, hash_info
->port
);
1155 proto_item_set_generated(ti
);
1158 if (hash_info
->server_port
== pinfo
->destport
) {
1159 if ( hash_info
->version
== 4) {
1160 display_socks_v4(tvb
, offset
, pinfo
, socks_tree
, hash_info
, state_info
);
1161 } else if ( hash_info
->version
== 5) {
1162 client_display_socks_v5(tvb
, offset
, pinfo
, socks_tree
, hash_info
, state_info
);
1165 if ( hash_info
->version
== 4) {
1166 display_socks_v4(tvb
, offset
, pinfo
, socks_tree
, hash_info
, state_info
);
1167 } else if ( hash_info
->version
== 5) {
1168 server_display_socks_v5(tvb
, offset
, pinfo
, socks_tree
, hash_info
, state_info
);
1176 /* call next dissector if ready */
1177 if ( pinfo
->num
> hash_info
->start_done_frame
){
1178 call_next_dissector(tvb
, offset
, pinfo
, tree
, socks_tree
,
1179 hash_info
, state_info
, tcpinfo
);
1182 return tvb_reported_length(tvb
);
1187 dissect_socks_tls(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void *data
) {
1189 return dissect_socks(tvb
, pinfo
, tree
, data
);
1191 /* lets fake a tcpinfo, which TLS does not give us */
1194 tmp
.is_reassembled
= false;
1198 tmp
.urgent_pointer
= 0;
1199 return dissect_socks(tvb
, pinfo
, tree
, &tmp
);
1204 proto_register_socks( void){
1206 static int *ett
[] = {
1212 static hf_register_info hf
[] = {
1216 { "Version", "socks.version", FT_UINT8
, BASE_DEC
, NULL
,
1221 { "Remote Address", "socks.dst", FT_IPv4
, BASE_NONE
, NULL
,
1225 { &hf_socks_ip6_dst
,
1226 { "Remote Address(ipv6)", "socks.dstV6", FT_IPv6
, BASE_NONE
, NULL
,
1230 { &hf_gssapi_payload
,
1231 { "GSSAPI data", "socks.gssapi.data", FT_BYTES
, BASE_NONE
, NULL
,
1235 { &hf_gssapi_command
,
1236 { "SOCKS/GSSAPI command", "socks.gssapi.command", FT_UINT8
, BASE_DEC
,
1237 VALS(gssapi_command_table
), 0x0, NULL
, HFILL
1240 { &hf_gssapi_length
,
1241 { "SOCKS/GSSAPI data length", "socks.gssapi.length", FT_UINT16
, BASE_DEC
, NULL
,
1246 { "SOCKS v4a Remote Domain Name", "socks.v4a_dns_name", FT_STRINGZ
, BASE_NONE
,
1247 NULL
, 0x0, NULL
, HFILL
1250 { &hf_socks_dstport
,
1251 { "Remote Port", "socks.dstport", FT_UINT16
,
1252 BASE_DEC
, NULL
, 0x0, NULL
, HFILL
1256 { "Command", "socks.command", FT_UINT8
,
1257 BASE_DEC
, VALS(cmd_strings
), 0x0, NULL
, HFILL
1260 { &hf_socks_results_4
,
1261 { "Results(V4)", "socks.results", FT_UINT8
,
1262 BASE_DEC
, VALS(reply_table_v4
), 0x0, NULL
, HFILL
1265 { &hf_socks_results_5
,
1266 { "Results(V5)", "socks.results", FT_UINT8
,
1267 BASE_DEC
, VALS(reply_table_v5
), 0x0, NULL
, HFILL
1270 { &hf_client_auth_method_count
,
1271 { "Authentication Method Count", "socks.auth_method_count", FT_UINT8
,
1272 BASE_DEC
, NULL
, 0x0, NULL
, HFILL
1275 { &hf_client_auth_method
,
1276 { "Method", "socks.auth_method", FT_UINT8
,
1277 BASE_DEC
, NULL
, 0x0, NULL
, HFILL
1280 { &hf_socks_reserved
,
1281 { "Reserved", "socks.reserved", FT_UINT8
,
1282 BASE_DEC
, NULL
, 0x0, NULL
, HFILL
1285 { &hf_socks_reserved2
,
1286 { "Reserved", "socks.reserved", FT_UINT16
,
1287 BASE_DEC
, NULL
, 0x0, NULL
, HFILL
1291 { "Port", "socks.port", FT_UINT16
,
1292 BASE_DEC
, NULL
, 0x0, NULL
, HFILL
1295 { &hf_server_accepted_auth_method
,
1296 { "Accepted Auth Method", "socks.auth_accepted_method", FT_UINT8
,
1297 BASE_DEC
, NULL
, 0x0, NULL
, HFILL
1300 { &hf_server_auth_status
,
1301 { "Status", "socks.auth_status", FT_UINT8
,
1302 BASE_DEC
, NULL
, 0x0, NULL
, HFILL
1305 { &hf_server_remote_host_port
,
1306 { "Remote Host Port", "socks.remote_host_port", FT_UINT16
,
1307 BASE_DEC
, NULL
, 0x0, NULL
, HFILL
1310 { &hf_socks_subnegotiation_version
,
1311 { "Subnegotiation Version", "socks.subnegotiation_version", FT_UINT8
, BASE_DEC
, NULL
,
1315 { &hf_socks_username
,
1316 { "User name", "socks.username", FT_STRING
, BASE_NONE
,
1317 NULL
, 0x0, NULL
, HFILL
1320 { &hf_socks_password
,
1321 { "Password", "socks.password", FT_STRING
, BASE_NONE
,
1322 NULL
, 0x0, NULL
, HFILL
1325 { &hf_socks_remote_name
,
1326 { "Remote name", "socks.remote_name", FT_STRING
, BASE_NONE
,
1327 NULL
, 0x0, NULL
, HFILL
1330 { &hf_socks_address_type
,
1331 { "Address Type", "socks.address_type", FT_UINT8
,
1332 BASE_DEC
, VALS(address_type_table
), 0x0, NULL
, HFILL
1335 { &hf_socks_fragment_number
,
1336 { "Fragment Number", "socks.fragment_number", FT_UINT8
,
1337 BASE_DEC
, NULL
, 0x0, NULL
, HFILL
1340 { &hf_socks_ping_end_command
,
1341 { "Ping: End command", "socks.ping_end_command", FT_NONE
,
1342 BASE_NONE
, NULL
, 0x0, NULL
, HFILL
1345 { &hf_socks_ping_results
,
1346 { "Ping Results", "socks.ping_results", FT_NONE
,
1347 BASE_NONE
, NULL
, 0x0, NULL
, HFILL
1350 { &hf_socks_traceroute_end_command
,
1351 { "Traceroute: End command", "socks.traceroute_end_command", FT_NONE
,
1352 BASE_NONE
, NULL
, 0x0, NULL
, HFILL
1355 { &hf_socks_traceroute_results
,
1356 { "Traceroute Results", "socks.traceroute_results", FT_NONE
,
1357 BASE_NONE
, NULL
, 0x0, NULL
, HFILL
1362 proto_socks
= proto_register_protocol ( "Socks Protocol", "Socks", "socks");
1364 proto_register_field_array(proto_socks
, hf
, array_length(hf
));
1365 proto_register_subtree_array(ett
, array_length(ett
));
1367 socks_udp_handle
= register_dissector_with_description("socks_udp", "SOCKS over UDP", socks_udp_dissector
, proto_socks
);
1368 socks_handle
= register_dissector_with_description("socks_tcp", "SOCKS over TCP", dissect_socks
, proto_socks
);
1369 socks_handle_tls
= register_dissector_with_description("socks_tls", "SOCKS over TLS", dissect_socks_tls
, proto_socks
);
1374 proto_reg_handoff_socks(void) {
1376 /* dissector install routine */
1378 dissector_add_uint_with_preference("tcp.port", TCP_PORT_SOCKS
, socks_handle
);
1380 ssl_dissector_add(0, socks_handle_tls
);
1384 * Editor modelines - https://www.wireshark.org/tools/modelines.html
1389 * indent-tabs-mode: nil
1392 * vi: set shiftwidth=4 tabstop=8 expandtab:
1393 * :indentSize=4:tabSize=8:noTabs=true: