Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Checking range of IP addresses

Reply
Thread Tools

Checking range of IP addresses

 
 
tim@roguevalleygroup.com
Guest
Posts: n/a
 
      07-23-2005
A Perl script I wrote for a CGI is getting spammed. So I'm identifying
and blocking ranges of IP addresses.

At first I used IF statements like this:

# 218.73.64.0 - 218.73.79.255 banned
@ip = split(/\./, $ENV{'REMOTE_ADDR'});
if ($ip[0] == 218 && $ip[1] == 73 && ($ip[2] >= 64 && $ip[2] <= 79)) {
$banned = 1;
}

I know that's probably horrible programming and not very elegant. But
it worked.

Now the list I want to block is getting longer. I could continue to add
IF statements but I feel that's probably stupid.

Any suggestions how I might try to more elegantly process a list of
banned IP's?

Thanks!
--Tim

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-23-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Any suggestions how I might try to more elegantly process a list of
> banned IP's?


I'd look at CPAN. Net::IP sounds promising, doesn't it?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-24-2005
(E-Mail Removed) wrote:
> A Perl script I wrote for a CGI is getting spammed. So I'm identifying
> and blocking ranges of IP addresses.
>
> At first I used IF statements like this:
>
> # 218.73.64.0 - 218.73.79.255 banned
> @ip = split(/\./, $ENV{'REMOTE_ADDR'});
> if ($ip[0] == 218 && $ip[1] == 73 && ($ip[2] >= 64 && $ip[2] <= 79)) {
> $banned = 1;
> }
>
> I know that's probably horrible programming and not very elegant. But
> it worked.
>
> Now the list I want to block is getting longer. I could continue to add
> IF statements but I feel that's probably stupid.
>
> Any suggestions how I might try to more elegantly process a list of
> banned IP's?


Taking the chance that also this idea is stupid, I couldn't help
reinventing the wheel:

my $ip = '218.73.72.100';
print "Banned\n" if banned($ip);

sub banned {
my $ip = shift;
while (<DATA>) {
if (/^(\S+)\s*-\s*(\S+)$/) {
my $begin = pack 'C4', split /\./, $1;
my $end = pack 'C4', split /\./, $2;
my $packedip = pack 'C4', split /\./, $ip;
return 1 if $packedip ge $begin and $packedip le $end;
} else {
chomp;
return 1 if $ip eq $_;
}
}
return 0;
}

__DATA__
60.70.80.90
218.73.64.0 - 218.73.79.255

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      07-24-2005
Gunnar Hjalmarsson wrote:

> Taking the chance that also this idea is stupid, I couldn't help
> reinventing the wheel:


Your wheel doesn't work.

> my $ip = '218.73.72.100';
> print "Banned\n" if banned($ip);

print "Still banned\n" if banned($ip);

The second call to banned() does not operate the same way
as the first call. Not good.

-Joe
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-24-2005
Joe Smith wrote:
> Gunnar Hjalmarsson wrote:
>> Taking the chance that also this idea is stupid, I couldn't help
>> reinventing the wheel:

>
> Your wheel doesn't work.
>
>> my $ip = '218.73.72.100';
>> print "Banned\n" if banned($ip);

>
> print "Still banned\n" if banned($ip);
>
> The second call to banned() does not operate the same way
> as the first call. Not good.


Well, I never claimed it would work for multiple calls. Whether one call
is sufficient depends on how it's supposed to be used, doesn't it? The
OP seemed to just need a way to decide if REMOTE_ADDR would get access
to a script, and there is only one REMOTE_ADDR ...

The main point with posting the code snippet was to call the OP's
attention to how the pack() function can be used.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
tim@roguevalleygroup.com
Guest
Posts: n/a
 
      07-25-2005
Gunnar Hjalmarsson wrote:

> Taking the chance that also this idea is stupid, I couldn't help
> reinventing the wheel:


Thank you for both of your replies. I'd not previously heard of Net::IP
and I'm studying it.

I also like your new wheel, even though it doesn't handle multiple
calls. I admit knowing nothing about this pack() function. I will study
your code until I understand it.

Thanks again!

--Tim

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-25-2005
(E-Mail Removed) wrote:
> I also like your new wheel, even though it doesn't handle multiple
> calls.


If you find the approach useful, a natural application of it is to store
and maintain the list of IP ranges in a separate file, and let the
banned() function open/close the file. Doing so would take care of 'the
multiple call issue' (even if I don't think that's an issue in your case).

> I admit knowing nothing about this pack() function. I will study
> your code until I understand it.


To be honest, I'm not too comfortable with it either, but I have learned
that it's useful for comparing and sorting IP addresses. Please feel
free to ask here if you get stuck.

> Thanks again!


You're welcome.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      07-25-2005
Gunnar Hjalmarsson <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> (E-Mail Removed) wrote:
> > A Perl script I wrote for a CGI is getting spammed. So I'm identifying
> > and blocking ranges of IP addresses.


[...]

> Taking the chance that also this idea is stupid, I couldn't help
> reinventing the wheel:
>
> my $ip = '218.73.72.100';
> print "Banned\n" if banned($ip);
>
> sub banned {
> my $ip = shift;
> while (<DATA>) {
> if (/^(\S+)\s*-\s*(\S+)$/) {
> my $begin = pack 'C4', split /\./, $1;
> my $end = pack 'C4', split /\./, $2;
> my $packedip = pack 'C4', split /\./, $ip;
> return 1 if $packedip ge $begin and $packedip le $end;

^^^^^^^^^^^
> } else {
> chomp;
> return 1 if $ip eq $_;

^^^^^^^^^^^
> }
> }
> return 0;
> }


Any reason why you don't just

return $packedip ge $begin and $packedip le $end;

and

return $ip eq $_;

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      07-25-2005
Joe Smith <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Gunnar Hjalmarsson wrote:
>
> > Taking the chance that also this idea is stupid, I couldn't help
> > reinventing the wheel:

>
> Your wheel doesn't work.
>
> > my $ip = '218.73.72.100';
> > print "Banned\n" if banned($ip);

> print "Still banned\n" if banned($ip);
>
> The second call to banned() does not operate the same way
> as the first call. Not good.


That's easily fixed. Change

sub banned {
my $ip = shift;
while (<DATA>) {
# ...
}
}

to

{
my @data;
sub banned {
@data = <DATA> unless @data;
my $ip = shift;
for ( @data ) {
# ...
}
}
}

Anno

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-25-2005
Anno Siegel wrote:
> Gunnar Hjalmarsson wrote:
>>
>> return 1 if $packedip ge $begin and $packedip le $end;

> ^^^^^^^^^^^
>> } else {
>> chomp;
>> return 1 if $ip eq $_;

> ^^^^^^^^^^^
>
> Any reason why you don't just
>
> return $packedip ge $begin and $packedip le $end;
>
> and
>
> return $ip eq $_;


No reason whatsoever.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to implement a firewall for Windows platform that blocks based on Mac addresses instead of IP addresses cagdas.gerede@gmail.com C Programming 1 12-07-2006 04:30 AM
Physical Addresses VS. Logical Addresses namespace1 C++ 3 11-29-2006 03:07 PM
PIX - overtaking '2 addresses on the same range' limit. AM Cisco 5 07-14-2005 06:29 PM
Generating and printing a range of ip addresses Stephen Briley Python 3 02-16-2004 02:01 AM
Range of addresses in ACL on PIX 515E Ian Cisco 2 01-28-2004 09:25 AM



Advertisments