Chapter-3 Web Security
Chapter-3 Web Security
Chapter-3
Web Security
1
12/22/2023
2
12/22/2023
3
12/22/2023
4
12/22/2023
• The above SQL statement only reflects the rows for which the predicate in the
WHERE clause is TRUE.
• The predicate is a logical expression; multiple predicates can be combined using
keywords AND and OR.
5
12/22/2023
• This 1=1 predicate looks quite useless in real queries, but it will become
useful in SQL Injection attacks
6
12/22/2023
• If this channel is not implemented properly, malicious users can attack the
database
• SQL Injection attacks
7
12/22/2023
HTTP POST requests: data (foo and bar) are placed inside the data field of
the HTTP request
8
12/22/2023
The following example shows a PHP script getting data from a GET request
9
12/22/2023
• Assume that a user inputs a random string in the password entry and types
‘EID5002’ #’ in the eid entry. The SQL statement will become the following
10
12/22/2023
Modify Database
• Consider the form created for changing passwords
• An HTTP POST request will be sent to the server-side script which uses an
UPDATE statement
11
12/22/2023
12
12/22/2023
• The above attack doesn’t work against MySQL, because in PHP’s mysqli
extension, the mysqli::query() API doesn’t allow multiple queries to run in
the database server
• The error could look similar to the following (from Microsoft SQL Server):
Microsoft SQL Native Client error '80040e14'
/target.asp, line 9
13
12/22/2023
• The SQL interpreter can still parse user input as part of an SQL query
• This is how the Blind SQL Injection technique come into play (sometimes
called Inferential SQL Injection). There are two variants
• Content-based Blind SQL Injection
14
12/22/2023
• This will cause the query to return FALSE and no items are displayed in
the list.
15
12/22/2023
• This returns TRUE, and the details of item with ID 34 are shown.
This is a clear indication that the page is vulnerable
16
12/22/2023
• If we do want to run multiple SQL statements, we can use $mysqli -> multi_query()
[not recommended]
SQL Injection
Countermeasures
17
12/22/2023
18
12/22/2023
c
c
Prepared Statement
• Fundament cause of SQL injection: mixing data and code
• Fundament solution: separate data and code - Decouple the code and the data
• Main Idea: Sending code and data in separate channels to the database server
o This way the database server knows not to retrieve any code from the data channel
• Prepared Statement
o Using prepared statements, we send an SQL statement template to the database, with
certain values called parameters left unspecified
o The database parses, compiles and performs query optimization on the SQL statement
template and stores the result without executing it
o We later bind data to the prepared statement
19
12/22/2023
Prepared Statement
The vulnerable version: code
and data are mixed together
Send code
Send data
Start execution
20
12/22/2023
Mitigation
Limit privileges; reduces power of exploitation
• Can limit commands and/or tables a user can access
• Allow SELECT queries on Orders_Table but not on Creditcards_Table
21
12/22/2023
22
12/22/2023
Separate page
HTTP is stateless
• The server does not hold any information on previous requests
• The problem: a client has to access various pages before completing a
specific task and the client state should be kept along all those pages
How does the server know if two requests come from the same browser?
Example: the server doesn’t require a user to log at each HTTP request
23
12/22/2023
Hidden Fields
Example
• The web server can send a hidden HTML form field along with a unique session
ID as follows:
<input type="hidden“ name="sessionid" value="12345">
• When the form is submitted, the specified name and value are automatically
included in the GET or POST data
24
12/22/2023
Hidden Fields
Hidden Fields
25
12/22/2023
Hidden Fields
• Disadvantage of this approach
o It requires careful and tedious programming effort, as all the pages
have to be dynamically generated to include this hidden field
o Session ends as soon as the browser is closed
26
12/22/2023
Cookies
• A cookie has a name and a value
27
12/22/2023
Cookies
• A cookie has a name and a value, and other attribute such as domain and
path, expiration date, version number, and comments
28
12/22/2023
Cookies
• A cookie has a name and a value, and other attribute such as domain and
path, expiration date, version number, and comments
Cookies
• Request with cookies
29
12/22/2023
Tracking users
• Advertisers want to know your behavior
• Ideally build a profile across different websites
• Visit the Apple Store, then see iPad ads on Amazon?
30
12/22/2023
Cross-Site Requests
● When a page from a website sends an HTTP
request back to the website, it is called same-
site request
31
12/22/2023
Cross-Site Requests
● When a request is sent to example.com from a page coming from
example.com, the browser attaches all the cookies belonging to example.com
● Now, when a request is sent to example.com from another site (different from
example.com), the browser will attach the cookies too
● Because of above behaviour of the browsers, the server cannot distinguish between
the same-site and cross-site requests
o It is possible for third-party websites to forge requests that are exactly the same as
the same-site requests
o This is called Cross-Site Request Forgery (CSRF)
32
12/22/2023
33
12/22/2023
34
12/22/2023
● POST requests can be generated using HTML forms. The above form
has two text fields and a Submit button.
● When the user clicks on the Submit button, POST request will be sent
out to the URL specified in the action field with to and amount fields
included in the body.
● Attacker’s job is to click on the button without the help from the user
35
12/22/2023
Countermeasures: CSRF
36
12/22/2023
37
12/22/2023
38
12/22/2023
39
12/22/2023
The goal of an attacker is to slip code into the browser under the guise of
conforming to the same-origin policy: - Subverting the SOP
● Site evil.com provides a malicious script
● Attacker tricks the vulnerable server (bank.com) to send attacker’s script to the
user’s browser!
● Victim’s browser believes that the script’s origin is bank.com... because it does!
● Malicious script runs with bank.com’s access privileges
o Code can do whatever the user can do inside the session
40
12/22/2023
41
12/22/2023
42
12/22/2023
43
12/22/2023
44
12/22/2023
45
12/22/2023
46
12/22/2023
47
12/22/2023
Countermeasures - XSS
Countermeasures: Filter/Escape
● Typical defense is sanitizing: remove all executable portions of
user-provided content that will appear in HTML pages
○ E.g., look for <script>...</script> or
<javascript>...</javascript> from provided content and
remove it
○ So, if I fill in the “name” field for Facebook as
<script>alert(0)</script> and the script tags removed
● Often done on blogs, e.g., WordPress
https://wordpress.org/plugins/html-purified/
48
12/22/2023
Countermeasures: Filter/Escape
● It is difficult to implement as there are many ways to embed code
other than <script> tag
o lots of ways to introduce Javascript; e.g., CSS tags and XML-encoded data:
<div style="background-image:
url(javascript:alert(’JavaScript’))">...</div>
49
12/22/2023
50
12/22/2023
51
12/22/2023
● A more reliable authentication process would require two or all of these three factors
such as something you know with something you have
■ This form is known as the two-factor or multilevel authentication
52
12/22/2023
● Identifies which subsequent HTTP requests are being made by each user
53
12/22/2023
● Expiration date/time
● Path and domain – browser sends cookie to URLs from the domain
+ within the path
○ In the URL
http://example.com/restricted.html?session_id=ddee4454xerAFW45ex
54
12/22/2023
● They are well tested so using the API defined in the language is
recommended
○ In PHP: session_start(), session_destroy()
● The process of determining what that person is allowed to do, or what they
have access to
○ For example, an application may have separate roles for regular users and
administrators
55
12/22/2023
56
12/22/2023
57
12/22/2023
58
12/22/2023
59
12/22/2023
○ Session Sniffing
● For instance, consider a web application where users log in and are given a session
ID that is stored in a cookie
○ If the session ID is easily predictable or the cookie is not properly secured, an attacker
could steal the session ID and use it to access the user’s account without logging in
○ If the session ID is not invalidated after a user logs out, an attacker could potentially
reuse the session ID to continue accessing the user’s account even after the user has
logged out
60
12/22/2023
alert(document.cookie);
</SCRIPT>
61
12/22/2023
62
12/22/2023
63
12/22/2023
64
12/22/2023
65
12/22/2023
● In most cases, the reason that access control is broken is simply because it
has not been implemented
○ The mitigation is to implement it!
66
12/22/2023
67
12/22/2023
68
12/22/2023
○ Thoroughly audit and test access controls to ensure they work as designed
69
12/22/2023
● Protection:
○ Do the contrary…
● In user story development determine the correct flow and failure states, ensure
they are well understood and agreed upon by responsible and impacted parties
● Analyze assumptions and conditions for expected and failure flows, ensure they
are still accurate and desirable
● Secure design is neither an add-on nor a tool that you can add to software
70
12/22/2023
71