先給出printf函數(shù)格式控制的基本語法:
還有一篇網(wǎng)文中,更為詳細(xì)的講述了C語言格式輸入輸出的內(nèi)容:
如有興趣,請查看:C語言格式輸入輸出總結(jié) http://hi.bccn.net/space-241412-do-blog-id-10499.html
printf的格式控制的完整格式:
% - 0 m.n l或h 格式字符
下面對組成格式說明的各項(xiàng)加以說明:
①%:表示格式說明的起始符號,不可缺少。
②-:有-表示左對齊輸出,如省略表示右對齊輸出。
③0:有0表示指定空位填0,如省略表示指定空位不填。
④m.n:m指域?qū)挘磳?yīng)的輸出項(xiàng)在輸出設(shè)備上所占的字符數(shù)。N指精度。用于說明輸出的實(shí)型數(shù)的小數(shù)位數(shù)。為指定n時,隱含的精度為n=6位。
⑤l或h:l對整型指long型,對實(shí)型指double型。h用于將整型的格式字符修正為short型。
---------------------------------------
格式字符
格式字符用以指定輸出項(xiàng)的數(shù)據(jù)類型和輸出格式。
①d格式:用來輸出十進(jìn)制整數(shù)。有以下幾種用法:
%d:按整型數(shù)據(jù)的實(shí)際長度輸出。
%md:m為指定的輸出字段的寬度。如果數(shù)據(jù)的位數(shù)小于m,則左端補(bǔ)以空格,若大于m,則按實(shí)際位數(shù)輸出。
%ld:輸出長整型數(shù)據(jù)。
②o格式:以無符號八進(jìn)制形式輸出整數(shù)。對長整型可以用"%lo"格式輸出。同樣也可以指定字段寬度用“%mo”格式輸出。
例:
main()
{ int a = -1;
printf("%d, %o", a, a);
}
運(yùn)行結(jié)果:-1,177777
程序解析:-1在內(nèi)存單元中(以補(bǔ)碼形式存放)為(1111111111111111)2,轉(zhuǎn)換為八進(jìn)制數(shù)為(177777)8。
③x格式:以無符號十六進(jìn)制形式輸出整數(shù)。對長整型可以用"%lx"格式輸出。同樣也可以指定字段寬度用"%mx"格式輸出。
④u格式:以無符號十進(jìn)制形式輸出整數(shù)。對長整型可以用"%lu"格式輸出。同樣也可以指定字段寬度用“%mu”格式輸出。
⑤c格式:輸出一個字符。
⑥s格式:用來輸出一個串。有幾中用法
%s:例如:printf("%s", "CHINA")輸出"CHINA"字符串(不包括雙引號)。
%ms:輸出的字符串占m列,如字符串本身長度大于m,則突破獲m的限制,將字符串全部輸出。若串長小于m,則左補(bǔ)空格。
%-ms:如果串長小于m,則在m列范圍內(nèi),字符串向左靠,右補(bǔ)空格。
%m.ns:輸出占m列,但只取字符串中左端n個字符。這n個字符輸出在m列的右側(cè),左補(bǔ)空格。
%-m.ns:其中m、n含義同上,n個字符輸出在m列范圍的左側(cè),右補(bǔ)空格。如果n>m,則自動取n值,即保證n個字符正常輸出。
⑦f格式:用來輸出實(shí)數(shù)(包括單、雙精度),以小數(shù)形式輸出。有以下幾種用法:
%f:不指定寬度,整數(shù)部分全部輸出并輸出6位小數(shù)。
%m.nf:輸出共占m列,其中有n位小數(shù),如數(shù)值寬度小于m左端補(bǔ)空格。
%-m.nf:輸出共占n列,其中有n位小數(shù),如數(shù)值寬度小于m右端補(bǔ)空格。
⑧e格式:以指數(shù)形式輸出實(shí)數(shù)。可用以下形式:
%e:數(shù)字部分(又稱尾數(shù))輸出6位小數(shù),指數(shù)部分占5位或4位。
%m.ne和%-m.ne:m、n和”-”字符含義與前相同。此處n指數(shù)據(jù)的數(shù)字部分的小數(shù)位數(shù),m表示整個輸出數(shù)據(jù)所占的寬度。
⑨g格式:自動選f格式或e格式中較短的一種輸出,且不輸出無意義的零。
---------------------------------------
關(guān)于printf函數(shù)的進(jìn)一步說明:
如果想輸出字符"%",則應(yīng)該在“格式控制”字符串中用連續(xù)兩個%表示,如:
printf("%f%%", 1.0/3);
輸出0.333333%。
---------------------------------------
對于單精度數(shù),使用%f格式符輸出時,僅前7位是有效數(shù)字,小數(shù)6位.
對于雙精度數(shù),使用%lf格式符輸出時,前16位是有效數(shù)字,小數(shù)6位.
再給出一些特殊用法:
由高手指點(diǎn)
對于m.n的格式還可以用如下方法表示(例)
int m=10,n=5;
char ch[]="abcdefghijklmnopqrst";
printf("%*.*s\n",m,n,ch);//輸出為 abcde
前邊的*定義的是總的寬度,后邊的定義的是輸出的個數(shù)。分別對應(yīng)外面的參數(shù)m和n 。我想這種方法的好處是可以在語句之外對參數(shù)m和n賦值,從而控制輸出格式。
---------------------------------------------------------------
利用%p可以輸出指針,默認(rèn)是16進(jìn)制輸出的,因此%#p等同于%p,也可以用10進(jìn)制輸出,格式為%ip
今天(06.6.9)又看到一種輸出格式 %n 可以將所輸出字符串的長度值賦紿一個變量, 見下例:
int slen;
printf("hello world%n", &slen);
執(zhí)行后變量被賦值為11。
但是這一特性會被黑客用于惡意攻擊:
//有關(guān)這部分的詳細(xì)內(nèi)容,請查看完整的網(wǎng)頁:格式化字符串攻擊筆記 http://www.3800hk.com/news/w44/14907.html
%n格式符允許寫入指定數(shù)據(jù).
上面提到的例子基本上只能做到偷窺堆棧中數(shù)據(jù),了解堆棧結(jié)構(gòu)的目的.而和利用格式化字符串漏洞攻擊
還是有一定距離的,因?yàn)橹荒茏?傳說"中的read anywhere是無法改變程序流程的,不改變程序流程,就無法
讓程序按照我們的意愿執(zhí)行下去.:)
但是很幸運(yùn)*printf的%n格式化說明符它允許向后面一個存儲單元寫入前面輸出數(shù)據(jù)的總長度,那么只
要前面輸出數(shù)據(jù)的長度(這個長度的控制可以利用格式化說明符的特性,比如%.200d,這樣我們就可以控制輸
出數(shù)據(jù)長度為200了,想象一下如果我們用%f.呢?嗬嗬,堆棧地址固然很大,但是我們應(yīng)該可以構(gòu)造足夠的
%f....用來到達(dá)我們需要改寫的存儲單元)等于我們需要程序跳轉(zhuǎn)到的那個地址(通常是shellcode+nop的區(qū)
域),而%n恰到好處的將這一地址寫入適當(dāng)位置,那么我們就可以按照我們的意愿改變程序流程了.:)
不過這里有一點(diǎn)需要注意,如果格式化字符串攻擊時覆蓋函數(shù)的返回地址,那么實(shí)際上我們是去覆蓋存儲
這個函數(shù)返回地址的那塊存儲空間.也就是說我們是間接的覆蓋.這一點(diǎn)很重要,不能混淆.回想一下C語言的指
針.:)
現(xiàn)在,在VS2005中,提到:就目前來說,printf中的%n格式化指示符一般用于指定輸出的字符個數(shù)。這已經(jīng)確認(rèn)為一個安全隱患,并且已禁用,但可以使用set_printf_count_output來啟用它;通過傳遞給set_printf_count_output一個零值(0)可禁用它,而傳遞任意一個其他值可再次啟用。
示例代碼為:
int e;
int i;
e = _set_printf_count_output( 1 );
printf( "%%n support was %sabled.\n",
e ? "en" : "dis" );
printf( "%%n support is now %sabled.\n",
_get_printf_count_output() ? "en" : "dis" );
printf( "%.200d%n6789\n", e,&i ); // %n format should set i to 5
printf( "i = %d\n", i );
輸出:
%n support was disabled.
%n support is now enabled.
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000006789
i = 200
討論完了%n,下面再說說對于
使用64位整數(shù)
LONGLONG llValue;//也可以用__int64或INT64
int iValue;
printf(“%d, %d”, llValue, iValue);
iValue的值永遠(yuǎn)不會被輸出,第一個%d輸出的是llValue的低32位,第二個%d輸出的是llValue的高32位。所以程序應(yīng)該修改為:
printf(“%I64d, %d”, llValue, iValue);
對64位整數(shù)的輸入輸出,在POJ上的C++環(huán)境下(即VC),64位整數(shù)是:
__int64 (注意int前面是兩個下劃線)
輸入輸出格式為”%I64d”.
在G++環(huán)境下(即Dev C++) 64位整數(shù)是
long long
輸入輸出格式為”%lld”.
最后,貼出從VS2005F11進(jìn)去的printf源代碼:
/***
*printf.c - print formatted
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines printf() - print formatted data
*
*******************************************************************************/
#include <cruntime.h>
#include <stdio.h>
#include <dbgint.h>
#include <stdarg.h>
#include <file2.h>
#include <internal.h>
#include <mtdll.h>
#include <stddef.h>
#include <process.h>
/***
*int printf(format, ...) - print formatted data
*
*Purpose:
* Prints formatted data on stdout using the format string to
* format data and getting as many arguments as called for
* Uses temporary buffering to improve efficiency.
* _output does the real work here
*
*Entry:
* char *format - format string to control data format/number of arguments
* followed by list of arguments, number and type controlled by
* format string
*
*Exit:
* returns number of characters printed
*
*Exceptions:
*
*******************************************************************************/
int __cdecl printf (
const char *format,
...
)
/*
* stdout 'PRINT', 'F'ormatted
*/
{
va_list arglist;
int buffing;
int retval;
_VALIDATE_RETURN( (format != NULL), EINVAL, -1);
va_start(arglist, format);
_lock_str2(1, stdout);
__try {
buffing = _stbuf(stdout);
retval = _output_l(stdout,format,NULL,arglist);
_ftbuf(buffing, stdout);
}
__finally {
_unlock_str2(1, stdout);
}
return(retval);
}
int __cdecl _printf_l (
const char *format,
_locale_t plocinfo,
...
)
{
va_list arglist;
va_start(arglist, plocinfo);
return _vprintf_l(format, plocinfo, arglist);
}
int __cdecl _printf_s_l (
const char *format,
_locale_t plocinfo,
...
)
{
va_list arglist;
va_start(arglist, plocinfo);
return _vprintf_s_l(format, plocinfo, arglist);
}
int __cdecl printf_s (
const char *format,
...
)
{
va_list arglist;
va_start(arglist, format);
return _vprintf_s_l(format, NULL, arglist);
}
int __cdecl _printf_p_l (
const char *format,
_locale_t plocinfo,
...
)
{
va_list arglist;
va_start(arglist, plocinfo);
return _vprintf_p_l(format, plocinfo, arglist);
}
int __cdecl _printf_p (
const char *format,
...
)
{
va_list arglist;
va_start(arglist, format);
return _vprintf_p_l(format, NULL, arglist);
}
static UINT_PTR __enable_percent_n = 0;
/***
*int _set_printf_count_output(int)
*
*Purpose:
* Enables or disables %n format specifier for printf family functions
*
*Internals:
* __enable_percent_n is set to (__security_cookie|1) for security reasons;
* if set to a static value, an attacker could first modify __enable_percent_n
* and then provide a malicious %n specifier. The cookie is ORed with 1
* because a zero cookie is a possibility.
******************************************************************************/
int __cdecl _set_printf_count_output(int value)
{
int old = (__enable_percent_n == (__security_cookie | 1));
__enable_percent_n = (value ? (__security_cookie | 1) : 0);
return old;
}
/***
*int _get_printf_count_output()
*
*Purpose:
* Checks whether %n format specifier for printf family functions is enabled
******************************************************************************/
int __cdecl _get_printf_count_output()
{
return ( __enable_percent_n == (__security_cookie | 1));
}