翻译文章,原文:Writing a scanner to find reflected XSS vulnerabilities — Part 1[1]
代码地址:https://github.com/akhil-reni/xsstutorial , 可以直接看代码,感觉文章本身有点乱,看完才觉得好多地方说的很乱
2016年,我从事一个与burp suite非常相似的Web应用程序扫描程序项目,该项目代理来自浏览器或Selenium自动化工具的HTTP请求,并将它们发送到不同的模块/插件以进行漏洞扫描。我们构建的架构是相当模块化的,大多数应用程序都是用Python编写的,而前端则是使用Django和Celery编写的,用于异步任务。
我写这篇博客的目的是帮助安全工程师为自己或社区编写漏洞扫描程序。
在构建任何东西时,我们首先需要坚持基础知识并弄清楚以下几点:
•它如何运作? 创建一个简单的流程图来说明扫描器的工作流程,它可能是包括:需要什么输入,怎么分析这些数据,最终输出什么数据。
•使用什么技术实现? 选择合适的技术非常重要,在选择一种技术时,应当了解需要的功能库以及如何根据需要扩展他们。但最重要的是你熟悉它。如果我用20小时编写的GoLang代码和我用5小时编写的Python代码,在功能输出上仅仅是好一些,那么我会义无反顾的使用PYthon编写代码。
让我们开始吧,因为我之前的项目是使用Python,所以我会坚持下去。首先,我们先创建一个功能图。需要了解了解和如何标识他,请看文章:reflected cross-site scripting vulnerability[2]。
![c5c74bedd317153a85b72c2654b77d66.png](https://i-blog.csdnimg.cn/blog_migrate/a304e3cdb6b71a47b9ded5efd229bf32.jpeg)
它是什么工作的?
整个扫描器可以分成以下几个模块:
•原始HTTP请求解析器
•初始探测器
•上下文分析器
•Payload生成器
•Payload验证
首先创建每个模块,然后最后将它们整合在一起。
创建一个python virtualenv
pip3 install virtualenvpython3 -m virtualenv xss_env
激活virtualenv
cd xss_env/Scripts && activate
在virtualenv文件夹之外创建一个新文件夹
mkdir rxss
1 原始HTTP请求解析器
现在我们已经设置好环境,我们开始编写一些代码。第一个模块将是原始HTTP请求解析器,该解析器从文件中获取输入并转换为请求对象。为此,我们将使用python3中的现有http库
from __future__ import absolute_import, unicode_literalsfrom http.server import BaseHTTPRequestHandlerfrom io import BytesIOclass HTTPRequest(BaseHTTPRequestHandler): def __init__(self, request_text): self.rfile = BytesIO(request_text) self.raw_requestline = self.rfile.readline() self.error_code = self.error_message = None self.parse_request() def send_error(self, code, message): self.error_code = code self.error_message = message
上面的类接受原始的HTTP字符串并将其转换为请求对象。
POST /search.php?test=query HTTP/1.1Host: testphp.vulnweb.comUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Accept-Language: en-US,en;q=0.5Accept-Encoding: gzip, deflateContent-Type: application/x-www-form-urlencodedContent-Length: 27Origin: http://testphp.vulnweb.comConnection: closeReferer: http://testphp.vulnweb.com/search.php?test=queryUpgrade-Insecure-Requests: 1searchFor=asdas&goButton=go
将以上请求保存在request.txt中
from __future__ import absolute_import, unicode_literalsfrom http.server import BaseHTTPRequestHandlerfrom io import BytesIOclass HTTPRequest(BaseHTTPRequestHandler): def __init__(self, request_text): self.rfile = BytesIO(request_text) self.raw_requestline = self.rfile.readline() self.error_code = self.error_message = None self.parse_request() def send_error(self, co