update Users set password=
‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘!Abb1‘ where username=‘administrator‘
图 5 使用 QUOTENAME 而非 REPLACE。上面的例子和此例的唯一不同在于,在上例中,开发人员为用户名、新密码和旧密码添加了单引号分隔符,而在此例中,由 QUOTENAME 函数添加。由于用户提供的数据没有变化,因此上例中使用的同一攻击字符串仍然可以被攻击者利用。图 6 是在中间层应用程序中编写的 C/C++ 函数的缩写版本,可实现相同功能。它容易受到相同的攻击。
Figure6Truncation Problems in C++
DWORD ChangePassword(char* psUserName, char* psOld, char* psNew)
{
char* psEscapedUserName = NULL;
char* psEscapedOldPW = NULL;
char* psEscapedNewPW = NULL;
char szSQLCommand[100];
HRESULT hr=0;
// Input Validation
...
// Calculate and allocate the new buffer with length
// userdatalen*2 + 1
// Escape all single quotes with double quotes
...
//Construct the query
hr = StringCchPrintf(szSQLCommand, sizeof(szSQLCommand)/sizeof(char),
"Update Users set password=‘%s’ where username=‘%s’"
"AND password=‘%s’,
psEscapedNewPW, psEscapedUserName, psEscapedOldPW);
if (S_OK != hr)
{
// handle error cases
}
// Execute and return
}
Figure5Using QUOTENAME to Avoid Injection