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

spo_alert_fast.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_fast
00023  * 
00024  * Purpose:  output plugin for fast alerting
00025  *
00026  * Arguments:  alert file
00027  *   
00028  * Effect:
00029  *
00030  * Alerts are written to a file in the snort fast alert format
00031  *
00032  * Comments:   Allows use of fast alerts with other output plugin types
00033  *
00034  */
00035 
00036 /* output plugin header file */
00037 #ifdef HAVE_CONFIG_H
00038 #include "config.h"
00039 #endif
00040 
00041 #include "event.h"
00042 #include "decode.h"
00043 #include "debug.h"
00044 #include "plugbase.h"
00045 #include "spo_plugbase.h"
00046 #include "parser.h"
00047 #include "util.h"
00048 #include "log.h"
00049 #include "mstring.h"
00050 
00051 #include "snort.h"
00052 
00053 #include <stdio.h>
00054 #include <stdlib.h>
00055 #include <string.h>
00056 
00057 #ifdef HAVE_STRINGS_H
00058 #include <strings.h>
00059 #endif
00060 
00061 #ifndef WIN32
00062 #include <sys/socket.h>
00063 #include <netinet/in.h>
00064 #include <arpa/inet.h>
00065 #endif /* !WIN32 */
00066 
00067 #include <sys/types.h>
00068 
00069 typedef struct _SpoAlertFastData
00070 {
00071     FILE *file;
00072     u_int8_t packet_flag;
00073 } SpoAlertFastData;
00074 
00075 void AlertFastInit(u_char *);
00076 SpoAlertFastData *ParseAlertFastArgs(char *);
00077 void AlertFastCleanExitFunc(int, void *);
00078 void AlertFastRestartFunc(int, void *);
00079 void AlertFast(Packet *, char *, void *, Event *);
00080 
00081 
00082 
00083 /*
00084  * Function: SetupAlertFast()
00085  *
00086  * Purpose: Registers the output plugin keyword and initialization 
00087  *          function into the output plugin list.  This is the function that
00088  *          gets called from InitOutputPlugins() in plugbase.c.
00089  *
00090  * Arguments: None.
00091  *
00092  * Returns: void function
00093  *
00094  */
00095 void AlertFastSetup(void)
00096 {
00097     /* link the preprocessor keyword to the init function in 
00098        the preproc list */
00099     RegisterOutputPlugin("alert_fast", NT_OUTPUT_ALERT, AlertFastInit);
00100     DEBUG_WRAP(DebugMessage(DEBUG_INIT,"Output plugin: AlertFast is setup...\n"););
00101 }
00102 
00103 
00104 /*
00105  * Function: AlertFastInit(u_char *)
00106  *
00107  * Purpose: Calls the argument parsing function, performs final setup on data
00108  *          structs, links the preproc function into the function list.
00109  *
00110  * Arguments: args => ptr to argument string
00111  *
00112  * Returns: void function
00113  *
00114  */
00115 void AlertFastInit(u_char *args)
00116 {
00117     SpoAlertFastData *data;
00118 
00119     DEBUG_WRAP(DebugMessage(DEBUG_INIT,"Output: AlertFast Initialized\n"););
00120 
00121     pv.alert_plugin_active = 1;
00122 
00123     /* parse the argument list from the rules file */
00124     data = ParseAlertFastArgs(args);
00125 
00126     DEBUG_WRAP(DebugMessage(DEBUG_INIT,"Linking AlertFast functions to call lists...\n"););
00127     
00128     /* Set the preprocessor function into the function list */
00129     AddFuncToOutputList(AlertFast, NT_OUTPUT_ALERT, data);
00130     AddFuncToCleanExitList(AlertFastCleanExitFunc, data);
00131     AddFuncToRestartList(AlertFastRestartFunc, data);
00132 }
00133 
00134 void AlertFast(Packet *p, char *msg, void *arg, Event *event)
00135 {
00136     char timestamp[TIMEBUF_SIZE];
00137     SpoAlertFastData *data = (SpoAlertFastData *)arg;
00138 
00139     bzero((char *) timestamp, TIMEBUF_SIZE);
00140     ts_print(p == NULL ? NULL : (struct timeval *) & p->pkth->ts, timestamp);
00141 
00142     /* dump the timestamp */
00143     fwrite(timestamp, strlen(timestamp), 1, data->file);
00144 
00145 
00146     if(msg != NULL)
00147     {
00148         fwrite(" [**] ", 6, 1, data->file);
00149 
00150         if(event != NULL)
00151         {
00152             fprintf(data->file, "[%lu:%lu:%lu] ",
00153                     (unsigned long) event->sig_generator,
00154                     (unsigned long) event->sig_id,
00155                     (unsigned long) event->sig_rev);
00156         }
00157 
00158         if(pv.alert_interface_flag)
00159         {
00160             fprintf(data->file, " <%s> ", PRINT_INTERFACE(pv.interface));
00161             fwrite(msg, strlen(msg), 1, data->file);
00162         }
00163         else
00164         {
00165             fwrite(msg, strlen(msg), 1, data->file);
00166         }
00167 
00168         fwrite(" [**] ", 6, 1, data->file);
00169     }
00170 
00171     /* print the packet header to the alert file */
00172     if(p && p->iph)
00173     {
00174         PrintPriorityData(data->file, 0);
00175 
00176         fprintf(data->file, "{%s} ", protocol_names[p->iph->ip_proto]);
00177 
00178 
00179 
00180         if(p->frag_flag)
00181         {
00182             /* just print the straight IP header */
00183             fputs(inet_ntoa(p->iph->ip_src), data->file);
00184             fwrite(" -> ", 4, 1, data->file);
00185             fputs(inet_ntoa(p->iph->ip_dst), data->file);
00186         }
00187         else
00188         {
00189             switch(p->iph->ip_proto)
00190             {
00191                 case IPPROTO_UDP:
00192                 case IPPROTO_TCP:
00193                     /* print the header complete with port information */
00194                     fputs(inet_ntoa(p->iph->ip_src), data->file);
00195                     fprintf(data->file, ":%d -> ", p->sp);
00196                     fputs(inet_ntoa(p->iph->ip_dst), data->file);
00197                     fprintf(data->file, ":%d", p->dp);
00198                     break;
00199                 case IPPROTO_ICMP:
00200                 default:
00201                     /* just print the straight IP header */
00202                     fputs(inet_ntoa(p->iph->ip_src), data->file);
00203                     fwrite(" -> ", 4, 1, data->file);
00204                     fputs(inet_ntoa(p->iph->ip_dst), data->file);
00205             }
00206         }
00207     }               /* end of if (p) */
00208     if(p && data->packet_flag)
00209     {
00210         fputc('\n', data->file);
00211 
00212         if(p->iph)
00213             PrintIPPkt(data->file, p->iph->ip_proto, p);
00214         else if(p->ah)
00215             PrintArpHeader(data->file, p);
00216     }
00217 
00218     fputc('\n', data->file);
00219 
00220     fflush(data->file);
00221     return;
00222 }
00223 
00224 /*
00225  * Function: ParseAlertFastArgs(char *)
00226  *
00227  * Purpose: Process the preprocessor arguements from the rules file and 
00228  *          initialize the preprocessor's data struct.  This function doesn't
00229  *          have to exist if it makes sense to parse the args in the init 
00230  *          function.
00231  *
00232  * Arguments: args => argument list
00233  *
00234  * Returns: void function
00235  *
00236  */
00237 SpoAlertFastData *ParseAlertFastArgs(char *args)
00238 {
00239     char **toks;
00240     int num_toks;
00241     char *filename;
00242     SpoAlertFastData *data;
00243 
00244     data = (SpoAlertFastData *)SnortAlloc(sizeof(SpoAlertFastData));
00245 
00246     if(args == NULL)
00247     {
00248         data->file = OpenAlertFile(NULL);
00249         return data;
00250     }
00251 
00252     DEBUG_WRAP(DebugMessage(DEBUG_LOG, "ParseAlertFastArgs: %s\n", args););
00253 
00254     toks = mSplit(args, " ", 2, &num_toks, 0);
00255     if(strcasecmp("stdout", toks[0]) == 0)
00256         data->file = stdout;
00257     else
00258     {
00259         filename = ProcessFileOption(toks[0]);
00260         data->file = OpenAlertFile(filename);
00261         free(filename);
00262     }
00263     if(num_toks > 1)
00264     {
00265         if(strcasecmp(toks[1], "packet") == 0)
00266         {
00267             data->packet_flag = 1;
00268         }
00269         else
00270         {
00271             FatalError("Unrecognized alert_fast option: %s\n", toks[1]);
00272         }
00273     }
00274     /* free toks */
00275     mSplitFree(&toks, num_toks);
00276 
00277     return data;
00278 }
00279 
00280 void AlertFastCleanExitFunc(int signal, void *arg)
00281 {
00282     SpoAlertFastData *data = (SpoAlertFastData *)arg;
00283     /* close alert file */
00284     DEBUG_WRAP(DebugMessage(DEBUG_LOG,"AlertFastCleanExitFunc\n"););
00285     fclose(data->file);
00286     /*free memory from SpoAlertFastData */
00287     free(data);
00288 }
00289 
00290 void AlertFastRestartFunc(int signal, void *arg)
00291 {
00292     SpoAlertFastData *data = (SpoAlertFastData *)arg;
00293     /* close alert file */
00294     DEBUG_WRAP(DebugMessage(DEBUG_LOG,"AlertFastRestartFunc\n"););
00295     fclose(data->file);
00296     /*free memory from SpoAlertFastData */
00297     free(data);
00298 }
00299 

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