A bytecode repo in the web root
EKOPARTY CTF 2017 SlowShell: reading a PHP app from its HHVM repo file
SlowShell was a 498-point web and reverse task that only two teams solved. The source was compiled away, yet the deployment served its own compiled repository, and the login logic had a search space of ten thousand.
- 01
Inspect
The task gave a service address and the two commands that built and started it. They show HHVM in repo-authoritative mode, with all code compiled into a single bytecode repository.
- 02
Hypothesize
If the built-in HTTP server serves its working directory, the repository file itself may be downloadable, and a repository is just a database that can be opened and read.
- 03
Test
The repo file is a SQLite database. Dumping the bytecode of its two scripts and rewriting it as PHP shows a command endpoint behind a token and a login that produces the token.
- 04
Solve
The password is cut to four characters and turned into a number, so the secret space is tiny, and the token derives deterministically from it. The command endpoint then returns the flag file.
SlowShell came from EKOPARTY CTF 2017, a jeopardy-style contest organized by NULL Life on 16 and 17 September 2017, with 183 teams listed on CTFtime. The task was worth 498 points, carried both web and reverse engineering tags, and was solved by only two teams. The mix explains the low count: the web part gave no source code, and the reverse part had to be done on a bytecode format few players had ever opened.
What the task provided
The description gave a service address and the two shell commands used to build and start it. They are the real clue. The first compiles a directory of PHP scripts with HHVM, the PHP virtual machine from Facebook, into a repository under HHVM/. The second starts HHVM as a server with the Proxygen HTTP engine and the flag hhvm.repo.authoritative=true, pointing it at the repository file.
HHVM normally behaves like PHP: it reads script files on request and compiles them with a just-in-time engine. In repo-authoritative mode, everything is compiled ahead of time into a SQLite3 database of optimized bytecode and metadata, and the server runs only from that file. That gives the compiler more room to optimize, since it knows the full program. It also means the application’s entire code lives in one file, and the question becomes where that file sits.
First observation: the build output is public
Proxygen, the built-in server, serves files from the directory it was started in. The repository had been written to a subfolder of that same directory. The practical consequence is a classic information exposure: the compiled application, which is its own source for anyone willing to read it, was downloadable from the site like any static asset. A byte-level view helps recognize it, since a SQLite file has a clear texture with page-aligned blocks, long runs of zeros and text-heavy areas, and its header starts with the readable string SQLite format 3.
Opening the file with a SQLite client showed ordinary tables. Among them was a list of the compiled units, which identified the endpoints, here a login script and a command script. Finding the entry points this way replaces the guessing normally needed in a black-box web task.
Reading bytecode as a program
HHVM can print the bytecode of a unit from the repository with a bytecode dump option on the command line. The output is HipHop bytecode, HHBC, for a stack machine: values are pushed, instructions consume them, results are pushed back. Rewriting it by hand into PHP is laborious but mechanical. Look for string literals, calls to builtin functions, comparisons and jumps, and the structure of the original comes back. The task needed no specialized tooling beyond a patient read and the public HHBC specification.
The two scripts turned out simple. The command script reads a configuration file from /etc, and if a token query parameter equals the stored token, it runs the cmd parameter as a system command. A token-gated command runner is the goal of any attacker, so the question became how the token is made.
The weakness in the login
The login script computes the token, and its core is short enough to quote:
$user = substr($_POST['username'], 0, 32);
$pass = substr($_POST['password'], 0, 4);
$pass = strval(intval($pass));
Three details matter. The password is truncated to four characters. It is then forced through an integer conversion, so whatever the characters were, the result is one of at most ten thousand numbers. Finally, the code hashes that value with sixteen rounds of MD5, checks it with password_verify against a bcrypt hash, and derives the token from the password hash using a fixed salt, so the token is a deterministic function of a secret from a ten-thousand-element set.
A secret that comes from such a small set is not protected by what is done to it afterwards. Chained MD5 and a high bcrypt cost make each guess slower, and the contest author evidently wanted the name SlowShell to be earned, but cost multiplies a tiny number only by a constant. Ten thousand slow guesses is still ten thousand guesses, a tractable job. A deterministic token also has no use for a live server: it can be reproduced anywhere once the secret is known, so the login handler is not even the real gate.
The command endpoint sets one more bad example. Its comparison == is loose in PHP, and a token carried in a query string appears in server logs, browser history and referrer headers. The flag, in the usual EKO{...} format, sat in a file the command runner could read.
Dead ends
Trying to attack the running service blind goes nowhere, since the pages show only a form and an error message. Treating the repository as an opaque blob, or looking for a decompiler that does not exist, costs time. The productive route is to accept that the bytecode is readable and to read the two units fully before testing any idea.
What defenders should take away
Build artifacts do not belong in a web root, and a server that serves its own directory needs the directory chosen deliberately. Compiled code is not secret code: treat bytecode as source. Passwords need full length, a per-user random salt and a slow hash like bcrypt or Argon2 applied to the whole value, and a deterministic token derived from it should be replaced by a random session identifier. Throttle attempts, move credentials out of URLs, and never expose a generic command runner behind a single shared secret.
For more on spotting file types by their byte patterns, see binary visualization explained and the digram guide. The sibling task from the same contest is covered in the Silk Road writeup.
Questions readers ask
What is repo authoritative mode in HHVM?
It is a deployment mode where HHVM compiles every script ahead of time into a SQLite database of optimized bytecode and then runs only from that database. It allows heavier optimization than loading PHP files on demand, but the source files are no longer consulted at runtime.
Does compiling PHP to bytecode hide the source?
Only from casual reading. Bytecode is a complete description of the program and HHVM can dump it as text. Reading it takes patience, since it is a stack machine, but logic, constants and string literals are all present. Compilation is an optimization, not protection.
Why is a four-character password so weak here?
The code keeps only the first four characters and converts them to an integer, which leaves at most ten thousand possible values. The extra hashing rounds slow each guess down but cannot enlarge a search space that small.
How should the login have been built?
Keep the build artifacts out of the web root, hash a full-length password with a per-user random salt, use a real session instead of a token in the URL, rate-limit attempts, and never give a web page a generic command execution endpoint.
New to capture the flag? Formats, teams and a first-month plan in one place.
Read the CTF guide