前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >快速使用py构建自己的web渗透框架

快速使用py构建自己的web渗透框架

作者头像
糖果
发布2019-11-20 20:36:47
6110
发布2019-11-20 20:36:47
举报
文章被收录于专栏:糖果的实验室糖果的实验室

确定想要实现的功能

确定需求是最重要的事情,没有挨过打产品经理应该深有体会,挨过打的产品经理体会更深。作为一个想要自我实现某些琐碎功能代替手工的小菜,更要珍惜自己的时间。

常用库和自定义函数

可能用到的库包括但不限于requests、re、os、sys、importlib、time、string、random、socket、pybloom、ipy、tldextract、urllib、bs4、hashlib、selenium、argparse、threading、concurrent、multiprocessing、pymongo、pymysql、redis、celery、subprocess。

以及一些需要自己编写的函数等(以下示例函数可直接使用)。

例如:web字典生成

代码语言:javascript
复制
def urlbaklist(url):  # 接收带协议的域名,返回域名字典
    url1 = urlparse(url)
    url2 = tldextract.extract(url)
    if ":" in url1.netloc:
        domainname = url1.netloc.split(":")[0]
    else:
        domainname =url1.netloc
    domainname2 = url2.subdomain + url2.domain + url2.suffix
    domain = url2.domain + "." + url2.suffix
    domain2 = url2.domain + url2.suffix
    domain3 = url2.domain + "_" + url2.suffix  # baidu_com
    sub = url2.subdomain
    subdomain2 = url2.subdomain + url2.domain
    center = url2.domain
    domainlist=[domainname, domainname2, domain, domain2,domain3,subdomain2, center]
    if url2.subdomain:
        domainlist.append(url2.subdomain)
    domainlist = list(set(domainlist))
    return domainlist

网页编码设置

代码语言:javascript
复制
def getcharset(html):
    charset = "utf-8"
    m = re.compile('<meta .*(http-equiv="?Content-Type"?.*)?charset="?([a-zA-Z0-9_-]+)"?', re.I).search(html)
    if m and m.lastindex == 2:
        charset = m.group(2).lower()
    if charset=="" or charset==None:
        charset="utf-8"
    return charset

适当的webshell选择,当然也可以选择cmdshell,吃完饭回来重新编辑,里面的shell不见了,应该是被过滤掉了,心痛一秒钟,这样空着吧就。

代码语言:javascript
复制
def webshell(disable=None):
    #https://www.leavesongs.com/PENETRATION/php-callback-backdoor.html
    #https://bugs.leavesongs.com/%E8%BF%90%E7%BB%B4%E5%AE%89%E5%85%A8/lnmp%E8%99%9A%E6%8B%9F%E4%B8%BB%E6%9C%BAphp%E6%B2%99%E7%9B%92%E7%BB%95%E8%BF%87-%E5%91%BD%E4%BB%A4%E6%89%A7%E8%A1%8C/
    #https://blog.csdn.net/whatday/article/details/54880851
    #http://www.cnblogs.com/LittleHann/p/3522990.html
    #http://wooyun.jozxing.cc/static/bugs/wooyun-2015-0145879.html    "pcntl_exec" unix扩展,不通用,放弃
    # disable绕过
    #https://www.exehack.net/5158.html
    #http://www.cnblogs.com/R4v3n/articles/9081202.html
    #http://www.freebuf.com/articles/web/169156.html
    if disable==None:
        return ""
    elif "preg_replace" not in disable:
        return ""
    elif "create_function" not in disable:
        return ""
    elif "assert" not in disable:
        return ""
    elif "eval" not in disable:
        return ""
    elif "mb_ereg_replace" not in disable:
        return  ""
    elif "preg_filter" not in disable:
        return  ""
    else:
        return None

域名提取

代码语言:javascript
复制
def getdomain(line):#域名提取
    url=urlparse(line)
    if ":" in url.netloc:
        domain = url.netloc.split(":")[0]
    else:
        domain =url.netloc
    return domain

对入口进行统一管理

由于要调用的各种扫描器和各位大佬写的框架或小工具对参数的接收各不相同,所以需要自己动手,丰衣足食,并在需要的时候将url或ip转换为需要的格式。

调用开源扫描器

调用masscan,其他大佬的小工具同理

代码语言:javascript
复制
def mascportscan(iplist,port,scanthread):#ip列表,端口,线程
    portips1=time.strftime("%Y%m%d%H%M%S", time.localtime())+"port.txt"
    portscancommand="masscan --rate "+str(scanthread)+" -p"+str(port)+" -iL "+iplist+" -oL "+portips1
    portscan=subprocess.Popen(portscancommand,shell=True,stdout=subprocess.PIPE,universal_newlines=True)
    while True:
        nextline=portscan.stdout.readline()
        if nextline!="":
            print(nextline.strip())
        if nextline=="" and portscan.poll()!=None:
            break
    if portscan.poll():
        print(str(port)+"端口扫描完成!")
        portips2=time.strftime("%Y%m%d%H%M%S", time.localtime())+str(port)+"scanok.txt"#输出到文件夹备用
        if os.path.getsize(portips1)!=0:
            with open(portips1) as f:
                ls=f.readlines()
                ls.pop()
                ls.pop(0)
                ls=list(set(ls))
                print("开放"+str(port)+"端口的有"+str(len(ls))+"个!")
            with open(portips2,"a+")as f2:
                for i in ls:
                    ips=i.strip().split()
                    f2.write(ips[3]+"\n")
            return ls

EXP调用

代码语言:javascript
复制
def getExp(cms):
    exps = []
    try:
        for dirpath, _, filenames in os.walk("./exps/"+cms+"/",topdown=False):
            for filename in filenames:
                if '__init__' not in filename and ".pyc" not in filename and '.DS_Store' not in filename and ".txt" not in filename:
                    exps.append(filename)
        return exps
    except:
        return None

exp = getExp(name)
shotname, extension = os.path.splitext(i)
exprun = importlib.import_module("exps." + name + "." + shotname)
if "run_scan" in dir(exprun):
    vnl = exprun.run_scan(target)

注意:

在拼接文件路径的时候,需要使用os.path.dirname(__file__)+"具体路径或文件名" 的方式获取配置文件或临时文件路径,如果直接使用相对路径去拼接,在当前文件能够争议使用,但是被第三方调用的时候就会出错。

在python3下使用requests和urllib会自动修改开发者手动修改的Accept-Encoding,可以使用pycurl和urllib3。python2下可直接使用。

结果整合及分布式处理和入库

可对大量任务使用celery+redis构建分布式任务队列进行任务处理,尝试过使用Jpython+多进程,但仍有一些库不受支持,就此放弃。mongodb、或者mysql作为最终存储,涉及基本字段有:id,scan,cms,port,waf,title,vul(数组形式,漏洞位置及对应漏洞名称),time,waf,note(处理记录)等。

参考

https://docs.python.org/zh-cn/3.7/

http://cn.python-requests.org/zh_CN/latest/

https://github.com/jaybaird/python-bloomfilter.git

https://www.cnblogs.com/alex3714/p/6351797.html

https://www.jianshu.com/p/9e422d9f1ce2

本文章来自彼此分享,仅供白帽子、安全爱好者研究学习,对于用于非法途径的行为,发布者及作者不承担任何责任。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-10-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 糖果的实验室 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 确定想要实现的功能
  • 常用库和自定义函数
  • 对入口进行统一管理
  • 调用开源扫描器
  • EXP调用
  • 结果整合及分布式处理和入库
  • 参考
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档