1 diff -Nur linux-2.6.1-rc2.org/include/linux/netfilter_ipv4/ipt_psd.h linux-2.6.1-rc2/include/linux/netfilter_ipv4/ipt_psd.h
2 --- linux-2.6.1-rc2.org/include/linux/netfilter_ipv4/ipt_psd.h 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.1-rc2/include/linux/netfilter_ipv4/ipt_psd.h 2004-01-07 18:17:50.030389768 +0100
8 +#include <linux/param.h>
9 +#include <linux/types.h>
12 + * High port numbers have a lower weight to reduce the frequency of false
13 + * positives, such as from passive mode FTP transfers.
15 +#define PORT_WEIGHT_PRIV 3
16 +#define PORT_WEIGHT_HIGH 1
19 + * Port scan detection thresholds: at least COUNT ports need to be scanned
20 + * from the same source, with no longer than DELAY ticks between ports.
22 +#define SCAN_MIN_COUNT 7
23 +#define SCAN_MAX_COUNT (SCAN_MIN_COUNT * PORT_WEIGHT_PRIV)
24 +#define SCAN_WEIGHT_THRESHOLD SCAN_MAX_COUNT
25 +#define SCAN_DELAY_THRESHOLD (HZ * 3)
28 + * Keep track of up to LIST_SIZE source addresses, using a hash table of
29 + * HASH_SIZE entries for faster lookups, but limiting hash collisions to
30 + * HASH_MAX source addresses per the same hash value.
32 +#define LIST_SIZE 0x100
34 +#define HASH_SIZE (1 << HASH_LOG)
35 +#define HASH_MAX 0x10
37 +struct ipt_psd_info {
38 + unsigned int weight_threshold;
39 + unsigned int delay_threshold;
40 + unsigned short lo_ports_weight;
41 + unsigned short hi_ports_weight;
44 +#endif /*_IPT_PSD_H*/
45 diff -Nur linux-2.6.1-rc2.org/net/ipv4/netfilter/ipt_psd.c linux-2.6.1-rc2/net/ipv4/netfilter/ipt_psd.c
46 --- linux-2.6.1-rc2.org/net/ipv4/netfilter/ipt_psd.c 1970-01-01 01:00:00.000000000 +0100
47 +++ linux-2.6.1-rc2/net/ipv4/netfilter/ipt_psd.c 2004-01-07 18:17:50.031389616 +0100
50 + This is a module which is used for PSD (portscan detection)
51 + Derived from scanlogd v2.1 written by Solar Designer <solar@false.com>
52 + and LOG target module.
54 + Copyright (C) 2000,2001 astaro AG
56 + This file is distributed under the terms of the GNU General Public
57 + License (GPL). Copies of the GPL can be obtained from:
58 + ftp://prep.ai.mit.edu/pub/gnu/GPL
60 + 2000-05-04 Markus Hennig <hennig@astaro.de> : initial
61 + 2000-08-18 Dennis Koslowski <koslowski@astaro.de> : first release
62 + 2000-12-01 Dennis Koslowski <koslowski@astaro.de> : UDP scans detection added
63 + 2001-01-02 Dennis Koslowski <koslowski@astaro.de> : output modified
64 + 2001-02-04 Jan Rekorajski <baggins@pld.org.pl> : converted from target to match
67 +#include <linux/module.h>
68 +#include <linux/skbuff.h>
69 +#include <linux/ip.h>
71 +#include <linux/spinlock.h>
72 +#include <linux/netfilter_ipv4/ip_tables.h>
73 +#include <linux/netfilter_ipv4/ipt_psd.h>
76 +#define DEBUGP printk
78 +#define DEBUGP(format, args...)
81 +MODULE_LICENSE("GPL");
82 +MODULE_AUTHOR("Dennis Koslowski <koslowski@astaro.com>");
84 +#define HF_DADDR_CHANGING 0x01
85 +#define HF_SPORT_CHANGING 0x02
86 +#define HF_TOS_CHANGING 0x04
87 +#define HF_TTL_CHANGING 0x08
90 + * Information we keep per each target port
93 + u_int16_t number; /* port number */
94 + u_int8_t proto; /* protocol number */
95 + u_int8_t and_flags; /* tcp ANDed flags */
96 + u_int8_t or_flags; /* tcp ORed flags */
100 + * Information we keep per each source address.
103 + struct host *next; /* Next entry with the same hash */
104 + clock_t timestamp; /* Last update time */
105 + struct in_addr src_addr; /* Source address */
106 + struct in_addr dest_addr; /* Destination address */
107 + unsigned short src_port; /* Source port */
108 + int count; /* Number of ports in the list */
109 + int weight; /* Total weight of ports in the list */
110 + struct port ports[SCAN_MAX_COUNT - 1]; /* List of ports */
111 + unsigned char tos; /* TOS */
112 + unsigned char ttl; /* TTL */
113 + unsigned char flags; /* HF_ flags bitmask */
117 + * State information.
121 + struct host list[LIST_SIZE]; /* List of source addresses */
122 + struct host *hash[HASH_SIZE]; /* Hash: pointers into the list */
123 + int index; /* Oldest entry to be replaced */
127 + * Convert an IP address into a hash table index.
129 +static inline int hashfunc(struct in_addr addr)
131 + unsigned int value;
134 + value = addr.s_addr;
138 + } while ((value >>= HASH_LOG));
140 + return hash & (HASH_SIZE - 1);
144 +ipt_psd_match(const struct sk_buff *pskb,
145 + const struct net_device *in,
146 + const struct net_device *out,
147 + const void *matchinfo,
153 + struct iphdr *ip_hdr;
154 + struct tcphdr *tcp_hdr;
155 + struct in_addr addr;
156 + u_int16_t src_port,dest_port;
157 + u_int8_t tcp_flags, proto;
159 + struct host *curr, *last, **head;
160 + int hash, index, count;
162 + /* Parameters from userspace */
163 + const struct ipt_psd_info *psdinfo = matchinfo;
166 + ip_hdr = pskb->nh.iph;
169 + if (ntohs(ip_hdr->frag_off) & IP_OFFSET) {
170 + DEBUGP("PSD: sanity check failed\n");
175 + proto = ip_hdr->protocol;
177 + if (proto != IPPROTO_TCP && proto != IPPROTO_UDP) {
178 + DEBUGP("PSD: protocol not supported\n");
182 + /* Get the source address, source & destination ports, and TCP flags */
184 + addr.s_addr = ip_hdr->saddr;
186 + tcp_hdr = (struct tcphdr*)((u_int32_t *)ip_hdr + ip_hdr->ihl);
188 + /* Yep, it´s dirty */
189 + src_port = tcp_hdr->source;
190 + dest_port = tcp_hdr->dest;
192 + if (proto == IPPROTO_TCP) {
193 + tcp_flags = *((u_int8_t*)tcp_hdr + 13);
199 + /* We're using IP address 0.0.0.0 for a special purpose here, so don't let
200 + * them spoof us. [DHCP needs this feature - HW] */
201 + if (!addr.s_addr) {
202 + DEBUGP("PSD: spoofed source address (0.0.0.0)\n");
206 + /* Use jiffies here not to depend on someone setting the time while we're
207 + * running; we need to be careful with possible return value overflows. */
210 + spin_lock(&state.lock);
212 + /* Do we know this source address already? */
215 + if ((curr = *(head = &state.hash[hash = hashfunc(addr)])))
217 + if (curr->src_addr.s_addr == addr.s_addr) break;
219 + if (curr->next) last = curr;
220 + } while ((curr = curr->next));
224 + /* We know this address, and the entry isn't too old. Update it. */
225 + if (now - curr->timestamp <= (psdinfo->delay_threshold*HZ)/100 &&
226 + time_after_eq(now, curr->timestamp)) {
228 + /* Just update the appropriate list entry if we've seen this port already */
229 + for (index = 0; index < curr->count; index++) {
230 + if (curr->ports[index].number == dest_port) {
231 + curr->ports[index].proto = proto;
232 + curr->ports[index].and_flags &= tcp_flags;
233 + curr->ports[index].or_flags |= tcp_flags;
238 + /* TCP/ACK and/or TCP/RST to a new port? This could be an outgoing connection. */
239 + if (proto == IPPROTO_TCP && (tcp_hdr->ack || tcp_hdr->rst))
242 + /* Packet to a new port, and not TCP/ACK: update the timestamp */
243 + curr->timestamp = now;
245 + /* Logged this scan already? Then drop the packet. */
246 + if (curr->weight >= psdinfo->weight_threshold)
249 + /* Specify if destination address, source port, TOS or TTL are not fixed */
250 + if (curr->dest_addr.s_addr != ip_hdr->daddr)
251 + curr->flags |= HF_DADDR_CHANGING;
252 + if (curr->src_port != src_port)
253 + curr->flags |= HF_SPORT_CHANGING;
254 + if (curr->tos != ip_hdr->tos)
255 + curr->flags |= HF_TOS_CHANGING;
256 + if (curr->ttl != ip_hdr->ttl)
257 + curr->flags |= HF_TTL_CHANGING;
259 + /* Update the total weight */
260 + curr->weight += (ntohs(dest_port) < 1024) ?
261 + psdinfo->lo_ports_weight : psdinfo->hi_ports_weight;
263 + /* Got enough destination ports to decide that this is a scan? */
264 + /* Then log it and drop the packet. */
265 + if (curr->weight >= psdinfo->weight_threshold)
268 + /* Remember the new port */
269 + if (curr->count < SCAN_MAX_COUNT) {
270 + curr->ports[curr->count].number = dest_port;
271 + curr->ports[curr->count].proto = proto;
272 + curr->ports[curr->count].and_flags = tcp_flags;
273 + curr->ports[curr->count].or_flags = tcp_flags;
280 + /* We know this address, but the entry is outdated. Mark it unused, and
281 + * remove from the hash table. We'll allocate a new entry instead since
282 + * this one might get re-used too soon. */
283 + curr->src_addr.s_addr = 0;
285 + last->next = last->next->next;
287 + *head = (*head)->next;
291 + /* We don't need an ACK from a new source address */
292 + if (proto == IPPROTO_TCP && tcp_hdr->ack)
295 + /* Got too many source addresses with the same hash value? Then remove the
296 + * oldest one from the hash table, so that they can't take too much of our
297 + * CPU time even with carefully chosen spoofed IP addresses. */
298 + if (count >= HASH_MAX && last) last->next = NULL;
300 + /* We're going to re-use the oldest list entry, so remove it from the hash
301 + * table first (if it is really already in use, and isn't removed from the
302 + * hash table already because of the HASH_MAX check above). */
304 + /* First, find it */
305 + if (state.list[state.index].src_addr.s_addr)
306 + head = &state.hash[hashfunc(state.list[state.index].src_addr)];
310 + if ((curr = *head))
312 + if (curr == &state.list[state.index]) break;
314 + } while ((curr = curr->next));
316 + /* Then, remove it */
319 + last->next = last->next->next;
321 + *head = (*head)->next;
324 + /* Get our list entry */
325 + curr = &state.list[state.index++];
326 + if (state.index >= LIST_SIZE) state.index = 0;
328 + /* Link it into the hash table */
329 + head = &state.hash[hash];
330 + curr->next = *head;
333 + /* And fill in the fields */
334 + curr->timestamp = now;
335 + curr->src_addr = addr;
336 + curr->dest_addr.s_addr = ip_hdr->daddr;
337 + curr->src_port = src_port;
339 + curr->weight = (ntohs(dest_port) < 1024) ?
340 + psdinfo->lo_ports_weight : psdinfo->hi_ports_weight;
341 + curr->ports[0].number = dest_port;
342 + curr->ports[0].proto = proto;
343 + curr->ports[0].and_flags = tcp_flags;
344 + curr->ports[0].or_flags = tcp_flags;
345 + curr->tos = ip_hdr->tos;
346 + curr->ttl = ip_hdr->ttl;
349 + spin_unlock(&state.lock);
353 + spin_unlock(&state.lock);
357 +static int ipt_psd_checkentry(const char *tablename,
358 + const struct ipt_ip *e,
360 + unsigned int matchsize,
361 + unsigned int hook_mask)
363 +/* const struct ipt_psd_info *psdinfo = targinfo;*/
365 + /* we accept TCP only */
366 +/* if (e->ip.proto != IPPROTO_TCP) { */
367 +/* DEBUGP("PSD: specified protocol may be TCP only\n"); */
371 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_psd_info))) {
372 + DEBUGP("PSD: matchsize %u != %u\n",
374 + IPT_ALIGN(sizeof(struct ipt_psd_info)));
381 +static struct ipt_match ipt_psd_reg = {
385 + ipt_psd_checkentry,
389 +static int __init init(void)
391 + if (ipt_register_match(&ipt_psd_reg))
394 + memset(&state, 0, sizeof(state));
396 + spin_lock_init(&(state.lock));
398 + printk("netfilter PSD loaded - (c) astaro AG\n");
402 +static void __exit fini(void)
404 + ipt_unregister_match(&ipt_psd_reg);
405 + printk("netfilter PSD unloaded - (c) astaro AG\n");
410 diff -Nur linux-2.6.1-rc2.org/net/ipv4/netfilter/Kconfig linux-2.6.1-rc2/net/ipv4/netfilter/Kconfig
411 --- linux-2.6.1-rc2.org/net/ipv4/netfilter/Kconfig 2004-01-07 18:05:30.207859888 +0100
412 +++ linux-2.6.1-rc2/net/ipv4/netfilter/Kconfig 2004-01-07 18:17:50.034389160 +0100
415 Fingerprints can be downloaded from http://www.openbsd.org/cgi-bin/cvsweb/src/etc/pf.os
417 +config IP_NF_MATCH_PSD
418 + tristate 'psd match support'
419 + depends on IP_NF_IPTABLES
421 + This option adds a `psd' match, which supplies portscan
422 + detection match (psd). This match will attempt to detect TCP and UDP
423 + port scans. This match was derived from Solar Designer's scanlogd.
425 + Suppported options are:
427 + --psd-weight-threshold <threshold>
429 + Total weight of the latest TCP/UDP packets with different
430 + destination ports coming from the same host to be treated as port
433 + --psd-delay-threshold <delay>
435 + Delay (in hundredths of second) for the packets with different
436 + destination ports coming from the same host to be treated as
437 + possible port scan subsequence.
439 + --psd-lo-ports-weight <weight>
441 + Weight of the packet with privileged (<=1024) destination port.
443 + --psd-hi-ports-weight <weight>
445 + Weight of the packet with non-priviliged destination port.
449 diff -Nur linux-2.6.1-rc2.org/net/ipv4/netfilter/Makefile linux-2.6.1-rc2/net/ipv4/netfilter/Makefile
450 --- linux-2.6.1-rc2.org/net/ipv4/netfilter/Makefile 2004-01-07 18:05:30.207859888 +0100
451 +++ linux-2.6.1-rc2/net/ipv4/netfilter/Makefile 2004-01-07 18:17:50.035389008 +0100
453 obj-$(CONFIG_IP_NF_MATCH_OWNER) += ipt_owner.o
454 obj-$(CONFIG_IP_NF_MATCH_TOS) += ipt_tos.o
456 +obj-$(CONFIG_IP_NF_MATCH_PSD) += ipt_psd.o
458 obj-$(CONFIG_IP_NF_MATCH_OSF) += ipt_osf.o