sql_injection:what_is_sql_injection
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
sql_injection:what_is_sql_injection [2016/10/13 10:14] – [Correctly setting up the connection] peter | sql_injection:what_is_sql_injection [2020/04/16 20:53] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== SQL Injection - What is SQL Injection ====== | ||
- | |||
- | ===== What is SQL Injection? ===== | ||
- | |||
- | If user input is inserted without modification into an SQL query, then the application becomes vulnerable to SQL injection, like in the following example: | ||
- | |||
- | <code php> | ||
- | $unsafe_variable = $_POST[' | ||
- | |||
- | mysql_query(" | ||
- | </ | ||
- | |||
- | That's because the user can input something like: | ||
- | |||
- | <code php> | ||
- | value' | ||
- | </ | ||
- | |||
- | and the query becomes: | ||
- | |||
- | <code sql> | ||
- | INSERT INTO `table` (`column`) VALUES(' | ||
- | </ | ||
- | |||
- | |||
- | ===== What can be done to prevent this from happening? ===== | ||
- | |||
- | Use prepared statements and parameterized queries. | ||
- | |||
- | You basically have two options to achieve this: | ||
- | |||
- | Using [[http:// | ||
- | |||
- | <code php> | ||
- | $stmt = $pdo-> | ||
- | |||
- | $stmt-> | ||
- | |||
- | foreach ($stmt as $row) { | ||
- | // do something with $row | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | Using [[http:// | ||
- | |||
- | <code mysql> | ||
- | $stmt = $dbConnection-> | ||
- | $stmt-> | ||
- | |||
- | $stmt-> | ||
- | |||
- | $result = $stmt-> | ||
- | while ($row = $result-> | ||
- | // do something with $row | ||
- | } | ||
- | </ | ||
- | |||
- | If you're connecting to a database other than MySQL, there is a driver-specific second option that you can refer to (e.g. **pg_prepare()** and **pg_execute()** for PostgreSQL). | ||
- | |||
- | |||
- | ===== Correctly setting up the connection ===== | ||
- | |||
- | Note that when using **PDO** to access a MySQL database real prepared statements **are not used by default**. | ||
- | |||
- | <code php> | ||
- | $dbConnection = new PDO(' | ||
- | |||
- | $dbConnection-> | ||
- | $dbConnection-> | ||
- | </ | ||
- | |||
- | In the above example the error mode isn't strictly necessary, **but it is advised to add it**. This way the script will not stop with a Fatal Error when something goes wrong. | ||
- | |||
- | What is **mandatory** however is the first setAttribute() line, which tells PDO to disable emulated prepared statements and use real prepared statements. | ||
- | |||
- | Although you can set the charset in the options of the constructor, | ||
- | |||
- | |||
- | ===== Explanation ===== | ||
- | |||
- | What happens is that the SQL statement you pass to **prepare** is parsed and compiled by the database server. | ||
- | |||
- | The important thing here is that the parameter values are combined with the compiled statement, not an SQL string. | ||
- | |||
- | Another benefit with using prepared statements is that if you execute the same statement many times in the same session it will only be parsed and compiled once, giving you some speed gains. | ||
- | |||
- | Oh, and since you asked about how to do it for an insert, here's an example (using PDO): | ||
- | |||
- | <code php> | ||
- | $preparedStatement = $db-> | ||
- | |||
- | $preparedStatement-> | ||
- | </ | ||
- | |||
- | |||
- | ===== Can Prepared Statements Be Used For Dynamic Queries? ===== | ||
- | |||
- | While you can still use prepared statements for the query parameters, the structure of the dynamic query itself cannot be parametrized and certain query features cannot be parametrized. | ||
- | |||
- | For these specific scenarios, the best thing to do is use a whitelist filter that restricts the possible values. | ||
- | |||
- | < | ||
- | // Value whitelist | ||
- | // $dir can only be ' | ||
- | $dir = !empty($direction) ? ' | ||
- | </ | ||
- | |||
- | |||
- | ===== Warnings ===== | ||
- | |||
- | All escaping a string does is suitably apply slashes before a quotation mark, but it doesn' | ||
- | |||
- | <code php> | ||
- | ' | ||
- | </ | ||
- | |||
- | now, the real escape strings might escape the quotes but the string will still contain the **DELETE** clause. | ||
- | |||
- | Please read https:// | ||
sql_injection/what_is_sql_injection.1476353658.txt.gz · Last modified: 2020/07/15 09:30 (external edit)