HTTP-REFERER这个变量已经越来越不可靠了,完全就是可以伪造出来的东东。
1.php代码:
代码如下
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/2.php");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:8.8.8.8', 'CLIENT-IP:8.8.8.8')); //构造IP
curl_setopt($ch, CURLOPT_REFERER, "http://www.google.com/ "); //构造来路
curl_setopt($ch, CURLOPT_HEADER, 1);
$out = curl_exec($ch);
curl_close($ch);
2.php
代码如下
function getClientIp() {
if (!empty($_SERVER["HTTP_CLIENT_IP"]))
$ip = $_SERVER["HTTP_CLIENT_IP"];
else if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
else if (!empty($_SERVER["REMOTE_ADDR"]))
$ip = $_SERVER["REMOTE_ADDR"];
else
$ip = "err";
return $ip;
}
echo "IP: " . getClientIp() . "";
echo "referer: " . $_SERVER["HTTP_REFERER"];
伪造成功,这是不是给“刷票”的朋友提供了很好的换IP的方案!!
再看一下相对复杂的处理办法
以下是伪造方法:
代码如下
PHP(前提是装了curl):
PHP代码
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, "http://www.hzhuti.com");
curl_setopt ($ch, CURLOPT_REFERER, "http://www.zhutiai.com/");
curl_exec ($ch);
curl_close ($ch);
PHP(不装curl用sock)
代码如下
$server = '127.0.0.1';
$host = '127.0.0.1';
$target = '/xxx.asp';
$referer = 'http://127.0.0.1/'; // Referer
$port = 80;
$fp = fsockopen($server, $port, $errno, $errstr, 30);
if (!$fp)
{
echo "$errstr ($errno)<br />n";
}
else
{
$out = "GET $target HTTP/1.1rn";
$out .= "Host: $hostrn";
$out .= "Cookie: ASPSESSIONIDSQTBQSDA=DFCAPKLBBFICDAFMHNKIGKEGrn";
$out .= "Referer: $refererrn";
$out .= "Connection: Closernrn";
fwrite($fp, $out);
while (!feof($fp))
{
echo fgets($fp, 128);
}
fclose($fp);
}
IP也可以伪造
代码如下
$fp = fsockopen ("192.168.0.128", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br>n";
} else {
$msg="GET /003.php HTTP/1.0rn";
$msg.="Host: 192.168.0.128rn";
$msg.="Referer: http://www.baidu.comrn";
$msg.="Client-IP: 1.1.1.1rn";
$msg.="X-Forwarded-For: 1.1.1.1rn"; //主要是这里来构造IP
$msg.="Connection: Closernrn";
fputs ($fp, $msg);
while (!feof($fp)) {
echo fgets ($fp,1024);
}
fclose ($fp);
}
记住用fsockopen时,命令最后一定要有rnrn,不然会超时。
其实这个可以用$_SERVER['REMOTE_ADDR']来得到正确IP,但是人们为了得到代理访问IP,而采用的得到IP的方法往往不是$_SERVER['REMOTE_ADDR'],这就为我们提供了机会。