printf函數里面大有文章!
先給出printf函數格式控制的基本語法:
還有一篇網文中,更為詳細的講述了C語言格式輸入輸出的內容:
如有興趣,請查看:C語言格式輸入輸出總結 http://hi.bccn.net/space-241412-do-blog-id-10499.html
printf的格式控制的完整格式:
% - 0 m.n l或h 格式字符
下面對組成格式說明的各項加以說明:
①%:表示格式說明的起始符號,不可缺少。
②-:有-表示左對齊輸出,如省略表示右對齊輸出。
③0:有0表示指定空位填0,如省略表示指定空位不填。
④m.n:m指域寬,即對應的輸出項在輸出設備上所占的字符數。N指精度。用于說明輸出的實型數的小數位數。為指定n時,隱含的精度為n=6位。
⑤l或h:l對整型指long型,對實型指double型。h用于將整型的格式字符修正為short型。
---------------------------------------
格式字符
格式字符用以指定輸出項的數據類型和輸出格式。
①d格式:用來輸出十進制整數。有以下幾種用法:
%d:按整型數據的實際長度輸出。
%md:m為指定的輸出字段的寬度。如果數據的位數小于m,則左端補以空格,若大于m,則按實際位數輸出。
%ld:輸出長整型數據。
②o格式:以無符號八進制形式輸出整數。對長整型可以用"%lo"格式輸出。同樣也可以指定字段寬度用“%mo”格式輸出。
例:
main()
{ int a = -1;
printf("%d, %o", a, a);
}
運行結果:-1,177777
程序解析:-1在內存單元中(以補碼形式存放)為(1111111111111111)2,轉換為八進制數為(177777)8。
③x格式:以無符號十六進制形式輸出整數。對長整型可以用"%lx"格式輸出。同樣也可以指定字段寬度用"%mx"格式輸出。
④u格式:以無符號十進制形式輸出整數。對長整型可以用"%lu"格式輸出。同樣也可以指定字段寬度用“%mu”格式輸出。
⑤c格式:輸出一個字符。
⑥s格式:用來輸出一個串。有幾中用法
%s:例如:printf("%s", "CHINA")輸出"CHINA"字符串(不包括雙引號)。
%ms:輸出的字符串占m列,如字符串本身長度大于m,則突破獲m的限制,將字符串全部輸出。若串長小于m,則左補空格。
%-ms:如果串長小于m,則在m列范圍內,字符串向左靠,右補空格。
%m.ns:輸出占m列,但只取字符串中左端n個字符。這n個字符輸出在m列的右側,左補空格。
%-m.ns:其中m、n含義同上,n個字符輸出在m列范圍的左側,右補空格。如果n>m,則自動取n值,即保證n個字符正常輸出。
⑦f格式:用來輸出實數(包括單、雙精度),以小數形式輸出。有以下幾種用法:
%f:不指定寬度,整數部分全部輸出并輸出6位小數。
%m.nf:輸出共占m列,其中有n位小數,如數值寬度小于m左端補空格。
%-m.nf:輸出共占n列,其中有n位小數,如數值寬度小于m右端補空格。
⑧e格式:以指數形式輸出實數。可用以下形式:
%e:數字部分(又稱尾數)輸出6位小數,指數部分占5位或4位。
%m.ne和%-m.ne:m、n和”-”字符含義與前相同。此處n指數據的數字部分的小數位數,m表示整個輸出數據所占的寬度。
⑨g格式:自動選f格式或e格式中較短的一種輸出,且不輸出無意義的零。
---------------------------------------
關于printf函數的進一步說明:
如果想輸出字符"%",則應該在“格式控制”字符串中用連續兩個%表示,如:
printf("%f%%", 1.0/3);
輸出0.333333%。
---------------------------------------
對于單精度數,使用%f格式符輸出時,僅前7位是有效數字,小數6位.
對于雙精度數,使用%lf格式符輸出時,前16位是有效數字,小數6位.
再給出一些特殊用法:
由高手指點
對于m.n的格式還可以用如下方法表示(例)
int m=10,n=5;
char ch[]="abcdefghijklmnopqrst";
printf("%*.*s\n",m,n,ch);//輸出為 abcde
前邊的*定義的是總的寬度,后邊的定義的是輸出的個數。分別對應外面的參數m和n 。我想這種方法的好處是可以在語句之外對參數m和n賦值,從而控制輸出格式。
---------------------------------------------------------------
利用%p可以輸出指針,默認是16進制輸出的,因此%#p等同于%p,也可以用10進制輸出,格式為%ip
今天(06.6.9)又看到一種輸出格式 %n 可以將所輸出字符串的長度值賦紿一個變量, 見下例:
int slen;
printf("hello world%n", &slen);
執行后變量被賦值為11。
但是這一特性會被黑客用于惡意攻擊:
//有關這部分的詳細內容,請查看完整的網頁:格式化字符串攻擊筆記 http://www.3800hk.com/news/w44/14907.html
%n格式符允許寫入指定數據.
上面提到的例子基本上只能做到偷窺堆棧中數據,了解堆棧結構的目的.而和利用格式化字符串漏洞攻擊
還是有一定距離的,因為只能做"傳說"中的read anywhere是無法改變程序流程的,不改變程序流程,就無法
讓程序按照我們的意愿執行下去.:)
但是很幸運*printf的%n格式化說明符它允許向后面一個存儲單元寫入前面輸出數據的總長度,那么只
要前面輸出數據的長度(這個長度的控制可以利用格式化說明符的特性,比如%.200d,這樣我們就可以控制輸
出數據長度為200了,想象一下如果我們用%f.呢?嗬嗬,堆棧地址固然很大,但是我們應該可以構造足夠的
%f....用來到達我們需要改寫的存儲單元)等于我們需要程序跳轉到的那個地址(通常是shellcode+nop的區
域),而%n恰到好處的將這一地址寫入適當位置,那么我們就可以按照我們的意愿改變程序流程了.:)
不過這里有一點需要注意,如果格式化字符串攻擊時覆蓋函數的返回地址,那么實際上我們是去覆蓋存儲
這個函數返回地址的那塊存儲空間.也就是說我們是間接的覆蓋.這一點很重要,不能混淆.回想一下C語言的指
針.:)
現在,在VS2005中,提到:就目前來說,printf中的%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
LONGLONG llValue;//也可以用__int64或INT64
int iValue;
printf(“%d, %d”, llValue, iValue);
iValue的值永遠不會被輸出,第一個%d輸出的是llValue的低32位,第二個%d輸出的是llValue的高32位。所以程序應該修改為:
printf(“%I64d, %d”, llValue, iValue);
對64位整數的輸入輸出,在POJ上的C++環境下(即VC),64位整數是:
__int64 (注意int前面是兩個下劃線)
輸入輸出格式為”%I64d”.
在G++環境下(即Dev C++) 64位整數是
long long
輸入輸出格式為”%lld”.
最后,貼出從VS2005F11進去的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));
}
posted on 2008-11-19 00:23 so true 閱讀(2912) 評論(1) 編輯 收藏 所屬分類: C&C++