How to: Linux Iptables block common attacks

by on July 6, 2005 · 18 comments· Last updated October 7, 2007

Following list summaries the common attack on any type of Linux computer:

Syn-flood protection

In this attack system is floods with a series of SYN packets. Each packets causes system to issue a SYN-ACK responses. Then system waits for ACK that follows the SYN+ACK (3 way handshake). Since attack never sends back ACK again entire system resources get fulled aka backlog queue. Once the queue is full system will ignored incoming request from legitimate users for services (http/mail etc). Hence it is necessary to stop this attack with iptables.

Force SYN packets check

Make sure NEW incoming tcp connections are SYN packets; otherwise we need to drop them:

iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP

Force Fragments packets check

Packets with incoming fragments drop them. This attack result into Linux server panic such data loss.

iptables -A INPUT -f -j DROP

XMAS packets

Incoming malformed XMAS packets drop them:

iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP

Drop all NULL packets

Incoming malformed NULL packets:

iptables -A INPIT -p tcp --tcp-flags ALL NONE -j DROP

Block Spoofing and bad addresses

Using iptables you can filter to drop suspicious source address. Network server should not accept packets claiming from the Internet that claim to originate from inside your network. Spoofing can be classified as:
a) IP spoofing – Disable the source address of authentication, for example rhosts based authentication. Filter RPC based services such as portmap and NFS,
b) DNS spoofing
Please see Iptables: How to avoid Spoofing and bad addresses attack tip for more information.

Also use NAT for your internal network. This makes difficult for attacker to spoof IP address from outside.

Filter incoming ICMP, PING traffic

It includes the ping of death attack and ICMP floods. You should block all ICMP and PING traffic for outside except for your own internal network (so that you can ping to see status of your own server) . See Linux : Iptables Allow or block ICMP ping request article.

Once system is secured, test your firewall with nmap or hping2 command:
# nmap -v -f FIREWALL-IP
# nmap -v -sX FIREWALL-IP
# nmap -v -sN FIREWALL-IP
# hping2 -X FIREWALL-IP

Further readings

  • Man page : hping2(8), nmap(1), iptables(8)


You should follow me on twitter here or grab rss feed to keep track of new changes.

Featured Articles:

{ 18 comments… read them below or add one }

1 dhaval February 5, 2008 at 7:07 am

Cyberciti is the great.

Reply

2 Jlmiller March 1, 2008 at 4:51 am

Cyberciti had the answers I was looking for and the information is quite easy.

Reply

3 Matt Newcombe April 2, 2008 at 5:40 pm

Many thanks – just what I was looking for.

Matt

Reply

4 Ryan Rodriguez August 14, 2008 at 6:12 pm

This would block the more common XMAS packets.

iptables -A INPUT -p tcp –tcp-flags ALL FIN,PSH,URG -j DROP

Reply

5 ak November 3, 2008 at 3:10 am

In Drop all NULL packets,

Please correct spelling.
Change INPIT to INPUT

Reply

6 Hamza Sani October 23, 2009 at 9:31 am

Cyberciti is really very knowledgeable website i would like to say thank you for this.

Reply

7 name October 31, 2009 at 1:38 pm

A better way of doing this would be to just use connection tracking:

-A INPUT -m conntrack –ctstate INVALID -j DROP
-A INPUT -m conntrack –ctstate ESTABLISHED,RELATED -j ACCEPT

And instead of blocking ICMP limiting is IMO a better way, -m limit.

Reply

8 kunoichi March 31, 2010 at 10:17 am

I would like to share with you my iptables rules… I am not gonna explain it in details.
They are ready to be used with the iptables-restore command, Just check your ports and substitute xx.xx.xx.xx with your IP… If you have more IPs just add more IP chains. customise to your requirments.


*filter

#clear
-F

#clear custom
-X

#default rules
-P INPUT DROP
-P OUTPUT DROP
-P FORWARD DROP

#allow localhost
-A INPUT -i lo -j ACCEPT
-A OUTPUT -o lo -j ACCEPT

#allow output for new, related and established connections
-A OUTPUT -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT

#
# PACKETS chain
#
-N PACKET
-A PACKET -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m limit --limit 1/sec -j ACCEPT
-A PACKET -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK RST -m limit --limit 1/sec -j ACCEPT
#limit ping to 1 per second
-A PACKET -p icmp -m icmp --icmp-type 8 -m limit --limit 1/sec -j ACCEPT

#
# STATE_TRACK chain (connection tracking)
#
-N STATE_TRACK
-A STATE_TRACK -m state --state RELATED,ESTABLISHED -j ACCEPT
-A STATE_TRACK -m state --state INVALID -j DROP

