SQL Injection Dangers


SQL Injection Dangers

Michael
Maria, can you believe that even in 2016, major companies were still getting hit by SQL injections? It's mind-boggling how prevalent it was, and honestly, still is to some extent.

Maria
It truly is, Michael. The consequences are severe, too. If you mess up, all your users' private data, like usernames and passwords, could end up exposed. That's a massive PR nightmare and legally problematic under data protection acts.

Michael
Absolutely. And just to be clear for our listeners, attempting these kinds of hacks on systems you don't own is illegal and will lead to jail time. We're only discussing this for educational purposes, on our own controlled environments.

Maria
Exactly. I remember watching an older Computerphile video where they explained SQL injections really well. So, instead of re-explaining the basics, I thought we could demonstrate just how easy it is to exploit a vulnerable website.

Michael
That's a great idea. It really drives home the danger for anyone in web development. I've set up a super basic, intentionally vulnerable web shop here, just a blue gradient and a search box. It's built on PHP, and I've intentionally not escaped strings.

Maria
Okay, so let's try a simple search. If I type 'hammer' into your shop's search bar, what happens? Does it pull up some results?

Michael
Yeah, if I search for 'hammer,' it brings up a claw hammer and a sledgehammer. And if I search for 'nails,' it shows different types of nails. So, the basic search functionality works as expected.

Maria
Right. So when I type 'hammer' and hit submit, a POST request goes to the PHP backend. PHP then constructs an SQL query like 'SELECT * FROM products WHERE name LIKE '%hammer%'' and retrieves the data.

Michael
Precisely. The problem begins if we insert a special SQL command character into that text box. It can malform the string PHP sends to the database, either making it nonsensical or creating two separate queries.

Maria
The first step to finding a vulnerability is to test if the input field is susceptible. If I put a single quote, like an apostrophe, into the search box and submit it, what should happen if it's vulnerable?

Michael
If it's vulnerable, you'll likely get a 'server error' message, maybe with an apology for the inconvenience. A secure site would either treat it as a literal character or say 'no product with name single quote,' because it's been properly escaped.

Maria
So, that server error is actually good news for an attacker because it confirms vulnerability. Now, to exploit it, we need to guess what the underlying SQL query might look like. I'm thinking something like 'SELECT * FROM some_table WHERE some_column LIKE '%[user_input]%''.

Michael
That's a good hypothesis. If I search for 'amme,' which is in the middle of 'hammer,' it still finds hammers, confirming those wildcards on either side of the input. So, yes, that's likely the structure.

Maria
Okay, so we can control that 'hammer' part of the query. I'm just visualizing this in a text editor with SQL syntax highlighting to help understand how the query changes. If I just input a single quote, the highlighting shows it breaks the SQL string, just as we suspected.

Michael
Exactly. The syntax highlighting graphically represents the malformed query that the PHP would send, confirming why we get that server error. It’s not a valid query, and the database throws an error.

Maria
So, our goal now is to turn that single quote into something that gives us information. First, it's crucial to identify the database management system. Is it MySQL, PostgreSQL, SQL Server? They all use SQL, but have slightly different syntax.

Michael
Knowing the specific DBMS allows us to target it directly, for example, by finding where it stores lists of table names, which would be incredibly helpful in filling in those blanks in our query. That's step one.

Maria
To start, we can get rid of the rest of the malformed query by adding an 'end of command' and a 'comment' after our single quote. In MySQL, that's usually ' --' or a semicolon. The syntax highlighting shows it turning gray, meaning it's commented out.

Michael
Right, so if my input is ' ' --', the query effectively becomes 'SELECT * FROM products WHERE name LIKE ''%' --', which means it selects everything. And look, it returns all the products in the database. Not a very good shop indeed!

Maria
That's a huge start. Now, let's try to figure out the DBMS. We can insert a command that only works on a specific system. For MySQL, there's a 'SLEEP()' function. Let's try 'hammer' UNION SELECT SLEEP(2) from some_table --'.

Michael
So, you're looking for 'hammer' AND for the database to sleep for two seconds? My hypothesis is if there are two hammers, it should take four seconds, because it processes the sleep for each row. Let's copy that whole string into the search box.

