327: Exploits of a Mom
|Exploits of a Mom|
Title text: Her daughter is named Help I'm trapped in a driver's license factory.
Mrs. Roberts receives a call from her son's school. The caller, likely one of the school's administrators, asks if the she really named her son
Robert'); DROP TABLE Students;--, a rather unusual name. Perhaps surprisingly, Mrs. Roberts responds in the affirmative, claiming that she uses the nickname "Little Bobby Tables". As the full name is read into the school's system's databases without data sanitization, it causes the student table in the database to be deleted.
The title of this comic is a pun—exploit can mean an accomplishment or heroic deed, but in computer science the term refers to a program or technique that takes advantage of a vulnerability in other software. In fact, one could say that her exploit is to exploit an exploit (her achievement is to make use of a vulnerability). The title can also refer to her choice of name for her son, which is rather extraordinary.
In SQL, a database programming language, commands are separated by semicolons
; and strings of text are often delimited using single quotes
'. Parts of commands may also be enclosed in parentheses
). Data entries are stored as "rows" within named "tables" of similar items (e.g.
Students). The command to delete an entire table (and every row of data in that table) is
DROP, as in
DROP TABLE Students;).
The exploited vulnerability here is that the single quote in the name input was not correctly "escaped" by the software. That is, if a student's name did indeed contain a quote mark, it should have been parsed as one of the characters making up the text string and not as the marker to close the string, which it erroneously was. Lack of such escaping is a common SQL vulnerability; this type of exploit is referred to as SQL injection. Mrs. Roberts thus reminds the school to make sure they have added data filtering code to prevent code injection exploits in the future.
For example, if the site was running PHP, the code might store the student's name in a variable called
$name, and generate an SQL statement to search the database and check that the name is valid, like this:
$sql = "SELECT * FROM Students WHERE (first_name='$name');";
For a student named "Annie", this would give the following SQL command:
SELECT * FROM Students WHERE (first_name='Annie');
which is a valid command where the 5-character string "Annie" has been substituted for "$name" in the PHP code above. However, with Mrs. Roberts' exploit, the SQL command becomes:
SELECT * FROM Students WHERE (first_name='Robert'); DROP TABLE Students;--');
As semicolons separate statements, this will be read by the interpreter as three commands:
SELECT * FROM Students WHERE (first_name='Robert');
DROP TABLE Students;
The first line runs as normal, caused by the
'); punctuation in part of Little Bobby Tables' name properly closing the current command. The second injected command then does the damage, deleting the student records from the school's database. The third line begins with two hyphens
-- which are used to mark a comment in SQL, meaning that the interpreter ignores it as well as the partial fragment of code originally after
$name in the PHP statement.
For this to work, it helps to know a little about the structure of the database. But it's quite a good guess that a school's student management database might have a table named
Students. Mrs. Roberts' exploit also assumes that the person who wrote the code used exactly one set of parentheses around
(first_name='$name') in the PHP example, so that the single close parenthesis in the name could match it, which apparently was a successful guess. Of course, in real life most exploits of this kind would be performed not by socially engineering a person's name such that it would eventually be entered into a database query, but rather by accessing some kind of input system (such as a website's login screen or search interface) and guessing various combinations by trial and error until something works, perhaps by first trying to inject the
SHOW TABLES command to see how the database is structured.
This xkcd comic has become rather famous, spawning at least one site about preventing SQL injection named http://bobby-tables.com.
- [Mrs. Roberts receives a call from her son's school.]
- Caller: Hi, This is your son's school. We're having some computer trouble.
- Mrs. Roberts: Oh, dear - did he break something?
- Caller: In a way -
- Caller: Did you really name your son
Robert'); DROP TABLE Students;--?
- Mrs. Roberts: Oh, yes. Little Bobby Tables, we call him.
- Caller: Well, we've lost this year's student records. I hope you're happy.
- Mrs. Roberts: And I hope you've learned to sanitize your database inputs.