100% found this document useful (1 vote)
2K views8 pages

WebGoat SQL Injection Tutorial

This document provides an introduction to SQL injection using the WebGoat training platform. It summarizes 13 challenges that demonstrate different SQL injection techniques, including retrieving data, modifying data, adding/removing columns, modifying privileges, numeric injection, roleplaying scenarios, query chaining, and dropping tables. The challenges progress from basic queries to more advanced techniques like commenting queries and covering tracks. Tips are provided for each challenge to help understand the SQL queries.

Uploaded by

jquga88
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views8 pages

WebGoat SQL Injection Tutorial

This document provides an introduction to SQL injection using the WebGoat training platform. It summarizes 13 challenges that demonstrate different SQL injection techniques, including retrieving data, modifying data, adding/removing columns, modifying privileges, numeric injection, roleplaying scenarios, query chaining, and dropping tables. The challenges progress from basic queries to more advanced techniques like commenting queries and covering tracks. Tips are provided for each challenge to help understand the SQL queries.

Uploaded by

jquga88
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Introduction to SQL Injection
  • Modifying Data with SQL Queries
  • Granting Permissions in SQL
  • Challenges and Examples
  • Advanced SQL Statements

@BlackSheepSpicy

WebGoat SQL Injection (Introduction)

WebGoat SQL Injection


(Introduction)
Twitter: @BlackSheepSpicy
Twitch: ​[Link]

Settle in ladies and gentlemen, we’re in for a long one. Also keep in mind I am in
no way a SME regarding SQL so the methods used here might not be (read: are
probably not) best practices.

2. Onto the first challenge then:

Before we go any further, let’s all take a look at the example table that OWASP
has provided further up the page:

Lowkey feel a bit spoiled with the amount of information we have to complete this challenge​.
@BlackSheepSpicy
WebGoat SQL Injection (Introduction)

Armed with all the database info we could ever want, we can now construct our
query:

Lets go ahead and break down this query anyway to inflate my ego a bit, if you’re
already familiar with SQL feel free to skip this section:
- select:​ pretty straightforward statement, just tells the database to return the
following information.
- *: ​this symbol is a bit vague, but in SQL (and in many other contexts) this is
known as a wildcard character, basically it’s just short for the word “any” or “all”
- from employees: ​just telling the database which table to fetch the info from (in
this case the ​employees​ table)
- Where last_name = ‘Franco’: ​conditional statement where we specify that we
only want rows where the column ​last_name​ has only the word “​Franco​” in it.

3. Moving on, we are now tasked with modifying data with a query:

There’s a couple new statements to be used here from the previous challenge,
but once again not the worst thing ever, especially when we can reference the example
in the previous challenge:

All this query does is tells the database to ​update​ the table ​employees ​to ​set
the column ​department ​to ​sales ​for any entry ​where ​the column ​last_name​ has only
the letters “​Barnett​”.
@BlackSheepSpicy
WebGoat SQL Injection (Introduction)

4. So now we know how to modify existing data, what if we want to add or remove
variables (or columns, depending on how pedantic you want to be)? Let’s take a look at
the challenge on page 4:

Now your first reaction might be to use the update statement again here
(hopefully not, please be smarter than me and read the content webgoat offers in the
pages) however instead we must instead use a new statement: ​alter.​ The resulting
query looks like this:

This query tells the database to ​alter ​the ​table employees ​to ​add ​the variable
(column) ​phone ​which will be a ​variable character field ​that can contain a maximum of
20 ​characters.

5. SQL can do a lot of different things, and when I say a lot I mean ​a lot​. Some
might say too much (provided we have admin rights but honestly when has that ever
stopped us before?) and honestly I'd be inclined to believe them. Enter the challenge on
page 5:

Yep… we can modify privileges straight from queries… that’s… that’s where we’re at...

Alrighty so… we got our objective… let’s… let's write our query then….
@BlackSheepSpicy
WebGoat SQL Injection (Introduction)

If you’ve followed along with the previous challenges in this writeup this should
be pretty clear. All it says is to ​grant ​the permission ​alter table​ ​to UnauthorizedUser
seriously who’s fucking idea was this?
9. Cool! We know the basics of SQL, now lets see how we can break it! Forward
unto the page 9 challenge (after taking a look at the couple pages prior to this, there’s
some bomb info there regarding SQLI):

So… its a drop down menu, not as exciting as manually typing it out and feeling
1337 af but its still good practice, let’s break down my solution here (after I correct my
notes because I somehow managed to write down the wrong solution, I'm a professional
btw):

Or… we could just let WebGoat steal my thunder, that’s fine...

SQL is like a basic gen x kid: as long as you keep saying true it’ll spill its guts to you if left unchecked.

10. Welp we know how string SQL injections work now for the most part, turns out
we can do it with other data types too:
@BlackSheepSpicy
WebGoat SQL Injection (Introduction)

Well… we would do a numeric SQLI if we wanted to follow the rules… but like
WebGoat said before… all we have to do is have a statement return true… so lets do
that:

I mean we returned true ¯\_(ツ)_/¯

It’s also worth mentioning that rather than methodically looking for the injection
flaw I just kinda fuzzed both inputs with “or true” until something good happened.

11. Y’all wanna get into some roleplay? Because OWASP is into it apparently:
@BlackSheepSpicy
WebGoat SQL Injection (Introduction)

So once again rather than doing this with any sort of tact we can just use the
shotgun method again using everything we have learned up to this point:

