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

spo_alert_full.c

Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
00003 ** Copyright (C) 2000,2001 Andrew R. Baker <andrewb@uab.edu>
00004 **
00005 ** This program is free software; you can redistribute it and/or modify
00006 ** it under the terms of the GNU General Public License as published by
00007 ** the Free Software Foundation; either version 2 of the License, or
00008 ** (at your option) any later version.
00009 **
00010 ** This program is distributed in the hope that it will be useful,
00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 ** GNU General Public License for more details.
00014 **
00015 ** You should have received a copy of the GNU General Public License
00016 ** along with this program; if not, write to the Free Software
00017 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 */
00019 
00020 /* $Id$ */
00021 
00022 /* spo_alert_full
00023  * 
00024  * Purpose:  output plugin for full alerting
00025  *
00026  * Arguments:  alert file (eventually)
00027  *   
00028  * Effect:
00029  *
00030  * Alerts are written to a file in the snort full alert format
00031  *
00032  * Comments:   Allows use of full alerts with other output plugin types
00033  *
00034  */
00035 
00036 #ifdef HAVE_CONFIG_H
00037 #include "config.h"
00038 #endif
00039 
00040 #include "event.h"
00041 #include "decode.h"
00042 #include "plugbase.h"
00043 #include "spo_plugbase.h"
00044 #include "debug.h"
00045 #include "parser.h"
00046 #include "util.h"
00047 #include "log.h"
00048 #include "mstring.h"
00049 
00050 #include "snort.h"
00051 
00052 #ifdef HAVE_STRINGS_H
00053 #include <strings.h>
00054 #endif
00055 
00056 #include <stdio.h>
00057 #include <stdlib.h>
00058 
00059 
00060 typedef struct _SpoAlertFullData
00061 {
00062     FILE *file;
00063 } SpoAlertFullData;
00064 
00065 void AlertFullInit(u_char *);
00066 SpoAlertFullData *ParseAlertFullArgs(char *);
00067 void AlertFull(Packet *, char *, void *, Event *);
00068 void AlertFullCleanExit(int, void *);
00069 void AlertFullRestart(int, void *);
00070 
00071 
00072 /*
00073  * Function: SetupAlertFull()
00074  *
00075  * Purpose: Registers the output plugin keyword and initialization 
00076  *          function into the output plugin list.  This is the function that
00077  *          gets called from InitOutputPlugins() in plugbase.c.
00078  *
00079  * Arguments: None.
00080  *
00081  * Returns: void function
00082  *
00083  */
00084 void AlertFullSetup(void)
00085 {
00086     /* link the preprocessor keyword to the init function in 
00087        the preproc list */
00088     RegisterOutputPlugin("alert_full", NT_OUTPUT_ALERT, AlertFullInit);
00089 
00090     DEBUG_WRAP(DebugMessage(DEBUG_INIT,"Output plugin: AlertFull is setup...\n"););
00091 }
00092 
00093 
00094 /*
00095  * Function: AlertFullInit(u_char *)
00096  *
00097  * Purpose: Calls the argument parsing function, performs final setup on data
00098  *          structs, links the preproc function into the function list.
00099  *
00100  * Arguments: args => ptr to argument string
00101  *
00102  * Returns: void function
00103  *
00104  */
00105 void AlertFullInit(u_char *args)
00106 {
00107     SpoAlertFullData *data;
00108     DEBUG_WRAP(DebugMessage(DEBUG_INIT, "Output: AlertFull Initialized\n"););
00109     
00110     pv.alert_plugin_active = 1;
00111 
00112     /* parse the argument list from the rules file */
00113     data = ParseAlertFullArgs(args);
00114     DEBUG_WRAP(DebugMessage(DEBUG_INIT,"Linking AlertFull functions to call lists...\n"););
00115 
00116     /* Set the preprocessor function into the function list */
00117     AddFuncToOutputList(AlertFull, NT_OUTPUT_ALERT, data);
00118     AddFuncToCleanExitList(AlertFullCleanExit, data);
00119     AddFuncToRestartList(AlertFullRestart, data);
00120 }
00121 
00122 void AlertFull(Packet *p, char *msg, void *arg, Event *event)
00123 {
00124     char timestamp[TIMEBUF_SIZE];
00125     SpoAlertFullData *data = (SpoAlertFullData *)arg;
00126 
00127     if(msg != NULL)
00128     {
00129         fwrite("[**] ", 5, 1, data->file);
00130 
00131         if(event != NULL)
00132         {
00133                 fprintf(data->file, "[%lu:%lu:%lu] ",
00134                         (unsigned long) event->sig_generator,
00135                         (unsigned long) event->sig_id,
00136                         (unsigned long) event->sig_rev);
00137         }
00138 
00139         if(pv.alert_interface_flag)
00140         {
00141             fprintf(data->file, " <%s> ", PRINT_INTERFACE(pv.interface));
00142             fwrite(msg, strlen(msg), 1, data->file);
00143             fwrite(" [**]\n", 6, 1, data->file);
00144         }
00145         else
00146         {
00147             fwrite(msg, strlen(msg), 1, data->file);
00148             fwrite(" [**]\n", 6, 1, data->file);
00149         }
00150     }
00151     else
00152     {
00153         fwrite("[**] Snort Alert! [**]\n", 23, 1, data->file);
00154     }
00155 
00156     if(p && p->iph)
00157     {
00158         PrintPriorityData(data->file, 1);
00159     }
00160 
00161     DEBUG_WRAP(DebugMessage(DEBUG_LOG, "Logging Alert data!\n"););
00162 
00163     bzero((char *) timestamp, TIMEBUF_SIZE);
00164     ts_print(p == NULL ? NULL : (struct timeval *) & p->pkth->ts, timestamp);
00165 
00166     /* dump the timestamp */
00167     fwrite(timestamp, strlen(timestamp), 1, data->file);
00168     if(p && p->iph)
00169     {
00170         /* print the packet header to the alert file */
00171 
00172         if(pv.show2hdr_flag)
00173         {
00174             Print2ndHeader(data->file, p);
00175         }
00176 
00177         PrintIPHeader(data->file, p);
00178 
00179         /* if this isn't a fragment, print the other header info */
00180         if(!p->frag_flag)
00181         {
00182             switch(p->iph->ip_proto)
00183             {
00184                 case IPPROTO_TCP:
00185                     PrintTCPHeader(data->file, p);
00186                     break;
00187 
00188                 case IPPROTO_UDP:
00189                     PrintUDPHeader(data->file, p);
00190                     break;
00191 
00192                 case IPPROTO_ICMP:
00193                     PrintICMPHeader(data->file, p);
00194                     break;
00195 
00196                 default:
00197                     break;
00198             }
00199 
00200             PrintXrefs(data->file, 1);
00201         }
00202 
00203         fputc('\n', data->file);
00204     } /* End of if(p) */
00205     else
00206     {
00207         fputs("\n\n", data->file);
00208     }
00209 
00210     fflush(data->file);
00211     return;
00212  
00213 
00214 
00215 }
00216 
00217 
00218 /*
00219  * Function: ParseAlertFullArgs(char *)
00220  *
00221  * Purpose: Process the preprocessor arguements from the rules file and 
00222  *          initialize the preprocessor's data struct.  This function doesn't
00223  *          have to exist if it makes sense to parse the args in the init 
00224  *          function.
00225  *
00226  * Arguments: args => argument list
00227  *
00228  * Returns: void function
00229  *
00230  */
00231 SpoAlertFullData *ParseAlertFullArgs(char *args)
00232 {
00233     char **toks;
00234     int num_toks;
00235     char *filename;
00236     SpoAlertFullData *data;
00237 
00238     data = (SpoAlertFullData *)SnortAlloc(sizeof(SpoAlertFullData));
00239     if(args == NULL)
00240     {
00241         data->file = OpenAlertFile(NULL);
00242         return data;
00243     }
00244     DEBUG_WRAP(DebugMessage(DEBUG_LOG,"ParseAlertFullArgs: %s\n", args););
00245 
00246     toks = mSplit(args, " ", 2, &num_toks, 0);
00247     if(strcasecmp("stdout", toks[0]) == 0)
00248         data->file = stdout;
00249     else
00250     {
00251         filename = ProcessFileOption(toks[0]);
00252         data->file = OpenAlertFile(filename);
00253         free(filename);
00254     }
00255     mSplitFree(&toks, num_toks);
00256     return data;
00257 }
00258 
00259 void AlertFullCleanExit(int signal, void *arg)
00260 {
00261     SpoAlertFullData *data = (SpoAlertFullData *)arg;
00262     /* close alert file */
00263     DEBUG_WRAP(DebugMessage(DEBUG_LOG,"AlertFullCleanExit\n"););
00264     fclose(data->file);
00265     /* free memory from SpoAlertFullData */
00266     free(data);
00267 }
00268 
00269 void AlertFullRestart(int signal, void *arg)
00270 {
00271     SpoAlertFullData *data = (SpoAlertFullData *)arg;
00272     /* close alert file */
00273     DEBUG_WRAP(DebugMessage(DEBUG_LOG,"AlertFullRestart\n"););
00274     fclose(data->file);
00275     /* free memory from SpoAlertFullData */
00276     free(data);
00277 }
00278 

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