Imported Upstream version 2.7
[ossec-hids.git] / src / analysisd / decoders / plugins / pf_decoder.c
1 /* @(#) $Id: ./src/analysisd/decoders/plugins/pf_decoder.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All rights reserved.
6  *
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
10  * Foundation.
11  *
12  * License details at the LICENSE file included with OSSEC or
13  * online at: http://www.ossec.net/en/licensing.html
14  */
15
16
17 #include "shared.h"
18 #include "eventinfo.h"
19
20
21 /* OpenBSD PF decoder init */
22 void *PF_Decoder_Init()
23 {
24     debug1("%s: Initializing PF decoder..", ARGV0);
25
26     /* There is nothing to do over here */
27     return(NULL);
28 }
29
30
31 /* OpenBSD PF decoder
32  * Will extract the action,srcip,dstip,protocol,srcport,dstport
33  *
34  * Examples:
35  * Mar 30 15:33:26 enigma pf: Mar 30 15:32:33.483712 rule 2/(match) pass in on xl0: 140.211.166.3.6667 > 192.168.2.10.16290: P 7408:7677(269) ack 1773 win 2520 <nop,nop,timestamp 3960674784 2860123562> (DF)
36  * Mar 30 15:47:05.522341 rule 4/(match) block in on lo0: 127.0.0.1.48784 > 127.0.0.1.23: S 1381529123:1381529123(0) win 16384 <mss 33184,nop,nop,sackOK,nop,wscale 0,[|tcp]> (DF) [tos 0x10]
37  * Mar 30 15:54:22.171929 rule 3/(match) pass out on xl0: 192.168.2.10.1514 > 192.168.2.190.1030:  udp 73
38  * Mar 30 15:54:22.174412 rule 3/(match) pass out on xl0: 192.168.2.10.1514 > 192.168.2.190.1030:  udp 89
39  * Mar 30 17:47:40.390143 rule 2/(match) pass in on lo0: 127.0.0.1 > 127.0.0.1: icmp: echo reply
40  * Mar 30 17:47:41.400075 rule 3/(match) pass out on lo0: 127.0.0.1 > 127.0.0.1: icmp: echo request
41  */
42 void *PF_Decoder_Exec(Eventinfo *lf)
43 {
44     int port_count = 0;
45     char *tmp_str;
46     char *aux_str;
47
48
49     /* tmp_str should be: Mar 30 15:54:22.171929 rule 3/(match) pass out .. */
50     tmp_str = strchr(lf->log, ')');
51
52
53     /* Didn't match */
54     if(!tmp_str)
55     {
56         return(NULL);
57     }
58
59     /* Going to the action entry */
60     tmp_str++;
61     if(*tmp_str != ' ')
62     {
63         return(NULL);
64     }
65     tmp_str++;
66
67
68     /* tmp_str should be: pass out on xl0: 192.168.2.10.1514 .. */
69
70
71     /* Getting action */
72     if(*tmp_str == 'p')
73     {
74         os_strdup("pass", lf->action);
75     }
76     else if(*tmp_str == 'b')
77     {
78         os_strdup("block", lf->action);
79     }
80     else
81     {
82         /* Unknown action */
83         return(NULL);
84     }
85
86
87     /* Jumping to the src ip */
88     tmp_str = strchr(tmp_str, ':');
89     if(!tmp_str)
90     {
91         return(NULL);
92     }
93     tmp_str++;
94     if(*tmp_str != ' ')
95     {
96         return(NULL);
97     }
98     tmp_str++;
99
100
101
102     /* tmp_str should be: 192.168.2.10.1514 > .. */
103     aux_str = strchr(tmp_str, ' ');
104     if(!aux_str)
105         return(NULL);
106
107
108     /* Setting aux_str to 0 for strdup */
109     *aux_str = '\0';
110
111     os_strdup(tmp_str, lf->srcip);
112
113     /* Aux str has a valid pointer to lf->log now */
114     *aux_str = ' ';
115     aux_str++;
116
117
118
119     /* Setting the source port if present */
120     tmp_str = lf->srcip;
121     while(*tmp_str != '\0')
122     {
123         if(*tmp_str == '.')
124             port_count++;
125
126
127         /* Found port */
128         if(port_count == 4)
129         {
130             *tmp_str = '\0';
131             tmp_str++;
132             os_strdup(tmp_str, lf->srcport);
133             break;
134         }
135
136         tmp_str++;
137     }
138
139
140     /* Invalid rest of log */
141     if(*aux_str != '>')
142         return(NULL);
143
144
145     aux_str++;
146     if(*aux_str != ' ')
147         return(NULL);
148     aux_str++;
149
150
151     /* tmp_str should be: 192.168.2.10.1514: .. .. */
152     tmp_str = strchr(aux_str, ':');
153     if(!tmp_str)
154         return(NULL);
155
156
157     /* Setting aux_str to 0 for strdup */
158     *tmp_str = '\0';
159
160     os_strdup(aux_str, lf->dstip);
161
162
163     /* tmp str has a valid pointer to lf->log now */
164     *tmp_str = ':';
165     tmp_str++;
166
167
168     /* Getting destination port */
169     aux_str = lf->dstip;
170     port_count = 0;
171     while(*aux_str != '\0')
172     {
173         if(*aux_str == '.')
174             port_count++;
175
176
177         /* Found port */
178         if(port_count == 4)
179         {
180             *aux_str = '\0';
181             aux_str++;
182             os_strdup(aux_str, lf->dstport);
183             break;
184         }
185
186         aux_str++;
187     }
188
189
190     /* Getting protocol */
191     while(*tmp_str != '\0')
192     {
193         if(*tmp_str == ' ')
194         {
195             tmp_str++;
196             continue;
197         }
198         else if(*tmp_str == 'u')
199         {
200             os_strdup("UDP", lf->protocol);
201         }
202         else if(*tmp_str == 'i')
203         {
204             os_strdup("ICMP", lf->protocol);
205         }
206         else
207         {
208             os_strdup("TCP", lf->protocol);
209         }
210
211         break;
212     }
213
214     return(NULL);
215 }
216
217 /* END Decoder */