-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 NEFARIOUSPLAN-CANONICAL-V1 {"body_md":"## The same field was read twice\n\nThe H3XploR PoC's `request_rce.txt` posts this body:\n\n```http\nPOST /loginok.html HTTP/1.1\nHost: target:5466\nContent-Type: application/x-www-form-urlencoded\nCookie: client_lang=french\n\nusername=anonymous%00]]%0dlocal+h+%3d+io.popen(\"ipconfig\")%0dlocal+r+%3d+h%3aread(\"*a\")%0dh%3aclose()%0dprint(r)%0d--&password=&username_val=anonymous&password_val=\n```\n\nURL decoding produces one buffer for the `username` field:\n\n```\nanonymous\\x00]]\\rlocal h = io.popen(\"ipconfig\")\\rlocal r = h:read(\"*a\")\\rh:close()\\rprint(r)\\r--\n```\n\nWing FTP's HTTP handler passes that buffer to two consumers in sequence.\n\nThe first consumer is `c_CheckUser()`. Its job is to identify the caller and decide whether the username is permitted under the configured auth policy. It accesses the field through C-string semantics, where a NULL byte terminates the value. Under that contract, the username is the prefix that precedes the first `\\x00`. The prefix is `anonymous`. Wing FTP enables anonymous login by default on the wingData HTB box the published PoCs target, and on any production install where the operator did not explicitly disable the account. The check returns success.\n\nThe second consumer is the session writer. Its job is to persist the new session's state to disk as a Lua module the server can re-load on subsequent requests for the same UID. The writer accesses the field through length-prefixed buffer semantics. Under that contract, the username is every byte the HTTP handler decoded: `anonymous`, the NULL, the `]]`, the Lua statements, the trailing `--`.\n\nThe writer interpolates the buffer into Lua source using the standard long-bracket string literal: `username = [[…]]`. Long-bracket strings are Lua's chosen escape for embedded content because they accept any byte sequence that does not contain a balanced closing bracket of the same nesting level. The writer assumes nothing between `[[` and `]]` can escape, because the literal has no backslash interpretation and no other special characters. That assumption holds if the interpolated value does not itself contain `]]`. The interpolated value contains `]]`.\n\nThe resulting file looks approximately like this:\n\n```lua\n-- Session/.lua\nusername = [[anonymous^@]]\nlocal h = io.popen(\"ipconfig\")\nlocal r = h:read(\"*a\")\nh:close()\nprint(r)\n--]]\npassword = [[]]\n-- (rest of session state)\n```\n\n`^@` is the NULL byte rendered as Lua source. The `[[` on the first line opens the bracket string. The `]]` after `anonymous^@` closes it. Everything between that `]]` and the `--]]` four lines later is Lua at file scope. The trailing `--` consumes the template's closing bracket so the rest of the file still parses.\n\n## The NULL byte is where the parsers split\n\nThe two consumers disagree about where the username ends. C-string accessors read until they encounter a NULL byte; the NULL is the terminator and is not part of the value. Length-prefixed accessors read until they reach the explicit length; the NULL is just a byte. Neither accessor is wrong about its own contract. The two contracts are incompatible. The bug is to hand one untrusted buffer to both.\n\nWing FTP's HTTP layer produces buffer-plus-length pairs when it decodes form fields; the buffer carries the raw decoded bytes, the length carries the count, NULLs included. `c_CheckUser()` takes a `const char*` (or a thin wrapper around one) and applies C-string semantics. The session writer takes the buffer-plus-length pair and applies byte-buffer semantics. Both reads are correct against their respective APIs. The bytes are identical. The length the two readers infer is not.\n\nThe NVD entry for CVE-2025-47812 names the cause as \"Lua code injection in `loginok.html` due to NULL byte handling and improper user input validation.\" Both halves of that phrase obscure the shape. NULL byte handling in `c_CheckUser()` is correct on its own terms. The function does what its API contract describes. User-input validation passes. The anonymous-login policy is what the check is for, and `anonymous` matches it. The bug is that the two reads of the username produce different values, and the server commits to action on both.\n\nThis shape has a name in the catalog. The differential between two consumers reading the same buffer with different length conventions is a [null truncation differential](/patterns/null-truncation-differential): the gate sees a name, the sink sees a payload, the buffer is the same. The sink itself is the [content is command](/patterns/content-is-command) class the catalog already names. A passive-looking field, the username, feeds an interpreter, the Lua VM, that treats the rendered file as instructions. The session writer's job description is to render content. The Lua VM that `loadfile()`s the result is an interpreter. The bridge is `[[…]]` with no provision for the closing bracket that arrived in the content.\n\n## The execution path\n\nThe session file's name is the UID; the server returns the UID to the client as `Set-Cookie: UID=...` on the response to `/loginok.html`. Any subsequent request that arrives with a `UID` cookie causes the server to call `loadfile(\"Session/.lua\")` and execute the returned chunk inside its embedded Lua VM. The H3XploR PoC picks `/dir.html` because the directory-listing handler loads the session early in its flow. The s4n4tg rewrite confirms the same target on the GET path. Any handler that loads the session works.\n\nThe chunk runs at file scope inside the FTP server's Lua VM. `io.popen` and `os.execute` are in scope. The VM is hosted in the Wing FTP process. On Windows installations the process runs as `SYSTEM`. On Linux installations it runs as whatever user `wftpserver.service` is configured for; the vendor's default systemd unit is `root`. The full exploit is two HTTP requests with no authentication state between them.\n\n## Why a session is a Lua source file\n\nThe architectural reason this bug exists is that Wing FTP persists sessions as Lua source files. The HTTP front end is implemented as Lua scripts running inside the embedded VM; the session module is itself Lua. The fastest way for the VM to restore a session on a subsequent request is to `loadfile()` it, because `loadfile()` is built into the language and yields a callable function with no parser to write.\n\nThe cost of the design is that every field written to a session has to be interpolated into Lua source. Long-bracket strings are the standard escape, and they accept any byte sequence except a balanced closing bracket of the same nesting level. A closing bracket arrives in any field the client controls and Wing FTP does not pre-validate. The fix is either to track every interpolation site and apply a balanced-bracket-aware escape, or to refuse to interpolate fields that contain bracket sequences. Wing FTP 7.4.4 picks the latter for the username. The other fields in the session file are not part of this CVE.\n\nPoC: [H3XploR/Exploit_CVE-2025-47812](https://github.com/H3XploR/Exploit_CVE-2025-47812), [0xS4N4TG/CVE-2025-47812](https://github.com/0xS4N4TG/CVE-2025-47812). Original research: Julien (@MrTuxracer), [What the NULL?! Wing FTP Server RCE](https://www.rcesecurity.com/2025/06/what-the-null-wing-ftp-server-rce-cve-2025-47812/).","closing_line":"7.4.4 makes `c_CheckUser()` and the session writer agree on where the username ends. Every other field in the session file is still rendered into `[[…]]` by a writer that reads them as whole buffers.","hook_md":"The PoC sends `POST /loginok.html` to a Wing FTP Server install with a `username` field that decodes to `anonymous\\x00]]\\rlocal h = io.popen(\"id\")\\rlocal r = h:read(\"*a\")\\rh:close()\\rprint(r)\\r--`. Wing FTP's `c_CheckUser()` reads that field through a C-string accessor, stops at the NULL byte, and approves the request as anonymous. The session writer reads the same buffer through a length-prefixed accessor, never hits the NULL, and renders every byte into a Lua source file at `Session/.lua` as a `[[bracket string]]` literal. The `]]` in the payload closes the literal. Everything after it is Lua code at file scope. The second request asks for `/dir.html` with the returned `UID` cookie; the server `loadfile()`s the session file and calls the chunk. The injected code runs as the Wing FTP process owner.","post_id":455,"slug":"wing-ftp-cve-2025-47812-username-read-twice","title":"CVE-2025-47812: Wing FTP Read The Username Twice","type":"initial","unreadable_sentence":"Wing FTP's `c_CheckUser()` saw `anonymous`. The session writer saw Lua. They were given the same POST field."} -----BEGIN PGP SIGNATURE----- iHUEARYIAB0WIQRf0htP5+SjynlxywneZjl4jgkQJgUCanY8AQAKCRDeZjl4jgkQ Jqi0AP9kBlUomBG+wGAdGqeibNxZJSxB5YDhgNG54KxVeqV5XgEA8k+scvZWk+VY Lyp6ZYsUiFx53U1X2pckObPAfXW6EQk= =IaNE -----END PGP SIGNATURE-----