170d1 Block traffic based on GeoIP - The FreeBSD Forums
The FreeBSD Forums  

Go Back   The FreeBSD Forums > Server & Networking > Firewalls

Firewalls IPFW, PF, IPF (but not limited) related discussion

Reply
 
Thread Tools Display Modes
  #1  
Old January 7th, 2009, 00:28
UltraLinuz UltraLinuz is offline
Junior Member
 
Join Date: Jan 2009
Posts: 2
Thanks: 3
Thanked 0 Times in 0 Posts
Default Block traffic based on GeoIP

I would like to add a rule to my pfi.conf that blocks any incoming traffic based on the geografic location of the IP-address.

for example I would only allow IP addresses from France to access my apache server. All other trafic should be blocked.

As it looks to me one can do pretty advanced thing in pf but I am a complete newbee to FreeBSD. It looks to me that one somehow needs to use GeoIP and use geoiplookup to determine the origin of the IP address.

Is this somehow possible?
Reply With Quote
  #2  
Old January 7th, 2009, 01:19
danger@'s Avatar
danger@ danger@ is offline
FreeBSD Developer
 
Join Date: Oct 2007
Location: Bratislava, Slovakia
Posts: 774
Thanks: 4
Thanked 165 Times in 84 Posts
Default

I doubt this is possible to do with any firewall package available in FreeBSD. One thing that comes to my mind is to do a geoip based dns routing, i.e. you route DNS queries from France to the real web server, and others, say, to 127.0.0.1...

I can provide you with a patch for bind to do that.
__________________
Looking for administrator? http://www.syscare.sk
Reply With Quote
The Following User Says Thank You to danger@ For This Useful Post:
UltraLinuz (January 8th, 2009)
  #3  
Old January 7th, 2009, 05:48
Djn Djn is offline
Member
 
Join Date: Nov 2008
Location: Horten, Norway
Posts: 392
Thanks: 3
Thanked 62 Times in 53 Posts
Default

Alternatively, you could set up a named list of blocked networks, and then use pfctl to populate it from a cron job?
(Assuming there's some way to pick out the networks representing the relevant countries.)
Reply With Quote
  #4  
Old January 7th, 2009, 18:37
anomie's Avatar
anomie anomie is offline
Member
 
Join Date: Nov 2008
Location: Texas
Posts: 783
Thanks: 49
Thanked 113 Times in 100 Posts
Default

Well, you could use the per-country IP network lists: http://www.ipdeny.com/ipblocks/

As a security measure I'm not sure how effective this will be against a determined attacker. He can simply stage an attack from inside one of the (many) allowed networks.
Reply With Quote
The Following 3 Users Say Thank You to anomie For This Useful Post:
jjthomas (January 13th, 2010), Ruler2112 (January 13th, 2010), UltraLinuz (January 8th, 2009)
  #5  
Old January 7th, 2009, 23:50
Djn Djn is offline
Member
 
Join Date: Nov 2008
Location: Horten, Norway
Posts: 392
Thanks: 3
Thanked 62 Times in 53 Posts
Default

True, but there are large botnets or something operating from a few specific countries - blocking them will remove quite a lot of the log noise.
Reply With Quote
The Following User Says Thank You to Djn For This Useful Post:
UltraLinuz (January 8th, 2009)
  #6  
Old January 8th, 2009, 01:01
UltraLinuz UltraLinuz is offline
Junior Member
 
Join Date: Jan 2009
Posts: 2
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Hehehe! This looks great!

It's a little bit of a hassle handeling the files but it will work. I wasn't aware of ipdeny.com but it will basically result in the same, most likely even with better performance.

And indeed it's not a very reliable protection mechanism, but I'd like to get rid of all this spam and intrision attempts from these few countries I really never visit, mail nor offer any services to.
Reply With Quote
  #7  
Old January 12th, 2009, 00:23
Maurovale Maurovale is offline
Junior Member
 
Join Date: Nov 2008
Location: Portugal
Posts: 25
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Hi there is a Mod of geoip that you can use with apache (http://www.maxmind.com/app/mod_geoip).

But also as already been said you can you the IPs from ipdeny (http://www.ipdeny.com/) and use it directly with pf firewall or make a script to load the rules in your firewall in case you use ipfw.

Best Regards
Reply With Quote
  #8  
Old January 12th, 2010, 22:14
Excalibur Excalibur is offline
Junior Member
 
Join Date: Jan 2010
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Well, using the ipdeny blocks seems to be a good idea. But won't that slow down things a bit? If you're blocking like 25 countries from a server like I'm doing with IPTables&GeoIP on RHEL, that would be crazy because of how many IP-blocks you need to add.

I'm not sure what is the best method to block 25+ countries w/o slowing down things like crazy.

Additionally, Apache's GeoIP isn't really that helpful, it doesn't prevent access to the server itself from blocked hosts.

Ideas/Suggestions?
Reply With Quote
  #9  
Old January 13th, 2010, 22:23
Ruler2112's Avatar
Ruler2112 Ruler2112 is offline
Member
 
Join Date: Sep 2009
Location: Michigan, USA
Posts: 408
Thanks: 11
Thanked 23 Times in 21 Posts
Default

I created a script to block IPs listed in the EmergingThreats.org list, updating both the list and firewall from the nightly cron job. It would be very simple to run a separate copy from cron for each country you wish to block and thereby have an updated list and firewall for each.

There's also some discussion on the efficiency and memory usage of having huge tables of IPs to block in that thread. Basically, pf sorts the IPs when it adds them to a table so lookups are very fast and the amount of memory consumed is inconsequential.

That ipdeny site is great - I never knew such a thing existed before!
Reply With Quote
The Following User Says Thank You to Ruler2112 For This Useful Post:
Excalibur (January 13th, 2010)
  #10  
Old January 13th, 2010, 22:34
Excalibur Excalibur is offline
Junior Member
 
Join Date: Jan 2010
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I'm sorry, as I'm really new to FreeBSD, does this work with IPFW as well?

Quote:
Originally Posted by Ruler2112 View Post
I created a script to block IPs listed in the EmergingThreats.org list, updating both the list and firewall from the nightly cron job. It would be very simple to run a separate copy from cron for each country you wish to block and thereby have an updated list and firewall for each.

There's also some discussion on the efficiency and memory usage of having huge tables of IPs to block in that thread. Basically, pf sorts the IPs when it adds them to a table so lookups are very fast and the amount of memory consumed is inconsequential.

That ipdeny site is great - I never knew such a thing existed before!
__________________
Excalibur
Reply With Quote
  #11  
Old January 14th, 2010, 11:25
ctaranotte ctaranotte is offline
Junior Member
 
Join Date: Nov 2008
Posts: 85
Thanks: 3
Thanked 16 Times in 9 Posts
Default

I have found GEOIP more accurate than IPDENY (purely subjective).

mod_geoip is 100% working, maybe you should check your httpd.conf

Anyway, if you want to go the pf + GeoIP route:

1) jail httpd (man jail for more info)
2) look at net/tableutil in ports
3) visit tableutil home
4) look at the example of script handling peerguardian blocklists (GeoIP lists share the same format)
5) between
Code:
gunzip -c - | sed "s/.*:\([0-9.-]\)/\1/" | \
and
Code:
tableutil -q text 2> /dev/null > /tmp/blocklist
insert some grep instructions to keep only the countries of interests.

