Jump to content

banned ips flaw


adv

Recommended Posts

hello

i have the following question

i have a script that gets the user ip and if the ip is in the 'file' it redirects it to google

 


<?php
$ip=$_SERVER['REMOTE_ADDR'];
$file=file('bad_ips');
foreach($file as $files){
  if(stristr($files, $ip) === FALSE) {
   header('location:index1.php');
  }else {
header('location:http://google.com');
  }	
}
?>

 

in the file the ips are line by line

 

example:

2.2.2.2

4.4.4.4

 

but the problem is this

if the ip `2.2.2.2` enters i wnat to search in the file for just 2.2

if searches only if i put the entire ip in the `file`  2.2.2.2

 

 

 

Link to comment
Share on other sites

I don't see the correct logic in your code, it's checking each ip in the list, so only the first one checked ...it will take you to that header location.

 

try this

 

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$explode_ip = explode(".",$ip);
$iprange2 = $explode_ip[0] . "." . $explode_ip[1];
$iprange3 = $iprange2 . "." . $explode_ip[3];

$file=file('bad_ips');// no file extension?

if (in_array($ip, $file) || in_array($iprange2, $file) || in_array($iprange3, $file)) {
   header("location: http://www.google.com/");
} else {
   header('location:index1.php');
}

?>

 

I forgot to add.... just use ranges like 2.2 or 2.2.2 in your list

Link to comment
Share on other sites

changed it a little

 

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$explode_ip = explode(".",$ip);
$iprange2 = $explode_ip[0] . "." . $explode_ip[1];
$iprange3 = $iprange2 . "." . $explode_ip[3];

$data = file('bad_ips.txt');// no file extension?
foreach ($data as $line) {
$banned_ips[] = trim($line);
}

if (in_array($ip, $banned_ips) || in_array($iprange2, $banned_ips) || in_array($iprange3, $banned_ips)) {
   header("location: http://www.google.com/");
} else {
   header('location:index1.php');
}

?>

Link to comment
Share on other sites

I prefer working with IP addresses as numbers rather than strings, so the way I'd handle it would be to use a subnet mask with the IP address. Convert the address and mask with ip2long, and perform a bitwise AND to do the comparison. You could write it into a function to return a boolean TRUE/FALSE to determine whether to redirect or not, if you felt like it.

 

<?php
$ip = '2.2.2.2'; // address currently being checked
$ip = ip2long('2.2.2.2'); // convert to long int
$mask = ip2long('255.255.0.0'); // compare only first two octets of the IP address
$file = file('file.txt', FILE_IGNORE_NEW_LINES);  // read the file into an array
$file = array_map('ip2long', $file);  // convert values from file to long int

// Loop through the values, comparing the values using bitwise AND operation to compare IP address to banned list. If first 2 octets match, the header() redirect is sent.
foreach( $file as $v ) {
//var_dump($v); echo '<br>';
if( ($ip & $mask) === ($v & $mask) ) {
	//echo long2ip($ip) . " Matches " . long2ip($v) . '<br>';  // uncomment to see result
	header('Location: http://www.google.com');
	exit();
}
}

Link to comment
Share on other sites

good point, was trying to keep it simple

 

whichever way that works is good

 

but personally, I think it's better to block them at the front door through htaccess

with a code similar to this

## IP BANNING
<Limit GET POST>
order allow,deny
deny from 42.12.5.34
deny from 193.110.145.185
deny from 212.173.53.
deny from 69.242.
allow from all
</Limit>

 

 

Link to comment
Share on other sites

QuickOldCar  and if i use it like this

 

<Limit GET POST> 
order allow,deny 
deny from 69.
deny from 31.

allow from all
</Limit>

 

does it takes from only the start of the ip

i mean if the ip is 31.144.202.134 does it take it from 31. only

and not if the ip is 61.31.212.134

does it block the second ip beginig in 61.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.