Fixing SQL injection in ASP and MS SQL
Note: This post is part of our series on “How to Fix SQL Injection Vulnerabilities“. The series contains examples on how to fix SQL Injection Vulnerabilities in various programming languages.
An SQL Injection attack is a code injection attack when input from an attacker reaches one of your databases without any filteration or validation. As a result, a malicious user can execute Read / Write / Delete / Update query in your database. In addition to this he can also run system level commands. The following example shows how to prevent a malicious input in ASP by filtering user input before it is passed to MS SQL.
Parameterized query
string commandText = "SELECT * FROM Customers WHERE [email protected]"; SqlCommand cmd = new SqlCommand(commandText, conn); cmd.Parameters.Add("@Location",Location);
Validating User input
string Lastname = this.lastnameTb.Text.Replace("'", "''"); string sql = "Update Users SET Lastname=' "+ Lastname +"' WHERE id="+userID;