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

This is how it works: Lets suppose the administrator is looking for records of student

Robert'); DROP TABLE STUDENTS; --

Since the admin account has high privileges deleting the table from this account is possible.

The code to retrieve user name from request is

Now the query would be something like this (to search the student table)

String query="Select * from student where username='"+student_name+"'";

statement.executeQuery(query); //Rest of the code follows

The resultant query becomes

Select * from student where username='Robert'); DROP TABLE STUDENTS; --

Since the user input is not sanitized, The above query has is manipulated into 2 parts

Select * from student where username='Robert'); 

DROP TABLE STUDENTS; --

The double dash (--) will just comment out remaining part of the query.

This is dangerous as it can nullify password authentication, if present

The first one will do the normal search.

The second one will drop the table student if the account has sufficient privileges (Generally the school admin account will run such query and will have the privileges talked about above).

Examples related to security

Monitoring the Full Disclosure mailinglist Two Page Login with Spring Security 3.2.x How to prevent a browser from storing passwords JWT authentication for ASP.NET Web API How to use a client certificate to authenticate and authorize in a Web API Disable-web-security in Chrome 48+ When you use 'badidea' or 'thisisunsafe' to bypass a Chrome certificate/HSTS error, does it only apply for the current site? How does Content Security Policy (CSP) work? How to prevent Screen Capture in Android Default SecurityProtocol in .NET 4.5

Examples related to validation

Rails 2.3.4 Persisting Model on Validation Failure Input type number "only numeric value" validation How can I manually set an Angular form field as invalid? Laravel Password & Password_Confirmation Validation Reactjs - Form input validation Get all validation errors from Angular 2 FormGroup Min / Max Validator in Angular 2 Final How to validate white spaces/empty spaces? [Angular 2] How to Validate on Max File Size in Laravel? WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery

Examples related to sql-injection

Preventing SQL injection in Node.js What are good ways to prevent SQL injection? Found 'OR 1=1/* sql injection in my newsletter database How can prepared statements protect from SQL injection attacks? Why do we always prefer using parameters in SQL statements? SQL injection that gets around mysql_real_escape_string() Java - escape string to prevent SQL injection How does a PreparedStatement avoid or prevent SQL injection? How does the SQL injection from the "Bobby Tables" XKCD comic work? Are PDO prepared statements sufficient to prevent SQL injection?