Server-Side Exploitation


Web Security.

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.



Command Injection

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.

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.

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.

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.

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

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.

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?

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.

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.


Archive Handling

You can send someone all the files in a project and still leave them guessing how the directories fit together. An archive bundles the files and their layout into a single file, so whoever unpacks it can put the whole tree back the way you had it.

The Unix program tar got its name from its original job as a tape archiver. On tape, the reader encounters one file after another in a stream of bytes, so tar puts a header before each file giving its pathname and the number of bytes to read. The same layout works on disk, and you can compress the stream if you want the archive to take up less space.


Fun fact. Ever unpacked an old project and found its scripts were already executable? Their permission bits made the trip too! Tar can preserve symbolic links as well, and a directory you unpacked five seconds ago can look years old because tar saved its modification time.

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.

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?


30-Day Scoreboard:

This scoreboard reflects solves for challenges in this module after the module launched in this dojo.

Rank Hacker Badges Score