对于缓冲区溢出,使用 pthread 时堆栈地址是多少?

发布于 2024-08-24 12:01:04 字数 271 浏览 16 评论 0原文

我正在上一门计算机安全课程,有一个额外的学分分配是将可执行代码插入到缓冲区溢出中。我有我正在尝试操作的目标程序的 c 源代码,并且我已经达到了可以成功覆盖当前函数堆栈帧的 eip 的程度。但是,我总是遇到分段错误,因为我提供的地址总是错误的。问题是当前函数位于 pthread 内部,因此,堆栈的地址似乎总是在程序的不同运行之间发生变化。是否有任何方法可以查找 pthread 中的堆栈地址(或估计 pthread 中的堆栈地址)? (注意:pthread_create 的第二个参数为 null,因此我们不会手动分配堆栈地址)

I'm taking a class in computer security and there is an extra credit assignment to insert executable code into a buffer overflow. I have the c source code for the target program I'm trying to manipulate, and I've gotten to the point where I can successfully overwrite the eip for the current function stack frame. However, I always get a Segmentation fault, because the address I supply is always wrong. The problem is that the current function is inside a pthread, and therefore, the address of the stack seems to always change between different runs of the program. Is there any method for finding the stack address within a pthread (or for estimating the stack address within a pthread)? (note: pthread_create's 2nd argument is null, so we're not manually assigning a stack address)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青芜 2024-08-31 12:01:04

我建议阅读关于利用缓冲区溢出漏洞的优秀(如果有点过时)文章/教程粉碎堆栈为了乐趣和利润

以下是简短摘录:

问题是我们不知道在内存空间的哪个位置
我们正在尝试利用代码(以及后面的字符串)的程序
它)将被放置。解决这个问题的一种方法是使用 JMP 和 CALL
操作说明。 JMP和CALL指令可以使用IP相对寻址,
这意味着我们可以跳转到当前IP的偏移量,而不需要
知道我们想要跳转到的内存中的确切地址。


您可以通过一些内联汇编来检索堆栈指针的当前值。 为了乐趣和利润而粉碎堆栈中的所有示例都会溢出缓冲区在 main 中,但是您可以轻松地使用相同的技术来溢出从 pthread 调用的函数中的缓冲区。下面的代码基于文章 (overflow1.c) 中的示例构建,以表明相同的技术可以使用 pthreads 工作。您将使用的实际技术取决于您尝试利用的目标程序。


/* get value of sp off the stack - not essential to example */
unsigned long get_sp()
{
   __asm__("movl %esp,%eax"); /* equiv. of 'return esp;' in C */
}

int foo()
{
   char buffer[96];

   /* overflow buffer to overwrite return address */
   /* and place code to be executed into buffer. */
   ...

   return 0;
}

void *thread(void *arg)
{
   printf("thread stack 0x%x\n", get_sp()); 

   foo();   

   return NULL;
}

int main(int argc, char **argv) 
{
   printf("main stack 0x%x\n", get_sp());   

   pthread_t t;
   pthread_create(&t, NULL, thread, NULL);
   pthread_join(t, NULL);

   return 0;
}

I suggest reading the excellent (if a bit dated) article/tutorial on exploiting buffer overflow vulnerabilities Smashing The Stack For Fun And Profit.

Here's a brief excerpt:

The problem is that we don't know where in the memory space of the
program we are trying to exploit the code (and the string that follows
it) will be placed. One way around it is to use a JMP, and a CALL
instruction. The JMP and CALL instructions can use IP relative addressing,
which means we can jump to an offset from the current IP without needing
to know the exact address of where in memory we want to jump to.


You can retrieve the current value of the stack pointer with a bit of inline assembly. All the examples in Smashing The Stack For Fun And Profit overflow a buffer in main, but you can just as easily use the same techniques to overflow a buffer in a function called from a pthread. The code below is built on an example from the article (overflow1.c) to show that the same techniques will work using pthreads. The actual technique you will use will depend on the target program you are trying to exploit.


/* get value of sp off the stack - not essential to example */
unsigned long get_sp()
{
   __asm__("movl %esp,%eax"); /* equiv. of 'return esp;' in C */
}

int foo()
{
   char buffer[96];

   /* overflow buffer to overwrite return address */
   /* and place code to be executed into buffer. */
   ...

   return 0;
}

void *thread(void *arg)
{
   printf("thread stack 0x%x\n", get_sp()); 

   foo();   

   return NULL;
}

int main(int argc, char **argv) 
{
   printf("main stack 0x%x\n", get_sp());   

   pthread_t t;
   pthread_create(&t, NULL, thread, NULL);
   pthread_join(t, NULL);

   return 0;
}
哀由 2024-08-31 12:01:04

除了我之前的回答之外,您可能还想阅读以下内容:

以下文章更多地关注堆溢出:

In addition to my previous answer, you may also want to read the following:

The following article focuses more on heap overflows:

橘寄 2024-08-31 12:01:04

如果不了解有关应用程序的更多信息,就很难知道,但首先想到的是堆喷射

Without knowing more about the application it's a little hard to know, but the first thing that comes to mind is heap spraying.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文