[英]C - The %x format specifier
I have a small question.我有一个小问题。 I know that the %x format specifier can be used to read values from the stack in a format string attack.我知道 %x 格式说明符可用于在格式字符串攻击中从堆栈中读取值。
I found the following code:我找到了以下代码:
%08x%08x%08x%08x
What does the 08 mean? 08是什么意思? What is it doing exactly?它究竟在做什么? Thanks :)谢谢 :)
Break-down:分解:
8
says that you want to show 8 digits 8
表示要显示 8 位数字0
that you want to prefix with 0
's instead of just blank spaces 0
您想以0
为前缀,而不仅仅是空格x
that you want to print in lower-case hexadecimal. x
要以小写十六进制打印。Quick example (thanks to Grijesh Chauhan):快速示例(感谢 Grijesh Chauhan):
#include <stdio.h>
int main() {
int data = 29;
printf("%x\n", data); // just print data
printf("%0x\n", data); // just print data ('0' on its own has no effect)
printf("%8x\n", data); // print in 8 width and pad with blank spaces
printf("%08x\n", data); // print in 8 width and pad with 0's
return 0;
}
Output:输出:
1d
1d
1d
0000001d
Also see http://www.cplusplus.com/reference/cstdio/printf/ for reference.另请参阅http://www.cplusplus.com/reference/cstdio/printf/以供参考。
%08x
表示每个数字都应该打印至少 8 个字符宽,并用零填充所有缺失的数字,例如对于 '1' 输出将是00000001
The format string attack on printf you mentioned isn't specific to the "%x" formatting - in any case where printf has more formatting parameters than passed variables, it will read values from the stack that do not belong to it.您提到的对 printf 的格式字符串攻击并非特定于“%x”格式 - 在 printf 具有比传递的变量更多的格式参数的任何情况下,它将从堆栈中读取不属于它的值。 You will get the same issue with %d for example.例如,您将遇到与 %d 相同的问题。 %x is useful when you want to see those values as hex.当您想以十六进制形式查看这些值时,%x 很有用。
As explained in previous answers, %08x will produce a 8 digits hex number, padded by preceding zeros.如之前的答案所述,%08x 将产生一个 8 位十六进制数字,由前面的零填充。
Using the formatting in your code example in printf, with no additional parameters:在 printf 中使用代码示例中的格式,没有附加参数:
printf ("%08x %08x %08x %08x");
Will fetch 4 parameters from the stack and display them as 8-digits padded hex numbers.将从堆栈中获取 4 个参数并将它们显示为 8 位填充的十六进制数字。
That specifies the how many digits you want it to show.这指定了您希望它显示的数字数量。
integer value or * that specifies minimum field width.指定最小字段宽度的整数值或 *。 The result is padded with space characters (by default), if required, on the left when right-justified, or on the right if left-justified.结果用空格字符填充(默认情况下),如果需要,右对齐时在左侧,如果左对齐则在右侧。 In the case when * is used, the width is specified by an additional argument of type int.在使用 * 的情况下,宽度由 int 类型的附加参数指定。 If the value of the argument is negative, it results with the - flag specified and positive field width.如果参数的值为负,则结果为 - 指定的标志和正的字段宽度。
From http://en.wikipedia.org/wiki/Printf_format_string来自http://en.wikipedia.org/wiki/Printf_format_string
use 0 instead of spaces to pad a field when the width option is specified.当指定宽度选项时,使用 0 而不是空格来填充字段。 For example, printf("%2d", 3)
results in " 3", while printf("%02d", 3)
results in "03".例如, printf("%2d", 3)
结果是“ 3”,而printf("%02d", 3)
结果是“03”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.