当前位置:Gxlcms > PHP教程 > php使用curl伪造IP来源的代码

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

时间:2021-07-01 10:21:17 帮助过:16人阅读

  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. ?>

人气教程排行