3232
3333static void ndpi_int_irc_add_connection (struct ndpi_detection_module_struct * ndpi_struct , struct ndpi_flow_struct * flow , ndpi_confidence_t confidence )
3434{
35+ NDPI_LOG_INFO (ndpi_struct , "Found IRC\n" );
3536 ndpi_set_detected_protocol (ndpi_struct , flow , NDPI_PROTOCOL_IRC , NDPI_PROTOCOL_UNKNOWN , confidence );
3637}
3738
@@ -50,122 +51,49 @@ static void ndpi_search_irc_tcp(struct ndpi_detection_module_struct *ndpi_struct
5051{
5152 struct ndpi_packet_struct * packet = & ndpi_struct -> packet ;
5253
53- u_int16_t c = 0 ;
54- u_int16_t i = 0 ;
55-
5654 NDPI_LOG_DBG (ndpi_struct , "search irc\n" );
57- if ((flow -> detected_protocol_stack [0 ] != NDPI_PROTOCOL_IRC && (flow -> packet_counter > 10 ))
58- || (flow -> packet_counter >= 10 )) {
59- NDPI_EXCLUDE_DISSECTOR (ndpi_struct , flow );
60- return ;
61- }
62-
63- if (flow -> detected_protocol_stack [0 ] != NDPI_PROTOCOL_IRC && flow -> packet_counter < 20
64- && packet -> payload_packet_len >= 8 ) {
65- if (get_u_int8_t (packet -> payload , packet -> payload_packet_len - 1 ) == 0x0a
66- || (ntohs (get_u_int16_t (packet -> payload , packet -> payload_packet_len - 2 )) == 0x0a00 )) {
67- if (memcmp (packet -> payload , ":" , 1 ) == 0 ) {
68- if (packet -> payload [packet -> payload_packet_len - 2 ] != 0x0d
69- && packet -> payload [packet -> payload_packet_len - 1 ] == 0x0a ) {
70- ndpi_parse_packet_line_info_any (ndpi_struct );
71- } else if (packet -> payload [packet -> payload_packet_len - 2 ] == 0x0d ) {
72- ndpi_parse_packet_line_info (ndpi_struct , flow );
73- } else {
74- flow -> l4 .tcp .irc_3a_counter ++ ;
75- packet -> parsed_lines = 0 ;
76- }
77- for (i = 0 ; i < packet -> parsed_lines ; i ++ ) {
78- if ((packet -> line [i ].len > 0 ) && packet -> line [i ].ptr [0 ] == ':' ) {
79- flow -> l4 .tcp .irc_3a_counter ++ ;
80- if (flow -> l4 .tcp .irc_3a_counter == 7 ) { /* ':' == 0x3a */
81- NDPI_LOG_INFO (ndpi_struct , "found irc. 0x3a. seven times." );
82- ndpi_int_irc_add_connection (ndpi_struct , flow , NDPI_CONFIDENCE_DPI );
83- return ;
84- }
85- }
86- }
87- if (flow -> l4 .tcp .irc_3a_counter == 7 ) { /* ':' == 0x3a */
88- NDPI_LOG_INFO (ndpi_struct , "found irc. 0x3a. seven times." );
89- ndpi_int_irc_add_connection (ndpi_struct , flow , NDPI_CONFIDENCE_DPI );
90- return ;
91- }
92- }
9355
94- if ((memcmp (packet -> payload , "USER " , 5 ) == 0 )
95- || (memcmp (packet -> payload , "NICK " , 5 ) == 0 )
96- || (memcmp (packet -> payload , "PASS " , 5 ) == 0 )
97- || (memcmp (packet -> payload , ":" , 1 ) == 0 && ndpi_check_for_NOTICE_or_PRIVMSG (ndpi_struct ) != 0 )
98- || (memcmp (packet -> payload , "PONG " , 5 ) == 0 )
99- || (memcmp (packet -> payload , "PING " , 5 ) == 0 )
100- || (memcmp (packet -> payload , "JOIN " , 5 ) == 0 )
101- || (memcmp (packet -> payload , "MODE " , 5 ) == 0 )
102- || (memcmp (packet -> payload , "NOTICE " , 7 ) == 0 )
103- || (memcmp (packet -> payload , "PRIVMSG " , 8 ) == 0 )
104- || (memcmp (packet -> payload , "VERSION " , 8 ) == 0 )) {
105- char * user = ndpi_strnstr ((char * )packet -> payload , "USER " , packet -> payload_packet_len );
106-
107- if (user ) {
108- char buf [32 ], msg [64 ], * sp ;
109-
110- snprintf (buf , sizeof (buf ), "%.*s" , (int )(packet -> payload_packet_len - (user + 5 - (char * )packet -> payload )), user + 5 );
111- sp = buf ;
112- strsep (& sp , " \r\n" );
56+ /* Simple detection, expecially from the beginning of the flow */
57+
58+ if (packet -> payload_packet_len >= 8 &&
59+ (get_u_int8_t (packet -> payload , packet -> payload_packet_len - 1 ) == 0x0a ||
60+ ntohs (get_u_int16_t (packet -> payload , packet -> payload_packet_len - 2 )) == 0x0a00 )) {
61+
62+ if (memcmp (packet -> payload , "USER " , 5 ) == 0 ||
63+ memcmp (packet -> payload , "NICK " , 5 ) == 0 ||
64+ memcmp (packet -> payload , "PASS " , 5 ) == 0 ||
65+ (memcmp (packet -> payload , ":" , 1 ) == 0 && ndpi_check_for_NOTICE_or_PRIVMSG (ndpi_struct ) != 0 ) ||
66+ memcmp (packet -> payload , "PONG " , 5 ) == 0 ||
67+ memcmp (packet -> payload , "HELLO " , 6 ) == 0 ||
68+ memcmp (packet -> payload , "YOURIP " , 7 ) == 0 ||
69+ memcmp (packet -> payload , "PING " , 5 ) == 0 ||
70+ memcmp (packet -> payload , "JOIN " , 5 ) == 0 ||
71+ memcmp (packet -> payload , "MODE " , 5 ) == 0 ||
72+ memcmp (packet -> payload , "NOTICE " , 7 ) == 0 ||
73+ memcmp (packet -> payload , "PRIVMSG " , 8 ) == 0 ||
74+ memcmp (packet -> payload , "VERSION " , 8 ) == 0 ) {
75+ char * user = ndpi_strnstr ((char * )packet -> payload , "USER " , packet -> payload_packet_len );
76+
77+ if (user ) {
78+ char buf [32 ], msg [64 ], * sp ;
79+
80+ snprintf (buf , sizeof (buf ), "%.*s" , (int )(packet -> payload_packet_len - (user + 5 - (char * )packet -> payload )), user + 5 );
81+ sp = buf ;
82+ strsep (& sp , " \r\n" );
11383
114- snprintf (msg , sizeof (msg ), "Found IRC username (%s)" , buf );
115- ndpi_set_risk (ndpi_struct , flow , NDPI_CLEAR_TEXT_CREDENTIALS , msg );
116- }
117-
118- NDPI_LOG_DBG2 (ndpi_struct ,
119- "USER, NICK, PASS, NOTICE, PRIVMSG one time" );
120- if (flow -> l4 .tcp .irc_stage == 2 ) {
121- NDPI_LOG_INFO (ndpi_struct , "found irc" );
122- ndpi_int_irc_add_connection (ndpi_struct , flow , NDPI_CONFIDENCE_DPI );
123- flow -> l4 .tcp .irc_stage = 3 ;
124- }
125- if (flow -> l4 .tcp .irc_stage == 1 ) {
126- NDPI_LOG_DBG2 (ndpi_struct , "second time, stage=2" );
127- flow -> l4 .tcp .irc_stage = 2 ;
128- }
129- if (flow -> l4 .tcp .irc_stage == 0 ) {
130- NDPI_LOG_DBG2 (ndpi_struct , "first time, stage=1" );
131- flow -> l4 .tcp .irc_stage = 1 ;
132- }
133- /* irc packets can have either windows line breaks (0d0a) or unix line breaks (0a) */
134- if (packet -> payload [packet -> payload_packet_len - 2 ] == 0x0d
135- && packet -> payload [packet -> payload_packet_len - 1 ] == 0x0a ) {
136- ndpi_parse_packet_line_info (ndpi_struct , flow );
137- if (packet -> parsed_lines > 1 ) {
138- NDPI_LOG_DBG2 (ndpi_struct , "packet contains more than one line" );
139- for (c = 1 ; c < packet -> parsed_lines ; c ++ ) {
140- if (packet -> line [c ].len > 4 && (memcmp (packet -> line [c ].ptr , "NICK " , 5 ) == 0
141- || memcmp (packet -> line [c ].ptr , "USER " , 5 ) == 0 )) {
142- NDPI_LOG_INFO (ndpi_struct , "found IRC: two icq signal words in the same packet" );
143- ndpi_int_irc_add_connection (ndpi_struct , flow , NDPI_CONFIDENCE_DPI );
144- flow -> l4 .tcp .irc_stage = 3 ;
145- return ;
146- }
147- }
148- }
149-
150- } else if (packet -> payload [packet -> payload_packet_len - 1 ] == 0x0a ) {
151- ndpi_parse_packet_line_info_any (ndpi_struct );
152- if (packet -> parsed_lines > 1 ) {
153- NDPI_LOG_DBG2 (ndpi_struct , "packet contains more than one line" );
154- for (c = 1 ; c < packet -> parsed_lines ; c ++ ) {
155- if (packet -> line [c ].len > 4 && (memcmp (packet -> line [c ].ptr , "NICK " , 5 ) == 0
156- || memcmp (packet -> line [c ].ptr , "USER " ,
157- 5 ) == 0 )) {
158- NDPI_LOG_INFO (ndpi_struct , "found IRC: two icq signal words in the same packet" );
159- ndpi_int_irc_add_connection (ndpi_struct , flow , NDPI_CONFIDENCE_DPI );
160- flow -> l4 .tcp .irc_stage = 3 ;
161- return ;
162- }
163- }
164- }
165- }
84+ snprintf (msg , sizeof (msg ), "Found IRC username (%s)" , buf );
85+ ndpi_set_risk (ndpi_struct , flow , NDPI_CLEAR_TEXT_CREDENTIALS , msg );
16686 }
87+
88+ NDPI_LOG_DBG2 (ndpi_struct , "IRC stage: %d\n" , flow -> l4 .tcp .irc_stage );
89+ flow -> l4 .tcp .irc_stage ++ ;
90+ /* 3 consecutive valid packets */
91+ if (flow -> l4 .tcp .irc_stage == 3 )
92+ ndpi_int_irc_add_connection (ndpi_struct , flow , NDPI_CONFIDENCE_DPI );
93+ return ;
16794 }
16895 }
96+ NDPI_EXCLUDE_DISSECTOR (ndpi_struct , flow );
16997}
17098
17199void init_irc_dissector (struct ndpi_detection_module_struct * ndpi_struct )
0 commit comments