Iptables and suricata as IPS
Building a Suricata ruleset with iptables has always been a complicated task when trying to combined the rules that are necessary for the IPS with the firewall rules. Suricata has always used Netfilter advanced features allowing some more or less tricky methods to be used.
For the one not familiar with IPS using Netfilter, here’s a few starting points:
- IPS receives the packet coming from kernel via rules using the NFQUEUE target
- The IPS must received all packets of a given flow to be able to handle detection cleanly
- The NFQUEUE target is a terminal target: when the IPS verdicts a packet, it is or accepted (and leave current chain)
So the ruleset needs to send all packets to the IPS. A basic ruleset for an IPS could thus looks like:
iptables -A FORWARD -j NFQUEUE
With such a ruleset, all packets going through the box are sent to the IPS.
If now you want to combine this with your ruleset, then usually your first try is to add rules to the filter chain:
iptables -A FORWARD -j NFQUEUE iptables -A FORWARD -m conntrack --ctstate ESTABLISHED -j ACCEPT # your firewall rules here
But this will not work because of point 2: All packets sent via NFQUEUE to the IPS are or blocked or if accepted, leave the FORWARD chain directly and are going for evaluation to the next chain (mangle POSTROUTING in our case).
With such a ruleset, the result is that there is no firewall but an IPS in place.
As mentioned before there is some existing solutions (see Building a Suricata ruleset for extensive information). The simplest one is to dedicated one another chain such as mangle to IPS:
iptables -A FORWARD -t mangle -j NFQUEUE iptables -A FORWARD -m conntrack --ctstate ESTABLISHED -j ACCEPT # your firewall rules here
No conflict here but you have to be sure nothing in your system will use the the mangle table or you will have the same problem as the one seen previously in the filter chain. So there was no universal and simple solution to implement an IPS and a firewall ruleset with iptables.
IPS the easy way with Nftables
In Nftables, chains are defined by the user using nft command line. The user can specify:
- The hook: the place in packet life where the chain will be set. See this diagram for more info.
- prerouting: chain will be placed before packet are routed
- input: chain will receive packets going to the box
- forward: chain will receive packets routed by the box
- postrouting: chain will receive packets after routing and before sending packets
- output: chain will receive packet sent by the host
- The chain type: define the objective of the chain
- filter: chain will filter packet
- nat: chain will only contains NAT rules
- route: chain is containing rule that may change the route (previously now as mangle)
- The priority: define the evaluation order of the different chains of a given hook. It is an integer that can be freely specified. But it also permits to place chain before or after some internal operation such as connection tracking.
In our case, we want to act on forwarded packets. And we want to have a chain for filtering followed by a chain for IPS. So the setup is simple of chain is simple
nft -i nft> add table filter nft> add chain filter firewall { type filter hook forward priority 0;} nft> add chain filter IPS { type filter hook forward priority 10;}
With this setup, a packet will reach the firewall chain first where it will be filtered. If the packet is blocked, it will be destroy inside of the kernel. It the packet is accepted it will then jump to the next chain following order of increasing priority. In our case, the packet reaches the IPS chain.
Now, that we’ve got our chains we can add filtering rules, for example:
nft add rule filter firewall ct state established accept nft add rule filter firewall tcp dport ssh counter accept nft add rule filter firewall tcp dport 443 accept nft add rule filter firewall counter log drop
And for our Suricata IPS, that’s just trivial:
nft add rule filter IPS queue
A bit more details
The queue target in nftables
The complete support for the queue target will be available in Linux 3.14. The syntax looks as follow:
nft add rule filter output queue num 3 total 2 options fanout
This rule sends matching packets to 2 load-balanced queues (total 2) starting at 3 (num 3).
fanout is one of the two queue options:
- fanout: When used together with total load balancing, this will use the CPU ID as an index to map packets to the queues. The idea is that you can improve perfor mance if there’s a queue per CPU. This requires total with a value superior to 1 to be specified.
- bypass: By default, if no userspace program is listening on an Netfilter queue,then all packets that are to be queued are dropped. When this option is used, the queue rule behaves like ACCEPT instead, and the packet will move on to the next table.
For a complete description of queueing mechanism in Netfilter see Using NFQUEUE and libnetfilter_queue.
If you want to test this before Linux 3.14 release, you can get nft sources from nftables git and use next-3.14 branch.
Chain priority
For reference, here are the priority values of some important internal operations and of iptables static chains:
- NF_IP_PRI_CONNTRACK_DEFRAG (-400): priority of defragmentation
- NF_IP_PRI_RAW (-300): traditional priority of the raw table placed before connection tracking operation
- NF_IP_PRI_SELINUX_FIRST (-225): SELinux operations
- NF_IP_PRI_CONNTRACK (-200): Connection tracking operations
- NF_IP_PRI_MANGLE (-150): mangle operation
- NF_IP_PRI_NAT_DST (-100): destination NAT
- NF_IP_PRI_FILTER (0): filtering operation, the filter table
- NF_IP_PRI_SECURITY (50): Place of security table where secmark can be set for example
- NF_IP_PRI_NAT_SRC (100): source NAT
- NF_IP_PRI_SELINUX_LAST (225): SELInux at packet exit
- NF_IP_PRI_CONNTRACK_HELPER (300): connection tracking at exit
For example, one can create in nftables an equivalent of the raw PREROUTING chain of iptables by doing:
# nft -i nft> add chain filter pre_raw { type filter hook prerouting priority -300;}
i have added rule in nftables for arp packets filtering i am sending packet in queue. i have written application which will received packets from queue but i am not receiving any packets but when i will used same application for ip packets it working fine can somebody suggest me where i am wrong or how to receive arp packet from queue
main() { int fd; char buf[65535]; int len=0; handle =nfq_open();
if(handle ==NULL) { printf(“error in nfq_open\n”); return 1; }
if(nfq_unbind_pf(handle,AF_INET)<0) { printf("error during nfq_unbind_pf\n"); exit(1); }
if(nfq_bind_pf(handle,AF_INET)<0) { printf("error in binding\n"); exit(1); }
qh=nfq_create_queue(handle,0,&callback,NULL);
if(qh==NULL) { printf("error to create new queue\n"); exit(1); }
if(nfq_set_mode(qh,NFQNL_COPY_PACKET,0xffff)<0)
{
printf("error to set mode \n");
exit(1);
}
if(nfq_set_queue_maxlen(qh,NFQ_MAX_LEN)<0)
{
printf("error setting queue size\n");
}
fd =nfq_fd(handle);
if(fd== (-1))
{
printf("error in opening nft queue\n");
exit(1);
}
while(1) {
len = recv(fd, buf, sizeof(buf), 0);
printf("pkt received in main thread buf: %s length :%d\n",buf,len); }
nfq_destroy_queue(qh);
printf("closing library handle\n"); nfq_close(handle); /* closing the handler */
}