WEB-RPO攻击
RPO攻击
RPO(Relative Path Overwrite) —— 相对路径覆盖,是一种新型攻击技术,主要是利用浏览器的一些特性和部分服务端的配置差异导致的漏洞,通过一些技巧,我们可以通过相对路径来引入其他的资源文件,以至于达成我们想要的目的。
RPO利用条件以及利用结果
RPO攻击依赖于
- 浏览器与服务器对于url地址解析反应的不同。这种反应的不同可以由 两者解析差异,WEB缓存技术和配置差异 造成
- 前端代码使用相对于本页目录的相对路径来加载css/js
../css/style.css
或./style.js
利用结果:
浏览器把服务器返回的其他文件当做css/js来解析。通常单单RPO是没有什么作用的,因为服务器不会有什么恶意的css/js存在。
但是配合上一些可以控制的文件(比如可以控制php页面返回自定义css代码,或js代码)。控制服务端使其返回恶意文件+RPO形成解析,导致了XSS,信息泄露等漏洞产生。
浏览器与服务端的差异
先了解关于服务端和客户端浏览器在解析和识别上的差异性,以Apache服务器和Nginx服务器为例。
ps.测试代码取自p00mj博客
服务器对于URL的解析
对于%2f的解析
对于Apache服务器:
访问http://localhost/RPO/index.php
,成功

对于/
URL编码%2f
访问http://localhost/RPO%2findex.php
,失败

对于Nginx服务器:
访问http://localhost/RPO/index.php
,成功

访问http://localhost/RPO%2findex.php
,成功

所以
- Nginx web服务器是会对带有%2f的URL解析的
- Apache服务器时不能对带有%2f的URL进行解析的
对于后续无意义目录的解析
两种web服务器结果一致:
访问http://localhost/1.php
,成功

访问http://localhost/1.php/abc/ced.js/style.css
,成功,并且返回时跟1.php一样的界面

由此可知:
服务器在找到有内容的页面后,不会解析后续没有意义的目录
客户端浏览器对于URL的解析
由于需要通过服务器的返回才能判断浏览器对于URL编码的解析,此处使用Nginx服务器。
访问http://localhost/RPO/aa%2fbb/index.php
,css获取../style.css

浏览器请求的页面:http://localhost/RPO/aa%2fbb/index.php
浏览器”应该”请求的CSS:http://localhost/RPO/aa/index.php
浏览器实际请求的css:http://localhost/RPO/style.css
由此可知:
浏览器是不会识别%2f作为路径分割的
利用场景-pathinfho
- 服务端:能不能对带有%2f的URL解析的WEB服务器都行(此例子是apache服务器)
- 客户端:所以对于客户端也没有要求.
- css获取:通过相对路径获取(此例子中
href="./style.css"
)
之所以对于两种不同的服务器没有限定,因为实际上这种利用场景我们都是通过/去访问服务器
先来了解浏览器根据相对路径请求css原则
访问http://localhost/RPO/index.php/country/wei

访问http://localhost/RPO/index.php/country/wei/

可以知道浏览器是根据最后一个/作为当前目录,在通过相对路径知道css地址,再向服务端去请求该地址的CSS
所以访问http://localhost/RPO/index.php/country/wei/style.css
作为css
而通过之前对于后续无意义目录的例子,可以知道css的内容应该跟index.php的内容一致

所以,此处就形成了把php文件当做是静态文件解析
单单这种形式的漏洞,利用空间较少,你必须要能向index.php中嵌入css(如果是加载作为js的话,就是嵌入js代码)才行,除了对于IE9,IE10的也可以利用同域文件前端小姿势
pathinfo例子
此例子参考rpo二三事
由于我们需要能在index.php中写入css,构筑index.php代码,进入同目录下css(其内容不重要)1
2
3
4
5
6<html>
<link href="style.css" rel="stylesheet" />
echo htmlspecialchars($_SERVER['PHP_SELF']);
echo "<br><";
</html>
正常访问情况

访问http://localhost/index.php/payload/

观察css内容,意料之中执行index.php页面代码

开始注入payload{}*{background-color:red}

其他css-payload 可以参考前端小姿势
利用场景- 基于%2f
- 服务器:要求可以解析URL编码的WEB服务器(此处以Nginx为例)
- 浏览器:不解析URL编码
- css获取:通过相对路径获取
场景假设:index.php利用相对路径../css/style.css
引用style.css。我们可以修改profile.php向其中注入css代码。1
2
3
4
5
6目录结构
--php -- index.php
| |
| -- profile.php
|
css -- style.css
访问:http://localhost/RPO/aa/php/profile.php/aa%2f..%2f../index.php
服务器视角:http://localhost/RPO/aa/php/profile.php/aa/../../index.php
即http://localhost/RPO/aa/php/index.php
浏览器视角:http://localhost/RPO/aa/php/profile.php/aa%2f..%2f../index.php
CSS视角:http://localhost/RPO/aa/php/profile.php/css/style.css
加上服务器对于后续无意义目录的解析
最终实际从服务端加载的CSS:http://localhost/RPO/aa/php/profile.php

所以,此处就形成了把不同目录下的php文件当做是静态文件解析
利用的性质是:客户端与服务器对于%2f解析的不同,以及服务器对于多余目录的无视
基于%2f的跨目录解析例子
直接上CTF题目吧
防护RPO
- 不使用相对路径,加载css/js : 最有效了
- 在php页面最开头,声明
<!DOCTYPE html>
,使用规范模式解析
细节请参考rpo二三事
关于WEB缓存的攻击
不细说了,原理类似,应用环境不同 戳这里
参考
1 | http://www.freebuf.com/articles/web/166731.html |