JavaScript & XSS

DOM

The Document Object Model is a programmatic API led interface for a web browser which creates smooth, responsive web apps without requiring new round-trip visits to a website. JavaScript is used to interface directly with the DOM.

JavaScript Primer

Object oriented programming language, generally used in web applications or on browsers. There are some specific terms which are similar to other types of programming languages.

Term

Meaning

Function

Just like any other language function, JS uses { } to determine a function's code.

Properties

Fields of attributes assigned to an object, can be hundreds of them. These properties can be referenced for easy manipulation of objects

Methods

Also known as member functions, belong to objects.

Browser Objects

This is a list of objects that can be referenced for the browser.

document.forms                // Find all forms on DOM
document.forms.length         // Find number of forms on DOM
document.forms[0].action      // Read the action of the first form in DOM
document.forms[0].action == "https://m4lwhere.org"    // Change the action of the form
document.cookie               // Lists cookies, will not work for HttpOnly
window.location.hostname      // Hostname of current site
window.location.href          // Full URL of current site
window.location.pathname      // URI only of current site (no hostname!)
window.location.protcol       // List HTTP or HTTPS
document.images               // All images in DOM
document.images.src           // Get the list of image objects in DOM
document.links                // All links in DOM
document.scripts              // All JavaScript scripts in DOM
document.readyState           // If page is loading or not
document.referrer             // Returns URI that linked to current page
document.title                // Title of current DOM
document.write                // Add text or other data to the document
ClipboardEvent.copy           // Event listener for a copy action
ClipboardEvent.paste          // Event listener for a paste action


console.log("haha");          // Prints the value of a command to the console, useful for debugging

encodeURIComponent("<script>alert(1)</script>")    // Encodes the URI for us :)

document.addEventListener('copy', (event) => {      // Creates a event listener which executes the alert when a copy is made
    alert('copy action initiated')
});

const paragraphs = document.querySelectorAll("p");    // Get a number of all paragraphs on the DOM
alert(paragraphs[0].nodeName);
paragraphs[(Math.floor(Math.random()*(paragraphs.length)))].hidden = true;    // Mark a random paragraph as hidden, making it appear as though it was deleted

<h1 style=-moz-transform:rotate(-180deg);>m4lwhere</h1>

// Get a list of all char values in an array named "year"
var codes = []
for (i=0; i < year.length; i++) { codes.push(year.charCodeAt([i]))}

// sum of all elements in an array
var sum = 0;
for (var i = 0; i < codes.length; i++) {sum += codes[i]}

XSS

Start by submitting a unique but benign string to identify where it is stored in the DOM/application. Can be placed in HTML content, tag attribute, or JS code. When cookies are assigned by a website, they SHOULD be given the HttpOnly attribute. This prevents JS from being able to touch the cookie at all. This will show up when the cookie is assigned.

Trigger POST Based Reflected XSS

This creates a button which when clicked will trigger a POST request with data sent to an known vulnerable endpoint. This assumes that there are no CSRF protections to prevent submissions on the vulnerable website. The JavaScript beneath the form will click on the button automatically as well, which forces the POST request to occur without user input.

Filter Evasion

Need to figure out what is being filtered, then how we can get around it. Angle brackets < > and <script> are commonly blocked, so we can target DOM events, encoded payloads, or payloads without these characters.

Password Prompt

Create a fake username and password prompt to trick users into passing their login info. This can be used to fill any auto-login info and be automatically stolen.

Fun Payloads

Here's some good payloads

AJAX

Asynchronous JavaScript and XML, used to add more content dynamically to a page without refreshing the entire page. Main object used to generate this ability is the XMLHttpRequest function. A new function, Fetch is a newer API with more features.

Tools

XSSer, xsssniper, XSScrapy

References

https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction

Catching cookies with exploits can be done several ways. Likely the easiest is to send the cookie in a GET request and capture it with python3 -m http.server. We can get more detailed however!

Last updated

Was this helpful?