2013-05-26 115 views
2

虽然我正在尝试像这篇文章:http://www.cs.wright.edu/people/faculty/tkprasad/courses/cs781/alephOne.html一样进行碎片堆栈利用,但遇到了需要将堆栈指针转换为字符串的问题。将int转换为十六进制格式的字符串

我知道如何以十六进制格式(使用printf)打印int,但不知道如何将其作为内部字符串表示形式存储。我需要将它内部存储为一个字符串,以便将其传递到memcpy函数中。

我需要的理论函数是下面的“convertFromIntToHexCharStar”。

unsigned long NOPSledPointer = get_sp() + 150; 
char * address = convertFromIntToHexCharStar(NOPSledPointer); 

它旨在使用此函数作为参数。它给出堆栈指针。

unsigned long get_sp(void) { 
    __asm__("movl %esp,%eax"); 
} 

我想堆栈指针转换为十六进制字符*所以我可以做的memcpy这样的:

char buffer[517]; 

/* Initialize buffer with 0x90 (NOP instruction) */ 
memset(&buffer, 0x90, 517); 

/* Fill the buffer with appropriate contents here */ 
memcpy((void*) buffer, (void*) address, 4); 

我需要填写与十六进制表示的地址的记忆,因为我知道它在过去有效。

所以,我要求的是帮助将其转换为字符串,或另一种更简单的方法来做到这一点NOP雪橇(这是我真正想要解决的问题)。我要多次填写地址,这样会增加覆盖堆栈上返回地址的几率,但为了简洁起见,我只给出了将“address”写入“buffer”的一行代码。

我已经搜索过stackoverflow &谷歌,找不到任何东西。在此先感谢您的帮助!

+1

'sprintf'就像'printf'一样工作,但将结果放入字符串中。 – Gene

回答

1

snprintf解决了我的问题,因为我知道堆栈指针的大小是4字节。

这个网站帮助了我: http://www.cplusplus.com/reference/cstdio/snprintf/

这里是下面的代码解决方案,我用,以确保其工作正常一些打印语句。

#include <stdio.h> 

unsigned long get_sp(void) 
{ 
    __asm__("movl %esp,%eax"); 
} 


int main() 
{ 
    unsigned long numberToConvert = get_sp(); 
    char address[9]; 
    snprintf(address, 9, "%08lX", numberToConvert); 

    printf("Number To Convert: %lu \n", numberToConvert); 
    printf("Expected hex number: %08lX \n", numberToConvert); 
    printf("Actual hex number: %s \n", address); 

    return 0; 
} 
相关问题