All write-ups
25 min read

The Ultimate Guide to Web Security

WebPentestingPortSwiggerBug Bounty

The Ultimate Guide to Web Security

Welcome to the definitive field guide for Web Application Security. This document synthesizes core concepts across major vulnerability classes, breaking down their mechanics, theoretical impact, and essential defensive strategies.

[!NOTE] This guide is intended for educational purposes and defensive engineering. Understanding how vulnerabilities operate is the first step in building resilient, secure systems.


1. Cross-Site Scripting (XSS)

Cross-Site Scripting (XSS) occurs when an application includes untrusted data in a web page without proper validation or escaping. This allows the execution of malicious scripts in the victim’s browser.

The Mechanics

XSS vulnerabilities are generally categorized into three main types:

Type Description
Reflected XSS The malicious payload is reflected off the web application to the victim's browser, typically via a URL parameter.
Stored XSS The payload is permanently stored on the target server (e.g., in a database, forum post, or comment field) and served to users viewing the affected content.
DOM-based XSS The vulnerability exists in the client-side code rather than the server-side code. The DOM is modified by JavaScript using attacker-controlled data.

Theoretical Example

A vulnerable search page might reflect user input directly into the HTML:

<!-- Vulnerable PHP Code -->
<h1>Search Results for: <?php echo $_GET['query']; ?></h1>

If a user inputs <script>alert(1)</script>, the browser interprets the input as executable JavaScript.

Defensive Remediation

To effectively prevent XSS, developers must adopt a defense-in-depth approach:

  1. Context-Aware Output Encoding: Always encode untrusted data before inserting it into an HTML document. The type of encoding depends on where the data is placed (HTML body, JavaScript variable, CSS, etc.).
  2. Content Security Policy (CSP): Implement a strict CSP to restrict the sources from which scripts can be loaded and executed.
  3. Use Safe APIs: Modern frameworks (like React or Angular) automatically encode output by default. Avoid unsafe sinks like innerHTML in vanilla JavaScript.

2. Server-Side Request Forgery (SSRF)

Server-Side Request Forgery (SSRF) is a vulnerability that allows an attacker to induce the server-side application to make HTTP requests to an arbitrary domain of the attacker's choosing.

The Mechanics

In a typical SSRF scenario, the server might try to fetch a remote resource based on a URL provided by the user. If this input is not validated, the server can be tricked into interacting with internal infrastructure (e.g., 127.0.0.1 or 192.168.x.x) or external systems.

Common Targets

Theoretical Example

Consider a web application with a "Check Stock" feature that queries a backend API:

POST /product/stock HTTP/1.1
Host: example.com

stockApi=http://stock.backend.internal:8080/check

If the stockApi parameter is modifiable, it could be pointed to the server's loopback interface:

stockApi=http://localhost/admin/delete?username=carlos

Defensive Remediation


3. SQL Injection (SQLi)

SQL Injection allows an attacker to interfere with the queries that an application makes to its database. This can allow unauthorized viewing of data, modification, or even complete system compromise.

The Mechanics

SQLi occurs when user input is unsafely embedded into a database query, altering the query's logic.

// Vulnerable Code Example
$username = $_POST['user'];
$query = "SELECT * FROM users WHERE username = '$username'";
$db->execute($query);

By manipulating the input, the query's structure is altered, bypassing authentication or extracting additional data.

Defensive Remediation


4. File Upload Vulnerabilities

Allowing users to upload files can expose a server to significant risks if the files are not properly validated.

The Mechanics

If a web server allows the upload of executable scripts (like .php, .jsp, or .py) and stores them in a directory accessible from the web, an attacker can request the file to execute arbitrary code on the server.

Defensive Remediation


[!IMPORTANT] Security is an ongoing process. Continuous learning, regular auditing, and adherence to secure coding practices are essential to protecting web applications in a constantly evolving threat landscape.