Skip to contentSkip to conversation

OSF update: netlink logging

PATCH RESEND In review Evgeniy Polyakov 2 participants 3 messages Jan 31, 2004 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 OSF update: netlink logging

Hello, Harald.
Hello, all.

This patches add possibility to log OSF events through netlink. I've stolen NETLINK_NFLOG and mcast group 1 in OSF module from ULOG, so you can't use ipt_ULOG and ipt_osf at the same time(or you will catch some unusal events in ulogd/osfd).
Patchset also adds missing diffs for

Some "smart" hardware/software changes MSS while doing NAT/DSL, so make additional compare with "good" MSS value(predefined 1460). Kudos to Michal Zalewski and Mike Frantzen.

Whitespace cleanup.

Packets from outside(loopback is outside too) may have equal to fingerprint ttl, not necessarily lesser.

If we have timestamp option, do not compare it with one from fingerprint.
(For now it is always 0).

ip->ttl must be less than finger's ttl if fingerprint is valid.

and probably others.

Patchset is against latest pom-ng and userspace.
Please test and apply.

For testing i also attach trivial osfd.c.

Evgeniy Polyakov ( s0mbre )

Only failure makes us experts. -- Theo de Raadt

osf/help +14 -17
@@ -3,25 +3,15 @@
 Original idea was lurked in some OpenBSD mailing list (thanks
 grange@open...) and than adopted for Linux netfilter in form of this code.
 
-Original table was created by Michal Zalewski <[email protected]> for
-his excellent p0f and than changed a bit for more convenience.
+Original fingerprint table was created by Michal Zalewski <[email protected]>.
 
 This module compares some data(WS, MSS, options and it's order, ttl,
 df and others) from first SYN packet (actually from packets with SYN
-bit set) with hardcoded in fingers[] table ones.
+bit set) with dinamically loaded OS fingerprints.
 
-Example: (Of course this only an example, do not get inspired by this)
-
-# iptables -N LINUX
-# iptables -A LINUX -j LOG --log-prefix "Linux"
-
-# iptables -A INPUT -p tcp -m osf --genre Linux -j LINUX
-# iptables -A INPUT -p tcp -m osf --genre FreeBSD -j REJECT
-
-NOTE: -p tcp is obviously required as it is a TCP match.
 
 OSF also has:
---log 1/0. 
+  --log 1/0. 
 	If present, OSF will log determined genres even if they don't match
 	desired one.	
 	0 - log all determined entries, 
@@ -29,16 +19,23 @@
 	
 	Example:
 	#iptables -I INPUT -j ACCEPT -p tcp -m osf --genre Linux --log 1 --smart
+	NOTE: -p tcp is obviously required as it is a TCP match.
 
 	In syslog you find something like this:
-	ipt_osf: Windows [Windows XP Pro SP1, 2000 SP3]: 11.22.33.55:4024 -> 11.22.33.44:139
+	ipt_osf: Windows [2000:SP3:Windows XP Pro SP1, 2000 SP3]: 11.22.33.55:4024 -> 11.22.33.44:139
 	ipt_osf: Unknown: 16384:106:1:48:020405B401010402 44.33.22.11:1239 -> 11.22.33.44:80
 
   --smart
   	if present, OSF will use some smartness to determine remote OS.
-	Now only not use TTL( with it far remote machines can be determined).
+	OSF will use initial TTL only if source of connection is in our local network.
+  
+  --netlink
+	If present, OSF will log all events also through netlink NETLINK_NFLOG groupt 1.
+
+Fingerprints can be loaded and read through /proc/sys/net/ipv4/osf file.
+One can flush all fingerprints with following command:
+echo -en FLUSH > /proc/sys/net/ipv4/osf
 
-Fingerprints can be loaded through /proc/sys/net/ipv4/osf file.
-Only one fingerprint per open/close.
+Only one fingerprint per open/write/close.
 
 Fingerprints can be downloaded from http://www.openbsd.org/cgi-bin/cvsweb/src/etc/pf.os

