Show Menu
Cheatography

JavaScript Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

About

Most common client­-side scripting language in use. Originally designed by Netscape in 95 and called LiveSc­ript.
Use for for reading and unders­tanding scripts from the target systems.
Can be used to write attacks against target systems.
In web pages is can be inline with HTML via <sc­rip­t> tags, as a part of an HTML item <img src="li­nk" onload­="ja­vas­cri­pt;­">, or loaded from an external document <script src=ht­tp:­//e­vil.ag­ent­/ba­d.j­s>
It is an object­-or­iented language.

Control Statements

while loop
Runs a block of code until a condition is met
for loop
Runs for a set number of times

Variables

Any type of data can be assigned to a variable without concern.
Declar­ation: var x;
Variables can be assigned at declar­ation or later. var x="s­tri­ng"; or x="s­tri­ng";
If a variable is re-dec­lared after a value is assigned to it, the original value is still assigned.
Global variables are those declared outside of functions and are accessible everyw­here.
Instance variables are those declared within a function and are exclusive to the function.

Functions

Functions can be declared anywhere within the page, but it is safest to declare in the <HE­AD> to ensure they are loaded before being called.
function name(var1, var2) { some code }
To return data from a function use return var; statement within the function.
Call a function using functi­on_­name()

Events

onload
Page of item is finished loading
onunload
User leaves the page the script is on
onerror
An error occurs loading page or item
onclick
Item is clicked on with mouse
onsubmit
The form is submitted
onfocus
The item receives focus
onblur
The item loses focus
onchange
Content of field changes
onmous­eover
The mouse is hovering over item
Every item in a page has a series of associated events. The event calls a function.

Events within Attacks

onload
Change content of page after it loads
onunload
Launch pop-under window to retain control of a zombie browser
onsubmit
Change form values so the transa­ction is one of the attacker's choosing.
onfocus
Send HTTP request to attacker's web server to reveal which controls the user is selecting.
onerror
Used within web scanners injected via XSS to determine a resource does not exist. Usefule when port scanning a network using JavaSc­ript.
onclick
Change where a link points without the user knowing.
onmouseover
Track the movement of the mouse across a page.
onblur
Send the contents of a form field to an attacker.

Document Object Model (DOM)

Provides standard interface tot he document allowing scripts to dynami­cally access and update content, structure, or style of the page.
Doc referenced is either HTML or XML
DOM provides native objects to access various items of interest:
docume­nt.f­or­ms[0] refers to 1st form on page
docume­nt.w­ri­te(­"­str­ing­") write string to the page
docume­nt.w­ri­te(­doc­ume­nt.c­ookie) will write value of the page's cookies to the page
Form object is used to access a specific form
form.a­cti­on=­[URL] sets the forms action to the URL allowing for redire­cting the browser to another page
form.s­ubmit() will submit form

DOM Nodes

Viewed as a tree the HTML tag is the root and has two children <HE­AD> and <BO­DY>. Each other them have children and so forth.

Object Methods and Properties

Objects have to be initia­lized instead of being assigned to a variable; var string=new String();
Objects have proper­ties, attributes of the object, and methods, which are actions performed on the object.
Devs can create their own objects.
When referring to a property of an object, we use the format object.pr­operty, such as docume­nt.r­ef­errer.
Calling a method is similar but also requires ( ) with values determined by the method.
 

Objects and Associated Properties and Methods

Object Type
Method
Property
String
split() parses the string
length returns size
Date
getTime() returns current time
getMonth() returns current month
Array
join() joins the elements in the array
sort() sorts the array
Window
open() creates a new browser window
alert() pops up a dialog box
Document
write() writes content to the page
referrer() returns referring URL
Location
reload() reloads doc
port() returns the port of the current page
History
back() is the same as the back button
Length returns history item count

Selecting and Changing Content

Scripts can find specific content by walking the DOM.
The script can read the item's attributes and associated items such as text.
The script can then rewrite the item.
function countt­ags­(tag) {
count = docume­nt.g­et­Ele­men­tsB­yTa­gNa­me(­tag­).l­ength
return count
}

Intera­cting with Cookies

strCoo­kie­=do­cum­ent.cookie returns only the name=value pairs
Parsing the cookie takes a little work.
1. Parse to split each name=value pair. var arrVal­ues­=do­cum­ent.co­oki­e.s­pli­t(';');
2. Loop through each pair and split on the =. (4:22)
Setting cookies only requires 3-4 parame­ters: A cookie name and value pair
Expiration time for the cookie ad URI path that is able to access it
Data NOT required for session cookies
docume­nt.c­oo­kie­="us­eri­d=p­erson; expire­s=Wed, 1-Nov-­2017; path=/­";