欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-04-28 18:16:30
...
  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. ?>
复制代码