1 /* @(#) $Id: ./src/analysisd/alerts/log.c, 2012/03/30 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
7 * This program is a free software; you can redistribute it
8 * and/or modify it under the terms of the GNU General Public
9 * License (version 2) as published by the FSF - Free Software
17 #include "getloglocation.h"
19 #include "eventinfo.h"
25 #include "GeoIPCity.h"
27 #define RFC1918_10 (167772160 & 4278190080) /* 10/8 */
28 #define RFC1918_172 (2886729728 & 4293918720) /* 172.17/12 */
29 #define RFC1918_192 (3232235520 & 4294901760) /* 192.168/16 */
30 #define NETMASK_8 4278190080 /* 255.0.0.0 */
31 #define NETMASK_12 4293918720 /* 255.240.0.0 */
32 #define NETMASK_16 4294901760 /* 255.255.0.0 */
34 static const char * _mk_NA( const char * p ){
39 /* Convert an dot-quad IP address into long format
41 unsigned long StrIP2Int(char *ip) {
42 unsigned int c1,c2,c3,c4;
43 /* IP address is not coming from user input -> We can trust it */
44 /* only minimal checking is performed */
46 if ((len < 7) || (len > 15)) return 0;
48 sscanf(ip, "%d.%d.%d.%d", &c1, &c2, &c3, &c4);
49 return((unsigned long)c4+c3*256+c2*256*256+c1*256*256*256);
54 /* Use the GeoIP API to locate an IP address
56 char *GeoIPLookup(char *ip)
60 char buffer[OS_SIZE_1024 +1];
63 /* Dumb way to detect an IPv6 address */
64 if (strchr(ip, ':')) {
66 gi = GeoIP_open(Config.geoip_db_path, GEOIP_INDEX_CACHE);
68 merror(INVALID_GEOIP_DB, ARGV0, Config.geoip6_db_path);
71 gir = GeoIP_record_by_name_v6(gi, (const char *)ip);
75 /* If we have a RFC1918 IP, do not perform a DB lookup (performance) */
76 longip = StrIP2Int(ip);
77 if (longip == 0 ) return("Unknown");
78 if ((longip & NETMASK_8) == RFC1918_10 ||
79 (longip & NETMASK_12) == RFC1918_172 ||
80 (longip & NETMASK_16) == RFC1918_192) return("");
82 gi = GeoIP_open(Config.geoip_db_path, GEOIP_INDEX_CACHE);
84 merror(INVALID_GEOIP_DB, ARGV0, Config.geoip_db_path);
87 gir = GeoIP_record_by_name(gi, (const char *)ip);
90 sprintf(buffer,"%s,%s,%s",
91 _mk_NA(gir->country_code),
92 _mk_NA(GeoIP_region_name_by_code(gir->country_code, gir->region)),
103 /* Drop/allow patterns */
108 /* OS_Store: v0.2, 2005/02/10 */
109 /* Will store the events in a file
110 * The string must be null terminated and contain
111 * any necessary new lines, tabs, etc.
114 void OS_Store(Eventinfo *lf)
116 if(strcmp(lf->location, "ossec-keepalive") == 0)
120 if(strstr(lf->location, "->ossec-keepalive") != NULL)
126 "%d %s %02d %s %s%s%s %s\n",
131 lf->hostname != lf->location?lf->hostname:"",
132 lf->hostname != lf->location?"->":"",
142 void OS_LogOutput(Eventinfo *lf)
145 char geoip_msg_src[OS_SIZE_1024 +1];
146 char geoip_msg_dst[OS_SIZE_1024 +1];
147 geoip_msg_src[0] = '\0';
148 geoip_msg_dst[0] = '\0';
149 if (Config.loggeoip) {
150 if (lf->srcip) { strncpy(geoip_msg_src, GeoIPLookup(lf->srcip), OS_SIZE_1024); }
151 if (lf->dstip) { strncpy(geoip_msg_dst, GeoIPLookup(lf->dstip), OS_SIZE_1024); }
155 "** Alert %d.%ld:%s - %s\n"
156 "%d %s %02d %s %s%s%s\nRule: %d (level %d) -> '%s'"
157 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n%.1256s\n",
160 lf->generated_rule->alert_opts & DO_MAILALERT?" mail ":"",
161 lf->generated_rule->group,
166 lf->hostname != lf->location?lf->hostname:"",
167 lf->hostname != lf->location?"->":"",
169 lf->generated_rule->sigid,
170 lf->generated_rule->level,
171 lf->generated_rule->comment,
173 lf->srcip == NULL?"":"\nSrc IP: ",
174 lf->srcip == NULL?"":lf->srcip,
177 (strlen(geoip_msg_src) == 0)?"":"\nSrc Location: ",
178 (strlen(geoip_msg_src) == 0)?"":geoip_msg_src,
184 lf->srcport == NULL?"":"\nSrc Port: ",
185 lf->srcport == NULL?"":lf->srcport,
187 lf->dstip == NULL?"":"\nDst IP: ",
188 lf->dstip == NULL?"":lf->dstip,
191 (strlen(geoip_msg_dst) == 0)?"":"\nDst Location: ",
192 (strlen(geoip_msg_dst) == 0)?"":geoip_msg_dst,
198 lf->dstport == NULL?"":"\nDst Port: ",
199 lf->dstport == NULL?"":lf->dstport,
201 lf->dstuser == NULL?"":"\nUser: ",
202 lf->dstuser == NULL?"":lf->dstuser,
207 /* Printing the last events if present */
208 if(lf->generated_rule->last_events)
210 char **lasts = lf->generated_rule->last_events;
213 printf("%.1256s\n",*lasts);
216 lf->generated_rule->last_events[0] = NULL;
227 /* OS_Log: v0.3, 2006/03/04 */
228 /* _writefile: v0.2, 2005/02/09 */
229 void OS_Log(Eventinfo *lf)
232 char geoip_msg_src[OS_SIZE_1024 +1];
233 char geoip_msg_dst[OS_SIZE_1024 +1];
234 geoip_msg_src[0] = '\0';
235 geoip_msg_dst[0] = '\0';
236 if (Config.loggeoip) {
237 if (lf->srcip) { strncpy(geoip_msg_src, GeoIPLookup(lf->srcip), OS_SIZE_1024 ); }
238 if (lf->dstip) { strncpy(geoip_msg_dst, GeoIPLookup(lf->dstip), OS_SIZE_1024 ); }
241 /* Writting to the alert log file */
243 "** Alert %d.%ld:%s - %s\n"
244 "%d %s %02d %s %s%s%s\nRule: %d (level %d) -> '%s'"
245 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n%.1256s\n",
248 lf->generated_rule->alert_opts & DO_MAILALERT?" mail ":"",
249 lf->generated_rule->group,
254 lf->hostname != lf->location?lf->hostname:"",
255 lf->hostname != lf->location?"->":"",
257 lf->generated_rule->sigid,
258 lf->generated_rule->level,
259 lf->generated_rule->comment,
261 lf->srcip == NULL?"":"\nSrc IP: ",
262 lf->srcip == NULL?"":lf->srcip,
265 (strlen(geoip_msg_src) == 0)?"":"\nSrc Location: ",
266 (strlen(geoip_msg_src) == 0)?"":geoip_msg_src,
272 lf->srcport == NULL?"":"\nSrc Port: ",
273 lf->srcport == NULL?"":lf->srcport,
275 lf->dstip == NULL?"":"\nDst IP: ",
276 lf->dstip == NULL?"":lf->dstip,
279 (strlen(geoip_msg_dst) == 0)?"":"\nDst Location: ",
280 (strlen(geoip_msg_dst) == 0)?"":geoip_msg_dst,
286 lf->dstport == NULL?"":"\nDst Port: ",
287 lf->dstport == NULL?"":lf->dstport,
289 lf->dstuser == NULL?"":"\nUser: ",
290 lf->dstuser == NULL?"":lf->dstuser,
295 /* Printing the last events if present */
296 if(lf->generated_rule->last_events)
298 char **lasts = lf->generated_rule->last_events;
301 fprintf(_aflog,"%.1256s\n",*lasts);
304 lf->generated_rule->last_events[0] = NULL;
307 fprintf(_aflog,"\n");
317 /* Initializing fw log regexes */
318 if(!OSMatch_Compile(FWDROP, &FWDROPpm, 0))
320 ErrorExit(REGEX_COMPILE, ARGV0, FWDROP,
324 if(!OSMatch_Compile(FWALLOW, &FWALLOWpm, 0))
326 ErrorExit(REGEX_COMPILE, ARGV0, FWALLOW,
333 /* FW_Log: v0.1, 2005/12/30 */
334 int FW_Log(Eventinfo *lf)
336 /* If we don't have the srcip or the
337 * action, there is no point in going
340 if(!lf->action || !lf->srcip || !lf->dstip || !lf->srcport ||
341 !lf->dstport || !lf->protocol)
347 /* Setting the actions */
350 /* discard, drop, deny, */
360 os_strdup("DROP", lf->action);
369 os_strdup("CLOSED", lf->action);
381 os_strdup("ALLOW", lf->action);
384 if(OSMatch_Execute(lf->action,strlen(lf->action),&FWDROPpm))
387 os_strdup("DROP", lf->action);
389 if(OSMatch_Execute(lf->action,strlen(lf->action),&FWALLOWpm))
392 os_strdup("ALLOW", lf->action);
397 os_strdup("UNKNOWN", lf->action);
405 "%d %s %02d %s %s%s%s %s %s %s:%s->%s:%s\n",
410 lf->hostname != lf->location?lf->hostname:"",
411 lf->hostname != lf->location?"->":"",