SQL Injection
Learn about how SQL injection works and its impact.
We'll cover the following
Storytime
Let’s start with a story. Mike is the system admin for a small private school. His main responsibility is maintaining the network and computers. Recently, he started automating various tasks around the school by building a web application for internal use. He has no formal training and minimal programming experience. Knowing the basics of PHP, he built a pretty stable customer relationship manager for the school and even received kudos from the superintendent for streamlining operations and saving the school money.
Everything was going well for Mike until a particular new student started. The student’s name is Little Bobby Tables. One day, Jon from the admin office called Mike to ask why the system was down. After inspection, Mike found that the table containing all the students’ information was missing entirely. You see, Little Bobby’s full name is actually “Robert’); DROP TABLE students;–”. There aren’t any backups of the database; it has been on Mike’s “to do” list for a while, but he hadn’t gotten around to it yet. Mike is in big trouble.
SQL injection
Real world
While it’s unlikely a child’s name will contain harmful SQL code, this kind of SQL injection vulnerability happens in the real world all the time:
- In 2012, LinkedIn leaked over 6 million users’ data due to an undisclosed SQL injection vulnerability.
- In 2012, Yahoo! exposed 450,000 user passwords.
- In 2012, 400,000 passwords were compromised from Nvidia.
- In 2012, 150,000 passwords were compromised from Adobe.
- In 2013, Harmony had roughly 1.5 million user passwords exposed.
For most of these, precise details are undisclosed. We can’t be sure these were due to SQL injection attacks, but it is likely.
How SQL injection works
If you use user input without modification, a malicious user can pass unexpected data and fundamentally change your SQL queries.
If your code looks something like this:
UPDATE users
SET first_name="' + req.body.first_name + '" WHERE id=1001;
You would expect the generated SQL to be:
UPDATE users
SET first_name="Liz" WHERE id=1001;
But if your malicious user types their first name as:
Liz", last_name="Lemon"; --
The generated SQL then becomes:
UPDATE users
SET first_name="Liz", last_name="Lemon"; --" WHERE id=1001;
Now all of your users are named Liz Lemon, and that’s just not cool.
SQL injection in action
If preventive measures are not taken, SQL injection attacks can cause many problems. Let’s inject some SQL code. Hitting the RUN button will open a web application. The edit function is vulnerable. Try to exploit it!
Open the application in a new tab by clicking on the URL below. If you perform any injection attack, refresh the page to see the changes.