簡體   English   中英

將int轉換為十六進制格式的字符串

[英]Convert int to string in hex format

當我嘗試像本文一樣進行粉碎堆疊攻擊時: http : //www.cs.wright.edu/people/faculty/tkprasad/courses/cs781/alephOne.html ,但我遇到了一個需要解決的問題將堆棧指針轉換為字符串。

我知道如何以十六進制格式(使用printf)打印出int,但不知道如何將其存儲為內部字符串表示形式。 我需要在內部將其存儲為字符串,以便可以將其傳遞給memcpy函數。

我需要的理論功能是下面的“ convertFromIntToHexCharStar”。

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

旨在將此函數用作參數。 它給出了堆棧指針。

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

我想將堆棧指針轉換為十六進制char *,以便可以像這樣執行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雪橇(這是我要解決的真正問題)。 我打算多次填寫地址,這樣會增加覆蓋堆棧上返回地址的幾率,但是為了簡潔起見,我只給出了一行代碼,將“地址”寫入“緩沖​​區”。

我已經搜索了stackoverflow和Google,卻找不到任何東西。 在此先感謝您的幫助!

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;
}

暫無
暫無

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

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