一个简单的XSS检测工具

检测的主要流程:
发送随机flag -> 确定参数回显 -> 确定回显位置以及情况(html,js语法解析)
-> 根据情况根据不同payload探测 -> 使用html,js语法解析确定是否多出来了标签,属性,js语句等等。

在拿到一个域名后开始深度检索存在的所有链接(href,src),在经过数据清洗后得到有用的页面(去除js等其他目录)

def SearchDir(TestUrl):
    header = {
        'User-Agent':
            'Mozilla / 5.0(Windows NT 10.0; Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 73.0.3683.75Safari / 537.36'
    }
    html = requests.get(TestUrl,header)
    print(html.text)
    _html = BeautifulSoup(html.text,'lxml')
    href_all = re.findall(r"href=\".+?\"", str(_html))
    src_all = re.findall(r"src=\".+?\"", str(_html))
    NoNeedDir = ['.js','.css','.jpg','.png','javascript','.gif','.bmp']
    for i in href_all:
        #判断是否为同一域名
        flag_http = 0
        if 'http://' in i :
            if TestUrl in i:
                flag_http=1
            else:
                continue
        i = i.replace('href=','')
        i = i.replace('"',"")
        flag = 0
        for p in NoNeedDir:
            if  p in i:
                flag=1
                break
        if flag==0:
            if flag_http == 1:
                if i in urlList:
                    continue
                urlList.append(i)
            else:
                url = TestUrl + i
                if url in urlList:
                    continue
                urlList.append(url)
    for i in src_all:
        flag_http = 0
        if 'http://' in i :
            if TestUrl in i:
                flag_http=1
            else:
                continue
        i = i.replace('src=', '')
        i = i.replace('"', "")
        flag = 0
        for p in NoNeedDir:
            if p in i:
                flag = 1
                break
        if flag == 0:
            if flag_http==1:
                urlList.append(i)
            else:
                urlList.append(TestUrl+i)
                #检测玩href与src中的目录后存入urlList列表中

下一步开始检测存在的参数。这里主要以两种形式。1.内置一些常见参数
这里是根据大佬的一篇博客里写的作为借鉴。
2.进行html分析。这里爬虫使用的是BeautifulSoup。(在确定回显的时候还是HTML语法树比较好用。)
在检测参数时顺序:
1).检测url里存在的参数
2).form标签里的参数 例如:提取input 标签里的name值(如果经过js处理后的是检测不出来的)
同时提取参数提交方式(get post)

blindParams = [  # common paramtere names to be bruteforced for parameter discovery
    'redirect', 'redir', 'url', 'link', 'goto', 'debug', '_debug', 'test', 'get', 'index', 'src', 'source', 'file',
    'frame', 'config', 'new', 'old', 'var', 'rurl', 'return_to', '_return', 'returl', 'last', 'text', 'load', 'email',
    'mail', 'user', 'username', 'password', 'pass', 'passwd', 'first_name', 'last_name', 'back', 'href', 'ref', 'data', 'input',
    'out', 'net', 'host', 'address', 'code', 'auth', 'userid', 'auth_token', 'token', 'error', 'keyword', 'key', 'q', 'query', 'aid',
    'bid', 'cid', 'did', 'eid', 'fid', 'gid', 'hid', 'iid', 'jid', 'kid', 'lid', 'mid', 'nid', 'oid', 'pid', 'qid', 'rid', 'sid',
    'tid', 'uid', 'vid', 'wid', 'xid', 'yid', 'zid', 'cal', 'country', 'x', 'y', 'topic', 'title', 'head', 'higher', 'lower', 'width',
    'height', 'add', 'result', 'log', 'demo', 'example', 'message']
#分别将提取的到参数存入字典中
def Collect_parameters(testUrl):

    #http://www.xylbh.cn/yzsp.aspx?w=83&code=100230
    html = requests.get(testUrl)
    '''
    检测html,input标签,提交数据如果相应在页面中,则构建payload
    '''
    _html = BeautifulSoup(html.text, 'lxml')
    all_form = _html.find_all('form')
    for i in all_form:
        #print(i)
        #print('-----------------------------')
        #method[0]是action
        #method[1]是method
        Action_method = checkMethod(str(i))
        if Action_method:
            pass
        else:
            continue
        #如果是get 显示在url里,如果是post显示在DATA里
        if Action_method[1]=="get":
            Parameter = checkparameter(str(i))
            # print(Action_method[0])
            # print(Parameter)
            onedic = {'action': Action_method[0], 'method': Action_method[1], 'params': Parameter, 'url': testUrl}
            #print(onedic)
            if onedic in AMDic_get:
                pass
            else:
                AMDic_get.append(onedic)
        elif Action_method[1]=='post':
            PostData = checkparameter(str(i))
            # print(Action_method[1])
            # print(Action_method[0])
            # print(PostData)
            onedic={'action':Action_method[0],'method':Action_method[1],'params':PostData,'url':testUrl}
            if onedic in AMDic_post:
                pass
            else:
                AMDic_post.append(onedic)

确定回显:

class myHTMLParser(HTMLParser):
    def __init__(self,flag):
        HTMLParser.__init__(self)
        self.startag = ''
        self.endtag = ''
        self.falg = flag

        self.loaction = []
    def handle_decl(self, decl):
        HTMLParser.handle_decl(self, decl)

    def handle_starttag(self, tag, attrs):
        #由于检测时会出现误差,这里使用html树和BeautifulSoup一起检测提高准确率
        HTMLParser.handle_starttag(self, tag, attrs)
        self.startag=tag
        flagtag = ''
        if attrs:
            flagtag+='<'+tag
            #print(attrs)
            for i in attrs:
                for p in i:
                    try:
                        flagtag+=p
                    except:
                        pass
            if self.falg in flagtag:
                print(flagtag)
                self.loaction.append('intag')
    def handle_endtag(self, tag):
        HTMLParser.handle_endtag(self, tag)
        #print('</' + tag + '>')
        self.endtag = tag
    def handle_data(self, data):
        HTMLParser.handle_data(self, data)
        if self.falg in data:
            # print(self.startag)
            # print(data)
            if 'script' in self.startag:
                self.loaction.append('script')
            else:
                self.loaction.append('outtag')
        # 下面的代码是处理类似于<br/>这样的没有闭合的标签
    def handle_startendtag(self, tag, attrs):
        HTMLParser.handle_startendtag(self, tag, attrs)
    def handle_comment(self, data):
        #在注释中的内容
        HTMLParser.handle_comment(self, data)
    def close(self):
        HTMLParser.close(self)
        return self.loaction

文笔不好这里附上学习的博客:
https://paper.seebug.org/1119/
准确率还是比较客观的。不过只有又简单的检测并没有payload,后续还需要手工进行确认。
附上一下检测结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章