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 #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
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
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 void AlertFastSetup(void)
00096 {
00097
00098
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
00106
00107
00108
00109
00110
00111
00112
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
00124 data = ParseAlertFastArgs(args);
00125
00126 DEBUG_WRAP(DebugMessage(DEBUG_INIT,"Linking AlertFast functions to call lists...\n"););
00127
00128
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
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
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
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
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
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 }
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
00226
00227
00228
00229
00230
00231
00232
00233
00234
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
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
00284 DEBUG_WRAP(DebugMessage(DEBUG_LOG,"AlertFastCleanExitFunc\n"););
00285 fclose(data->file);
00286
00287 free(data);
00288 }
00289
00290 void AlertFastRestartFunc(int signal, void *arg)
00291 {
00292 SpoAlertFastData *data = (SpoAlertFastData *)arg;
00293
00294 DEBUG_WRAP(DebugMessage(DEBUG_LOG,"AlertFastRestartFunc\n"););
00295 fclose(data->file);
00296
00297 free(data);
00298 }
00299