There are several cases where this escape function does not work. The most obvious is that a single quote is not used:
string table= "\"" + table.Replace("'", "''") + "\"" string var= "`" + var.Replace("'", "''") + "`" string index= " " + index.Replace("'", "''") + " " string query = "select * from `"+table+"` where name=\""+var+"\" or id="+index
In this case, you can "break out" using the double quotation mark, the reverse mark. In the latter case, there is nothing to "break out", so you can simply write 1 union select password from users-- or any sql payload that the attacker wants.
The following condition, in which this escape function fails, is if the substring is taken after the string has been escaped (and yes, I discovered vulnerabilities like this in the wild):
string userPassword= userPassword.Replace("'", "''") string userName= userInput.Replace("'", "''") userName = substr(userName,0,10) string query = "select * from users where name='"+userName+"' and password='"+userPassword+"'";
In this case, the username abcdefgji' will be passed to the function abcdefgji'' by the escape function, and then return to abcdefgji' , taking the substring. This can be used by setting a password value for any sql statement, in which case or 1=1-- will be interpreted as sql, and the username will be interpreted as abcdefgji'' and password= . The final query is as follows:
select * from users where name='abcdefgji'' and password=' or 1=1
T-SQL and other advanced sql injection methods that have already been mentioned. Extended SQL injection in SQL Server applications is a great article, and you should read it if you haven't already.
The final problem is Unicode attacks. This class of vulnerabilities arises because the escape function does not know about encoding with a mulit byte, and it can be used by an attacker to "destroy" the escape character . Turning "N" into a string will not help, since it does not affect the value of multibyte characters later in the string. However, this type of attack is very unusual because the database must be configured to accept unicode GBK strings (and I'm not sure if MS-SQL can do this).
Second-order code entry is still possible; this attack pattern is created by checking the data sources controlled by the attacker. Escaping is used to represent control characters as a character literal. If the developer forgets to avoid the value obtained with select and then uses this value in another request, then bam , the attacker will have a personal letter with the letter at their disposal.
Check everything, do not believe anything.