【问题标题】:Do you know a simple php script to throw 403, after ip check?你知道一个简单的 php 脚本在 ip 检查后抛出 403 吗?
【发布时间】:2012-07-10 10:17:08
【问题描述】:

我有一个脚本,其中付款处理器带有付款确认。 为了确保页面安全,因为它可以访问订单信息和其他与用户相关的内容,我必须通过 ip(/24) 限制访问,如下所示:

$ipAllowed = array(
'192.192.192',
'172.172.172'
);
$ipAllowed = str_replace(".", "\.", implode("|", $ipAllowed));

if(!preg_match("/^($ipAllowed)\.[0-9]{1,3}$/", $_SERVER['REMOTE_ADDR'])){
     header('HTTP/1.0 403 Forbidden');
     die('You are not allowed to access this file.');
}

*ip只是一个例子

在我使用之前:

if(!in_array(@$_SERVER['REMOTE_ADDR'], array('ips here'))); //only works with full ip

!in_array 比我现在使用的要整洁得多,但我需要一些适用于 /24 ips 的东西,甚至两者都适用!

您是否知道一些工作得更好/更快、更可靠、更整洁的东西?

@rap-2-h 正如你所说,这是使用完整 ip、/24 甚至 /16 的更简洁的版本

$ipAllowed = array( '192.168.1.153' '172.172.172'); 
$allowed = false; 

foreach($ipAllowed as $ip): 
    if(strpos($_SERVER['REMOTE_ADDR'], $ip) === 0) $allowed = true; 
endforeach; 

if (!$allowed) { 
    header('HTTP/1.0 403 Forbidden'); 
    die('You are not allowed to access this file.'); 
}

【问题讨论】:

  • 我希望你也有其他的安全措施。 IP 作为纵深防御很好,但我会避免将其作为唯一衡量标准。
  • 是的!之后,整个方法开始计算 POST 参数并采取相应的行动(所有参数的散列也使用密码密钥发送)。我可以依靠它,但我想要安全。
  • "所有参数的散列也使用密码密钥发送" 密钥散列如何工作?是好东西,比如 HMAC,还是坏东西,比如 H(k||m)?
  • 除了这个ip检查部分都很好!

标签: php arrays security http-status-code-403


【解决方案1】:

你可以试试这样的:

$ipAllowed = array('192.192.192', '172.172.172');

$allowed = false;
foreach($ipAllowed as $ip) {
     if (strpos($_SERVER['REMOTE_ADDR'], $ip) !== false) {
         $allowed = true;
     }
}
if (!$allowed) {
    header('HTTP/1.0 403 Forbidden');
    die('You are not allowed to access this file.');     
}

所以您的$ipAllowed 数组中只能有 ip 片段。 它不是很优雅,但它应该可以工作......

【讨论】:

  • 使用 strpos 实际上比像我一样盲目地砍掉最后一个八位字节更好。
  • 这似乎很合适,但它也可以访问整个 ip 类,因为 172.0.0.0 也返回 true 或 0。不是吗?
  • 172.172.172.11.172.172.172 将允许使用此代码。如果你只想要172.172.172.X,你可以像这样替换条件if (strpos($_SERVER['REMOTE_ADDR'], $ip) === 0)
  • 根据您的回答,这是适用于所有类型 ips 的最终版本,而且相当简洁。谢谢!
【解决方案2】:

使用此功能检查您的 ip 是否在指定网络中:

例如:在网络 192.168.1.0/24 中是 192.168.1.25

<?php

/*
 * ip_in_range.php - Function to determine if an IP is located in a
 *                   specific range as specified via several alternative
 *                   formats.
 *
 * Network ranges can be specified as:
 * 1. Wildcard format:     1.2.3.*
 * 2. CIDR format:         1.2.3/24  OR  1.2.3.4/255.255.255.0
 * 3. Start-End IP format: 1.2.3.0-1.2.3.255
 *
 * Return value BOOLEAN : ip_in_range($ip, $range);
 *
 * Copyright 2008: Paul Gregg <pgregg@pgregg.com>
 * 10 January 2008
 * Version: 1.2
 *
 * Source website: http://www.pgregg.com/projects/php/ip_in_range/
 * Version 1.2
 *
 * This software is Donationware - if you feel you have benefited from
 * the use of this tool then please consider a donation. The value of
 * which is entirely left up to your discretion.
 * http://www.pgregg.com/donate/
 *
 * Please do not remove this header, or source attibution from this file.
 */


