首页  >  文章  >  后端开发  >  php使用curl伪造IP来源的代码

php使用curl伪造IP来源的代码

WBOY
WBOY原创
2016-07-25 09:03:31785浏览
  1. $ch = curl_init();
  2. $url = "http://localhost/target_ip.php";
  3. $header = array(
  4. 'CLIENT-IP:58.68.44.61',
  5. 'X-FORWARDED-FOR:58.68.44.61',
  6. );
  7. curl_setopt($ch, CURLOPT_URL, $url);
  8. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  9. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  10. $page_content = curl_exec($ch);
  11. curl_close($ch);
  12. echo $page_content;
  13. ?>
复制代码

请求的目标文件---target_ip.php:

  1. echo getenv('HTTP_CLIENT_IP');
  2. echo getenv('HTTP_X_FORWARDED_FOR');
  3. echo getenv('REMOTE_ADDR');
  4. ?>
复制代码

目标文件target_ip里面的IP打印顺序是目前很多开源系统的IP获取顺序 访问fake_ip.php,看到结果: 58.68.44.61 58.68.44.61 127.0.0.1

最后,为大家提供二个小例子。 例1:

  1. $ch = curl_init();
  2. curl_setopt($ch, CURLOPT_URL, "http://localhost/2.php");
  3. curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:8.8.8.8', 'CLIENT-IP:8.8.8.8')); //构造IP
  4. curl_setopt($ch, CURLOPT_REFERER, "http://bbs.it-home.org/ "); //构造来路
  5. curl_setopt($ch, CURLOPT_HEADER, 1);
  6. $out = curl_exec($ch);
  7. curl_close($ch);
  8. ?>
复制代码

例2:

  1. function getClientIp() {
  2. if (!empty($_SERVER["HTTP_CLIENT_IP"]))
  3. $ip = $_SERVER["HTTP_CLIENT_IP"];
  4. else if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
  5. $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
  6. else if (!empty($_SERVER["REMOTE_ADDR"]))
  7. $ip = $_SERVER["REMOTE_ADDR"];
  8. else
  9. $ip = "err";
  10. return $ip;
  11. }
  12. echo "IP: " . getClientIp() . "";
  13. echo "referer: " . $_SERVER["HTTP_REFERER"];
  14. ?>
复制代码


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn