£199.99 £139.99 for one year of Unlimited learning. Offer ends on 14 November 2022 at 23:59 (UTC). T&Cs apply

Find out more
Mitigation
Skip main navigation

Mitigation

This video explains how to identify vulnerabilities to injection attacks by assessing the source code.
6.6
Welcome to the third and last part of Injection Flaws session. In this last part, we will discuss injection flaws mitigation. We will start discussing what makes an application vulnerable. Since OWASP Juice Shop is open source, we will spot the vulnerable source code. Then we will discuss how to avoid such vulnerabilities. First big mistake– Juice Shop asks for an email address, but no validation was performed upon our payload. A validation such as this one would have aborted the backend execution before merging submitted data with the SQL query template. Sometimes, required data doesn’t have a well-defined pattern as an email address, and so validation doesn’t come easy.
47.5
In such cases, the characters range should be limited and dangerous ones removed from the input or the input rejected. Client provided data is directly concatenated to a SQL query template in and by the backend server. Remind that the backend server is written in a programming language other than SQL, so it knows nothing about how to handle SQL queries. The backend is just doing arbitrary strings concatenation. Some characters have special meaning to specific tools or contexts. In our SQL query, the single quote character is used to wrap strings like the email address.
83.7
When external data is appended to the SQL query template inside single quotes, and that data contains one or more single quote characters, then the resulting query will be different from what the template was written for. To avoid this, the single quote character in external data has to be escaped so that it is read as is with no special meaning. Escaping is not an easy task to be done externally to the target tool or context. To do it, you will need to know all special characters beforehand, and you’ll certainly miss one or a few of them. Let’s see what makes Juice Shop vulnerable.
121.6
This is the OWASP Juice Shop project page, and we’re going to jump from here to the project repo.
131.4
The project repo has all the source code, but now, we are looking for a specific folder, which should have all application roots.
146.7
Inside this folder, we should find the login.js file, which should have all the login features.
163.4
We have our first login function, and then in or after login function, but this one doesn’t look to have any SQL query template.
178.2
In the next one, we can see some select from the user’s table, and then the email and password both are matched against user input data. So this is a good candidate. Let’s copy and paste it to our text editor and make it readable.
229.5
This is how the query template looks like before interpolation. We’re going to replace the variables with the admin account details and see how the final query looks like.
246.5
So this is what gets sent to the database and executed on a legit login request. Let’s do the same with our payload.
272.8
Since what’s after the semicolon is ignored by the database server, this is the statement that gets executed.
284.5
When we are dealing with SQL, prepared statements are that safety zone. Instead of concatenating a SQL template with user input data in the backend server without the right context, you’ll first provide a SQL interpreter, the query template with some placeholders where you expect user input data to be replaced. On a second step, you will provide input data to replace the placeholders, but this operation will be done by the SQL interpreter, which knows its one context better than anything or anyone else. Always validate external data. When it does not meet your expectations or requirements, reject it. Make sure that provided data only has allowed characters. Whitelists tend to be safer than blacklists.
329.5
You know better what you want than what you don’t. As I said before, escaping is not an easy task. If there’s no safe API available, look for well documented and maintained escaping libraries specific to your target interpretor. When dealing with SQL queries, always limit the number of records to return to mitigate huge data leaks in case you get compromised. In our next session, we will discuss broken authentication flaws. Until then, take your time to carefully read the injection section of OWASP Top 10.

This video explains how to identify vulnerabilities to injection attacks by assessing the source code. Additionally, how to prevent these injection flaws from occurring is also explained.

Reading source code can be time consuming if you are not familiar with the system. In this video, you will learn how to identify the specific areas of code that may make a system vulnerable to injection attacks. Once you understand how to identify flaws in the code, you will learn how to change the code to mitigate attacks and increase your security.

Investigate and share: Go to this document on cheatsheets and read the information there. Share what you found interesting or useful in the comments section below.

This article is from the free online

Advanced Cyber Security Training: OWASP Top 10 and Web Application Fundamentals

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education