summaryrefslogtreecommitdiffstats
path: root/filter/ulogd_filter_PWSNIFF.c
blob: 4812dbcc52df343cd85b882ab8f01d97d3ea60ba (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
/* ulogd_PWSNIFF.c, Version $Revision$
 *
 * ulogd logging interpreter for POP3 / FTP like plaintext passwords.
 *
 * (C) 2000-2003 by Harald Welte <laforge@gnumonks.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 
 *  as published by the Free Software Foundation
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "config.h"
#include <ulogd/ulogd.h>
#include <ulogd/common.h>
#include <ulogd/plugin.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <netinet/tcp.h>

#ifdef DEBUG_PWSNIFF
#define DEBUGP(x) ulogd_log(ULOGD_DEBUG, x)
#else
#define DEBUGP(format, args...)
#endif


#define PORT_POP3	110
#define PORT_FTP	21

static u_int16_t pwsniff_ports[] = {
	PORT_POP3,
	PORT_FTP,
	/* feel free to include any other ports here, provided that their
	 * user/password syntax is the same */
};

static unsigned char *_get_next_blank(unsigned char* begp, unsigned char *endp)
{
	unsigned char *ptr;

	for (ptr = begp; ptr < endp; ptr++) {
		if (*ptr == ' ' || *ptr == '\n' || *ptr == '\r')
			return ptr-1;	
	}
	return NULL;
}

static int interp_pwsniff(struct ulogd_pluginstance *pi, unsigned *flags)
{
	struct ulogd_key *ret = pi->output.keys;
	struct iphdr *iph;
	void *protoh;
	struct tcphdr *tcph;	
	unsigned int tcplen;
	unsigned char  *ptr, *begp, *pw_begp, *endp, *pw_endp;
	int len, pw_len, i, cont = 0;

	if (!key_src_valid(&pi->input.keys[0]))
		return 0;
	
	iph = (struct iphdr *) key_ptr(&pi->input.keys[0]);
	protoh = (u_int32_t *)iph + iph->ihl;
	tcph = protoh;
	tcplen = ntohs(iph->tot_len) - iph->ihl * 4;

	len = pw_len = 0;
	begp = pw_begp = NULL;

	if (iph->protocol != IPPROTO_TCP)
		return 0;
	
	for (i = 0; i < ARRAY_SIZE(pwsniff_ports); i++)
	{
		if (ntohs(tcph->dest) == pwsniff_ports[i]) {
			cont = 1; 
			break;
		}
	}
	if (!cont)
		return 0;

	DEBUGP("----> pwsniff detected, tcplen=%d, struct=%d, iphtotlen=%d, "
		"ihl=%d\n", tcplen, sizeof(struct tcphdr), ntohs(iph->tot_len),
		iph->ihl);

	for (ptr = (unsigned char *) tcph + sizeof(struct tcphdr); 
			ptr < (unsigned char *) tcph + tcplen; ptr++)
	{
		if (!strncasecmp((char *)ptr, "USER ", 5)) {
			begp = ptr+5;
			endp = _get_next_blank(begp, (unsigned char *)tcph + tcplen);
			if (endp)
				len = endp - begp + 1;
		}
		if (!strncasecmp((char *)ptr, "PASS ", 5)) {
			pw_begp = ptr+5;
			pw_endp = _get_next_blank(pw_begp, 
					(unsigned char *)tcph + tcplen);
			if (pw_endp)
				pw_len = pw_endp - pw_begp + 1;
		}
	}

	if (len) {
		key_set_ptr(&ret[0], malloc(len + 1));
		if (!key_ptr(&ret[0])) {
			ulogd_log(ULOGD_ERROR, "OOM (size=%u)\n", len);
			return 0;
		}
		strncpy((char *) key_ptr(&ret[0]), (char *)begp, len);
		*((char *)key_ptr(&ret[0]) + len + 1) = '\0';
	}
	if (pw_len) {
		key_set_ptr(&ret[1], malloc(pw_len + 1));
		if (!key_ptr(&ret[1])){
			ulogd_log(ULOGD_ERROR, "OOM (size=%u)\n", pw_len);
			return 0;
		}
		strncpy((char *)key_ptr(&ret[1]), (char *)pw_begp, pw_len);
		*(ret[1].u.val.str + pw_len + 1) = '\0';

	}
	return 0;
}

static struct ulogd_key pwsniff_inp[] = {
	{
		.name 	= "raw.pkt",
	},
};

static struct ulogd_key pwsniff_outp[] = {
	KEY_FLAGS(STRING, "pwsniff.user", ULOGD_RETF_FREE),
	KEY_FLAGS(STRING, "pwsniff.pass", ULOGD_RETF_FREE),
};

static struct ulogd_plugin pwsniff_plugin = {
	.name	= "PWSNIFF",
	.input	= {
		.keys = pwsniff_inp,
		.num_keys = ARRAY_SIZE(pwsniff_inp),
		.type = ULOGD_DTYPE_PACKET,
	},
	.output	= {
		.keys = pwsniff_outp,
		.num_keys = ARRAY_SIZE(pwsniff_outp),
		.type = ULOGD_DTYPE_PACKET,
	},
	.interp = &interp_pwsniff,
	.rev = ULOGD_PLUGIN_REVISION,
};

void __upi_ctor init(void)
{
	ulogd_register_plugin(&pwsniff_plugin);
}