// decbin32
// In order to simplify working with IP addresses (in binary) and their
// netmasks, it is easier to ensure that the binary strings are padded
// with zeros out to 32 characters - IP addresses are 32 bit numbers
Function decbin32 ($dec) {
  return str_pad(decbin($dec), 32, '0', STR_PAD_LEFT);
}

// ip_in_range
// This function takes 2 arguments, an IP address and a "range" in several
// different formats.
// Network ranges can be specified as:
// 1. Wildcard format:     1.2.3.*
// 2. CIDR format:         1.2.3/24  OR  1.2.3.4/255.255.255.0
// 3. Start-End IP format: 1.2.3.0-1.2.3.255
// The function will return true if the supplied IP is within the range.
// Note little validation is done on the range inputs - it expects you to
// use one of the above 3 formats.
Function ip_in_range($ip, $range) {
  if (strpos($range, '/') !== false) {
    // $range is in IP/NETMASK format
    list($range, $netmask) = explode('/', $range, 2);
    if (strpos($netmask, '.') !== false) {
      // $netmask is a 255.255.0.0 format
      $netmask = str_replace('*', '0', $netmask);
      $netmask_dec = ip2long($netmask);
      return ( (ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec) );
    } else {
      // $netmask is a CIDR size block
      // fix the range argument
      $x = explode('.', $range);
      while(count($x)<4) $x[] = '0';
      list($a,$b,$c,$d) = $x;
      $range = sprintf("%u.%u.%u.%u", empty($a)?'0':$a, empty($b)?'0':$b,empty($c)?'0':$c,empty($d)?'0':$d);
      $range_dec = ip2long($range);
      $ip_dec = ip2long($ip);

      # Strategy 1 - Create the netmask with 'netmask' 1s and then fill it to 32 with 0s
      #$netmask_dec = bindec(str_pad('', $netmask, '1') . str_pad('', 32-$netmask, '0'));

      # Strategy 2 - Use math to create it
      $wildcard_dec = pow(2, (32-$netmask)) - 1;
      $netmask_dec = ~ $wildcard_dec;

      return (($ip_dec & $netmask_dec) == ($range_dec & $netmask_dec));
    }
  } else {
    // range might be 255.255.*.* or 1.2.3.0-1.2.3.255
    if (strpos($range, '*') !==false) { // a.b.*.* format
      // Just convert to A-B format by setting * to 0 for A and 255 for B
      $lower = str_replace('*', '0', $range);
      $upper = str_replace('*', '255', $range);
      $range = "$lower-$upper";
    }

    if (strpos($range, '-')!==false) { // A-B format
      list($lower, $upper) = explode('-', $range, 2);
      $lower_dec = (float)sprintf("%u",ip2long($lower));
      $upper_dec = (float)sprintf("%u",ip2long($upper));
      $ip_dec = (float)sprintf("%u",ip2long($ip));
      return ( ($ip_dec>=$lower_dec) && ($ip_dec<=$upper_dec) );
    }

    echo 'Range argument is not in 1.2.3.4/24 or 1.2.3.4/255.255.255.0 format';
    return false;
  }

}
?>

【讨论】:

  • 别忘了他说它们是/24 子网而不是完整的IP 地址。我认为您可能必须循环 ipAllowed 并根据 strpos 检查地址是否有效?
  • 不,因为“192.192.192”永远不会是 REMOTE_ADDR,因为 IP 地址包含四个八位字节。
  • 并注意 $_SERVER['REMOTE_ADDR'] 可能是假的,所以要小心这个以摆脱攻击
  • 据我所知,in_array 不会搜索干草堆的部分或针的部分,它必须匹配整个单词!没有?
  • 它可能有效,但我想要一些简单而整洁的东西,我不想对其进行基准测试。
【解决方案3】:
<?php

$ips = array(
    '192.160.0',
    '172.0.0'
);

/** 
 * Strip off the last number.
 */
$_SERVER['REMOTE_ADDR'] = '192.160.0.254';
$ip = preg_replace( '~\.(\d+)$~', '', $_SERVER['REMOTE_ADDR'] );

if( in_array( $ip, $ips ) ) {
    var_dump( 'allowed' );
}

【讨论】:

  • 是的!它做了它应该做的事情,但这正是我的问题,它应该更智能、更快地工作。由于 strpos 比 preg 更快,并且像我一样切碎最后一部分,在任何情况下都不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-08
  • 1970-01-01
  • 1970-01-01
  • 2014-05-20
  • 2014-09-25
  • 1970-01-01
相关资源
最近更新 更多