Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to defend against mysql injection and cross site scripting

At the moment, I apply a 'throw everything at the wall and see what sticks' method of stopping the aforementioned issues. Below is the function I have cobbled together:

function madSafety($string)
{

$string = mysql_real_escape_string($string);
$string = stripslashes($string);
$string = strip_tags($string);
return $string;

}

However, I am convinced that there is a better way to do this. I am using FILTER_ SANITIZE_STRING and this doesn't appear to to totally secure.

I guess I am asking, which methods do you guys employ and how successful are they? Thanks

like image 517
Drew Avatar asked Feb 20 '09 10:02

Drew


People also ask

How do you prevent SQL injection attacks?

Prevention techniques such as input validation, parametrized queries, stored procedures, and escaping work well with varying attack vectors. However, because of the large variation in the pattern of SQL injection attacks they are often unable to protect databases.

What is the difference between cross-site scripting and SQL injection?

As we mentioned earlier, Cross-Site Scripting is more common in JavaScript and is used in this language, while SQL Injection includes Structured Query Language. In addition, through Cross-Site Scripting, malicious code is injected into the site. If users enter a site where malicious code has been placed there by the hackers, they will be hacked.

How to prevent cross-site scripting attacks?

How to Prevent Cross-Site Scripting Attacks 1 Non-Persistent Cross-site scripting attack. Non-persistent XSS is also known as reflected cross-site vulnerability. ... 2 Persistent cross-site scripting attack. Persistent cross-site scripting is also known as stored cross-site scripting. ... 3 DOM-based cross-site scripting attack. ...

What is SQL injection and how does it affect your application?

While SQL Injection can affect any data-driven application that uses a SQL database, it is most often used to attack web sites. SQL Injection is a code injection technique that hackers can use to insert malicious SQL statements into input fields for execution by the underlying SQL database.


2 Answers

Just doing a lot of stuff that you don't really understand, is not going to help you. You need to understand what injection attacks are and exactly how and where you should do what.

In bullet points:

  • Disable magic quotes. They are an inadequate solution, and they confuse matters.
  • Never embed strings directly in SQL. Use bound parameters, or escape (using mysql_real_escape_string).
  • Don't unescape (eg. stripslashes) when you retrieve data from the database.
  • When you embed strings in html (Eg. when you echo), you should default to escape the string (Using htmlentities with ENT_QUOTES).
  • If you need to embed html-strings in html, you must consider the source of the string. If it's untrusted, you should pipe it through a filter. strip_tags is in theory what you should use, but it's flawed; Use HtmlPurifier instead.

See also: What's the best method for sanitizing user input with PHP?

like image 77
troelskn Avatar answered Oct 02 '22 21:10

troelskn


The best way against SQL injection is to bind variables, rather then "injecting" them into string. http://www.php.net/manual/en/mysqli-stmt.bind-param.php

like image 44
vartec Avatar answered Oct 02 '22 22:10

vartec