#include <asm/types.h>

#include <sys/types.h>
#include <sys/socket.h>

#include <linux/netlink.h>
#include <linux/rtnetlink.h>

#include <arpa/inet.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include "osf/ipt_osf.h"

#ifndef NIPQUAD

#define NIPQUAD(addr) \
	((unsigned char *)&addr)[0], \
	((unsigned char *)&addr)[1], \
	((unsigned char *)&addr)[2], \
	((unsigned char *)&addr)[3]

#endif

int main()

{
	int s;
	char buf[1024];
	int len;
	struct nlmsghdr *reply;
	struct sockaddr_nl l_local;
	struct ipt_osf_nlmsg *data;
	
	memset(buf, 0, sizeof(buf));
	
	s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_NFLOG);
	if (s == -1)
	{
		perror("socket");
		return -1;
	}
	
	l_local.nl_family = AF_NETLINK;
	l_local.nl_groups = 1;
	l_local.nl_pid    = getpid();
	
	if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1)
	{
		perror("bind");
		close(s);
		return -1;
	}

	while (1)
	{
		len = recv(s, buf, sizeof(buf), 0);
		if (len == -1)
		{
			perror("recv buf");
			close(s);
			return -1;
		}
		reply = (struct nlmsghdr *)buf;
		
		printf("first nlmsghdr received: len=%d\n", len);
		printf("nlsmsghdr: len=%d, type=%d, flags=%d, seq=%d, pid=%d, sender_pid=%d\n", 
				reply->nlmsg_len, reply->nlmsg_type, 
				reply->nlmsg_flags, reply->nlmsg_seq, reply->nlmsg_pid,
				getpid());

		switch (reply->nlmsg_type)
		{
			case NLMSG_ERROR:
				printf("Error message received.\n");
				break;
			case NLMSG_DONE:
				data = (struct ipt_osf_nlmsg *)NLMSG_DATA(reply);
				printf("%s [%s:%s:%s] : %u.%u.%u.%u:%u -> %u.%u.%u.%u:%u hops=%d\n", 
					data->f.genre, data->f.version,
					data->f.subtype, data->f.details,
					NIPQUAD(data->ip.saddr), ntohs(data->tcp.source),
					NIPQUAD(data->ip.daddr), ntohs(data->tcp.dest),
					data->f.ttl - data->ip.ttl);
				break;
		}
	}

	close(s);
	return 0;
}
extensions/libipt_osf.c +11 -3
@@ -31,9 +31,9 @@
 #include <ctype.h>
 
 #include <iptables.h>
-#include <linux/netfilter_ipv4/ipt_osf.h>
+#include <linux/netfilter_ipv4/ipt_osf.h>
 
-#define IPTABLES_VERSION	"1.2.6a" /* It looks like FIXME */
+#define IPTABLES_VERSION	"1.2.7a" /* Permanent FIXME */
 
 static void help(void)
 {
@@ -42,7 +42,8 @@
 		"--smart		Use some smart extensions to determine OS (do not use TTL).\n"
 		"--log level		Log all(or only first) determined genres even if "
 					"they do not match desired one. "
-					"Level may be 0(all) or 1(only first entry).\n",
+					"Level may be 0(all) or 1(only first entry).\n"
+		"--netlink		Log through netlink(NETLINK_NFLOG).\n",
 		IPTABLES_VERSION);
 }
 
@@ -51,6 +52,7 @@
 	{ .name = "genre",	.has_arg = 1, .flag = 0, .val = '1' },
 	{ .name = "smart",	.has_arg = 0, .flag = 0, .val = '2' },
 	{ .name = "log",	.has_arg = 1, .flag = 0, .val = '3' },
+	{ .name = "netlink",	.has_arg = 0, .flag = 0, .val = '4' },
 	{ .name = 0 }
 };
 
@@ -101,6 +103,12 @@
 			*flags |= IPT_OSF_LOG;
 			info->loglevel = atoi(argv[optind-1]);
 			info->flags |= IPT_OSF_LOG;
