首頁  >  文章  >  後端開發  >  php使用curl偽造IP來源的程式碼

php使用curl偽造IP來源的程式碼

WBOY
WBOY原創
2016-07-25 09:03:31784瀏覽
  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