1 /* @(#) $Id: prelude.c,v 1.14 2009/11/09 20:18:52 dcid Exp $ */
3 /* Copyright (C) 2009 Trend Micro Inc.
6 * This program is a free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License (version 3) as published by the FSF - Free Software
15 #include <libprelude/prelude.h>
16 #include <libprelude/prelude-log.h>
17 #include <libprelude/idmef-message-print.h>
21 #include "eventinfo.h"
23 #define DEFAULT_ANALYZER_NAME "OSSEC"
24 #define ANALYZER_CLASS "Host IDS, File Integrity Checker, Log Analyzer"
25 #define ANALYZER_MODEL "Ossec"
26 #define ANALYZER_MANUFACTURER __site
27 #define ANALYZER_VERSION __version
35 /** OSSEC to prelude severity mapping. **/
36 char *(ossec2prelude_sev[])={"info","info","info","info",
37 "low","low","low","low",
38 "medium", "medium", "medium", "medium",
39 "high", "high", "high", "high", "high"};
43 static prelude_client_t *prelude_client;
46 void prelude_idmef_debug(idmef_message_t *idmef)
51 prelude_io_set_file_io(pio, stderr);
52 idmef_message_print(idmef, pio);
53 prelude_io_destroy(pio);
59 add_idmef_object(idmef_message_t *msg, const char *object, const char *value)
65 /* Can value be null? better check in here. */
71 ret = idmef_path_new_fast(&path, object);
77 ret = idmef_value_new_from_path(&val, path, value);
80 idmef_path_destroy(path);
84 ret = idmef_path_set(path, msg, val);
87 merror("%s: OSSEC2Prelude: IDMEF: Cannot add object '%s': %s.",
88 ARGV0, object, prelude_strerror(ret));
91 idmef_value_destroy(val);
92 idmef_path_destroy(path);
99 setup_analyzer(idmef_analyzer_t *analyzer)
102 prelude_string_t *string;
104 ret = idmef_analyzer_new_model(analyzer, &string);
107 prelude_string_set_constant(string, ANALYZER_MODEL);
109 ret = idmef_analyzer_new_class(analyzer, &string);
112 prelude_string_set_constant(string, ANALYZER_CLASS);
114 ret = idmef_analyzer_new_manufacturer(analyzer, &string);
117 prelude_string_set_constant(string, ANALYZER_MANUFACTURER);
119 ret = idmef_analyzer_new_version(analyzer, &string);
122 prelude_string_set_constant(string, ANALYZER_VERSION);
128 merror("%s: OSSEC2Prelude: %s: IDMEF error: %s.",
129 ARGV0, prelude_strsource(ret), prelude_strerror(ret));
136 void prelude_start(char *profile, int argc, char **argv)
139 prelude_client = NULL;
142 ret = prelude_init(&argc, argv);
145 merror("%s: %s: Unable to initialize the Prelude library: %s.",
146 ARGV0, prelude_strsource(ret), prelude_strerror(ret));
150 ret = prelude_client_new(&prelude_client,
151 profile!=NULL?profile:DEFAULT_ANALYZER_NAME);
154 merror("%s: %s: Unable to create a prelude client object: %s.",
155 ARGV0, prelude_strsource(ret), prelude_strerror(ret));
161 ret = setup_analyzer(prelude_client_get_analyzer(prelude_client));
164 merror("%s: %s: Unable to setup analyzer: %s",
165 ARGV0, prelude_strsource(ret), prelude_strerror(ret));
167 prelude_client_destroy(prelude_client,
168 PRELUDE_CLIENT_EXIT_STATUS_FAILURE);
174 ret = prelude_client_set_flags(prelude_client,
175 prelude_client_get_flags(prelude_client)
176 | PRELUDE_CLIENT_FLAGS_ASYNC_TIMER);
179 merror("%s: %s: Unable to set prelude client flags: %s.",
180 ARGV0, prelude_strsource(ret), prelude_strerror(ret));
184 /* Setting uid and gid of ossec. */
185 prelude_client_profile_set_uid(prelude_client_get_profile(prelude_client),
186 Privsep_GetUser(USER));
187 prelude_client_profile_set_gid(prelude_client_get_profile(prelude_client),
188 Privsep_GetGroup(GROUPGLOBAL));
191 ret = prelude_client_start(prelude_client);
194 merror("%s: %s: Unable to initialize prelude client: %s.",
195 ARGV0, prelude_strsource(ret), prelude_strerror(ret));
197 prelude_client_destroy(prelude_client,
198 PRELUDE_CLIENT_EXIT_STATUS_FAILURE);
210 void OS_PreludeLog(Eventinfo *lf)
213 idmef_message_t *idmef;
216 /* Generate prelude alert */
217 ret = idmef_message_new(&idmef);
220 merror("%s: OSSEC2Prelude: Cannot create IDMEF message", ARGV0);
225 add_idmef_object(idmef, "alert.assessment.impact.description",
226 lf->generated_rule->comment);
228 add_idmef_object(idmef, "alert.assessment.impact.severity",
229 (lf->generated_rule->level > 15) ? "high":
230 ossec2prelude_sev[lf->generated_rule->level]);
232 add_idmef_object(idmef, "alert.assessment.impact.completion", "succeeded");
234 add_idmef_object(idmef, "alert.classification.text",
235 lf->generated_rule->comment);
238 /* Setting source info. */
239 add_idmef_object(idmef, "alert.source(0).Spoofed", "no");
240 add_idmef_object(idmef, "alert.source(0).Node.Address(0).address",
242 add_idmef_object(idmef, "alert.source(0).Service.port", lf->srcport);
246 add_idmef_object(idmef, "alert.source(0).User.UserId(0).name", lf->srcuser);
251 add_idmef_object(idmef, "alert.target(0).Service.name", lf->program_name);
252 add_idmef_object(idmef, "alert.target(0).Spoofed", "no");
256 add_idmef_object(idmef, "alert.target(0).Node.Address(0).address",
262 char new_prelude_target[256];
264 new_prelude_target[255] = '\0';
265 strncpy(new_prelude_target, lf->hostname, 255);
267 /* The messages can have the file, so we need to remove it.
269 * enigma->/var/log/authlog
270 * (esqueleto2) 192.168.2.99->/var/log/squid/access.log
272 tmp_str = strstr(new_prelude_target, "->");
277 add_idmef_object(idmef, "alert.target(0).Node.Address(0).address",
280 add_idmef_object(idmef, "alert.target(0).Service.name", lf->hostname);
281 add_idmef_object(idmef, "alert.target(0).Service.port", lf->dstport);
285 add_idmef_object(idmef, "alert.target(0).User.category", "2");
286 add_idmef_object(idmef, "alert.target(0).User.UserId(0).name", lf->dstuser);
290 /* Setting source file. */
291 add_idmef_object(idmef, "alert.additional_data(0).type", "string");
292 add_idmef_object(idmef, "alert.additional_data(0).meaning", "Source file");
293 add_idmef_object(idmef, "alert.additional_data(0).data", lf->location);
296 /* Setting full log. */
297 add_idmef_object(idmef, "alert.additional_data(1).type", "string");
298 add_idmef_object(idmef, "alert.additional_data(1).meaning", "Full Log");
299 add_idmef_object(idmef, "alert.additional_data(1).data", lf->full_log);
301 idmef_alert_set_analyzer(idmef_message_get_alert(idmef),
303 (prelude_client_get_analyzer(prelude_client)),
306 prelude_client_send_idmef(prelude_client, idmef);
307 idmef_message_destroy(idmef);