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.
The title of this comic is a pun - an exploit can simply mean an accomplishment or heroic deed, but in computer science it means a program or technique that takes advantage of a vulnerability in other software. In fact her exploit is to exploit an exploit (her achievement is to make use of a vulnerability). We can also assume that she regards the name she has given her son as an extraordinary deed.
Mrs. Roberts receives a call from her son's school. The caller asked if the she really named her son
Robert'); DROP TABLE students;-- and the mom claimed that they used the nickname "Little Bobby Tables". As the full name is read into the database without "sanitization" it causes the student table to be deleted.
In SQL, commands are separated by semicolons
; and data is often quoted using single quotes
'. Commands may also be enclosed in parentheses
). Data is stored in tables of similar items (e.g.
students) and individual entries are "rows" in the table. To delete an entire table (and every row of data in that table), you use the command
DROP TABLE students).
); closes the current command allowing the
DROP TABLE students; command to run. The
-- that follows it tells the interpreter to ignore everything after it so that the injection is complete. At the end, the school informs the mom that her exploit was successful and the mom reminds the school to make sure they have added data filtering code to prevent code injection exploits in the future.
The exploited vulnerability is that the single quote in the name input was not properly "escaped" by the software. Thus, when the name is embedded into some SQL statement, the quote is erroneously parsed as a closing quote inside that statement, rather than being parsed as part of the name. Lack of such escaping is a common SQL vulnerability; this type of exploit is referred to as SQL injection.
For example, if the site was running PHP, the code might take the student's name in a variable called $student and generate an SQL statement to check that the name is valid, like this:
$sql = "SELECT * FROM Students WHERE (student_name='" . $student . "')";
For a student named "Annie", this would give the following SQL command:
SELECT * FROM Students WHERE (student_name='Annie')
However, with Mom's exploit, this becomes:
SELECT * FROM Students WHERE (student_name='Robert'); DROP TABLE Students;--')
That can be seen as three statements, separated by semicolons: the
SELECT which runs as normal; the
DROP TABLE command which does the damage; and a comment, which consists of everything from the
-- to the end of the line (thus "eating" the
"')" characters) and prevents SQL from saying "hold on, there's some goofy syntax in this line".
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 student management database might have a table called
"Students". Mom's code also assumes that the person who wrote the SQL used exactly one set of parentheses around
"student_name='Robert'"; that might need a little trial and error to get it to work. (Of course, most exploits of this kind are done not by social-engineering the hapless user into typing your kid's funny name, but by getting access to the system yourself and trying lots of combinations until something works, probably starting with an injection of the
SHOW TABLES command to see how the database is structured.)
There is a 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.