Skip to contentSkip to conversation

Blocking Netranges Based on IP-to-Country CSV

DISCUSSION Gary & Mic McFall 2 participants 2 messages Sep 15, 2004 – Sep 16, 2004

lore
Share & tools
lore
#0 lore raw

Due to a number of issues, my organization is reviewing the ability to block certain country domains at the firewall. To be proactive, we want to automate that process via iptables & the CSV available at
<http://ip-to-country.webhosting.info>.

We have created a C program (IPCheck) in the /bin directory which, when given an IP long number, checks the ip-to-country CSV and returns a value for permit or not. We want to process the packet based on that return.

I'm a relative newbie with iptables & scripts. Listed below is some very crude code, some of which is a verbal description of intent. Any suggestions on how to shore this up so that it will work? Or should this concept be a new module in iptables?

#
# ***** DROP BAD IPS *****
#
/bin/IPCheck grep -w "SRC =" | cut -d = -f 2
if return = "Bad IP"
$IPT -A INPUT -$ETH0 $RATELIMIT -j INBADIPS

      ***** RULES - DROP  BAD IPS *****

# ***** REVIEW LOG AT /var/log/iptables.log *****
#
$IPT -A INBADIPS -j LOG --log-level debug --log-prefix "BADIP: " -j DROP

Thanks in advance for your help.

Gary McFall

#1 lore raw

Hello,

* Gary & Mic McFall <[email protected]> 15. Sep 04:

Quoted 2 lines

To be proactive, we want to automate that process via iptables & the CSV available at <http://ip-to-country.webhosting.info>.

I am very suspicious to configure packet filters with external (untrusted) data.

Quoted 1 line

We have created a C program (IPCheck) in the /bin directory

Maybe it should go to /usr/local/bin/... anyway.

Quoted 1 line

We want to process the packet based on that return.

Make it to return 0 for bad and !0 for 'good' IP#s.

Quoted 1 line

I'm a relative newbie with iptables & scripts.

<URL:http://iptables-tutorial.frozentux.net/iptables-tutorial.html>

Quoted 1 line

Listed below is some very crude code,

Aah... yes.

Quoted 1 line

Any suggestions on how to shore this up so that it will work?

# You need to create the userdefined chain bevor using it:
$IPT -N INBADIPS

Quoted 2 lines

# ***** DROP BAD IPS *****
/bin/IPCheck grep -w "SRC =" | cut -d = -f 2

# What are you going to use as input? This grep needs a source to

# parse.  Will it result in a list of IP#s?  I assume a single $IP.
Quoted 2 lines

if return = "Bad IP"
$IPT -A INPUT -$ETH0 $RATELIMIT -j INBADIPS

# This will not work. Use:
if /usr/local/bin/IPCheck $IP
then
$IPT -A INPUT -i eth0 -s $IP -j INBADIPS
fi
# What do you mean with $RATELIMIT? Are you looking for
# <URL:http://iptables-tutorial.frozentux.net/iptables-tutorial.html#TABLE.LIMITMATCH>?

Quoted 3 lines
      ***** RULES - DROP  BAD IPS *****

# ***** REVIEW LOG AT /var/log/iptables.log *****
$IPT -A INBADIPS -j LOG --log-level debug --log-prefix "BADIP: " -j DROP

# You can't have to targets in a rule. Split it:
$IPT -A INBADIPS -j LOG --log-level debug --log-prefix="BADIP: " $IPT -A INBADIPS -j DROP

Some questions you should ask yourself:
+ Is this a gateway? Should it go to FORWARD?
+ Will IP#s stay for ever at blacklist? How and when to remove them?
+ Is this really what you want?

Hope I understood you,
regards, Frank.

Signature · 1 line
Sigmentation fault