Showing posts with label Hacking. Show all posts
Showing posts with label Hacking. Show all posts
Monday, October 21, 2013
Preventing PHP websites from SQL injections

What is SQL injection:
SQL injection is a way of breaking the websites SQL query and customize it according to the hacker.
For example, if your website query for user to login is:
$u = "Ashwin";
$p = "TheCodePress";
mysql_query("SELECT * FROM users WHERE uname='$u' AND upass='$p' ");
But if i'm a hacker then i'll simply do something like this to break the security of your login system:
$u = "Ashwin' -- ";
$p = "Hacked";
mysql_query("SELECT * FROM users WHERE uname='$u' AND upass='$p' ");
So, what i have done ?
I have simply entered my correct username but after that i have added ' -- and this means comment in SQL.
So we have commented the rest of the portion of the query. That means we now don't need to enter password, we'll directly login into the website.
How to Solve this problem:
Well, the best way to do it without using any library or API is to use in built PHP functions. Such as mysql_real_escape_string(); and htmlentities();
If you are using mysql_real_escape_string function then all the vulnerable symbols will be parse, but it will parse safely.
PHP Code: So the code will be something like this:
$u = mysql_real_escape_string("Ashwin' -- ");
$p = mysql_real_escape_string("Hacked");
mysql_query("SELECT * FROM users WHERE uname='$u' AND upass='$p' ");
Is their any other better way to do it?
Yes, you can use the Library such as PDO or MYSQLI.
If you are more familiar with object oriented programming (OOP) then i'll prefer you to go with PDO.
Soon, i'll be too writing tutorials about PDO and Mysqli. You can learn it now on PHP manual.
Wednesday, October 16, 2013
Protecting your website from Vulnerable Script Tags and Codes

It's really important for you to protect your website from hackers, but without removing any features from your website.
In this post we are going to understand how to solve the problem of XSS - Cross Site Scripting problems.
What is XSS:
Cross-site scripting (XSS) is a type of computer security vulnerability typically found in Web applications. XSS enables attackers to inject client-side script into Web pages viewed by other users. A cross-site scripting vulnerability may be used by attackers to bypass access controls such as the same origin policy.
-Source Wikipedia
Now to prevent our website from XSS attack, we are going to use a PHP function called: htmlentities()
This function of PHP will help your website to parse all harmful tags safely.
For example, if you have a website which displays comments submitted by the user and that comment system is XSS vulnerable, then if a user will enter some comment like this:
Javascript Code:
<script type="text/javascript">
<script type="text/javascript">
window.location = "http://www.google.com";
</script>
and now this comment is stored in your website's comment database. So whenever any user will meet the page where this comment is loaded from your database. Then the user will re-directed to the specified website.
So to prevent this problem we are going to use PHP function: htmlentities()
This function will replace all the < > / " ' = & and other symbols to a non-vulnerable signs. Like to display and in your website we use HTML Entities: &
So in the similar way we are going to covert the vulnerable symbols in to non vulnerable HTML Entities.
You'll just need to wrap htmlentities() to the variable from which you POST the comment to the database.
PHP Code:
<?php
$comment = htmlentities($_POST['comment_area']);
?>
So that's how you can protect your website from XSS. In the next post we'll be discussing about how to prevent our website from getting hacked using SQL-injections.
About the javascript code, if we parse it using the htmlentities() function then this is the safe result and this can be added in our database.
Result:
<script type="text/javascript">
window.location = "http://www.google.com";
</script>
What we have done:
PHP Code:
<?php
$comment =
<<<comment
<script type="text/javascript">
window.location = "http://www.google.com";
</script>
comment;
echo htmlentities($comment);
?>
Liked the post ? subscribe us with your email to get upcoming tutorials directly in your inbox:window.location = "http://www.google.com";
</script>
What we have done:
PHP Code:
<?php
$comment =
<<<comment
<script type="text/javascript">
window.location = "http://www.google.com";
</script>
comment;
echo htmlentities($comment);
?>
Subscribe to:
Posts (Atom)