LinuxQuestions.org
Help answer threads with 0 replies.
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 02-18-2015, 12:35 AM   #1
noobie143
LQ Newbie
 
Registered: Sep 2014
Posts: 11

Rep: Reputation: Disabled
Using .htaccess to block ip ranges


I have used a .htaccess file with lines like this to block ip addresses from China

<Limit GET HEAD POST>
order allow,deny
allow from all
deny from 1.0.1.0/24
deny from 119.81.236.56/29
</Limit>

I know that the file works because when I spoof using Chinese ip addresses I get blocked.

However, when i type
netstat -tanp | grep ':80\b' | grep SYN_RECV | awk '{print $5}' | cut -d':' -f1 | sort | uniq -c | sort -nr

at the command prompt I still see a lot of Chinese ip addresses.

Can someone tell me what is happening?
 
Old 02-18-2015, 08:18 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
.htaccess rules deny the requests at apache level, the tcp connection is still made to the server it just doesn't get serviced by apache.
 
Old 02-18-2015, 08:30 AM   #3
noobie143
LQ Newbie
 
Registered: Sep 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thanks a lot. The ip's I blocked are still able to bring down my server periodically even though they can't get to Apache.

I have enabled Syn cookies now. Is there anything else I can do to stop the attacks? They seem pretty relentless.
 
Old 02-18-2015, 08:36 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
.htaccess doesn't do anything to block the traffic requests, if you can't block at router level then you can block with iptables. I've a blog post on how to implement iptables blocking of a list of countries. It assumes you know what iptables are and how to implement a basic firewall.
 
Old 02-18-2015, 08:53 AM   #5
noobie143
LQ Newbie
 
Registered: Sep 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
The link you posted doesn't seem to be working. I don't know anything about iptables but I suppose I shall have to learn in a hurry.

Thanks.
 
Old 02-18-2015, 08:58 AM   #6
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
If you're still spoofing or coming from a Chinese IP address then you'll have been blocked.....

Quote:
Originally Posted by My Blog Post
There are times when you’ll want to limit access and block whole countries. Why? Because there are times when it’s necessary.

Here’s a script that builds a script….

It downloads the IP ranges from www.ipdeny.com, works through a list of two letter country codes to create a bash script that will:
  • Delete an existing iptables chain.
  • Creates a new chain “BadCountry”.
  • Adds this to the top of the INPUT chain to pass anything on port 80 to the BadCountry chain.
  • Adds all the IP blocks in the relevant countries to the BadCountry chain with a reject/unreachable.

Feel free to adapt it to your needs.

(Oh, and you can also call the script with the parameter undo and it’ll delete the chain.)
Code:
#!/bin/bash

PARAM=${1}

if [ "${PARAM}" == "undo" ] ; then

  iptables -D INPUT -p tcp -m tcp --dport 80 -j BadCountry
  iptables --flush BadCountry
  iptables -X BadCountry

else

  echo $(date) IP Blocking GLOBAL START
  
  #First call ourselves to undo (delete the chain)
  ${0} undo

  #This is where the executable script that does the table update will live.
  TABLESCRIPT=/root/scripts/countrytables.sh

  #Change this to a folder you can write to
  cd /root/ipblocks
  
  #and delete any zone file tar/zip files  
  rm -f all-zones.tar.*

  echo $(date) Download Countries START

  wget "http://www.ipdeny.com/ipblocks/data/countries/all-zones.tar.gz"

  tar -zxvf all-zones.tar.gz > /dev/null

  echo $(date) Download Countries FINISH

  echo $(date) Build Countries START

  echo "#!/bin/bash" > ${TABLESCRIPT}

  echo "iptables -N BadCountry" >> ${TABLESCRIPT}

  echo "iptables -I INPUT -p tcp -m tcp --dport 80 -j BadCountry" >> ${TABLESCRIPT}

  echo "iptables -A BadCountry -j RETURN" >> ${TABLESCRIPT}

  for COUNTRY in hk cn in id kr my ph tw th vn pk ; do
    awk {'print "iptables -I BadCountry -s "$1" -j REJECT --reject-with icmp-port-unreachable"'} ${COUNTRY}.zone >> ${TABLESCRIPT}
  done

  echo $(date) Build Countries FINISH

  echo $(date) Updating iptables START

  #Make our script executable
  chmod 700 ${TABLESCRIPT}

  #And now execute it
  ${TABLESCRIPT}

  echo $(date) Updating iptables FINISH

fi
# Elvis Has Left The Server.
 
1 members found this post helpful.
Old 02-18-2015, 09:01 AM   #7
noobie143
LQ Newbie
 
Registered: Sep 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
This looks complicated. But thanks. I will read through it and get back when I run into trouble understanding something.
 
Old 02-18-2015, 09:06 AM   #8
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
It's a bit complicated, but what you're trying to do is a bit complicated as you're looking to ban whole countries at tcp level.

I would advise that you need to check and try some iptables tutorials before use as a badly configured iptables firewall could block you from having remote access to your server.

There is an existing thread on here http://www.linuxquestions.org/questi...hp-4175532605/ that covers the same thing.
 
Old 02-18-2015, 09:29 AM   #9
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Not to mention that using many "Deny from"s in an .htaccess is a huge resource waster.
Apache has to read that file for every file served. Whereas in a .conf file, it does not.
Firewall block keeps them from even wasting any further apache resources.

I use CSF (configserverfirewall) for country blocking.
 
Old 02-18-2015, 08:09 PM   #10
noobie143
LQ Newbie
 
Registered: Sep 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thanks once again. I have started reading tutorials.

The Syn Flood attacks are, as you said, at the OS level, so banning them from Apache by using .htaccess is not very useful. So why does restarting Apache make all the SYN connections go away?
 
Old 02-18-2015, 11:53 PM   #11
noobie143
LQ Newbie
 
Registered: Sep 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
I saw this in an iptables tutorial:

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

Syn-flood attack means that the attackers open a new connection, but do not state what they want (ie. SYN, ACK, whatever). They just want to take up our servers' resources. We won't accept such packages.

Will this help?
 
Old 02-19-2015, 12:37 AM   #12
noobie143
LQ Newbie
 
Registered: Sep 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
I posted a whole lot of lines like this covering Chinese ip ranges

iptables -A INPUT -s 1.0.1.0/24 -j DROP

into my /etc/sysconfig/iptables file

and did

service iptables restart

I also removed the .htaccess file I used earlier.

So far I can't see any SYN_RECV from China.
 
Old 02-19-2015, 02:54 AM   #13
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by noobie143 View Post
So why does restarting Apache make all the SYN connections go away?
Because it effectively "closes" port 80 and force closes any tcp connections to port 80.


Quote:
Originally Posted by noobie143 View Post
I posted a whole lot of lines like this covering Chinese ip ranges

iptables -A INPUT -s 1.0.1.0/24 -j DROP

into my /etc/sysconfig/iptables file

and did

service iptables restart

I also removed the .htaccess file I used earlier.

So far I can't see any SYN_RECV from China.

So what you've done is add a load of DROP rules to the INPUT chain. What my script does is similar. Mine downloads country IP ranges automatically, created a new chain that gets added to the "top" of the INPUT chain and only gets called for specific types of traffic (in my example only port 80) and also rejects the packet rather than drop it. Of course it would be very easy to change it to create DROP rules instead.


The reason I implemented my blocking in this way allows me to have my "static" iptables configuration and to generate the "BadCountry" chain dynamically. I can also run my script with the "undo" parameter to remove just the country blocking.


If you're happy with your method then that's the most important thing. Personally I'm lazy, I have my script run on a regular basis and don't need to manually create a lot of rules. ipdeny.com shows 5370 ip ranges for CN so manually adding that lot would take you a bit of time
 
Old 02-19-2015, 03:22 AM   #14
noobie143
LQ Newbie
 
Registered: Sep 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
My method did not work.

I copied the lines like iptables -A INPUT -s 223.0.0.0/12 -j DROP
to the /etc/sysconfig/iptables-config file

Apparently if there is a way of doing it by copy pasting, this is not the file to do it in
 
Old 02-19-2015, 03:58 AM   #15
noobie143
LQ Newbie
 
Registered: Sep 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
ip2location.com offers an iptables ban file. It has about 5000 lines each of which is like this:
iptables -A INPUT -s 1.0.1.0/24 -j DROP

I want to upload it to my server, read each line and execute it from the command prompt. I am asking for the simplest way because I do not want to execute bash scripts that I do not understand.
 
  


Reply



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
[SOLVED] Blocking class A IP Ranges with htaccess, some help please! codeman1234 Linux - Server 4 12-12-2014 05:25 PM
Apache - can I block some options in htaccess? nichu Linux - Server 0 08-30-2007 05:13 AM
Does .htaccess block search engine spiders? MicahCarrick Programming 2 08-24-2006 11:16 AM
IP Ranges Cottsay Linux - Networking 3 03-03-2006 11:45 PM
Ranges in OpenOffice CRego3D Linux - Software 0 06-19-2003 12:00 PM

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

All times are GMT -5. The time now is 07:55 PM.

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