SQL Injection is one of the most common, overlooked, and potentially destructive attacks on modern websites. When SQL code is posted to the web server via an input field and the code results in an unintended result, your have a SQL Injection attack. This sort of attack can be used for anything from viewing unintended data, bypassing authentication, or even removing data from the database.
SQL Injection Example
Let’s say you have a login form for authentication to your website marked up as so:
<form method="post" action="serversidescript">
<label for="username">Username:
<input type="text" name="username" id="username" />
</label>
<label for="password">Password:
<input type="password" name="password" id="password" />
</label>
<input type="submit" name="submit" id="submit" value="Login" />
</form>
On the server side, the script with SQL code could look something like this:
$sql = "select * from users where username = '$username' and password = '$password'";
The problem with this code is the variables from the form are input directly into the SQL query without checking their values. An attacker could bypass your authentication with the following code:
Username: sometext
Password: ‘ or ’1′=’1
Password: ‘ or ’1′=’1
The sql query line then becomes:
$sql = "select * from users where username = 'sometext' and password = '' or '1' = '1'";
The injected SQL code will effectively make this statement become “select * from users”.
How To Secure Against SQL Injection
The best way to secure your site against this type of attack is to assume any input from the user is malicious. Implementing defenses to check the input values against rigid validation and/or escaping dangerous characters will secure your website against SQL Injection attacks.
Each scripting language has its own set of useful functions to help with sanitizing input data from the user. In many cases, there are frameworks available, such as .NET, to further aid in defending against this type of attack.
Regular expressions, if used properly, are very effective at checking user input against a character set pattern. Make sure to thoroughly test your regular expressions. Even one mistake can have huge unintended results.
0 komentar:
Posting Komentar