summaryrefslogtreecommitdiffstats
path: root/src/sh_log_parse_apache.c
blob: 06cdf4eff4cb1c4ae0abacc17c424c2c8a2c1894 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/**************************************
 **
 ** PARSER RULES
 **
 ** (a) must set record->host 
 **     (eventually to dummy value)
 **
 ** (b) must set record->prefix
 **     (itoa(status)) 
 **
 **
 **************************************/

/* for strptime */
#define _XOPEN_SOURCE 500

#include "config_xor.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>

#ifdef USE_LOGFILE_MONITOR

#undef  FIL__
#define FIL__  _("sh_log_parse_apache.c")

/* Debian/Ubuntu: libpcre3-dev */
#ifdef HAVE_PCRE_PCRE_H
#include <pcre/pcre.h>
#else
#include <pcre.h>
#endif

#include "samhain.h"
#include "sh_pthread.h"
#include "sh_log_check.h"
#include "sh_utils.h"
#include "sh_string.h"

extern int flag_err_debug;

struct sh_fileinfo_apache {
  pcre * line_regex;
  int  * line_ovector;         /* captured substrings     */
  int    line_ovecnum;         /* how many captured       */
  
  int    pos_host;
  int    pos_status;
  int    pos_time;
  char * format_time;
};

static const char lf_error0[]    = N_("%error");
static const char lf_common0[]   = N_("%h %l %u %t \"%r\" %>s %b");
static const char lf_combined0[] = N_("%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"");

/* This variable is not used anywhere. It only exist
 * to assign &new to them, which keeps gcc from
 * putting it into a register, and avoids the 'clobbered
 * by longjmp' warning. And no, 'volatile' proved insufficient.
 */
void * sh_dummy_65_new = NULL;
void * sh_dummy_66_fti = NULL;
void * sh_dummy_67_ftr = NULL;

void * sh_eval_fileinfo_apache(char * str)
{
  struct sh_fileinfo_apache * result = NULL;
  unsigned int i, quotes;
  unsigned int nfields = 64;
  size_t       lengths[64];
  char *       new = NULL;
  char **      splits;
  char *       token;
  sh_string  * re_string;
  char *       p;
  volatile int          p_host = -1;
  volatile int          p_status = -1;
  volatile int          p_time = -1;
  char                * f_time = NULL;
  const char * error;
  int          erroffset;
  
  /* Take the address to keep gcc from putting them into registers. 
   * Avoids the 'clobbered by longjmp' warning. 
   */
  sh_dummy_65_new = (void*) &new;
  sh_dummy_66_fti = (void*) &f_time;
  sh_dummy_67_ftr = (void*) &result;

  if (0 == strncmp("common", str, 6))
    {
      new    = sh_util_strdup(_(lf_common0));
    }
  else if (0 == strncmp("combined", str, 8))
    {
      new    = sh_util_strdup(_(lf_combined0));
    }
  else if (0 == strncmp("error", str, 8))
    {
      new    = sh_util_strdup(_(lf_error0));
    }
  else
    {
      new    = sh_util_strdup(str);
    }

  if (flag_err_debug == S_TRUE)
    {
      SH_MUTEX_LOCK(mutex_thread_nolog);
      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
		      new,
		      _("eval_fileinfo"));
      SH_MUTEX_UNLOCK(mutex_thread_nolog);
    }

  splits = split_array_ws(new, &nfields, lengths);

  if (nfields < 1)
    {
      SH_FREE(splits);
      SH_FREE(new);
      return NULL;
    }

  /* Build the regex string re_string
   */
  re_string =  sh_string_new(0);
  sh_string_add_from_char(re_string, "^");

  for (i = 0; i < nfields; ++i)
    {

      if (i > 0)
	sh_string_add_from_char(re_string, " ");

      if (splits[i][0] != '"')
	quotes = 0;
      else
	quotes = 1;

      if (quotes && lengths[i] > 1 && splits[i][lengths[i]-1] == '"')
	{
	  splits[i][lengths[i]-1] = '\0'; /* cut trailing quote */
	  token = &(splits[i][1]);
	} else {
	  token = splits[i];
	}

      if(quotes)
	{
	  if(strcmp(token, "%r") == 0 || 
	     strstr(token, _("{Referer}")) != NULL || 
             strstr(token, _("{User-Agent}")) != NULL ||
	     strstr(token, _("{X-Forwarded-For}")) != NULL )
	    {
	      /*
	      p = "\"([^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\"";
	      sh_string_add_from_char(re_string, p);
	      */
	      sh_string_add_from_char(re_string, "\"([^");
	      sh_string_add_from_char(re_string, "\"\\\\");
	      sh_string_add_from_char(re_string, "]*");
	      sh_string_add_from_char(re_string, "(?:");
	      sh_string_add_from_char(re_string, "\\\\.");
	      sh_string_add_from_char(re_string, "[^\"");
	      sh_string_add_from_char(re_string, "\\\\]*");
	      sh_string_add_from_char(re_string, ")*)\"");
	    }    
	  else
	    {
	      sh_string_add_from_char(re_string, "(");
	      sh_string_add_from_char(re_string, "\\S+");
	      sh_string_add_from_char(re_string, ")");
	    }
	}
      else if (token[0] == 'R' && token[1] == 'E' && token[2] == '{' && token[strlen(token)-1] == '}') 
	{
	  char * lb =  strchr(token, '{');
	  char * rb = strrchr(token, '}');

	  if (lb && rb)
	    {
	      ++lb; *rb = '\0';
	      sh_string_add_from_char(re_string, lb);
	    }
	}
      else if (token[0] == '%' && token[strlen(token)-1] == 't') 
	{
	  char * lb = strchr(token, '{');
	  char * rb = strchr(token, '}');

	  sh_string_add_from_char(re_string, "\\[");
	  sh_string_add_from_char(re_string, "([^");
	  sh_string_add_from_char(re_string, "(\\]");
	  sh_string_add_from_char(re_string, "]+)");
	  sh_string_add_from_char(re_string, "\\]");

	  p_time = i+1;
	  if (lb && rb)
	    {
	      ++lb; *rb = '\0';
	      f_time = sh_util_strdup(lb);
	    }
	  else
	    {
	      f_time = sh_util_strdup(_("%d/%b/%Y:%T"));
	    }
	}
      else if (token[0] == '%' && token[1] == 'e' && 0 == strcmp(token, _("%error"))) 
	{
	  sh_string_add_from_char(re_string, "\\[");
	  sh_string_add_from_char(re_string, "([^");
	  sh_string_add_from_char(re_string, "]");
	  sh_string_add_from_char(re_string, "]+)");
	  sh_string_add_from_char(re_string, "\\]");

	  p_time = i+1; f_time = sh_util_strdup(_("%a %b %d %T %Y")); ++i;
	  sh_string_add_from_char(re_string, " ");

	  sh_string_add_from_char(re_string, "\\[");
	  sh_string_add_from_char(re_string, "([^");
	  sh_string_add_from_char(re_string, "]");
	  sh_string_add_from_char(re_string, "]+)");
	  sh_string_add_from_char(re_string, "\\]");

	  p_status = i+1;
	  sh_string_add_from_char(re_string, " ");

	  p = "(.+)";
	  sh_string_add_from_char(re_string, p);

	  nfields = 3;

	  break;
	}
      else
	{
	  sh_string_add_from_char(re_string, "(");
	  sh_string_add_from_char(re_string, "\\S+");
	  sh_string_add_from_char(re_string, ")");
	  if (token[0] == '%' && token[strlen(token)-1] == 's')
	    p_status = i+1;
	  else if (token[0] == '%' && token[strlen(token)-1] == 'v')
	    p_host = i+1;
	}
    }
  sh_string_add_from_char(re_string, "$");

  if (flag_err_debug == S_TRUE)
    {
      SH_MUTEX_LOCK(mutex_thread_nolog);
      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
		      sh_string_str(re_string),
		      _("eval_fileinfo"));
      SH_MUTEX_UNLOCK(mutex_thread_nolog);
    }

  result = SH_ALLOC(sizeof(struct sh_fileinfo_apache));
  result->line_regex = pcre_compile(sh_string_str(re_string), 0, 
				    &error, &erroffset, NULL);
  if (!(result->line_regex))
    {
      sh_string * msg =  sh_string_new(0);
      sh_string_add_from_char(msg, _("Bad regex: "));
      sh_string_add_from_char(msg, sh_string_str(re_string));
      
      SH_MUTEX_LOCK(mutex_thread_nolog);
      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
		      sh_string_str(msg),
		      _("eval_fileinfo"));
      SH_MUTEX_UNLOCK(mutex_thread_nolog);

      SH_FREE(result);
      SH_FREE(splits);
      SH_FREE(new);   
      sh_string_destroy(&msg);
      sh_string_destroy(&re_string);

      return NULL;
    }
  sh_string_destroy(&re_string);

  result->line_ovector  = SH_ALLOC(sizeof(int) * (nfields+1) * 3);
  result->line_ovecnum  = nfields;
  result->pos_host      = p_host;
  result->pos_status    = p_status;
  result->pos_time      = p_time;
  result->format_time   = f_time;

  SH_FREE(splits);
  SH_FREE(new);
  return (void*)result;
}

