I am used to developing in PHP/MySQL and have no experience developing with SQL Server. I've skimmed over the PHP MSSQL documentation and it looks similar to MySQLi in some of the methods I read about.
For example, with MySQL I utilize the function mysql_real_excape_string(). Is there a similar function with PHP/SQL Server?
What steps do I need to take in order to protect against SQL injection with SQL Server?
What are the differences between SQL Server and MySQL pertaining to SQL injection prevention?
also - is this post accurate? is the escape string character for SQL Server a single quote?
How to Prevent an SQL Injection. The only sure way to prevent SQL Injection attacks is input validation and parametrized queries including prepared statements. The application code should never use the input directly.
The SQL Injection usually occurs when you ask a user for input, like their name and instead of a name they give you a MySQL statement that you will unknowingly run on your database. Never trust the data provided by a user, process this data only after validation; as a rule, this is done by pattern matching.
SQL is a query programming language that manages RDBMS. MySQL is a relational database management system that uses SQL. SQL is primarily used to query and operate database systems. MySQL allows you to handle, store, modify and delete data and store data in an organized way.
If a web application or website uses SQL databases like Oracle, SQL Server, or MySQL, it is vulnerable to an SQL injection attack. Hackers use SQL injection attacks to access sensitive business or personally identifiable information (PII), which ultimately increases sensitive data exposure.
Use PDO and parameterized queries and every database engine looks almost the same.
Use parametrized queries with ADODB or PDO. These libraries know the best escape function to use based on the database it is connected to. They allow you to switch between mysql and ms-sql without introducing vulnerabilities.
SQL Injection for MySQL and MS-SQL are radically different.
SQL Injection for MS-SQL is much more serious. For one you can stack queries:
select * from `table` where id='1' ; drop table `table`;-- '
Escaping is totally different, addslashses() does not stop sql injection under MS-SQL. It uses a double quote system so this is an escaped query:
select * from table where test='trying to inject '' didn''t work!'
A hacker can also access cmd.exe using xp_cmdshell
from a sql query. Make sure this privilege has been removed!
Under MySQL you can't stack, so its common to use union select (only works when injection into a select, otherwise you can use a sub-select, but you can't stack a drop/update/delete/insert on top of a select):
select somthing from table where 1 union select password from mysql.user
Escaping is done with back slashes, addslashes() works most of the time, but mysql_real_escape_string() is a lot better.
select * from table where test='trying to inject \' didn\'t work!'
Also you want to disable file_priv otherwise a hacker might be able to drop a backdoor:
select test from table where name='jon' union select "<?php eval($_GET[e])?>" into outfile "/var/www/backdoor.php"-- '
it is not the tool that allows SQL injection attacks, it is the programmer and how they use it. both mysql and sql server allow you to get injected if you code incorrectly (blindly concatenate strings to create dynamic sql) and both provide parameter binding to avoid it.
No. There is nothing inherit in any database product to protect you against SQL injection because the problem is not rooted in the database. The problem is in the way outside applications formulate requests and send them to the database.
No, MSSQL provides no such function, and in Mysqli you shouldn't be using mysql_real_escape_string either. In both cases you should be using Prepared Statements or Stored Procedeures. I believe the PHP documentation provides ample explanation on how to use the MSSQL apis.
Parameterized queries are the way to go. The sqlsrv driver supports parameterized queries. Of course, this will only be useful to you if you are running PHP on Windows. On the chance that you are, there's more information (with examples) here: How and Why to Use Parameterized Queries.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With