攻擊者可以控制 format string,發起類似于緩沖區溢出的攻擊。
Format string 漏洞會在以下情況下發生:
1. 數據從不可信賴的數據源進入應用程序。
2. 數據作為 format string 參數傳送到某個函數,如 sprintf()、FormatMessageW()、syslog()、NSLog 或 NSString.stringWithFormat
例 1:下面的代碼將命令行參數作為 NSString.stringWithFormat: 中的 format string。
int main(int argc, char** argv) {
char buf[128];
...
[NSString stringWithFormat:argv[1], argv[2] ];
}
%x)來讀取堆棧中的內容,然后函數會作為即將格式化的參數使用。(在本例中,函數沒有采用任何即將格式化的參數。)
printf("%d %d %1$d %1$d\n", 5, 9);
5 9 5 5
syslog() 函數有時候以如下形式使用:
...
syslog(LOG_ERR, cmdBuf);
...
syslog() 的第二個參數是一個 format string,任何包含在 cmdBuf 內的格式化指令都會按照例 1 中的方式進行解析。 syslog() 的一種正確的使用方式:
...
syslog(LOG_ERR, "%s", cmdBuf);
...
String.stringByAppendingFormat() 函數有時候可以這樣使用:
...
NSString test = @"Sample Text.";
test = [test stringByAppendingFormat:[MyClass
formatInput:inputControl.text]];
...
stringByAppendingFormat() 的正確使用方式:
...
NSString test = @"Sample Text.";
test = [test stringByAppendingFormat:@"%@", [MyClass
formatInput:inputControl.text]];
...
[1] Standards Mapping - OWASP Top 10 2004 - (OWASP 2004) A5 Buffer Overflow
[2] Standards Mapping - Security Technical Implementation Guide Version 3 - (STIG 3) APP3510 CAT I, APP3560 CAT I
[3] Standards Mapping - Security Technical Implementation Guide Version 3.4 - (STIG 3.4) APP3510 CAT I, APP3560 CAT I
[4] Standards Mapping - Common Weakness Enumeration - (CWE) CWE ID 134
[5] Standards Mapping - Web Application Security Consortium 24 + 2 - (WASC 24 + 2) Format String Attack
[6] T. Newsham Format String Attacks Guardent, Inc.
[7] Standards Mapping - Payment Card Industry Data Security Standard Version 1.2 - (PCI 1.2) Requirement 6.3.1.1
[8] Standards Mapping - Payment Card Industry Data Security Standard Version 2.0 - (PCI 2.0) Requirement 6.5.2
[9] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1 - (PCI 1.1) Requirement 6.5.5
[10] Standards Mapping - SANS Top 25 2011 - (SANS Top 25 2011) Risky Resource Management - CWE ID 134