00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #ifdef HAVE_CONFIG_H
00044 #include "config.h"
00045 #endif
00046
00047 #ifdef HAVE_STRINGS_H
00048 #include <strings.h>
00049 #endif
00050
00051 #include <sys/types.h>
00052
00053 #include "decode.h"
00054 #include "plugbase.h"
00055 #include "parser.h"
00056 #include "log.h"
00057 #include "debug.h"
00058 #include "util.h"
00059 #include "mstring.h"
00060 #include "snort.h"
00061
00062 extern u_int8_t DecodeBuffer[DECODE_BLEN];
00063
00064
00065 #define TNC_IAC 0xFF
00066 #define TNC_SB 0xFA
00067 #define TNC_GA 0xF9
00068 #define TNC_EAL 0xF8
00069 #define TNC_EAC 0xF7
00070 #define TNC_AO 0xF6
00071 #define TNC_AYT 0xF5
00072 #define TNC_IP 0xF4
00073 #define TNC_BRK 0xF3
00074 #define TNC_DM 0xF2
00075 #define TNC_NOP 0xF1
00076 #define TNC_SE 0xF0
00077
00078 #define TNC_STD_LENGTH 3
00079
00080
00081 extern void TelNegInit(u_char *);
00082 extern void NormalizeTelnet(Packet *, void *);
00083 static void SetTelnetPorts(char *portlist);
00084
00085
00086 static char TelnetDecodePorts[65536/8];
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 void SetupTelNeg()
00102 {
00103
00104
00105
00106 RegisterPreprocessor("telnet_decode", TelNegInit);
00107
00108 DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN, "Preprocessor: Telnet Decode Decode is setup...\n"););
00109 }
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 void TelNegInit(u_char *args)
00124 {
00125 DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN, "Preprocessor: TelNeg Initialized\n"););
00126
00127 SetTelnetPorts(args);
00128
00129 AddFuncToPreprocList(NormalizeTelnet);
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 void NormalizeTelnet(Packet *p, void *context)
00147 {
00148 char *read_ptr;
00149 char *start = (char *) DecodeBuffer;
00150 char *write_ptr;
00151 char *end;
00152 int normalization_required = 0;
00153
00154 if(!(p->preprocessors & PP_TELNEG))
00155 {
00156 return;
00157 }
00158
00159
00160 if(!PacketIsTCP(p))
00161 {
00162 return;
00163 }
00164
00165
00166 if(!(TelnetDecodePorts[(p->dp/8)] & (1<<(p->dp%8))))
00167 {
00168 return;
00169 }
00170
00171
00172
00173
00174 if(p->dsize < TNC_STD_LENGTH)
00175 {
00176 return;
00177 }
00178
00179
00180 read_ptr = p->data;
00181 end = p->data + p->dsize;
00182
00183
00184 while(!normalization_required && (read_ptr < end))
00185 {
00186
00187 if(*read_ptr == (char) TNC_IAC)
00188 {
00189
00190 normalization_required = 1;
00191 }
00192
00193 read_ptr++;
00194 }
00195
00196 if(!normalization_required)
00197 {
00198 DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN, "Nothing to process!\n"););
00199 return;
00200 }
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 read_ptr = p->data;
00211
00212
00213
00214
00215 write_ptr = (char *) DecodeBuffer;
00216
00217
00218 while((read_ptr < end) && (write_ptr < ((char *) DecodeBuffer) + DECODE_BLEN))
00219 {
00220
00221 if(((read_ptr + 1) < end) &&
00222 (*read_ptr == (char) TNC_IAC) &&
00223 (*(read_ptr + 1) != (char) TNC_SB))
00224 {
00225
00226 switch(* ((unsigned char *)(read_ptr + 1)))
00227 {
00228 case TNC_NOP:
00229 read_ptr += 2;
00230 break;
00231 case TNC_EAC:
00232 read_ptr += 2;
00233
00234 if(write_ptr > start)
00235 {
00236 write_ptr--;
00237 }
00238 break;
00239 case TNC_EAL:
00240
00241 read_ptr += 2;
00242 break;
00243 case TNC_SE:
00244
00245
00246
00247
00248
00249 read_ptr += 2;
00250 break;
00251
00252 case TNC_BRK:
00253 case TNC_DM:
00254 case TNC_IP:
00255 case TNC_AO:
00256 case TNC_AYT:
00257 case TNC_GA:
00258 read_ptr += 2;
00259 break;
00260
00261
00262 case TNC_IAC:
00263 read_ptr++;
00264 *write_ptr++ = *read_ptr++;
00265 break;
00266 default:
00267
00268 read_ptr += TNC_STD_LENGTH;
00269 }
00270 }
00271
00272 else if(((read_ptr + 1) < end) &&
00273 (*read_ptr == (char) TNC_IAC) &&
00274 (*(read_ptr+1) == (char) TNC_SB))
00275 {
00276
00277 do
00278 {
00279 read_ptr++;
00280 } while((*read_ptr != (char) TNC_SE) && (read_ptr < end));
00281 if (*read_ptr == (char)TNC_SE)
00282 read_ptr++;
00283 }
00284 else
00285 {
00286 DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN, "overwriting %2X(%c) with %2X(%c)\n",
00287 (char)(*write_ptr&0xFF), *write_ptr,
00288 (char)(*read_ptr & 0xFF), *read_ptr););
00289
00290
00291 *write_ptr++ = *read_ptr++;
00292 }
00293 }
00294
00295 p->packet_flags |= PKT_ALT_DECODE;
00296
00297 p->alt_dsize = write_ptr - start;
00298
00299
00300
00301
00302
00303 return;
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317 static void SetTelnetPorts(char *portlist)
00318 {
00319 char portstr[STD_BUF];
00320 char **toks;
00321 int is_reset = 0;
00322 int num_toks = 0;
00323 int num = 0;
00324
00325 if(portlist == NULL || *portlist == '\0')
00326 {
00327 portlist = "21 23 25 119";
00328 }
00329
00330
00331 toks = mSplit(portlist, " ", 31, &num_toks, '\\');
00332
00333 LogMessage("telnet_decode arguments:\n");
00334
00335
00336 for(num = 0; num < num_toks; num++)
00337 {
00338 if(isdigit((int)toks[num][0]))
00339 {
00340 char *num_p = NULL;
00341 long t_num;
00342
00343 t_num = strtol(toks[num], &num_p, 10);
00344
00345 if(*num_p != '\0')
00346 {
00347 FatalError("Port Number invalid format: %s\n", toks[num]);
00348 }
00349 else if(t_num < 0 || t_num > 65335)
00350 {
00351 FatalError("Port Number out of range: %ld\n", t_num);
00352 }
00353
00354
00355
00356 if(!is_reset)
00357 {
00358 bzero(&TelnetDecodePorts, sizeof(TelnetDecodePorts));
00359 portstr[0] = '\0';
00360 is_reset = 1;
00361 }
00362
00363
00364
00365
00366 TelnetDecodePorts[(t_num/8)] |= 1<<(t_num%8);
00367
00368 if(strlcat(portstr, toks[num], STD_BUF - 1) >= STD_BUF)
00369 {
00370 FatalError("%s(%d) Portstr is truncated!\n", file_name, file_line);
00371 }
00372
00373 if(strlcat(portstr, " ", STD_BUF - 1) >= STD_BUF)
00374 {
00375 FatalError("%s(%d) Portstr is truncated!\n", file_name, file_line);
00376 }
00377 }
00378 else
00379 {
00380 FatalError(" %s(%d) => Unknown argument to telnet_decode "
00381 "preprocessor: \"%s\"\n",
00382 file_name, file_line, toks[num]);
00383 }
00384 }
00385
00386 mSplitFree(&toks, num_toks);
00387
00388
00389 LogMessage(" Ports to decode telnet on: %s\n", portstr);
00390 }