Web Security

Server-Side Exploitation

A website can be your way into the computer hosting it. To handle a request, a web application might open a file or run a command, using input you've supplied to decide what to do. If the application handles that input carelessly, you may be able to read files that were never meant to be public or execute commands of your own.

That access can expose source code, credentials, and other secrets stored on the server. Command execution can give you a shell with the application's permissions, putting the shell skills you've already practiced to work on someone else's machine! In this module, you'll follow your input through web applications and exploit the mistakes that make this possible.

18 challenges

Command Injection

CHALLENGE 01

Big Type

— solves

A web app often needs something the shell is already good at, so it takes the easy road and shells out: it glues a command together as a string and runs it. Marquee does this to turn your words into an ASCII banner with figlet.

The catch is that a shell scans a command for its own special characters before it runs anything. The developer expects your word to be a word, and drops it straight into the figlet command. You can send anything they forgot to block. A ; ends one command and starts the next, so where the developer sees one command, the shell is glad to run two.

The real code lives at /challenge/server. Read it, then see if you can trick it into handing you the flag!

CHALLENGE 02

Big Type

— solves

Web applications often do more than just serve static pages: a photo-sharing site might resize an uploaded picture before displaying it, while a reporting service might turn submitted data into a PDF before sending it back to the user.

Established command-line tools already handle jobs like these, so reusing their tested implementations saves the application's developers from writing and maintaining their own image converters or document renderers. One convenient way to call them is to shell out: the application builds a command with the desired options, perhaps including a filename or some text from the request, then asks a shell to run it and collects the result.

This connection between web requests and local programs goes back to the early web: the Common Gateway Interface (CGI), in use since 1993, let a web server pass a request to an external program and use its output to answer the client. These programs had access to local files and tools, so mistakes in how they handled request data could let an attacker read private files or execute commands on the server.

The shell parses the complete command string, so if the application trusts user input without proper quoting or validation, characters from the request can become shell syntax. A ;, for example, can separate the intended command from another one, giving you control over what the server executes when you were only supposed to supply text or a filename! This is command injection, which lets an attacker run injected commands with the server process's permissions.

In this challenge, the application uses figlet to turn your text into an ASCII banner, and your goal is to exploit its command injection vulnerability to read /flag. Read /challenge/server to see how it runs the command.

CHALLENGE 03

Quote Unquote

— solves

A banner should be allowed to contain punctuation without launching another command! The server now puts your text inside single quotes, making ; part of an argument and preventing $ from introducing an expansion, so both can reach figlet as ordinary text.

Your input still becomes part of the same string as the quotes the developer supplied, and the shell parses everything together. To get beyond a harmless banner, you'll have to account for the complete command, including the quotes around your input, instead of treating your input as a command on its own.

CHALLENGE 04

Redacted

— solves

Try a ; or a quote now, and the server rejects your request before starting a shell. The developer has traded quoted input for a blocklist, a list of characters the application refuses to accept. The list includes backslashes and many other characters used in shell syntax too, leaving you with far fewer ways to express a command.

That's a lot of forbidden punctuation, but the shell still receives a command containing your unquoted text and interprets whatever the filter lets through. You now have to satisfy two programs: pass the application's character check while still making the shell do more than render a banner.

CHALLENGE 05

Redacted

— solves

This time the developer went all in, blocking just about every dangerous character they could think of. Nearly every one. Find the character the filter forgot.

CHALLENGE 06

Blackout

— solves

Now the server has stopped showing its work! It still runs a command for each accepted request, but it throws away the command's output and sends back the same confirmation. An injected command could print the entire flag and the page would look exactly as if figlet had run normally.

The filter and your injection technique are unchanged from the previous challenge, but the response no longer tells you what your command did, so you're now dealing with blind command injection. Printing isn't the only effect a command can have, though; it can also change a file or send data, giving you other ways to observe its result from your terminal.

CHALLENGE 07

Solitary

— solves

Leaving a file behind is a handy way to recover data from a blind injection, provided your terminal can open it. This time, /tmp has a surprise: the server's /tmp and your terminal's /tmp are different directories. Your command can create a file successfully without giving you anything you can open from the terminal.

The server now runs in a sandbox, which limits what the application can access even after an attacker takes control. The sandbox gives the server a separate filesystem view with private writable directories. Your injected commands still run as root, and /flag is mounted read-only inside, so reading the flag is well within their reach!

