Repository navigation
How to combat SQL injections
If I wanted to protect my home against possible intruders, I could buy a camera, an alarm system, lock my doors or call the police, in the event of a home invasion, if you have a camera, the suspect will be identified and be put on a trial, where he will be judged Innocent until proven otherwise. When it comes to the internet, we take a different approach, we treat any user as a potential intruder until proven otherwise. Now, what if I want to protect my web application against possible intruders ? I can't buy a camera, or call the police and ask them to fix my code ! Protecting yourself against these types of attacks might seem tricky, but in fact, it is not as tricky as it seems, many tools and resources are available online, for free, that will guide you through the process of securing your web application against all kind of attacks, not just SQL Injections, but this document will only cover SQL Injections, I invite you to read the rest of my wiki for different type of attacks and how to prevent them, now, without further redo, let's get into the subject, today I will present you way to secure your web application against SQL Injections.
NEVER trust anybody's input, treat user input as malicious unless proved otherwise !
What I mean by this, is that you need to ensure that your user's input only contains characters that are allowed where they should be allowed. There is no possible case where a user should type <script> or these kind of Unicode characters =, "", OR, {, ; inside a password or email field, if a user tries to enter these, it is definitly a malicious attempt and you should have filters in place to ensure that nobody can type these
in your app. If you don't have these in place, add them right now, or the damages could be really ugly, and you could also have your private information leaked on the internet.
Validation is making sure that what the user enters, is not malicious commands that might cause damage to your application or database. As an example in PHP, you can use mysql_real_escape_string() to successfully escape characters that would be placed there for malicious purposes such as stealing info or destroying tables, what escaping the string means is that It will add a "/" in front of the malicious character to make sure the command is not executed, saving your app in the process.
A note on validation If your app contains client-side failsafes and protections, good job ! but that's not enough, don't rely on anything client-sided to secure your application, as it can easily be countered by someone who knows what they're doing
Instead of escaping the characters, you could use Command Parameters, that will be replaced by the user input later. here's an example in C# ASP.NET on how to use these to protect yourself against SQL Injections
SqlCommand cmd = new SqlCommand ("SELECT * FROM users WHERE username=@username AND password=@password",con);
SqlParameter username = new SqlParameter(); username.ParameterName = "@username"; username.value = txtUsername.Text; cmd.Parameters.Add(username);
SqlParameter password = new SqlParameter(); password.ParameterName = "@password"; password.value = txtPassword.Text; cmd.Parameters.Add(password); How this process works is simple:
-
The SqlCommand Object is created using @parameter_name as paradigm where the user input should be inserted.
-
You create multiple instances of the SqlParameter objects, inside them you insert the user's input
-
You add the SqlParamater object to the SqlCommand object's parameter. which will replace the parameters with the user's input.
I hope this article helped you learn more about how to secure your web application against Sql Injections