Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

docs(case-study): empirically test __wakeup guard vs __destruct gadget (real PHP)

Xerj Squad Ac89f8ed6d59a
diff --git a/docs/case-studies/wordpress-security-audit/GADGET-CHAINS.md b/docs/case-studies/wordpress-security-audit/GADGET-CHAINS.md
index 47ff156c0..0db239e46 100644
--- a/docs/case-studies/wordpress-security-audit/GADGET-CHAINS.md
+++ b/docs/case-studies/wordpress-security-audit/GADGET-CHAINS.md
@@ -79,10 +79,13 @@ catalog and is the authority. Corrected finding:
**`WP_HTML_Token::__destruct` → `call_user_func($this->on_destroy, $this->bookmark_name)`**
(both properties settable) — a **real POP-gadget shape** (unserialize a token
with `on_destroy='system'`, `bookmark_name='id'` → RCE on destruct).
-- **It is defused:** the class adds `__wakeup(){ throw new LogicException('… should
- never be unserialized'); }` (WP 6.4.2) — `unserialize` can't build the object, so
- the `__destruct` is unreachable via object injection. This is WP's deliberate
- hardening of a real gadget, not the absence of one.
+- **It is defused — empirically verified.** The class adds `__wakeup(){ throw … }`
+ (WP 6.4.2). Tested in real PHP 7.4/8.0/8.3 (`GADGET-WAKEUP-TEST.md`): a throwing
+ `__wakeup` **suppresses `__destruct`** (PHP skips the destructor of an object whose
+ `__wakeup` threw), direct AND nested — so the gadget genuinely cannot fire. The
+ control (same `__destruct`, no `__wakeup`) DID fire, proving the guard is what
+ stops it. (This corrects an earlier note that speculated the `__destruct` survives
+ the throw — it does not, in modern PHP.)
- Everything else is benign/not-a-gadget: IRI/Iri `__set`/`__unset` `call_user_func`
is internal `[$this,'set_'.$name]` dispatch (bounded, and `__set` isn't
unserialize-triggered); `imagick`/`PHPMailer` `__destruct` do cleanup
diff --git a/docs/case-studies/wordpress-security-audit/GADGET-WAKEUP-TEST.md b/docs/case-studies/wordpress-security-audit/GADGET-WAKEUP-TEST.md
new file mode 100644
index 000000000..56105da61
--- /dev/null
+++ b/docs/case-studies/wordpress-security-audit/GADGET-WAKEUP-TEST.md
@@ -0,0 +1,38 @@
+# Empirical note: does a throwing `__wakeup` stop a `__destruct` gadget?
+
+Ran the WP_HTML_Token gadget shape in **real PHP (Docker: 7.4.33, 8.0.30, 8.3.32)**
+with a control. Script: [`sink-census/gadget_wakeup_test.php`](sink-census/gadget_wakeup_test.php).
+
+```bash
+docker run --rm -v "$PWD":/app php:8.3-cli php /app/gadget_wakeup_test.php
+```
+
+## Setup
+- `Guarded` = WP_HTML_Token's shape: dangerous `__destruct`
+ (`call_user_func($this->on_destroy, $this->bookmark_name)`) **+** a throwing
+ `__wakeup`.
+- `NoGuard` = the **control**: identical dangerous `__destruct`, **no** `__wakeup`.
+- Payloads set `on_destroy='GADGET'` (a marker) and are unserialized directly and
+ nested inside a plain `Container`.
+
+## Result (identical on 7.4 / 8.0 / 8.3)
+| case | class | outcome |
+|---|---|---|
+| A | NoGuard, direct | **GADGET FIRED** (`__destruct` ran `call_user_func`) |
+| D | NoGuard, nested | **GADGET FIRED** |
+| B | Guarded, direct | `__wakeup` throws → **gadget did NOT fire** |
+| C | Guarded, nested | `__wakeup` throws → **gadget did NOT fire** |
+
+## Conclusion (verified, not from memory)
+- **A throwing `__wakeup` suppresses `__destruct`.** When `__wakeup` throws during
+ `unserialize`, PHP treats the object as failed and **skips the destructor** —
+ direct and nested, all tested versions. The "`__destruct` survives a `__wakeup`
+ throw" folklore is **false for PHP ≥ 7.4** (may have held in PHP 5).
+- **`WP_HTML_Token` is genuinely defused** by its `__wakeup` guard.
+- The **control fired**, proving the gadget is real and the guard is what stops it.
+ ⇒ a dangerous `__destruct` **without** a throwing `__wakeup` is a **live gadget**.
+
+## Consequence for the gadget hunt
+The exposed gadget inventory = classes with a dangerous magic method (esp.
+`__destruct`/`__toString`/`__call`) that **lack** a throwing `__wakeup` guard (or
+route through `__unserialize`, which this test did not cover). Build that list next.
diff --git a/docs/case-studies/wordpress-security-audit/sink-census/gadget_wakeup_test.php b/docs/case-studies/wordpress-security-audit/sink-census/gadget_wakeup_test.php
new file mode 100644
index 000000000..fe4b6cdb0
--- /dev/null
+++ b/docs/case-studies/wordpress-security-audit/sink-census/gadget_wakeup_test.php
@@ -0,0 +1,31 @@
+<?php
+class Guarded { // == WP_HTML_Token: dangerous __destruct + throwing __wakeup
+ public $bookmark_name; public $on_destroy;
+ function __construct($b=null,$od=null){ $this->bookmark_name=$b; $this->on_destroy=$od; }
+ function __destruct(){ if(is_callable($this->on_destroy)){ echo " [Guarded::__destruct] firing\n"; call_user_func($this->on_destroy,$this->bookmark_name); } }
+ function __wakeup(){ throw new \LogicException('should never be unserialized'); }
+}
+class NoGuard { // control: SAME dangerous __destruct, NO __wakeup
+ public $bookmark_name; public $on_destroy;
+ function __construct($b=null,$od=null){ $this->bookmark_name=$b; $this->on_destroy=$od; }
+ function __destruct(){ if(is_callable($this->on_destroy)){ echo " [NoGuard::__destruct] firing\n"; call_user_func($this->on_destroy,$this->bookmark_name); } }
+}
+class Container { public $data; }
+function GADGET($a){ echo " >>> GADGET EXECUTED arg='$a' <<<\n"; }
+echo "PHP ".PHP_VERSION."\n";
+function payload($cls,$name){ $o=new $cls($name,'GADGET'); $s=serialize($o); $o->on_destroy=null; unset($o); return $s; }
+
+echo "\n[A] CONTROL: NoGuard direct unserialize (no __wakeup) -> gadget SHOULD fire:\n";
+try{ unserialize(payload('NoGuard','A')); }catch(\Throwable $e){ echo " caught: ".$e->getMessage()."\n"; } gc_collect_cycles();
+
+echo "\n[B] Guarded direct unserialize (throwing __wakeup):\n";
+try{ unserialize(payload('Guarded','B')); }catch(\Throwable $e){ echo " caught wakeup: ".$e->getMessage()."\n"; } gc_collect_cycles();
+
+echo "\n[C] Guarded NESTED in a Container (does nesting bypass its __wakeup?):\n";
+$c=new Container(); $c->data=new Guarded('C-nested','GADGET'); $cs=serialize($c); $c->data->on_destroy=null; unset($c);
+try{ unserialize($cs); }catch(\Throwable $e){ echo " caught wakeup: ".$e->getMessage()."\n"; } gc_collect_cycles();
+
+echo "\n[D] NoGuard NESTED in a Container -> gadget SHOULD fire:\n";
+$c2=new Container(); $c2->data=new NoGuard('D-nested','GADGET'); $cs2=serialize($c2); $c2->data->on_destroy=null; unset($c2);
+try{ unserialize($cs2); }catch(\Throwable $e){ echo " caught: ".$e->getMessage()."\n"; } gc_collect_cycles();
+echo "\n=== END ".PHP_VERSION." ===\n";