19.09.2017 Views

the-web-application-hackers-handbook

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

710 Chapter 19 n Finding Vulnerabilities in Source Code<br />

Comparisons between signed and unsigned integers often lead to problems.<br />

In <strong>the</strong> following “fix” to <strong>the</strong> previous vulnerability, a signed integer (len) is<br />

compared with an unsigned integer (sizeof(strFileName)). If <strong>the</strong> user can<br />

engineer a situation where len has a negative value, this comparison will succeed,<br />

and <strong>the</strong> unchecked strcpy will still occur:<br />

BOOL CALLBACK CFiles::EnumNameProc(LPTSTR pszName, int len)<br />

{<br />

char strFileName[MAX_PATH];<br />

}<br />

if (len < sizeof(strFileName))<br />

strcpy(strFileName, pszName);<br />

...<br />

Format String Vulnerabilities<br />

Typically you can identify <strong>the</strong>se quickly by looking for uses of <strong>the</strong> printf and<br />

FormatMessage families of functions where <strong>the</strong> format string parameter is not<br />

hard-coded but is user-controllable. The following call to fprintf is an example:<br />

void logAu<strong>the</strong>nticationAttempt(char* username);<br />

{<br />

char tmp[64];<br />

snprintf(tmp, 64, “login attempt for: %s\n”, username);<br />

tmp[63] = 0;<br />

fprintf(g_logFile, tmp);<br />

}<br />

Source Code Comments<br />

Many software vulnerabilities are actually documented within source code<br />

comments. This often occurs because developers are aware that a particular<br />

operation is unsafe, and <strong>the</strong>y record a reminder to fix <strong>the</strong> problem later, but<br />

<strong>the</strong>y never get around to doing so. In o<strong>the</strong>r cases, testing has identified some<br />

anomalous behavior within <strong>the</strong> <strong>application</strong> that was commented within <strong>the</strong><br />

code but never fully investigated. For example, <strong>the</strong> authors encountered <strong>the</strong><br />

following within an <strong>application</strong>’s production code:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!