Maria
It's thinking... it's really thinking! And it took exactly four seconds! Even though it didn't return any products (my guess on the return value was off), the fact that it slept for four seconds confirms it's MySQL. This is a 'blind SQL injection' technique.

Michael
That's pretty clever. Even without direct output, the timing tells you what's happening. If it hadn't slept, we'd try the equivalent for PostgreSQL or SQL Server. You could also use this to test for table names, 'SELECT * FROM potential_table AND SLEEP(2)'.

Maria
Exactly, if it comes back slowly, you know that table exists. It takes a bit longer, but it's effective. Now that we know it's MySQL, and we're getting output to the screen, we can go further to extract information.

Michael
This isn't a blind injection anymore because the shop actually displays the product list. Shops often convert table data into HTML for display, so we can leverage that. We'll use the UNION keyword to stick our desired data onto the existing product list.

Maria
The UNION keyword lets us combine the results of two SELECT statements, as long as they have the same number of columns. We need to figure out how many columns the original 'hammer' query is selecting. Let's hypothesize three.

Michael
So, we'll try ' ' UNION SELECT 1,2,3 FROM dual --'. 'Dual' is a placeholder table in MySQL often used for testing. If this works, it should append '1, 2, 3' as a row to our hammer results.

Maria
Alright, let's try it. Copying that into the search bar... and there it is! Below the hammers, we see '1', '2', '3'! This is catastrophic for the website owner because we can output arbitrary query results to the screen.

Michael
Catastrophic is right. We're only a few steps away from getting user credentials. Instead of 1, 2, 3, let's select something more informative from MySQL's 'information_schema.tables' table.

Maria
Ah, the 'information_schema' is a metadata database that holds information about all tables and columns! So we can query that directly. Let's try ' UNION SELECT table_name, table_schema, 3 FROM information_schema.tables --'.

Michael
The syntax highlighting recognizes 'information_schema.tables,' which is a good sign. Let's paste that in. So, we're literally asking the database for its own table of tables, appending it to our output.

Maria
And there they are! Below the hammers, we see all these table names. We've got 'claw hammer', 'sledgehammer', then some internal MySQL tables, and then, crucially, 'stock' and 'users' for our shop database. The 'users' table sounds very interesting!

Michael
Indeed. Now that we know the table name is 'users,' we need to find out its column names. We can't just 'SELECT *' because it might have more than our three columns, which would break the UNION.

Maria
Right, so we'll query 'information_schema.columns' to get column details specifically for the 'users' table. ' UNION SELECT column_name, 2, 3 FROM information_schema.columns WHERE table_name = 'users' --'.

Michael
Perfect. Let's copy and paste that in. And there we have it! Below our hammers, and some other MySQL data, we see 'user_ID', 'user_login', 'user_hash', and 'user_type'. Those are the column names from the 'users' table!

Maria
This is it. 'user_login' is likely the username, and 'user_hash' is almost certainly a hashed password. Even if it's hashed, it's a prime candidate for cracking. Let's extract those actual values now.

Michael
So, for our final act, we'll construct a query to select 'user_login', 'user_hash', and 'user_type' directly from the 'users' table. ' UNION SELECT user_login, user_hash, user_type FROM users --'.

Maria
And there you have it! We've got a list of users, their hashed passwords, and their user types. And look, one of them is an 'admin'! That's who an attacker would focus on first for password cracking.

Michael
It's genuinely scary how quickly that was achieved with just a few queries. And for our listeners, these aren't real passwords, just test data like 'password,' so don't waste your time trying to crack them.

Maria
The takeaway for web developers is critical: you must sanitize all inputs and use parameterized queries. This prevents special characters from being interpreted as SQL commands. Otherwise, this kind of data exposure is inevitable.

Michael
And it's not just basic injections. There are 'second-order SQL injections' where a malicious input, even if initially escaped, gets stored and then executed later when the application uses it in a different query, like when changing a password.

Maria
So, constant vigilance is key. Remember the TalkTalk hack a few years ago? That was essentially done with a script that performed exactly what we just did, but in bulk and incredibly fast. Three commands on this site, and all that data could be dumped to a file.

Michael
It's a stark reminder that neglecting input sanitization and secure coding practices is embarrassing, illegal, and puts your customers' data at severe risk. Protect those inputs!

Ask anything
We've read this whole study set, so questions about it get the best answers.