Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

sp_icmp_id_check.c

Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 /* $Id$ */
00020 /* sp_icmp_id 
00021  * 
00022  * Purpose:
00023  *
00024  * Test the ID field of ICMP ECHO and ECHO_REPLY packets for specified 
00025  * values.  This is useful for detecting TFN attacks, amongst others.
00026  *
00027  * Arguments:
00028  *   
00029  * The ICMP ID plugin takes a number as an option argument.
00030  *
00031  * Effect:
00032  *
00033  * Tests ICMP ECHO and ECHO_REPLY packet ID field values and returns a 
00034  * "positive" detection result (i.e. passthrough) upon a value match.
00035  *
00036  * Comments:
00037  *
00038  * This plugin was developed to detect TFN distributed attacks.
00039  *
00040  */
00041 
00042 #ifdef HAVE_CONFIG_H
00043 #include "config.h"
00044 #endif
00045 
00046 #include <sys/types.h>
00047 #include <stdlib.h>
00048 #include <ctype.h>
00049 
00050 #include "rules.h"
00051 #include "decode.h"
00052 #include "plugbase.h"
00053 #include "parser.h"
00054 #include "debug.h"
00055 #include "util.h"
00056 #include "plugin_enum.h"
00057 
00058 typedef struct _IcmpIdData
00059 {
00060         u_short icmpid;
00061 
00062 } IcmpIdData;
00063 
00064 void ParseIcmpId(char *, OptTreeNode *);
00065 int IcmpIdCheck(Packet *, struct _OptTreeNode *, OptFpList *);
00066 void IcmpIdCheckInit(char *, OptTreeNode *, int);
00067 
00068 
00069 
00070 
00071 /****************************************************************************
00072  * 
00073  * Function: SetupIcmpIdCheck()
00074  *
00075  * Purpose: Registers the configuration function and links it to a rule
00076  *          keyword.  This is the function that gets called from InitPlugins
00077  *          in plugbase.c.
00078  *
00079  * Arguments: None.
00080  *
00081  * Returns: void function
00082  *
00083  ****************************************************************************/
00084 void SetupIcmpIdCheck(void)
00085 {
00086     /* map the keyword to an initialization/processing function */
00087     RegisterPlugin("icmp_id", IcmpIdCheckInit);
00088 
00089     DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN,"Plugin: IcmpIdCheck Setup\n"););
00090 }
00091 
00092 
00093 /****************************************************************************
00094  * 
00095  * Function: IcmpIdCheckInit(char *, OptTreeNode *)
00096  *
00097  * Purpose: Handles parsing the rule information and attaching the associated
00098  *          detection function to the OTN.
00099  *
00100  * Arguments: data => rule arguments/data
00101  *            otn => pointer to the current rule option list node
00102  *
00103  * Returns: void function
00104  *
00105  ****************************************************************************/
00106 void IcmpIdCheckInit(char *data, OptTreeNode *otn, int protocol)
00107 {
00108     if(protocol != IPPROTO_ICMP)
00109     {
00110         FatalError("%s(%d): ICMP Options on non-ICMP rule\n", file_name, file_line);
00111     }
00112 
00113     /* multiple declaration check */ 
00114     if(otn->ds_list[PLUGIN_ICMP_ID_CHECK])
00115     {
00116         FatalError("%s(%d): Multiple icmp id options in rule\n", file_name,
00117                 file_line);
00118     }
00119 
00120     /* allocate the data structure and attach it to the
00121        rule's data struct list */
00122     otn->ds_list[PLUGIN_ICMP_ID_CHECK] = (IcmpIdData *)
00123         SnortAlloc(sizeof(IcmpIdData));
00124 
00125     /* this is where the keyword arguments are processed and placed into the 
00126        rule option's data structure */
00127 
00128     ParseIcmpId(data, otn);
00129 
00130     /* finally, attach the option's detection function to the rule's 
00131        detect function pointer list */
00132     AddOptFuncToList(IcmpIdCheck, otn);
00133 }
00134 
00135 
00136 
00137 /****************************************************************************
00138  * 
00139  * Function: ParseIcmpId(char *, OptTreeNode *)
00140  *
00141  * Purpose: Convert the rule option argument to program data.
00142  *
00143  * Arguments: data => argument data
00144  *            otn => pointer to the current rule's OTN
00145  *
00146  * Returns: void function
00147  *
00148  ****************************************************************************/
00149 void ParseIcmpId(char *data, OptTreeNode *otn)
00150 {
00151     IcmpIdData *ds_ptr;  /* data struct pointer */
00152 
00153     /* set the ds pointer to make it easier to reference the option's
00154        particular data struct */
00155     ds_ptr = otn->ds_list[PLUGIN_ICMP_ID_CHECK];
00156 
00157     /* advance past whitespace */
00158     while(isspace((int)*data)) data++;
00159 
00160     ds_ptr->icmpid = atoi(data);
00161     ds_ptr->icmpid = htons(ds_ptr->icmpid);
00162 
00163     DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN,"Set ICMP ID test value to %d\n", ds_ptr->icmpid););
00164 }
00165 
00166 
00167 /****************************************************************************
00168  * 
00169  * Function: IcmpIdCheck(char *, OptTreeNode *)
00170  *
00171  * Purpose: Compare the ICMP ID field to the rule value.
00172  *
00173  * Arguments: data => argument data
00174  *            otn => pointer to the current rule's OTN
00175  *
00176  * Returns: If the detection test fails, this function *must* return a zero!
00177  *          On success, it calls the next function in the detection list 
00178  *
00179  ****************************************************************************/
00180 int IcmpIdCheck(Packet *p, struct _OptTreeNode *otn, OptFpList *fp_list)
00181 {
00182     if(!p->icmph)
00183         return 0; /* if error occured while icmp header
00184                    * was processed, return 0 automagically.
00185                */
00186     if(p->icmph->type == ICMP_ECHO || p->icmph->type == ICMP_ECHOREPLY)
00187     {
00188         /* test the rule ID value against the ICMP extension ID field */
00189         if(((IcmpIdData *) otn->ds_list[PLUGIN_ICMP_ID_CHECK])->icmpid == 
00190            p->icmph->s_icmp_id)
00191         {
00192             /* call the next function in the function list recursively */
00193             return fp_list->next->OptTestFunc(p, otn, fp_list->next);
00194         }
00195         else
00196         {
00197             /* you can put debug comments here or not */
00198             DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN, "ICMP ID check failed\n"););
00199         }
00200     }
00201 
00202     /* if the test isn't successful, this function *must* return 0 */
00203     return 0;
00204 }

Generated on Sun May 14 14:51:13 2006 by  doxygen 1.4.2