#
# PORTSCAN chain (drop common attacks)
#
-N PORTSCAN
-A PORTSCAN -p tcp --tcp-flags ACK,FIN FIN -j DROP
-A PORTSCAN -p tcp --tcp-flags ACK,PSH PSH -j DROP
-A PORTSCAN -p tcp --tcp-flags ACK,URG URG -j DROP
-A PORTSCAN -p tcp --tcp-flags FIN,RST FIN,RST -j DROP
-A PORTSCAN -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
-A PORTSCAN -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
-A PORTSCAN -p tcp --tcp-flags ALL ALL -j DROP
-A PORTSCAN -p tcp --tcp-flags ALL NONE -j DROP
-A PORTSCAN -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP
-A PORTSCAN -p tcp --tcp-flags ALL SYN,FIN,PSH,URG -j DROP
-A PORTSCAN -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP

#
# COMMON chain (everything passes through here)
#
-N COMMON
-A COMMON -j STATE_TRACK
-A COMMON -j PORTSCAN

#
# chains relations
#
-A INPUT -j COMMON
-A OUTPUT -j COMMON
-A FORWARD -j COMMON
-A FORWARD -j PACKET
#
# IP chain
#
-N IP1
-A INPUT -d xx.xx.xx.xx -j IP1
-A OUTPUT -d xx.xx.xx.xx -j IP1
-A FORWARD -d xx.xx.xx.xx -j IP1
#SSH On standard port
-A IP1 -p tcp --dport 22 -j ACCEPT
#HTTP HTTPS
-A IP1 -p tcp --dport 80 -j ACCEPT
-A IP1 -p tcp --dport 443 -j ACCEPT
#DNS
-A IP1 -p tcp --dport 53 -j ACCEPT
-A IP1 -p udp --dport 53 -j ACCEPT
Admin Panel (Webmin)
-A IP1 -p tcp --dport 10000 -j ACCEPT

# MORE IP CHAINS
#-N IP2
#-A INPUT -d xx.xx.xx.yy -j IP2
#-A OUTPUT -d xx.xx.xx.yy -j IP2
#-A FORWARD -d xx.xx.xx.yy -j IP2
#SSH
#-A IP2 -p tcp --dport 22 -j ACCEPT
#HTTP HTTPS
#-A IP2 -p tcp --dport 80 -j ACCEPT
#-A IP2 -p tcp --dport 443 -j ACCEPT
#DNS
#-A IP2 -p tcp --dport 53 -j ACCEPT
#-A IP2 -p udp --dport 53 -j ACCEPT

COMMIT

Reply

9 Andy February 12, 2012 at 11:08 am

Very nice, thank you for posting this!

Reply

10 Anonymous May 1, 2010 at 8:52 pm

Completely blocking ICMP may result in a Black Hole situation (RFC 2923) since ICMP is vital to the PMTUD process. This may lock out clients coming over a congested link with MTU sizes below 1500 (e.g. tunnels).

Reply

11 scott April 9, 2011 at 5:05 pm

i was wondering if there is a way you can do this in windows as well. i get alot of these attacks and i tried the blocking the ICMP rule in my firewall. it worked for a few days but the attacks continued.

Reply

12 JB July 29, 2011 at 8:28 pm

I am still able to get pinged using another computer running Ubuntu 11.04 and using nmap with those commands.

Reply

13 JBress November 4, 2011 at 2:25 pm

Great info, was getting molested with SYN attacks now things are stable!

Just want to correct a spelling mistak, for users like myself, who copy & paste everything into ssh

Drop null packets:
iptables -A INPIT -p tcp –tcp-flags ALL NONE -j DROP

Should be

iptables -A INPUT -p tcp –tcp-flags ALL NONE -j DROP

I knows its no biggie, but its easy to just copy & paste the mistake in and not check

Cheers!

Reply

14 Goback June 6, 2012 at 5:13 am

Hi guy
nice tut,
but what about “HTTP Slow Post” attacks?

Reply

15 Jouni "Rautamiekka" Järvinen August 6, 2012 at 12:59 pm

This tip is great, but it has flaws:
- typos
- grammar errors
- missing other types of attacks, like those told in comments

Reply

16 fame October 24, 2012 at 2:22 pm

kunoichi, GREAT SHARE!! This is a great iptables setup… and it works great with psad… so far best one that I have seen and liked… takes care of everything..

Reply

17 johnlth93 November 16, 2012 at 4:46 pm

iptables -A INPIT -p tcp –tcp-flags ALL NONE -j DROP
typo
iptables -A INPUT -p tcp –tcp-flags ALL NONE -j DROP

anyway, thanks for sharing <3

Reply

18 jose jeffrey a. mazaredo April 1, 2013 at 9:21 am

Hi,

You have a typo:

iptables -A INPIT -p tcp –tcp-flags ALL NONE -j DROP

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 3 + 2 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Tagged as: , , , , , , , ,

Previous post:

Next post: