Skip to content
RCE Write-up Advanced

Exploiting an Ancient Browser Engine in wkhtmltopdf

wkhtmltopdf embeds a 2012-era WebKit with a use-after-free in Array.prototype.sort (CVE-2012-3748). An attacker who can influence the HTML converted by such a service can run JavaScript in the old engine and escalate to remote code execution on the server.

Robin Trost Follow

Published on blog.syss.com

Modern applications make heavy use of “HTML to PDF” converters to create exports (e.g. invoices, reports or tickets). Some of them are still powered by wkhtmltopdf, which embeds a 2012-era WebKit to render the HTML.

Robin Trost, on blog.syss.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

The sort function caches a pointer to the array's backing store before calling the attacker's comparison callback, and then writes back through that stale pointer after the callback has caused the store to be reallocated and freed.

Impact

An attacker who can influence the HTML converted by a wkhtmltopdf service could run arbitrary commands as the converting process on the server.

What the attacker needed

Attacker-influenced HTML reaches wkhtmltopdf; JavaScript enabled in the converter (default)

The fix

Stop using wkhtmltopdf and migrate to a maintained renderer; if that is not possible, ensure no attacker-influenced input reaches it, disable JavaScript, disable local-file and remote-resource access, and sandbox the converter.

How it was found

  1. Influence the HTML that a wkhtmltopdf-based service converts to PDF.
  2. Trigger the use-after-free in Array.prototype.sort by growing the array inside the comparison callback.
  3. Reclaim the freed chunk with an ArrayBuffer to leak the chunk's own heap address.
  4. Use the arbitrary-free primitive to free that chunk again and reclaim it with a chosen object, creating an aliased view.
  5. Forge a fake Uint8Array to obtain arbitrary read/write, then hijack its vtable to pivot into a ROP chain.
  6. Call execve with a shell command to achieve code execution on the server.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

wkhtmltopdf is a tool that turns HTML into PDF files, and it uses a very old browser engine from 2012 to run any JavaScript in that HTML. That old engine has a known memory bug: if JavaScript changes an array while the array is being sorted, the engine later writes into memory it no longer owns. An attacker who can get their HTML processed can abuse this to run their own commands on the server.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.