python 云防护接入判断

python- 云防护接入判断

  • 原理:云防护的判断主要通过判断cname特征名,如接入创宇盾,cname中会有365cyd这串字符,如接入玄武盾,cname中则会有dbapp;cname和ip的获取主要通过解析模块。
    1. 代码
    2. 输入
    3. 输出

代码

  • 代码如下
#2018.1.12 by AlickLin

import socket
import dns.resolver

def url2domain(url): #将url处理一下,成规则的域名
    if("http" in url):
        url = url[7:]
    elif("https" in url):
        url = url[8:]
    if("/" in url):
        url = url.split("/")[0]
    return url

def domain2ip(domain): #将域名解析成ip
    try:
        ip = socket.gethostbyname(domain)
    except:
        ip = "解析错误"
    return ip

def domain2cname(domain): #将域名解析出cname
    try:
        CNAME = dns.resolver.query(domain,'CNAME')
        for i in CNAME.response.answer:
            for j in i.items:
                cname = j.to_text()
    except:
        cname = "未解析"
    return cname

def wafjudge(cname): #判断模块,判断云防护接入情况,此处可以根据各类waf的别名特征进行更改扩容
    if("365cyd" in cname):
        waf = "创宇盾"
    elif("ali" in cname):
        waf = "阿里云防护"
    elif("dbapp" in cname):
        waf = "安恒玄武盾"
    elif("未解析" in cname):
        waf = "未接入云防护"
    else:
        waf = "其他云防护"
    return waf
#main
urllist=open("url.txt","r")
waflist=open("waf.txt","w")
iplist =open("ip.txt","w")
cnamelist = open("cname.txt","w")
domainlist = open("domain.txt","w")
for oneurl in urllist.readlines():
    oneurl = oneurl.replace("\n","")
    oneurl = oneurl.replace(" ","")
    domain = url2domain(oneurl)
    ip = domain2ip(domain)
    cname = domain2cname(domain)
    waf = wafjudge(cname)
    waflist.write(waf+"\n")
    iplist.write(ip+"\n")
    cnamelist.write(cname+"\n")
    domainlist.write(domain+"\n")
urllist.close()
waflist.close()
iplist.close()
cnamelist.close()
domainlist.close()

输入

  • 输入是一行一个url 的txt文件,以下是文件内容例子
http://www.kq.gov.cn/
rsj.kq.gov.cn
http://www.shaoxingsx.jcy.gov.cn/
http://www.sxxjsj.gov.cn/
http://www.kqqcjda.cn/

输出

  1. ip.txt
220.191.226.18
220.191.226.18
223.202.67.54
60.190.216.117
183.61.0.169
  1. domain.txt
www.kq.gov.cn
rsj.kq.gov.cn
www.shaoxingsx.jcy.gov.cn
www.sxxjsj.gov.cn
www.kqqcjda.cn
  1. cname.txt
未解析
未解析
未解析
未解析
未解析
  1. waf.txt
未接入云防护
未接入云防护
未接入云防护
未接入云防护
未接入云防护

你可能感兴趣的:(python)