Giter VIP home page Giter VIP logo

conntrack-logger's Introduction

conntrack-logger

Tool to make best effort to log conntrack flows along with associated pids, which service cgroup they belong to and misc other info about them.

Think of it as an auditd extension to log network connections.

Main purpose is to keep track of what (if anything) in the system tries to establish fishy or unauthorized connections.

For example, imagine your IDS spots a occasional (e.g. 1 per day/week) connections to known botnet hosts from one of the intranet machines. You get a dump of some encrypted traffic that gets passed, but looking at the machine in question, you've no idea which pid initiated these at the time - only clue is transient port numbers, which are useful only while connection lasts.

This tool allows to attribute such logged connections to pids (which might be e.g. forked curl, hence not useful by itself) and services they belong to, assuming proper service-pid-tracking system (i.e. systemd cgroups) is in place.

Unlike e.g. netstat-monitor, it doesn't poll /proc/net/* paths (though still uses them to map flow back to pid), getting "new flow" events via libnetfilter_conntrack (netlink socket) instead, in a bit more efficient manner.

Usage

Just run nfct_logger.py and get the entries from its stdout (lines wrapped for readability):

# ./nfct_logger.py -p tcp
1373127181: ipv6/tcp 2001:470:1f0b:11de::12/55446 > 2607:f8b0:4006:802::1010/80 ::
	2354 1000:1000 /user/1000.user/1.session/systemd-1196/enlightenment.service ::
	curl -s -o /dev/null ipv6.google.com
1373127199: ipv4/tcp 192.168.0.12/34870 > 195.24.232.208/80 ::
	28865 1000:1000 /user/1000.user/1.session/systemd-1196/dbus.service ::
	python /usr/libexec/dbus-lastfm-scrobbler
1373127220: ipv4/tcp 127.0.0.1/59047 > 127.0.0.1/1234 ::
	2387 1000:1000 /user/1000.user/1.session/systemd-1196/enlightenment.service ::
	ncat -v cane 1234

Default log format (can be controlled via --format, timestamp format via --format-ts) is (wrapped):

{ts}: {ev.proto} {ev.src}/{ev.sport} > {ev.dst}/{ev.dport} ::
	{info.pid} {info.uid}:{info.gid} {info.service} :: {info.cmdline}

Info about pid might not be available for transient connections, like one-way udp packets, as these don't seem to end up in /proc/net/udp (or udp6) tables, hence it's hard to get socket "inode" number.

As netfilter, conntrack and netlink sockets are linux-specific things (afaik), script should not work on any other platforms, unless there is some compatibility layer in place.

nfct_cffi

Tool is based on bundled nfct_cffi module, which can be used from any python code:

from nfct_cffi import NFCT

src = NFCT().generator()
print 'Netlink fd: {} (to e.g. integrate into eventloop)'.format(next(src))
for data in src:
	print 'Got event: {}'.format(data)

Module uses libnetfilter_conntrack via CFFI.

Installation

It's a regular package for Python 2.7 (not 3.X), but not in pypi, so can be installed from a checkout with something like that:

% python setup.py install

Better way would be to use pip to install all the necessary dependencies as well:

% pip install 'git+https://github.com/mk-fg/conntrack-logger.git#egg=conntrack-logger'

Note that to install stuff in system-wide PATH and site-packages, elevated privileges are often required. Use "install --user", ~/.pydistutils.cfg or virtualenv to do unprivileged installs into custom paths.

Alternatively, ./nfct_logger.py can be run right from the checkout tree without any installation.

Requirements

  • Python 2.7 (not 3.X)
  • CFFI (for libnetfilter_conntrack bindings)
  • libnetfilter_conntrack
  • nf_conntrack_netlink kernel module (e.g. modprobe nf_conntrack_netlink)

CFFI uses C compiler to generate bindings, so gcc (or other compiler) should be available if module is being built from source or used from checkout tree.

To install these requirements on Debian/Ubuntu (tested on Ubuntu "Xenial" 16.04), use:

# apt install build-essential libnfnetlink-dev python-cffi libnetfilter-conntrack-dev libpython2.7-dev

Limitations

When new flow event is received from libnetfilter_conntrack, it doesn't have "pid" attribute associated with it, so script looks up corresponding line in /proc/net/* to pick "inode" number for connection from there, then does glob('/proc/[0-9]*/fd/[0-9]*'), readlink() on each to find which one leads to socket matching that inode and then grabs/prints info for the pid from there on match.

So for super-quick connections, slow pid context switching, lots of pids or something, it might fail to match socket/pid in time, while both are still around, printing only connection info instead.

Running curl on even the fastest url probably won't ever slip by the logging, but some fast app opening socket, sending a packet, then closing it immediately afterwards can do that.

auditd is probably a tool to track such things in a more dedicated way.

conntrack-logger's People

Contributors

mk-fg avatar mpontillo avatar simeonmiteff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

conntrack-logger's Issues

No accounting fields in event handler

While testing on Ubuntu enabled conntrack with accounting module.
I see the event using conntrack -E with bytes and packets field.
However, when using conntrack logger, such information is not present in the event xml (all flags enabled in the generator)
Any possible hint?

Best regards,
Zied

nfct_logger crashes on start up.

./nfct_logger.py -p tcp
python: api.c:306: nfct_callback_register2: Assertion `h != ((void *)0)' failed.
Aborted (core dumped)

That's a permission issue. running with sudo solved the issue.
I suggest testing for the appropriate permissions and not crashing :)

Failed to parse event data (on Ubuntu "Xenial" 16.04)

I see intermittent parse failures when I run this on Ubuntu 16.04, such as the following:

ERROR:root:Failed to parse event data: <flow type="new"><meta direction="original"><layer3 protonum="2" protoname="ipv4"><src>192.168.0.2</src><dst>1.75.7.178</dst></layer3><layer4 protonum="6" protoname="tcp"><sport>33367</sport><dport>80</dport></layer4></meta><meta direction="reply"><layer3 protonum="2" protoname="ipv4"><src>1.75.7.178</src><dst>1.2.4.5</dst></layer3><layer4 protonum="6" protoname="tcp"><sport>80</sport><dport>33367</dport></layer4></meta><meta direction="independent"><state>SYN_SENT</state><timeout>120</timeout><id>501034516</id><unreplied/></meta><when><hour>21</hour><min>39</min><sec>50</sec><wday>3</wday><day>11</day><month>7</month><year>2017</year></when></flow>

I haven't had a chance to triage further; will update this issue if I do.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.