+			break;
+		case '4': /* --netlink */
+			if (*flags & IPT_OSF_NETLINK)
+				exit_error(PARAMETER_PROBLEM, "Can't specify multiple smart parameter");
+			*flags |= IPT_OSF_NETLINK;
+			info->flags |= IPT_OSF_NETLINK;
 			break;
 		default:
 			return 0;
#1 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

extensions/libipt_osf.c line 36

Quoted 2 lines
extensions/libipt_osf.c +1 -1
-#define IPTABLES_VERSION	"1.2.6a" /* It looks like FIXME */
+#define IPTABLES_VERSION	"1.2.7a" /* Permanent FIXME */

Why do you define IPTABLES_VERSION ??

This is defined by the makefile, and it is not a good idea to re-define it, since then your compiled extension won't work [since current version is higher than 1.2.7a].

Apart from that, the patch is fine to me.
- --
- - Harald Welte <[email protected]> http://www.netfilter.org/
============================================================================
"Fragmentation is like classful addressing -- an interesting early
architectural error that shows how much experimentation was going
on while IP was being designed." -- Paul Vixie
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQFAG9h7XaXGVTD0i/8RApgtAKCDXEGCAVoT42hBkyaZRUAtmVKYPgCfULm1 3zA/UYiVRXYDH3EY0EDWMFk=
=Q6g+
-----END PGP SIGNATURE-----

#2 lore raw
Quoted 8 lines · On Sat, 31 Jan 2004 17:31:55 +0100 Harald Welte <[email protected]> wrote:

Review

Quoted 2 lines
(quoted diff) +1 -1
-#define IPTABLES_VERSION	"1.2.6a" /* It looks like FIXME */
+#define IPTABLES_VERSION	"1.2.7a" /* Permanent FIXME */

Why do you define IPTABLES_VERSION ??

This is defined by the makefile, and it is not a good idea to re-define it, since then your compiled extension won't work [since current version is higher than 1.2.7a].

Because I compile lib and modules separately from kernel and iptables with my own makefiles.
And I didn't know that it was defined somewhere ;)

Quoted 1 line

Apart from that, the patch is fine to me.

I attach patch for libipt_osf.c without IPTABLES_VERSION.

Thank you.

Quoted 3 lines

- --
- - Harald Welte <[email protected]>
http://www.netfilter.org/

Evgeniy Polyakov ( s0mbre )

Only failure makes us experts. -- Theo de Raadt

extensions/libipt_osf.c +9 -1
@@ -42,7 +42,8 @@
 		"--smart		Use some smart extensions to determine OS (do not use TTL).\n"
 		"--log level		Log all(or only first) determined genres even if "
 					"they do not match desired one. "
-					"Level may be 0(all) or 1(only first entry).\n",
+					"Level may be 0(all) or 1(only first entry).\n"
+		"--netlink		Log through netlink(NETLINK_NFLOG).\n",
 		IPTABLES_VERSION);
 }
 
@@ -51,6 +52,7 @@
 	{ .name = "genre",	.has_arg = 1, .flag = 0, .val = '1' },
 	{ .name = "smart",	.has_arg = 0, .flag = 0, .val = '2' },
 	{ .name = "log",	.has_arg = 1, .flag = 0, .val = '3' },
+	{ .name = "netlink",	.has_arg = 0, .flag = 0, .val = '4' },
 	{ .name = 0 }
 };
 
@@ -101,6 +103,12 @@
 			*flags |= IPT_OSF_LOG;
 			info->loglevel = atoi(argv[optind-1]);
 			info->flags |= IPT_OSF_LOG;
+			break;
+		case '4': /* --netlink */
+			if (*flags & IPT_OSF_NETLINK)
+				exit_error(PARAMETER_PROBLEM, "Can't specify multiple smart parameter");
+			*flags |= IPT_OSF_NETLINK;
+			info->flags |= IPT_OSF_NETLINK;
 			break;
 		default:
 			return 0;