Last words, in this script, the pf table is in etc/pfdata/blocklist.

Good luck,

Last edited by ctaranotte; January 14th, 2010 at 11:40.
Reply With Quote
  #12  
Old January 14th, 2010, 17:46
aragon aragon is offline
Giant Locked
 
Join Date: Nov 2008
Location: Cape Town, South Africa
Posts: 2,031
Thanks: 68
Thanked 253 Times in 203 Posts
Default

You could do it in real time asynchronously with pflogd and a userland app. It would work something like this:

1. Setup a pf rule set with two rules that log: (a) block rule that blocks source addresses that are added to a pf IP table, (b) accept rule that matches the first packet of every connection that isn't blocked above.

2. Write a daemon that tails pflogd's log file and does a GeoIP lookup on each block and accept log entry. If the IP is french, remove it from the blocking IP table. If it is non-french, add it to the blocking IP table.
Reply With Quote
  #13  
Old January 14th, 2010, 23:40
Ruler2112's Avatar
Ruler2112 Ruler2112 is offline
Member
 
Join Date: Sep 2009
Location: Michigan, USA
Posts: 408
Thanks: 11
Thanked 23 Times in 21 Posts
Default

Quote:
Originally Posted by Excalibur View Post
I'm sorry, as I'm really new to FreeBSD, does this work with IPFW as well?
The script basically handles the downloading and management of old versions of the file you download, then runs a command to refresh the firewall table. So the script will, but I don't know how to do the ipfw portion of it. You'd have to figure out how to create a table in the firewall, then set the correct variable in the script to whatever shell command updates the firewall table.
Reply With Quote
  #14  
Old March 12th, 2010, 10:16
sniper007's Avatar
sniper007 sniper007 is offline
Member
 
Join Date: Nov 2008
Location: Slovenia
Posts: 110
Thanks: 8
Thanked 2 Times in 2 Posts
Default

I figure out this site
http://www.ipaddresslocation.org/ip_...get_ranges.php

is more more accurate (for my country) than ipdeny.

The darkside of this page is output because don't create pure txt file with IP blocks, which is very useful for writing script (for updating)
__________________
If anything can go wrong, it will. If it can't, it will anyway
Reply With Quote
Reply

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Routing all traffic through a VPN tunnel telecommand Networking 4 December 18th, 2011 18:11
Your favorite text based application bsddaemon Off-Topic 87 August 19th, 2010 18:19
[Solved] Best way for block ssh attack without Firewall mfaridi Web & Network Services 16 July 6th, 2010 09:54
Licensed based Forum software ? senouf Off-Topic 21 April 30th, 2009 19:33
port based traffic redirection loko Firewalls 2 November 20th, 2008 14:47


All times are GMT +1. The time now is 06:17.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
The mark FreeBSD is a registered trademark of The FreeBSD Foundation and is used by The FreeBSD Project with the permission of The FreeBSD Foundation.
Web protection and acceleration provided by CloudFlare
0