Getting that data back is another matter. The character filter and missing command output remain, so your command needs to get the result across the sandbox's boundary without relying on a shared writable directory.

Path Traversal

CHALLENGE 08

One Level Up

— solves

In 1994, CERN's web server guide showed how to publish a whole directory of documents with one rule. If you taught a class and kept your course materials in a directory called handouts, you could put next week's reading online just by saving it alongside this week's. The server could leave the file lookup to the operating system, which already knew how to find a document by name.

When a student asks for week1/notes.txt, Linux looks for week1 inside handouts and then for notes.txt inside week1. You chose handouts when you set up the server, and the student supplies the pathname Linux follows from there. Since .. names the parent of the directory Linux is looking in, a lookup that starts in handouts can still end at a file outside it. If the student's choice of pathname lets them read that file, the server has a path traversal vulnerability.

Suppose the file outside handouts is the answer key for an exam. When the server opens a file on a student's behalf, Linux checks the permissions of the account running the server, so if that account can read the key, the student may end up with a copy.


Fun fact. In 2000, Microsoft documented a traversal flaw in its web server that let anonymous visitors read files outside the published folders with the permissions of the account that handled their requests. A file read like this could expose the password hashes in /etc/passwd on Unix systems without shadow passwords, or leak a site's signing key and let a visitor forge session cookies.

Path Traversal

CHALLENGE 09

One Level Up

— solves

This level looks at what happens when careless path handling meets a request from someone who isn't playing along. Files serves documents out of /challenge/files over HTTP, and it builds the path to each one from the name you hand it.

The developer has a filename in mind. But a path is a set of directions through the filesystem, and directions can go backward as easily as forward. Nothing here forces yours to stay inside /challenge/files. That's path traversal.

The code is at /challenge/server. Read through it, find the weak spot, and read /flag!

CHALLENGE 10

Double Take

— solves

Last time, Linux looked up the filename the student supplied. Now a filter tries to remove references to a parent directory with a single search-and-replace pass over the name. After those edits, how do you know the file Linux opens is one you meant to share?

CHALLENGE 11

Second Pass

— solves

This time the app repairs your path instead of refusing it, deleting the traversal it finds on the way through. It looks once and trusts the result. Can you hand it something that only becomes a traversal after it's done looking?

CHALLENGE 12

Absolutely

— solves

The filter now rejects the whole name if it mentions a parent directory. week1/notes.txt passes that check, and you know which handout the student gets because you told Linux to start looking for that relative path in handouts.

CHALLENGE 13

Dot Dot Denied

— solves

The last level let you walk out with .., so this one forbids .. outright. But banning one spelling of "somewhere else" only shows the developer still hasn't grasped how many ways a path can point away from where they meant it to. Can you still reach the flag?

CHALLENGE 14

Trim Reaper

— solves

You copy a handout's filename straight from the syllabus and still get “file not found” because you picked up a space at the end. Linux treats that space as part of the name, so a developer might trim whitespace before the lookup to spare you the trouble of spotting it. This time the developer trims a different set of characters from either end of the filename, expecting that whatever file Linux opens will be somewhere in handouts.

CHALLENGE 15

Strip Search

— solves

Another attempt, another misunderstanding. This one doesn't reject your input, it "cleans" it, shaving off the characters it thinks make a path dangerous. Trouble is, it only cleans the parts it bothers to look at. Read the server and find the parts it skips.

Archive Handling

CHALLENGE 17

Slippery Zippery

— solves

An empty directory seems like a good place to unpack an archive, since there's nothing in it to overwrite. You saw earlier how Linux looks up a relative pathname starting in a directory you choose. The extractor uses that same lookup for each file, and this time the result determines where it will write the bytes from the archive.

If a pathname resolves outside the extraction directory, even an archive unpacked into an empty folder can overwrite a file elsewhere. This form of path traversal is called Zip Slip. The person who packed the archive already supplied the contents, so if they can also choose the destination, they have an arbitrary file write.

CHALLENGE 18

Humpty Dumpty

— solves

During development, it's useful for a server to check whether a template has changed so you can see an edit on the next refresh. In production the same template might serve thousands of requests between deployments. Loading it once at startup lets the server render fresh data into each response without rereading the file on disk.

In this challenge, the templates are already in memory before your archive arrives. What can the arbitrary file write change on a running Linux system, even if the next page looks exactly the same?