How to: Linux Iptables block common attacks

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 INPUT -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)

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 29 comments so far... add one
CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
29 comments… add one
  • dhaval Feb 5, 2008 @ 7:07

    Cyberciti is the great.

  • Jlmiller Mar 1, 2008 @ 4:51

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

  • Matt Newcombe Apr 2, 2008 @ 17:40

    Many thanks – just what I was looking for.


  • Ryan Rodriguez Aug 14, 2008 @ 18:12

    This would block the more common XMAS packets.

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

    • Cody Jul 26, 2014 @ 11:48

      Yes, I was thinking similar. In fact, FIN,PSH,URG would be THE Xmas scan. ALL ALL is something else entirely. I was going to elaborate but the man page will state it better than I can (and why bother wording it the iptables man page is so thorough – admittedly it might not be as thorough as some may like, but those people can go find some other document …):

      tcp flags:
      Match when the TCP flags are as specified. The first argument mask is the flags which we should examine, written as a comma-separated list, and the second argument comp is a comma-separated list of flags which must be set. Flags are: SYN ACK FIN RST URG PSH ALL NONE.
      Hence the command
      iptables -A FORWARD -p tcp –tcp-flags SYN,ACK,FIN,RST SYN
      will only match packets with the SYN flag set, and the ACK, FIN and RST flags unset.

      Oh, as for Xmas, nmap(1):
      Xmas scan (-sX)
      Sets the FIN, PSH, and URG flags, lighting the packet up like a Christmas tree.

  • ak Nov 3, 2008 @ 3:10

    In Drop all NULL packets,

    Please correct spelling.
    Change INPIT to INPUT

  • Hamza Sani Oct 23, 2009 @ 9:31

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

  • name Oct 31, 2009 @ 13:38

    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.

  • kunoichi Mar 31, 2010 @ 10:17

    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.



    #clear custom

    #default rules

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

    #allow output for new, related and established connections

    # PACKETS chain
    -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)
    -A STATE_TRACK -m state --state INVALID -j DROP

    # PORTSCAN chain (drop common attacks)
    -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)

    # chains relations
    # 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
    -A IP1 -p tcp --dport 80 -j ACCEPT
    -A IP1 -p tcp --dport 443 -j ACCEPT
    -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

    #-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
    #-A IP2 -p tcp --dport 22 -j ACCEPT
    #-A IP2 -p tcp --dport 80 -j ACCEPT
    #-A IP2 -p tcp --dport 443 -j ACCEPT
    #-A IP2 -p tcp --dport 53 -j ACCEPT
    #-A IP2 -p udp --dport 53 -j ACCEPT


    • Andy Feb 12, 2012 @ 11:08

      Very nice, thank you for posting this!

  • Anonymous May 1, 2010 @ 20:52

    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).

  • SLX May 30, 2010 @ 14:44

    This rule
    iptables -A INPUT -p tcp ! –syn -m state –state NEW -j DROP
    will never work. The !SYN flag and NEW state are mutaully exclusive. Packet will never get NEW state if there has no been SYN flag earlier. And if you use connection tracking then this rule:
    iptables -A INPUT -f -j DROP
    also is not necessary, since enabling contrack also disables fragmented packets, they are merged before processing by iptables.

    • Cody May 28, 2014 @ 14:00

      If only that were the case (referring to the !syn state NEW).

      “NEW meaning that the packet has started a new connection, or otherwise associated with a connection which has not seen packets in both directions”

      For instance, if you were to do :
      iptables INPUT 1 -p tcp -m conntrack –ctstate NEW ! –syn -j LOG –log-prefix “state test ”
      and then used an ACK scan (e.g., with nmap) then you would find in your logs that you are in fact incorrect.

      That being noted, conntrack is the suggested module nowadays.

  • Sam Jul 20, 2010 @ 10:01

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

    Should be:

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

  • scott Apr 9, 2011 @ 17:05

    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.

  • JB Jul 29, 2011 @ 20:28

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

  • JBress Nov 4, 2011 @ 14:25

    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


  • Goback Jun 6, 2012 @ 5:13

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

  • Jouni "Rautamiekka" Järvinen Aug 6, 2012 @ 12:59

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

  • Cosmin Sep 27, 2012 @ 18:08

    19:54:10.607064 IP > P win 512
    19:54:10.607059 IP > P win 512
    19:54:10.607052 IP > P win 512

    how to deny iptables? reply me thanks you!

  • fame Oct 24, 2012 @ 14:22

    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..

  • johnlth93 Nov 16, 2012 @ 16:46

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

    anyway, thanks for sharing <3

  • jose jeffrey a. mazaredo Apr 1, 2013 @ 9:21


    You have a typo:

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

  • Jose Tapia Nov 6, 2013 @ 6:50

    Great Article, i always learn new things in your blog, thanks i appreciate

  • Kozan Mar 12, 2014 @ 18:54

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

    It should say INPUT.

  • Cody Jul 26, 2014 @ 13:09

    As for syn floods, here’s an interesting article (that’s not to suggest you should NOT check for SYN and state NEW – you should… and you should rate-limit from same host too, but it is an interesting thing to consider, the article):

  • Wm Aug 6, 2015 @ 21:22

    I just wanted to be the 100th person to let you know that:

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

    needs to be changed to:

    iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
  • James Oct 29, 2016 @ 3:07

    Another helpful rule for SYN flood attacks is to limit the number of connections from any single source IP. Here’s the command:

    iptables -A INPUT -p tcp --syn -m connlimit --connlimit-above 15 --connlimit-mask 32 -j REJECT --reject-with tcp-reset

  • ben Jan 28, 2017 @ 23:04

    what are you saying about all this? it’s enough?

    -A INPUT -m state --state INVALID -j DROP
    -A INPUT -p tcp ! --syn -m state --state NEW -j DROP
    -A INPUT -p tcp --tcp-flags ACK,FIN FIN -j DROP
    -A INPUT -p tcp --tcp-flags ACK,PSH PSH -j DROP
    -A INPUT -p tcp --tcp-flags ACK,URG URG -j DROP
    -A INPUT -p tcp --tcp-flags FIN,RST FIN,RST -j DROP
    -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
    -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
    -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
    -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
    -A INPUT -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP
    -A INPUT -p tcp --tcp-flags ALL SYN,FIN,PSH,URG -j DROP
    -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
    -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP
    -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -j DROP
    -A INPUT -p tcp -m tcp --tcp-flags SYN,RST SYN,RST -j DROP
    -A INPUT -p tcp -m tcp --tcp-flags FIN,RST FIN,RST -j DROP
    -A INPUT -p tcp -m tcp --tcp-flags FIN,ACK FIN -j DROP
    -A INPUT -p tcp -m tcp --tcp-flags ACK,URG URG -j DROP
    -A INPUT -p tcp -m tcp --tcp-flags PSH,ACK PSH -j DROP
    -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -j DROP
    -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,ACK,URG -j DROP
    -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,PSH,ACK,URG -j DROP
    -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN -j DROP

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum