0

我有2个系统:

  1. 3.13.0-35-generic ... x86_64 x86_64 x86_64 带有 gcc 的 GNU/Linux:4.8.2
  2. 2.6.32-21-generic #32-Ubuntu ... i686 GNU/Linux with gcc: 4.4.3

我在两个系统上编译了以下代码:

int numOfNops = 600;
unsigned char nops[numOfNops];
int i;
for (i=0; i < numOfNops; i++) {
    nops[i] = '\x90';
}
...
printf("GET /%s%s\x90\x90%s HTTP/1.0 \n", nops, buf, ESPs);

问题是“nops”数组的打印。

  1. 当我在 64 位系统 #1 上运行它时,输出看起来正是我想要的样子。
  2. 当我在 32 位系统 #2 上运行它时,printf() 输出的 NOP 部分包含额外的奇怪字符,即:

Hexdump 系统 #1

00000250  90 90 90 90 90 90 90 90  90 90 90 90 90 89 e3 da  |................|
00000260  c4 d9 73 f4 5f 57 59 49  49 49 49 49 49 49 49 49  |..s._WYIIIIIIIII|

Hexdump 系统 #2:

00000250  90 90 90 90 90 90 90 90  90 90 90 90 90 24 c5 12  |.............$..|
00000260  89 e3 da c4 d9 73 f4 5f  57 59 49 49 49 49 49 49  |.....s._WYIIIIII|

所以附加字符是:0x24 0xc5 0x12。

[问] 为什么会这样?

谢谢。

4

2 回答 2

7

您的缓冲区不是 NUL '\0' 终止的,因此您正在打印超出缓冲区本身的字符。

我建议尝试添加 nops[numOfNops - 1] = '\0'; 在调用 printf 之前。

于 2014-09-20T17:54:37.663 回答
4

Consider telling printf() exactly how many NOPs to print:

printf("GET /%.*s%s\x90\x90%s HTTP/1.0 \n", numOfNops, nops, buf, ESPs);

This avoids the problem that you didn't null-terminate the string.

(Note that strictly the %.*s notation tells printf() to format up to numOfNops characters, or until the first null byte, as the output of the conversion specification. Where you have a solid array of NOP values as in the question, this is the same as telling printf() to print exactly the given number of NOP values.)

于 2014-09-20T18:22:05.357 回答