struct sh_logrecord * sh_parse_apache (sh_string * logline, void * fileinfo)
{
  static struct tm old_tm;
  static time_t    old_time;

  char         tstr[128];
  char         sstr[128];
  const char * hstr;
  int          res;
  const char **hstr_addr = (const char **) &hstr;

  struct sh_fileinfo_apache * info = (struct sh_fileinfo_apache *) fileinfo;

  if (sh_string_len(logline) > 0 && flag_err_debug == S_TRUE)
    {
      SH_MUTEX_LOCK(mutex_thread_nolog);
      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
		      sh_string_str(logline),
		      _("sh_parse_apache"));
      SH_MUTEX_UNLOCK(mutex_thread_nolog);
    }

  if (logline == NULL || info == NULL)
    {
      return NULL;
    }

  res = pcre_exec(info->line_regex, NULL, 
		  sh_string_str(logline), (int)sh_string_len(logline), 0,
		  0, info->line_ovector, (3*(1+info->line_ovecnum)));

  if (res == (1+info->line_ovecnum))
    {
      struct sh_logrecord * record;
      time_t timestamp = 0;

      if (info->pos_time > 0)
	{
	  res = pcre_copy_substring(sh_string_str(logline), 
				    info->line_ovector, res,
				    info->pos_time, tstr, sizeof(tstr));
	  if (res <= 0)
	    goto corrupt;
	}
      else
	{
	  res = 0;
	  timestamp = 0;
	  info->format_time = sh_util_strdup(_("%d/%b/%Y:%T"));
	  sl_strlcpy(tstr, _("01/Jan/1970:00:00:00"), sizeof(tstr));
	}

      if (res > 0)
	{
	  struct tm btime;
	  char * ptr = NULL;

	  memset(&btime, '\0', sizeof(struct tm));
	  btime.tm_isdst = -1;
	  
	  /* example: 01/Jun/2008:07:55:28 +0200 */

	  ptr = /*@i@*/strptime(tstr, info->format_time, &btime);

	  if (ptr)
	    {
	      timestamp = conv_timestamp(&btime, &old_tm, &old_time);
	    }
	  else
	    goto corrupt;
	}

      if (info->pos_status > 0)
	{
	  res = pcre_copy_substring(sh_string_str(logline), 
				    info->line_ovector, res,
				    info->pos_status, sstr, sizeof(sstr));
	  if (res <= 0)
	    goto corrupt;
	}
      else
	{
	  sl_strlcpy(sstr, _("000"), sizeof(sstr));
	}

      if (info->pos_host > 0)
	{
	  res = pcre_get_substring(sh_string_str(logline), 
				   info->line_ovector, res,
				   info->pos_host, hstr_addr);
	  if (res <= 0)
	    goto corrupt;
	}
      else
	{
	  hstr = NULL;
	}

      record = SH_ALLOC(sizeof(struct sh_logrecord));
      
      record->timestamp = timestamp;
      record->timestr   = sh_string_new_from_lchar(tstr, strlen(tstr));

      if (hstr)
	record->host = sh_string_new_from_lchar(hstr, strlen(hstr));
      else
	record->host = sh_string_new_from_lchar(sh.host.name, strlen(sh.host.name));

      record->message   = sh_string_new_from_lchar(sh_string_str(logline), 
						   sh_string_len(logline));
      record->pid       = PID_INVALID;

      pcre_free_substring(hstr); 
      return record;
    }
  else
    {
      char msg[128];
      sl_snprintf(msg, sizeof(msg), _("Incorrect number of captured subexpressions: %d vs %d"),
		  res, info->line_ovecnum);
      
      SH_MUTEX_LOCK(mutex_thread_nolog);
      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
		      msg,
		      _("sh_parse_apache"));
      SH_MUTEX_UNLOCK(mutex_thread_nolog);
    }

  /* Corrupted logline */
 corrupt:

  {
    sh_string * msg =  sh_string_new(0);
    sh_string_add_from_char(msg, _("Corrupt logline: "));
    sh_string_add_from_char(msg, sh_string_str(logline));
    
    SH_MUTEX_LOCK(mutex_thread_nolog);
    sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
		    sh_string_str(msg),
		    _("sh_parse_apache"));
    SH_MUTEX_UNLOCK(mutex_thread_nolog);
    sh_string_destroy(&msg);
  }
  return NULL;
}

/* USE_LOGFILE_MONITOR */
#endif