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