UNIXSOCK plugin (v5b)
Hi,
First of all, sorry for the long time with no updates on this patch. This is a resend of the patches for the UNIXSOCK input plugin for ulogd2.
Please let me know if there are any issues.
PS: hope you enjoyed the oowall presentation :)
Changes for v5:
- align all structures on 64 bits instead of 32 This has been tested on a sparc64 (thanks to Jan)
- update pcap2ulog to handle different pcap formats
Changes for v4:
- use structures instead of reading integers directly
- all structures and fields are now aligned (4 bytes)
- update pcap2ulog script to use aligned data as well
Changes for v3:
- if the bufsize value (from conf) is 0, try to guess the buffer size using getsockopt(SOL_SOCKET, SO_RCVBUF ..)
- set default path to /var/run/ulogd/ulogd2.sock with mode 0600
- add new configuration options perms, owner and group to be able to set these parameters on the created socket
- change the input algorithm to make it more robust against junk data (which can happen if the client is desynchronized)
- fixed a few bugs
Description:
[1] add new input plugin UNISOCK, using a unix socket. This allows userspace applications to send packets to ulogd, for example to send packets offline. It uses a key-length-value protocol to handle optional fields or extensions.
[2] is a Perl script to load a PCAP file and send it to ulogd using the UNIXSOCK plugin.
Pierre
PATCH 1/2 Add new input plugin UNIXSOCK
From: Pierre Chifflier <[email protected]>
This input plugins creates a unix socket which can be used to log packets. Scripts or applications can connect to the socket (only one client allowed per socket) and send data in a Key-Length-Value format (including the payload).
--- input/packet/Makefile.am | 5 +- input/packet/ulogd_inppkt_UNIXSOCK.c | 826 ++++++++++++++++++++++++++++++++++ ulogd.conf.in | 7 + 3 files changed, 837 insertions(+), 1 deletions(-) create mode 100644 input/packet/ulogd_inppkt_UNIXSOCK.c
input/packet/Makefile.am +4 -1
@@ -3,7 +3,7 @@ AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include AM_CFLAGS=-fPIC -Wall LIBS= -pkglib_LTLIBRARIES = ulogd_inppkt_NFLOG.la ulogd_inppkt_ULOG.la +pkglib_LTLIBRARIES = ulogd_inppkt_NFLOG.la ulogd_inppkt_ULOG.la ulogd_inppkt_UNIXSOCK.la ulogd_inppkt_NFLOG_la_SOURCES = ulogd_inppkt_NFLOG.c ulogd_inppkt_NFLOG_la_LDFLAGS = -avoid-version -module $(LIBNETFILTER_LOG_LIBS)
@@ -12,3 +12,6 @@ ulogd_inppkt_NFLOG_la_CFLAGS = $(AM_CFLAGS) $(LIBNETFILTER_LOG_CFLAGS) ulogd_inppkt_ULOG_la_SOURCES = ulogd_inppkt_ULOG.c ulogd_inppkt_ULOG_la_LDFLAGS = -avoid-version -module ulogd_inppkt_ULOG_la_LIBADD = ../../libipulog/libipulog.la + +ulogd_inppkt_UNIXSOCK_la_SOURCES = ulogd_inppkt_UNIXSOCK.c +ulogd_inppkt_UNIXSOCK_la_LDFLAGS = -avoid-version -module
input/packet/ulogd_inppkt_UNIXSOCK.c new file +826 -0
826 lines not on this page · raw message
ulogd.conf.in +7 -0
@@ -27,6 +27,7 @@ loglevel=1 plugin="@libdir@/ulogd/ulogd_inppkt_NFLOG.so" #plugin="@libdir@/ulogd/ulogd_inppkt_ULOG.so" +#plugin="@libdir@/ulogd/ulogd_inppkt_UNIXSOCK.so" plugin="@libdir@/ulogd/ulogd_inpflow_NFCT.so" plugin="@libdir@/ulogd/ulogd_filter_IFINDEX.so" plugin="@libdir@/ulogd/ulogd_filter_IP2STR.so"
@@ -82,6 +83,9 @@ plugin="@libdir@/ulogd/ulogd_raw2packet_BASE.so" # this is a stack for logging packets to syslog after a collect via NFLOG #stack=log3:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,sys1:SYSLOG +# this is a stack for logging packets to syslog after a collect via NuFW +#stack=nuauth1:UNIXSOCK,base1:BASE,ip2str1:IP2STR,print1:PRINTPKT,sys1:SYSLOG + # this is a stack for flow-based logging to MySQL #stack=ct1:NFCT,ip2bin1:IP2BIN,mysql2:MYSQL
@@ -145,6 +149,9 @@ numeric_label=1 # you can label the log info based on the packet verdict nlgroup=1 #numeric_label=0 # optional argument +[nuauth1] +socket_path="/tmp/nuauth_ulogd2.sock" + [emu1] file="/var/log/ulogd_syslogemu.log" sync=1
Signature · 6 lines
1.7.1 -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 2/2 Add helper script pcap2ulog
In reply to Pierre ChifflierFrom: Pierre Chifflier <[email protected]>
This script uses the Net::Pcap Perl library to parse an pcap file and send packets to ulogd2 throught the UNIXSOCK input module.
--- contrib/pcap2ulog | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 166 insertions(+), 0 deletions(-) create mode 100755 contrib/pcap2ulog
contrib/pcap2ulog new file +166 -0
@@ -0,0 +1,166 @@ +#!/usr/bin/perl -w + +# Copyright (C) 2009-2010 Pierre Chifflier <[email protected]> +# +# This program is free software; you can redistribute it and/or modify +# it under the same terms as Perl itself, either Perl version 5.8.4 or, +# at your option, any later version of Perl 5 you may have available. + +use strict; + +use IO::Socket; +use Net::Pcap; + +my $ULOGD_SOCKET_MARK = 0x41c90fd4; + +my $dumpfile = shift or die "Unable to open pcap file"; +my($pcap_t, $err); +my($ulogd_client); +my $socketfile = "/var/run/ulogd2.sock"; +my $data_buffer; +my $linktype; +my $proto_offset; + +my %linktype_to_offset = ( + Net::Pcap::DLT_LINUX_SLL => 14, + Net::Pcap::DLT_EN10MB => 12, +); + +sub connect_ulogd2 { + (-S $socketfile) or die "ulogd2 socket $socketfile does not exist - is ulogd running ?"; + + $ulogd_client = IO::Socket::UNIX->new(Peer => $socketfile, + Type => SOCK_STREAM ) or die $!; + $ulogd_client->autoflush(0); +} + +sub print_padding +{ + my ($offset) = @_; + my $padding; + my $align = 8; + my $data; + + $padding = ($align - ($offset % $align)) % $align; + #print "offset: $offset padding $padding\n"; + + $data = "\0" x $padding; + $data_buffer .= $data; +} + +sub process_pkt { + my($user, $hdr, $pkt) = @_; + + if (($user ne "xyz") or !defined($hdr) or !defined($pkt)) { + print("Bad args passed to callback\n"); + print("Bad user data\n"), if ($user ne "xyz"); + print("Bad pkthdr\n"), if (!defined($hdr)); + print("Bad pkt data\n"), if (!defined($pkt)); + print("not ok\n"); + exit; + } + + #print "Header: len $hdr->{len}\n"; + #my $len = length $pkt; + #print "Packet length: $len\n"; + + my $size = length($pkt) - ($proto_offset+2); + + #my $pcaphdr = unpack ("H*", substr ($pkt, 0, 16)); + #printf("pcap hdr: $pcaphdr\n"); + my $proto = unpack ("H*", substr ($pkt, $proto_offset, 2)); + #printf("proto: $proto\n"); + + if ($proto ne "0800") { + print "ignoring packet with proto $proto\n"; + return; + } + + #my $ip_firstbyte = unpack ("H*", substr ($pkt, $proto_offset+2, 2)); + #printf("ip_firstbyte: $ip_firstbyte\n"); + + # decode packet for a SLL: + # packet type (sent by us: 4) + # link layer address type: 1 + # link layer address length: 6 + # src dst + # protocol (IP, ARP, PPP, SNMP ...) + # data + my $srcmac = substr ($pkt, 6, 6); + + (my $hex_src = unpack("H*", $srcmac)) =~ s/(..)/$1:/g; + chop $hex_src; + #printf "source mac: $hex_src\n"; + + my $hex_dst = "\0"; + + # format data + my $data; + + $data_buffer = undef; + + # ulogd packet signature + $data = pack ('N', $ULOGD_SOCKET_MARK); + + $data_buffer .= $data; + + my $options_num=2; + my $options_len=length($hex_src) + length($hex_dst); + # total length (will be filled later) + my $total_size = 0; + $data = pack ('n', $total_size); + $data_buffer .= $data; + + # reserved + payload length + payload + $data = pack ('Nna*', 0, $size, substr($pkt,$proto_offset+2,$size)); + $data_buffer .= $data; + print_padding($size); + + # options + my $OOB_IN = 2; + $data = pack ('NNa*', $OOB_IN, length($hex_src), $hex_src); + $data_buffer .= $data; + print_padding(length($hex_src)); + my $OOB_OUT = 3; + $data = pack ('NNa*', $OOB_OUT, length($hex_dst), $hex_dst); + $data_buffer .= $data; + print_padding(length($hex_dst)); + + # replace total size in buffer + my $l = length($data_buffer) - 4; + substr($data_buffer, 4, 2) = pack('n', $l); + + #(my $hex = unpack("H*", $data_buffer)) =~ s/(..)/$1 /g; + #print "$l will be encoded as " . unpack("H*", pack('n', $l)) . "\n"; + #print $hex, "\n"; + + print $ulogd_client $data_buffer; + + $ulogd_client->flush; + + #exit; +} + + +connect_ulogd2 or die $!; + +$pcap_t = Net::Pcap::open_offline($dumpfile, \$err); +if (!defined($pcap_t)) { + print("Net::Pcap::dump_open failed: ", Net::Pcap::geterr($pcap_t), "\n"); + exit; +} + +$linktype = Net::Pcap::pcap_datalink($pcap_t); + +if (not exists $linktype_to_offset{$linktype}) { + print("Unsupported link type ", Net::Pcap::pcap_datalink_val_to_name($linktype), "\n"); + exit 1; +} + +$proto_offset = $linktype_to_offset{$linktype}; + +Net::Pcap::loop($pcap_t, -1, \&process_pkt, "xyz"); +Net::Pcap::close($pcap_t); + + +
Signature · 1 line
1.7.1
Hi,
Quoted 6 lines · Pierre Chifflier
Hi,
First of all, sorry for the long time with no updates on this patch. This is a resend of the patches for the UNIXSOCK input plugin for ulogd2.
Please let me know if there are any issues.
Patches pushed to the git tree.
BR,
Quoted 35 lines
PS: hope you enjoyed the oowall presentation :)
Changes for v5:
- align all structures on 64 bits instead of 32 This has been tested on a sparc64 (thanks to Jan)
- update pcap2ulog to handle different pcap formatsChanges for v4:
- use structures instead of reading integers directly
- all structures and fields are now aligned (4 bytes)
- update pcap2ulog script to use aligned data as wellChanges for v3:
- if the bufsize value (from conf) is 0, try to guess the buffer size usinggetsockopt(SOL_SOCKET, SO_RCVBUF ..)
- set default path to/var/run/ulogd/ulogd2.sockwith mode 0600
- add new configuration options perms, owner and group to be able to set these parameters on the created socket
- change the input algorithm to make it more robust against junk data (which can happen if the client is desynchronized)
- fixed a few bugsDescription:
[1] add new input plugin UNISOCK, using a unix socket. This allows userspace applications to send packets to ulogd, for example to send packets offline. It uses a key-length-value protocol to handle optional fields or extensions.
[2] is a Perl script to load a PCAP file and send it to ulogd using the UNIXSOCK plugin.
Pierre
Signature · 3 lines
Eric Leblond <[email protected]> INL: http://www.inl.fr/ NuFW: http://www.nufw.org/