So in this instance we use ​true ​for the employee name because… well… they let
us… but notice how we had to use a different injection string for the TAN input, this is
because the database throws a fit if we give it an incorrectly formed string, so with that
we can just chuck in anything we want as long as we manage to finesse a true or
condition somewhere in there.

12. Continuing in our twisted roleplay, we are presented with a rather charged
challenge:
@BlackSheepSpicy
WebGoat SQL Injection (Introduction)

So let’s go commit a felony! Complete with new techniques to dive into!:

Query was too long to show in the input field, so here’s what we got to work with

So as we can see, pretty standard fare here for the most part, but take a look at
the beginning and end of the Employee Name input field we have the following
characters:
- ‘;​ : So we have the single quote to complete the string, and then we have a
semicolon following it up. The keen eyed among you might have noticed them at
the end of my previous queries. In SQL a semicolon is used to signify the end of
a query, though in this context we can use this to kill the original query and then
tack whatever query we want after the semicolon through something called
Query Chaining​.
- -- ​: In SQL two dashes are used to denote a comment, meaning the database
will ignore everything contained within it and another set of double dashes.
Which in our case, means everything after this statement is ignored, meaning
only our injected query will be processed, which is why we can leave TAN empty
and the database won’t throw a fit.

13. So now we’re waist deep in shit due to OWASP’s twisted fantasies, and I don’t
know about y’all but I ain't getting arrested over this, we have to cover our tracks:

It’s only illegal if we get caught right?


@BlackSheepSpicy
WebGoat SQL Injection (Introduction)

We also get introduced to a new statement: the confusingly named ​drop


statement (No idea why it isn’t just delete but we use what we got)! The drop statement
allows someone to completely remove a table from the database. Perfect for our
needs!:

So in this challenge we can just simply use query chaining and our comment
statement for added overkill then inject our drop statement to get away with it scott free
(until they load the log backups of course, then we’re a bit screwed...)

I really hope this write up will be helpful while getting into webapp pentesting! If
you want to see me do these challenges live be sure to drop by my Twitch when I’m live
and also follow my Twitter for some fresh memes!

Common questions

Powered by AI

Privileges can be modified directly through SQL queries by using commands like 'grant' to assign permissions such as 'alter table' to unauthorized users. This is dangerous because it allows attackers to escalate their access rights, potentially leading to compromised data integrity and unauthorized data manipulation .

The 'drop' statement in SQL completely removes a table from a database, erasing all records and table structure. Misuse of this command can lead to significant data loss and operational disruption, especially in cases of unauthorized access where an attacker may execute it to destroy evidence or cause damage .

Comments in SQL (denoted by '--') are used in injection attacks to manipulate query execution. By appending a comment, an attacker can nullify the rest of the original query, ensuring that only the injected part is executed. This technique facilitates altering query logic without syntax errors .

Yes, SQL injections can be executed on non-string input fields by using conditions that always evaluate to true, such as numeric fields using 'or 1=1'. This allows attackers to exploit queries dynamically depending on their logic, bypassing intended logic for numeric or other data-type entries .

The semicolon is used to end a SQL statement, allowing an attacker to terminate the original query and add a new one in an SQL injection attack, a technique known as Query Chaining. The double dash ('--') is used to denote a comment in SQL, meaning everything following it in the query is ignored by the server, which enables attackers to inject queries without executing unwanted parts of the original SQL statement .

Using the wildcard character '*' in SQL queries retrieves all available columns from the specified table. While this simplifies query writing, it can lead to inefficiencies by increasing data retrieval volume, especially when only specific columns are needed, and may expose more data than necessary, posing a potential security risk .

The 'select' statement in SQL is used to instruct the database to return certain data. The '*' wildcard character represents all columns in the table. The 'from' statement specifies which table the data should be retrieved from, directing the selection process to the specified table, in this case, the 'employees' table .

SQL injection vulnerabilities can be exploited by appending a condition that always evaluates to true, such as 'or true', causing the database to process the query in a way that reveals more information or grants unauthorized access. This technique can bypass authentication by tricking the database into always returning a valid result .

The 'alter' statement in SQL is used to modify an existing database structure, such as adding or removing a column in a table. In contrast, the 'update' statement is used to modify existing data within a table. 'Alter' changes the schema, while 'update' changes the data .

Role-play exercises, like those suggested by WebGoat, enhance understanding and prevention of SQL injection by simulating real-world attack scenarios. Participants act as both attackers and defenders, gaining insight into vulnerabilities and defense mechanisms, fostering a deeper, practical understanding of cybersecurity challenges and solutions .

@BlackSheepSpicy 
WebGoat SQL Injection (Introduction)  
WebGoat SQL Injection 
(Introduction)  
Twitter: @BlackSheepSpicy 
T
@BlackSheepSpicy 
WebGoat SQL Injection (Introduction)  
Armed with all the database info we could ever want, we can now cons
@BlackSheepSpicy 
WebGoat SQL Injection (Introduction)  
4.
So now we know how to modify existing data, what if we want to ad
@BlackSheepSpicy 
WebGoat SQL Injection (Introduction)  
If you’ve followed along with the previous challenges in this writeu
@BlackSheepSpicy 
WebGoat SQL Injection (Introduction)  
Well… we would do a numeric SQLI if we wanted to follow the rules… b
@BlackSheepSpicy 
WebGoat SQL Injection (Introduction)  
So once again rather than doing this with any sort of tact we can ju
@BlackSheepSpicy 
WebGoat SQL Injection (Introduction)  
So let’s go commit a felony! Complete with new techniques to dive in
@BlackSheepSpicy 
WebGoat SQL Injection (Introduction)  
We also get introduced to a new statement: the confusingly named ​dr

You might also like