Unit-Iii (Web Attacks)
Unit-Iii (Web Attacks)
In this section we explain what the same-origin policy (SOP) is and how it is implemented.
This uses the scheme http, the domain normal-website.com, and the port number 80. The
following table shows how the same-origin policy will be applied if content at the above URL tries
to access other origins:
URL accessed Access permitted?
http://normal-website.com/example/ Yes: same scheme, domain, and port
http://normal-website.com/example2/ Yes: same scheme, domain, and port
https://normal-website.com/example/ No: different scheme and port
http://en.normal-website.com/example/ No: different domain
http://www.normal-website.com/example/ No: different domain
*Internet Explorer will allow this access because IE does not take account of the port number
when applying the same-origin policy.
Some objects are writable but not readable cross-domain, such as the location object
or the location.href property from iframes or new windows.
Some objects are readable but not writable cross-domain, such as the length property of
the window object (which stores the number of frames being used on the page) and
the closed property.
The replace function can generally be called cross-domain on the location object.
You can call certain functions cross-domain. For example, you can call the
functions close, blur and focus on a new window. The postMessage function can
also be called on iframes and new windows in order to send messages from one domain
to another.
Due to legacy requirements, the same-origin policy is more relaxed when dealing with cookies,
so they are often accessible from all subdomains of a site even though each subdomain is
technically a different origin. You can partially mitigate this risk using the HttpOnly cookie flag.
It's possible to relax same-origin policy using document.domain. This special property allows
you to relax SOP for a specific domain, but only if it's part of your FQDN (fully qualified domain
name). For example, you might have a domain marketing.example.com and you would like
to read the contents of that domain on example.com. To do so, both domains need to
set document.domain to example.com. Then SOP will allow access between the two domains
despite their different origins. In the past it was possible to set document.domain to a TLD such
as com, which allowed access between any domains on the same TLD, but now modern
browsers prevent this.
Cross-site scripting
In this section, we'll explain what cross-site scripting is, describe the different varieties of cross-
site scripting vulnerabilities, and spell out how to find and prevent cross-site scripting.
Reflected XSS, where the malicious script comes from the current HTTP request.
Stored XSS, where the malicious script comes from the website's database.
DOM-based XSS, where the vulnerability exists in client-side code rather than server-side
code.
If the user visits the URL constructed by the attacker, then the attacker's script executes in the
user's browser, in the context of that user's session with the application. At that point, the script
can carry out any action, and retrieve any data, to which the user has access.
The application doesn't perform any other processing of the data, so an attacker can easily send
a message that attacks other users:
<p><script>/* Bad stuff here... */</script></p>
If the attacker can control the value of the input field, they can easily construct a malicious value
that causes their own script to execute:
You searched for: <img src=1 onerror='/* Bad stuff here... */'>
In a typical case, the input field would be populated from part of the HTTP request, such as a
URL query string parameter, allowing the attacker to deliver an attack using a malicious URL, in
the same manner as reflected XSS.
What can XSS be used for?
An attacker who exploits a cross-site scripting vulnerability is typically able to:
In a brochureware application, where all users are anonymous and all information is
public, the impact will often be minimal.
In an application holding sensitive data, such as banking transactions, emails, or
healthcare records, the impact will usually be serious.
If the compromised user has elevated privileges within the application, then the impact will
generally be critical, allowing the attacker to take full control of the vulnerable application
and compromise all users and their data.
SQL injection
In this section, we explain:
SQL injection (SQLi) is a web security vulnerability that allows an attacker to interfere with the
queries that an application makes to its database. This can allow an attacker to view data that
they are not normally able to retrieve. This might include data that belongs to other users, or any
other data that the application can access. In many cases, an attacker can modify or delete this
data, causing persistent changes to the application's content or behavior.
In some situations, an attacker can escalate a SQL injection attack to compromise the underlying
server or other back-end infrastructure. It can also enable them to perform denial-of-service
attacks.
A successful SQL injection attack can result in unauthorized access to sensitive data, such as:
Passwords.
Credit card details.
Personal user information.
SQL injection attacks have been used in many high-profile data breaches over the years. These
have caused reputational damage and regulatory fines. In some cases, an attacker can obtain a
persistent backdoor into an organization's systems, leading to a long-term compromise that can
go unnoticed for an extended period.
You can detect SQL injection manually using a systematic set of tests against every entry point in
the application. To do this, you would typically submit:
The single quote character ' and look for errors or other anomalies.
Some SQL-specific syntax that evaluates to the base (original) value of the entry point,
and to a different value, and look for systematic differences in the application responses.
Boolean conditions such as OR 1=1 and OR 1=2, and look for differences in the
application's responses.
Payloads designed to trigger time delays when executed within a SQL query, and look for
differences in the time taken to respond.
OAST payloads designed to trigger an out-of-band network interaction when executed
within a SQL query, and monitor any resulting interactions.
Alternatively, you can find the majority of SQL injection vulnerabilities quickly and reliably using
Burp Scanner.
Most SQL injection vulnerabilities occur within the WHERE clause of a SELECT query. Most
experienced testers are familiar with this type of SQL injection.
However, SQL injection vulnerabilities can occur at any location within the query, and within
different query types. Some other common locations where SQL injection arises are:
There are lots of SQL injection vulnerabilities, attacks, and techniques, that occur in different
situations. Some common SQL injection examples include:
Retrieving hidden data, where you can modify a SQL query to return additional results.
Subverting application logic, where you can change a query to interfere with the
application's logic.
UNION attacks, where you can retrieve data from different database tables.
Blind SQL injection, where the results of a query you control are not returned in the
application's responses.
Imagine a shopping application that displays products in different categories. When the user
clicks on the Gifts category, their browser requests the URL:
https://insecure-website.com/products?category=Gifts
This causes the application to make a SQL query to retrieve details of the relevant products from
the database:
SELECT * FROM products WHERE category = 'Gifts' AND released = 1
The application doesn't implement any defenses against SQL injection attacks. This means an
attacker can construct the following attack, for example:
https://insecure-website.com/products?category=Gifts'--
Crucially, note that -- is a comment indicator in SQL. This means that the rest of the query is
interpreted as a comment, effectively removing it. In this example, this means the query no longer
includes AND released = 1. As a result, all products are displayed, including those that are
not yet released.
You can use a similar attack to cause the application to display all the products in any category,
including categories that they don't know about:
https://insecure-website.com/products?category=Gifts'+OR+1=1--
Imagine an application that lets users log in with a username and password. If a user submits the
username wiener and the password bluecheese, the application checks the credentials by
performing the following SQL query:
SELECT * FROM users WHERE username = 'wiener' AND password =
'bluecheese'
If the query returns the details of a user, then the login is successful. Otherwise, it is rejected.
In this case, an attacker can log in as any user without the need for a password. They can do this
using the SQL comment sequence -- to remove the password check from the WHERE clause of
the query. For example, submitting the username administrator'-- and a blank password
results in the following query:
SELECT * FROM users WHERE username = 'administrator'--' AND password =
''
This query returns the user whose username is administrator and successfully logs the
attacker in as that user.
In cases where the application responds with the results of a SQL query, an attacker can use a
SQL injection vulnerability to retrieve data from other tables within the database. You can use
the UNION keyword to execute an additional SELECT query and append the results to the original
query.
For example, if an application executes the following query containing the user input Gifts:
This causes the application to return all usernames and passwords along with the names and
descriptions of products.
Many instances of SQL injection are blind vulnerabilities. This means that the application does
not return the results of the SQL query or the details of any database errors within its responses.
Blind vulnerabilities can still be exploited to access unauthorized data, but the techniques
involved are generally more complicated and difficult to perform.
The following techniques can be used to exploit blind SQL injection vulnerabilities, depending on
the nature of the vulnerability and the database involved:
You can change the logic of the query to trigger a detectable difference in the application's
response depending on the truth of a single condition. This might involve injecting a new
condition into some Boolean logic, or conditionally triggering an error such as a divide-by-
zero.
You can conditionally trigger a time delay in the processing of the query. This enables you
to infer the truth of the condition based on the time that the application takes to respond.
You can trigger an out-of-band network interaction, using OAST techniques. This
technique is extremely powerful and works in situations where the other techniques do not.
Often, you can directly exfiltrate data via the out-of-band channel. For example, you can
place the data into a DNS lookup for a domain that you control.
First-order SQL injection occurs when the application processes user input from an HTTP
request and incorporates the input into a SQL query in an unsafe way.
Second-order SQL injection occurs when the application takes user input from an HTTP request
and stores it for future use. This is usually done by placing the input into a database, but no
vulnerability occurs at the point where the data is stored. Later, when handling a different HTTP
request, the application retrieves the stored data and incorporates it into a SQL query in an
unsafe way. For this reason, second-order SQL injection is also known as stored SQL injection.
Second-order SQL injection often occurs in situations where developers are aware of SQL
injection vulnerabilities, and so safely handle the initial placement of the input into the database.
When the data is later processed, it is deemed to be safe, since it was previously placed into the
database safely. At this point, the data is handled in an unsafe way, because the developer
wrongly deems it to be trusted.
Some core features of the SQL language are implemented in the same way across popular
database platforms, and so many ways of detecting and exploiting SQL injection vulnerabilities
work identically on different types of database.
However, there are also many differences between common databases. These mean that some
techniques for detecting and exploiting SQL injection work differently on different platforms. For
example:
In the previous labs, you used the query string to inject your malicious SQL payload. However,
you can perform SQL injection attacks using any controllable input that is processed as a SQL
query by the application. For example, some websites take input in JSON or XML format and use
this to query the database.
These different formats may provide different ways for you to obfuscate attacks that are
otherwise blocked due to WAFs and other defense mechanisms. Weak implementations often
look for common SQL injection keywords within the request, so you may be able to bypass these
filters by encoding or escaping characters in the prohibited keywords. For example, the following
XML-based SQL injection uses an XML escape sequence to encode the S character in SELECT:
<stockCheck>
<productId>123</productId>
</stockCheck>
This will be decoded server-side before being passed to the SQL interpreter.
OS command injection
In this section, we explain what OS command injection is, and describe how vulnerabilities can
be detected and exploited. We also show you some useful commands and techniques for
different operating systems, and describe how to prevent OS command injection.
OS command injection is also known as shell injection. It allows an attacker to execute operating
system (OS) commands on the server that is running an application, and typically fully
compromise the application and its data. Often, an attacker can leverage an OS command
injection vulnerability to compromise other parts of the hosting infrastructure, and exploit trust
relationships to pivot the attack to other systems within the organization.
Injecting OS commands
In this example, a shopping application lets the user view whether an item is in stock in a
particular store. This information is accessed via a URL:
https://insecure-website.com/stockStatus?productID=381&storeID=29
To provide the stock information, the application must query various legacy systems. For
historical reasons, the functionality is implemented by calling out to a shell command with the
product and store IDs as arguments:
stockreport.pl 381 29
This command outputs the stock status for the specified item, which is returned to the user.
The application implements no defenses against OS command injection, so an attacker can
submit the following input to execute an arbitrary command:
& echo aiwefwlguh &
If this input is submitted in the productID parameter, the command executed by the application
is:
stockreport.pl & echo aiwefwlguh & 29
The echo command causes the supplied string to be echoed in the output. This is a useful way
to test for some types of OS command injection. The & character is a shell command separator.
In this example, it causes three separate commands to execute, one after another. The output
returned to the user is:
Error - productID was not provided
aiwefwlguh
29: command not found
The original stockreport.pl command was executed without its expected arguments,
and so returned an error message.
The injected echo command was executed, and the supplied string was echoed in the
output.
The original argument 29 was executed as a command, which caused an error.
Useful commands
After you identify an OS command injection vulnerability, it's useful to execute some initial
commands to obtain information about the system. Below is a summary of some commands that
are useful on Linux and Windows platforms:
Purpose of command Linux Windows
The output from the mail command (if any) is not returned in the application's responses, so
using the echo payload won't work. In this situation, you can use a variety of other techniques to
detect and exploit a vulnerability.
Never attempt to sanitize input by escaping shell metacharacters. In practice, this is just too
error-prone and vulnerable to being bypassed by a skilled attacker.
Host: portswigger.net
In some cases, such as when the request has been forwarded by an intermediary system, the
Host value may be altered before it reaches the intended back-end component. We will discuss
this scenario in more detail below.
The header value may also be used in a variety of interactions between different systems of the
website's infrastructure.
As the Host header is in fact user controllable, this practice can lead to a number of issues. If the
input is not properly escaped or validated, the Host header is a potential vector for exploiting a
range of other vulnerabilities, most notably: