Username:
Password:
Remember me:
Register

Back to forum: Web Programming (HTML, JS, CSS, PHP, MySQL)


Search forums via Google


0 Users appreciate this thread.

SQL Injection
Started by SPCOxion
(2014-04-22 01:38:21)
SPCOxion (2014-04-22 01:38:21)
This forum will mainly be about how to SQL inject, and how to protect against it.

Brief explanation of what it is show
So, what is SQL injection, anyway?

Say you have a register on your site. You have people type in their username and password. Of course, you're going to insert that data into a database... right?

Unless you're careful, people can hack your site like this.

Assuming you're storing the data in variables (i.e. $username = $_POST['username'] ), and then putting those variables into a string you'll user as a SQL query (i.e. $q = "INSERT INTO table VALUES (" . $username . ", " . $password . "", then you're vulnerable to SQL injection.

What happens if they type in bob"; TRUNCATE table?

Well, that's what SQL Injection is.

This isn't the only way to SQL Inject, so be careful.


How to do it show
If you read the introduction, then you already have an idea how to do it.

There are a few ways to do this, but the simplest way would be to type something into a login.

You type something like this in:
thisismyusername"; DROP something;

Hit enter/submit, and you're done.
(if it worked)


How it works show
How does it work?

Well, think about it for a second.
When you're logging in, this is the gist of what's going on in the code:

Source Code
<?php $a = $_POST['something1']; $b = $_POST['something2']; /*validation code shet here*/ mysqli_query($conn, "INSERT INTO something VALUES (" . $a . ", " . $b . ""; ?>


Emphasis on the mysqli_query.

Now, since they're putting a variable that you control into an SQL command, you can mess sht up.

Once you type something like tom"; TRUNCATE something into the thingymabob that's going to be put into an SQL command, you can pretty much do whatever you want with SQL.

Why?

Because then the SQL command would look like this:
Source Code
INSERT INTO table VALUES (tom); TRUNCATE something)


Now, obviously, that code has a few bugs in it, but you can sort those out.


How to protect against it show
How do you protect against it?
Very simple, if you're using MySQLi, like you should be.

Before you insert those variables into a database, do something like this...

Source Code
$a = $_POST['something']; $a = mysqli_real_escape_string(Connection info here, $a);


That's the simplest way, if not the only way; If you're that concerned about it, do a Google search about advanced protection methods or sumthin.

SPCOxion (2014-04-23 18:22:47)
this is about SQL injection, not PDO injection

nor is it about why we should and shouldn't use PDO
CoolApps (2014-05-01 16:54:46)
I wouldn't just use real escaping.


How it really works?, well...
When you're entering something with a quote such as 1=1', that closes the username and/or password in the query, 1=1 is always true and will select the first row, making you login into a user in ID 1.
If you want to investigate this vulnerability, refer to Google.


SQL Injectable sites, do ' at the end of the ID in a URL.
Also, in a vulnerable login, do 1=1' in the username and password box.


Do NOT try this on other people's site without permission.

This post has been edited one or more times, the last time was:
2014-05-02 00:22:25

Newer account: NodePoint
 

Log in to submit a comment

This topic's ID: 69267

Back to forum: Web Programming (HTML, JS, CSS, PHP, MySQL)




Total registered users: 8321
New registered users today: 7
Newest registered user: ElegantVulpes

©  Copyright 2026 3DSPlaza. All Rights Reserved