Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the SQL injection from the "Bobby Tables" XKCD comic work?

Just looking at:

XKCD Strip(Source: https://xkcd.com/327/)

What does this SQL do:

Robert'); DROP TABLE STUDENTS; -- 

I know both ' and -- are for comments, but doesn't the word DROP get commented as well since it is part of the same line?

like image 448
Blankman Avatar asked Dec 01 '08 21:12

Blankman


People also ask

How does the SQL injection work?

In SQL Injection, the UNION operator is commonly used to attach a malicious SQL query to the original query intended to be run by the web application. The result of the injected query will be joined with the result of the original query. This allows the attacker to obtain column values from other tables.

What is a Bobby table?

The name Bobby Tables inspired a website, bobby-tables.com, a guide for beginning programmers to learn the right way to avoid SQL injection in their code. A similarly named character, Mister Rogers, appears in 884: Rogers St., with the same code injection in his middle name.

What is SQL injection How does it works explain with an example?

SQL injection, also known as SQLI, is a common attack vector that uses malicious SQL code for backend database manipulation to access information that was not intended to be displayed. This information may include any number of items, including sensitive company data, user lists or private customer details.

What are the 3 classes of SQL injection attacks?

SQL Injection can be classified into three major categories – In-band SQLi, Inferential SQLi and Out-of-band SQLi.


2 Answers

It drops the students table.

The original code in the school's program probably looks something like

q = "INSERT INTO Students VALUES ('" + FNMName.Text + "', '" + LName.Text + "')"; 

This is the naive way to add text input into a query, and is very bad, as you will see.

After the values from the first name, middle name textbox FNMName.Text (which is Robert'); DROP TABLE STUDENTS; --) and the last name textbox LName.Text (let's call it Derper) are concatenated with the rest of the query, the result is now actually two queries separated by the statement terminator (semicolon). The second query has been injected into the first. When the code executes this query against the database, it will look like this

INSERT INTO Students VALUES ('Robert'); DROP TABLE Students; --', 'Derper') 

which, in plain English, roughly translates to the two queries:

Add a new record to the Students table with a Name value of 'Robert'

and

Delete the Students table

Everything past the second query is marked as a comment: --', 'Derper')

The ' in the student's name is not a comment, it's the closing string delimiter. Since the student's name is a string, it's needed syntactically to complete the hypothetical query. Injection attacks only work when the SQL query they inject results in valid SQL.

Edited again as per dan04's astute comment

like image 193
20 revs, 9 users 72% Avatar answered Oct 27 '22 20:10

20 revs, 9 users 72%


Let's say the name was used in a variable, $Name. You then run this query:

INSERT INTO Students VALUES ( '$Name' ) 

The code is mistakenly placing anything the user supplied as the variable. You wanted the SQL to be:

INSERT INTO Students VALUES ( 'Robert Tables` )

But a clever user can supply whatever they want:

INSERT INTO Students VALUES ( 'Robert'); DROP TABLE Students; --' )

What you get is:

INSERT INTO Students VALUES ( 'Robert' );  DROP TABLE STUDENTS; --' ) 

The -- only comments the remainder of the line.

like image 26
sinoth Avatar answered Oct 27 '22 21:10

sinoth