Lecture 11 Revision

Web Security

What is Web Security?

Web security encompasses the strategies, protocols, and tools designed to protect websites, web applications, and users from threats on the internet.

Definition:

Challenges:

Importance:

Strategies, internal protocols

Keep your users trust

OPTUS Data Breach in 2022

Large telecom company

Was not SQL injection or Cross-site scripting

SQL injection

alt text

SELECT * FROM user WHERE name=‘Charles’ AND passwd=‘topsecret’

Attack - use Charles

SELECT * FROM user WHERE name=‘Charles’  -- ‘ AND passwd=‘ ’

-- : the remainder of the statement is to be treated as a comment and not executed

Retrieving Hidden Data

https://somesite.com/products?category=handbags

SELECT * FROM products WHERE category = ‘handbags' AND released = 1

https://somesite.com/products?category=handbags' --

SELECT * FROM products WHERE category = ‘handbags’--’ AND released = 1

https://somesite.com/products?category=handbags

https://somesite.com/products?category=handbags'+OR+1=1--
SELECT * FROM products WHERE category = ‘handbags' OR 1=1--' AND released = 1
SELECT * FROM products WHERE category = ‘handbags' AND released = 1
https://somesite.com/products?category=handbags’;delete * from users --
SELECT * FROM user WHERE name=‘Charles’ AND passwd=‘topsecret’

close the query off, and add authorisation DML

Charles’; INSERT INTO groupMembership (userID, group) 
VALUES (SELECT userID FROM users
WHERE userName=‘Charles', 'Administrator'); --

SQL injection vulnerability

alt text

Prepared Statements in PHP

alt text

bind a variable to prevent SQL injection

SQL injection in Codeigniter

alt text

Cross-site Scripting

What is XSS?

When does it occur?

How serious is it?

alt text

Reflected XSS

Example: A user might enter a script into a search box that is immediately displayed on the result page without filtering, like so:

alt text

Stored XSS

Occurs when malicious scripts are permanently stored on target servers, such as in a database, message forum, visitor log, comment field, etc., and then later served to other users.

alt text

DOM-based XSS

Occurs when a script takes data from the client side, such as the URL, and processes it in a way that executes script without proper sanitization.

Example: Using URL parameters to modify the DOM:

In this case, the JavaScript will execute the script content of the name parameter as it is directly included in the DOM.

alt text

Preventing XSS

alt text

Example XSS in a Learning Management System

alt text

Cross-Origin Resource Sharing (CORS)

Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server to indicate any origins (domain, scheme, or port) other than its own from which a browser should permit loading resources.

Origin Concept: An origin is defined by the scheme (protocol), host (domain), and port of a URL. For instance, the origin of https://example.com:443 is different from https://example.com:8443 because they have different ports.

Same-Origin Policy: Browsers implement a same-origin policy by default. This policy restricts how a document or script loaded from one origin can interact with resources from another origin. Its primary purpose is to prevent malicious websites from interacting with other sites on which the user might be authenticated (like banking sites).

Cross-Origin Requests: When a script tries to request resources from a different origin (cross-origin), the browser blocks the request unless the server on the other end allows it explicitly.

CORS Example

https://reflectoring.io/complete-guide-to-cors/

Why is CORS important

Cross Site Request Forgery (CSRF)

CSRF - Example

Here’s a step-by-step breakdown of a typical CSRF attack:

  1. User Login: The user logs into a website, www.example.com, which authenticates the user and stores a session cookie on their browser.
  2. Malicious Request: The user, without logging out from www.example.com, visits a malicious website, www.evil.com. This site executes a harmful action by requesting www.example.com to perform a specific task (e.g., changing the user's email address or transferring funds).
  3. Browser Submits Request: The user’s browser automatically includes cookies pertaining to www.example.com with the request. If the session is still active, the server at www.example.com might execute the request without any additional verification.
  4. Action Executed: Without the user’s consent, the action is carried out as if the user had intended it.

CSRF Prevention in CodeIgniter

alt text

alt text

Open Web Applications Security Project

alt text

alt text