LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 11-12-2017, 09:31 AM   #1
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,555

Rep: Reputation: 177Reputation: 177
use iptables to limit denial of server DoS


I'm looking to use iptables to help limit denial of service attacks to my webserver. I'm no iptables expert, so I'd like the input of some experts on my idea. I'm thinking about the following. Will it work? Are there improvements to be had or a better idea generally? The idea is to block IPs having 50 or more hits per second.
Code:
/usr/sbin/iptables -N webdrop
/usr/sbin/iptables -A webdrop -j LOG --log-level 6 --log-prefix "Web DoS attempt "
/usr/sbin/iptables -A webdrop -j DROP
    
/usr/sbin/iptables -N webcount
/usr/sbin/iptables -A webcount -m recent --set
/usr/sbin/iptables -A webcount -m recent --rcheck --hitcount 50 -j webdrop
/usr/sbin/iptables -A webcount -j RETURN

/usr/sbin/iptables -A INPUT -p tcp --syn -m multiport --dports 80,443 -i eth0 -j webcount
/usr/sbin/iptables -A INPUT -p tcp --syn -m limit --limit 1/s --limit-burst 3 -i eth0 -m multiport --dports 22,443 -j ACCEPT

Last edited by mfoley; 11-12-2017 at 09:33 AM.
 
Old 11-13-2017, 10:35 AM   #2
camp0
Member
 
Registered: Dec 2016
Location: Dublin
Distribution: Fedora
Posts: 70

Rep: Reputation: 4
The iptables rules looks fine but may be other people have better suggestions, on the other and if you have an apache server behind you can have a look to http://dominia.org/djao/limitipconn.html, im sure that nginx have something similar.

For have a good approach to DoS attacks is to act at network layer(iptables) and at application layer (mod on apache or whatever).
 
1 members found this post helpful.
Old 11-13-2017, 11:40 AM   #3
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,555

Original Poster
Rep: Reputation: 177Reputation: 177
Quote:
Originally Posted by camp0 View Post
The iptables rules looks fine but may be other people have better suggestions, on the other and if you have an apache server behind you can have a look to http://dominia.org/djao/limitipconn.html, im sure that nginx have something similar.

For have a good approach to DoS attacks is to act at network layer(iptables) and at application layer (mod on apache or whatever).
Thanks - I'm trying the mod_limitipconn now, although I'll have to contrive to get more than 10 simultaneous connections going.

I'm still looking for some feedback on the iptables rules before I try those.
 
Old 11-13-2017, 10:33 PM   #4
lazydog
Senior Member
 
Registered: Dec 2003
Location: The Key Stone State
Distribution: CentOS Sabayon and now Gentoo
Posts: 1,249
Blog Entries: 3

Rep: Reputation: 194Reputation: 194
I would do something like this:

Code:
iptables -N webcount
iptables -A webcount -m recent --set --name webcount --rsource
iptables -A webcount -m recent --update --seconds 3600 --hitcount 50 --name webcount --rsource -j LOG --log-level 6 --log-prefix "Web MAX Connections Reached: "
iptables -A webcount -m recent --update --seconds 3600 --hitcount 50 --name webcount --rsource -j DROP
iptables -A webcount -j ACCEPT

iptables -A INPUT -i eth0 -p tcp -m conntrack --ctstate NEW -m tcp -m multiport --dports 80,443 -j webcount
iptables -A INPUT -i eth0 -p tcp -m conntrack --ctstate NEW -m tcp -m limit --limit 1/s --limit-burst 3 -m multiport --dport 22 -j ACCEPT
Don't forget to add your ESTABLISHED and RELATED rules also.
 
Old 11-19-2017, 10:12 PM   #5
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,555

Original Poster
Rep: Reputation: 177Reputation: 177
lazydog: your rules look more succinct, only one new chain. What does the --update --seconds 3600 do? Does it un-DROP the blocked IP after one hour?

(I do have the ESTABLISHED and RELATED rules define before this snippet)

Last edited by mfoley; 11-19-2017 at 10:13 PM.
 
Old 11-21-2017, 06:05 PM   #6
lazydog
Senior Member
 
Registered: Dec 2003
Location: The Key Stone State
Distribution: CentOS Sabayon and now Gentoo
Posts: 1,249
Blog Entries: 3

Rep: Reputation: 194Reputation: 194
--update: Updates the recent counter in webcount.
--seconds: This is the timer for the recent counter in webcount.

So in other words when a packet comes in for ports 80 or 443 they go to the webcount chain. This chain then looks at the counters in webcount for the ip address if it is not there it is added by update. If it is there the last time is updated with the present time and the the count is checked against the timer seconds. If the hit count is above hitcount and the time the ip was last seen is under seconds the rule is applied.
 
  


Reply

Tags
ddos attack, iptables



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Iptables for protect OpenVPN server from DOS attacks klandestin0 Linux - Security 1 08-19-2015 07:51 AM
ipv6 DOS (denial of service) question yhclqo Linux - Security 1 06-23-2011 11:31 AM
Web Server Crash - Denial of Service inspiredbymetal Linux - Server 3 11-11-2007 05:31 AM
How to limit logins to postfix mail server to prevent DOS imi@tux Linux - Server 2 04-30-2007 01:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 09:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration