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 02-03-2012, 03:33 AM   #1
tehwaffle
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Rep: Reputation: Disabled
IPTables + Cloudflare


Hello,

I'm a big newb when it comes to Linux and IPTables.
However, I need help with the following:

I use Cloudflare for my site and want to make sure that only IP's of Cloudflare can access my site.
So that means any traffic is not allowed to visit my site directly by typing in the IP of my site in the browser. (It's a VPS.)

However, I have no clue how to do this with IPTables.
To say it in short: only allow certain IP's to port 80 and drop/block the other IP's.

The IP's of Cloudflare are (only these IP's are allowed to access my site):
Code:
204.93.240.0/24 (204.93.240.0 - 204.93.240.255)
204.93.177.0/24 (204.93.177.0 - 204.93.177.255)
199.27.128.0/21 (199.27.128.0 - 199.27.135.255)
173.245.48.0/20 (173.245.48.0 - 173.245.63.255)
103.22.200.0/22 (103.22.200.0 - 103.22.203.255)
141.101.64.0/18 (141.101.64.0 - 141.101.127.255)
108.162.192.0/18  (108.162.192.0 - 108.162.255.255)
2400:CB00:/32 (2400:CB00:0000:0000 - 2400:CB00:FFFF:FFFF)
2606:4700:/32(2606:4700:0000:0000 - 2606::4700:FFFF:FFFF)
Could anyone tell me how to do this in IPTables? And how to un-do it?
I'm willing to contribute a donation to linuxquestions.org if someone can help me with this.

Thanks.
 
Old 02-03-2012, 04:07 AM   #2
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 193Reputation: 193
It is relatively easy.
Code:
for i in {"204.93.240.0/24", "204.93.177.0","199.27.128.0/21" etc etc}; do
    iptables -A INPUT -p tcp --dport 80 -s $i -J ACCEPT
done

iptables -A INPUT -p tcp --dport 80 -j DROP
the for loop creates a rule that will accept connections on port 80, from the subnets listed.
The next rule, will drop any connections on port 80, that dont match the above rules.
 
Old 02-03-2012, 04:10 AM   #3
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 193Reputation: 193
I should add, to undo it, I would probably have a script that loaded a set of rules depending if you wanted it restricted or not. This could be run by cron for example.

The script would basically just change the drop rule, to an accept rule, allowing port 80 to any source IP

You would apply the same rules, to ip6tables for your IPv6 addresses..

Last edited by fukawi1; 02-03-2012 at 04:11 AM.
 
  


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
iptables can't initialize iptables table `filter': Bad file descriptor donalbane Linux - Networking 2 08-17-2011 08:36 AM
iptables error in android: iptables-save and iptables-restore not working preetb123 Linux - Mobile 5 04-11-2011 01:56 PM
On what basis CHAIN integer values are generated in IPtables under iptables file? haariseshu Linux - Server 3 11-05-2009 04:25 AM
iptables v1.2.9: Unknown arg `/sbin/iptables' Try `iptables -h' or 'iptables --help' Niceman2005 Linux - Security 4 12-29-2005 08:20 PM
IPtables Log Analyzer from http://www.gege.org/iptables/ brainlego Linux - Software 0 08-11-2003 06:08 AM

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

All times are GMT -5. The time now is 12:30 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