new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / analysisd / decoders / plugins / pf_decoder.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All rights reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation.
8  */
9
10 #include "../plugin_decoders.h"
11
12 #include "shared.h"
13 #include "eventinfo.h"
14
15
16 /* OpenBSD PF decoder init */
17 void *PF_Decoder_Init()
18 {
19     debug1("%s: Initializing PF decoder..", ARGV0);
20
21     /* There is nothing to do over here */
22     return (NULL);
23 }
24
25 /* OpenBSD PF decoder
26  * Will extract the action,srcip,dstip,protocol,srcport,dstport
27  *
28  * Examples:
29  * 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)
30  * 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]
31  * 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
32  * 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
33  * Mar 30 17:47:40.390143 rule 2/(match) pass in on lo0: 127.0.0.1 > 127.0.0.1: icmp: echo reply
34  * Mar 30 17:47:41.400075 rule 3/(match) pass out on lo0: 127.0.0.1 > 127.0.0.1: icmp: echo request
35  */
36 void *PF_Decoder_Exec(Eventinfo *lf)
37 {
38     int port_count = 0;
39     char *tmp_str;
40     char *aux_str;
41
42     /* tmp_str should be: Mar 30 15:54:22.171929 rule 3/(match) pass out .. */
43     tmp_str = strchr(lf->log, ')');
44
45     /* Didn't match */
46     if (!tmp_str) {
47         return (NULL);
48     }
49
50     /* Go to the action entry */
51     tmp_str++;
52     if (*tmp_str != ' ') {
53         return (NULL);
54     }
55     tmp_str++;
56
57     /* tmp_str should be: pass out on xl0: 192.168.2.10.1514 .. */
58
59     /* Get action */
60     if (*tmp_str == 'p') {
61         os_strdup("pass", lf->action);
62     } else if (*tmp_str == 'b') {
63         os_strdup("block", lf->action);
64     } else {
65         /* Unknown action */
66         return (NULL);
67     }
68
69     /* Jump to the src ip */
70     tmp_str = strchr(tmp_str, ':');
71     if (!tmp_str) {
72         return (NULL);
73     }
74     tmp_str++;
75     if (*tmp_str != ' ') {
76         return (NULL);
77     }
78     tmp_str++;
79
80     /* tmp_str should be: 192.168.2.10.1514 > .. */
81     aux_str = strchr(tmp_str, ' ');
82     if (!aux_str) {
83         return (NULL);
84     }
85
86     /* Set aux_str to 0 for strdup */
87     *aux_str = '\0';
88
89     os_strdup(tmp_str, lf->srcip);
90
91     /* Aux str has a valid pointer to lf->log now */
92     *aux_str = ' ';
93     aux_str++;
94
95     /* Set the source port if present */
96     tmp_str = lf->srcip;
97     while (*tmp_str != '\0') {
98         if (*tmp_str == '.') {
99             port_count++;
100         }
101
102         /* Found port */
103         if (port_count == 4) {
104             *tmp_str = '\0';
105             tmp_str++;
106             os_strdup(tmp_str, lf->srcport);
107             break;
108         }
109
110         tmp_str++;
111     }
112
113     /* Invalid rest of log */
114     if (*aux_str != '>') {
115         return (NULL);
116     }
117
118     aux_str++;
119     if (*aux_str != ' ') {
120         return (NULL);
121     }
122     aux_str++;
123
124     /* tmp_str should be: 192.168.2.10.1514: .. .. */
125     tmp_str = strchr(aux_str, ':');
126     if (!tmp_str) {
127         return (NULL);
128     }
129
130     /* Set aux_str to 0 for strdup */
131     *tmp_str = '\0';
132
133     os_strdup(aux_str, lf->dstip);
134
135     /* tmp str has a valid pointer to lf->log now */
136     *tmp_str = ':';
137     tmp_str++;
138
139     /* Get destination port */
140     aux_str = lf->dstip;
141     port_count = 0;
142     while (*aux_str != '\0') {
143         if (*aux_str == '.') {
144             port_count++;
145         }
146
147         /* Found port */
148         if (port_count == 4) {
149             *aux_str = '\0';
150             aux_str++;
151             os_strdup(aux_str, lf->dstport);
152             break;
153         }
154
155         aux_str++;
156     }
157
158     /* Get protocol */
159     while (*tmp_str != '\0') {
160         if (*tmp_str == ' ') {
161             tmp_str++;
162             continue;
163         } else if (*tmp_str == 'u') {
164             os_strdup("UDP", lf->protocol);
165         } else if (*tmp_str == 'i') {
166             os_strdup("ICMP", lf->protocol);
167         } else {
168             os_strdup("TCP", lf->protocol);
169         }
170
171         break;
172     }
173
174     return (NULL);
175 }
176