Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netfilter kernel module to intercept packets and log them

I had a basic code. This code drops and logs all the incoming and outgoing packets. I want to write a netfilter kernel module to intercept packets and log them in the kernel logs. It should be able to detect different(show 1 or 2 as example) kinds of TCP based reconnaissance packets. The module should detect these packets and log to kernel logs. I don't want to filter the packets, just identify and log them.

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>

static struct nf_hook_ops nfho;         //struct holding set of hook function options

//function to be called by hook
unsigned int hook_func(unsigned int hooknum, struct sk_buff **skb, const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *))
{
  printk(KERN_INFO "packet dropped\n");                                             //log to var/log/messages
  return NF_DROP;                                                                   //drops the packet
}

//Called when module loaded using 'insmod'
int init_module()
{
  nfho.hook = hook_func;                       //function to call when conditions below met
  nfho.hooknum = NF_IP_PRE_ROUTING;            //called right after packet recieved, first hook in Netfilter
  nfho.pf = PF_INET;                           //IPV4 packets
  nfho.priority = NF_IP_PRI_FIRST;             //set to highest priority over all other hook functions
  nf_register_hook(&nfho);                     //register hook

  return 0;                                    //return 0 for success
}

//Called when module unloaded using 'rmmod'
void cleanup_module()
{
  nf_unregister_hook(&nfho);                     //cleanup – unregister hook
}
like image 393
Mukesh Gupta Avatar asked Sep 10 '16 13:09

Mukesh Gupta


1 Answers

First, this module drops only incoming packets. The reason is the following row: nfho.hooknum = NF_IP_PRE_ROUTING;. About your question: I don't understand what are "based reconnaissance packets", but you can extract all the data from the packet and show them in kernel logs. For example:

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/ip.h>
#include <linux/tcp.h>

static struct nf_hook_ops nfho;         //struct holding set of hook function options

//function to be called by hook
unsigned int hook_func(unsigned int hooknum, struct sk_buff *skb, const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *))
{
  struct iphdr *ip_header = (struct iphdr *)skb_network_header(skb); //you can access to IP source and dest - ip_header->saddr, ip_header->daddr
  struct tcphdr *tcp_header;
  if (ip_header->protocol == 6) //TCP protocol
  {
    printk(KERN_INFO "TCP Packet\n");
    tcp_header = (struct tcphdr *)(skb_transport_header(skb)+20); //Note: +20 is only for incoming packets
    printk(KERN_INFO "Source Port: %u\n", tcp_header->source); //can access dest in the same way
  }
  return NF_ACCEPT;                                                                   //accept the packet
}

//Called when module loaded using 'insmod'
int init_module()
{
  nfho.hook = hook_func;                       //function to call when conditions below met
  nfho.hooknum = NF_INET_PRE_ROUTING;            //called right after packet recieved, first hook in Netfilter
  nfho.pf = PF_INET;                           //IPV4 packets
  nfho.priority = NF_IP_PRI_FIRST;             //set to highest priority over all other hook functions
  nf_register_hook(&nfho);                     //register hook

  return 0;                                    //return 0 for success
}

//Called when module unloaded using 'rmmod'
void cleanup_module()
{
  nf_unregister_hook(&nfho);                     //cleanup – unregister hook
} 
like image 150
Reuven Plevinsky Avatar answered Nov 14 '22 12:11

Reuven Plevinsky