Labs / Security Scripting / Security scripting: triage, deobfuscation and scanning by hand

Lab 2 of 3 · Security scripting: triage, deobfuscation and scanning by hand

JavaScript triage: read the script the checkout page didn't ask for

Read a skimmer planted on a checkout page, decode where it sends what it steals, and watch a browser leak a cookie.

Start this lab 5 steps

The idea

Reuben runs the website for Thistlewood Market, a small online shop. A customer wrote in that their bank flagged a card minutes after they paid. When Reuben opens the checkout page's source he finds a line at the bottom he does not recognise: <script src="/assets/analytics.js">. The market runs no analytics on its checkout page.

His job is to find out what that script does and where it sends things, without trusting its name, then confirm it and remove it. Everything here is a harmless lab stand-in that carries the marker THISTLEWOOD-LAB-INERT. What the script steals is a cookie: a small piece of text the shop leaves in the shopper's browser to keep them signed in through checkout. In the lab that cookie is a made-up value, not a card.

A loaded script gets the whole page
A loaded script gets the whole page
Hide an address, then send to it
Hide an address, then send to it
The blob is an address
The blob is an address
Where the cookie goes
Where the cookie goes
Remove it, then remove why it worked
Remove it, then remove why it worked

Steps

  1. A script the page didn't ask for

    A web page is a list of instructions the browser follows. The line <script src="…"> means "fetch this file and run it here, with everything this page can touch": the form fields, the cookies, all of it. So the first question in triage, sizing up something suspect before you touch it, is which scripts a page pulls in and whether you can account for every one.

    The safe way to read a page is as text. cat prints a file to the screen and runs nothing in it. The shop serves the checkout page from the folder /tmp, so the file you read is the exact HTML the shopper's browser receives.

    Read it and look at the bottom, where scripts usually sit.

    shop:~# cat /tmp/checkout.html
  2. Read the script, not its name

    Now read the script itself. What matters is what its lines do, not what the file is called.

    JavaScript in a page can read the page's cookies through document.cookie. A site can mark a cookie HttpOnly, which hides it from scripts, but the shop left its checkout cookie readable. A script can also send data out by setting the address of an image: new Image().src = "http://…" makes the browser fetch that URL, and the request leaves the machine whether or not any picture comes back.

    The shape to look for is the same in any language: something that hides a value, next to something that sends it. In the browser the usual hiding tool is atob(), which decodes base64 back into text. Base64 writes any text as a run of letters and digits that nobody can read at a glance.

    So a script that runs atob() on a blob (a lump of unreadable data) and uses the result as a URL keeps its address out of the file you are reading. Read the script and find the two warning signs.

    shop:~# cat /tmp/assets/analytics.js
  3. Decode where it talks

    The script keeps its destination out of sight on purpose. The address sits in /assets/px.dat as base64 and is decoded only in the browser, so reading the script tells you it talks somewhere but not where.

    You do not need to run the script to find out. base64 -d turns an encoded file back into its original text and prints it, and it runs nothing. It is the same decoding the browser's atob() does, done in your terminal, where the result is only shown.

    Decode the hidden destination. Read the text, and never pass it on to anything that would act on it.

    shop:~# base64 -d /tmp/assets/px.dat
  4. Watch the browser leak the token, once, in the lab

    You have read what the script does. Now confirm it by letting a real browser run it in the lab. A shopper's browser is open on this closed network. It loads the checkout page and runs its scripts every few seconds, as a customer's would, and it carries a checkout cookie with the value THISTLEWOOD-LAB-INERT.

    This is safe only because the lab's network reaches nothing outside itself. Watching real traffic on a system whose owner has not authorised you in writing is not triage, and it can break the law even when you only meant to "check".

    The clearest proof is the traffic itself. The lab records what arrives at the collector (a packet capture) and keeps the recording for you to read.

    You can also look at the collector from the inside. Every machine keeps a list of its network connections, and ss prints it. Each connection has a port, the number that picks one program on a machine (a web server's is 80), and a state. A connection that has closed stays in the list for a moment in the state TIME-WAIT, so a TIME-WAIT line on port 80 at the collector means a browser reached it and left.

    collector:~# ss -ant
  5. The verdict, and pulling the skimmer

    Put it together. The checkout page loads a script it never had. The script is named to look like analytics, keeps its address in a base64 blob, and sends document.cookie to an outside machine that a shopper's browser really did reach. Each of those is a warning sign, and together they are a verdict: this is a web skimmer, code injected into a payment page to steal from shoppers.

    Closing it takes more than deleting a file. First remove the injected <script> line and the analytics.js file. Then fix the two things that let the skimmer work. The script could read the cookie only because the cookie was left readable, and marking it HttpOnly hides it from every page script. And someone was able to write a file into the web root, the folder the shop serves its pages from. Find how and close it, or the file comes straight back.

    Then look for the skimmer by what it does, not by its name. grep -rl searches a whole folder and prints only the names of the files that match, so a search for atob finds the decode-and-send pattern wherever it hides.

    Last, record an indicator, a trace you can search other places for. sha256sum prints a file's SHA-256 hash, a 64-character fingerprint that is the same wherever these exact bytes land.

    shop:~# grep -rl atob /tmp/assets
    shop:~# sha256sum /tmp/assets/analytics.js

Run these steps on real machines

Keep reading

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab