//nefariousplan

CVE-2025-47812: Wing FTP Read The Username Twice

vendor

patterns

cve

proof of concept

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/<UID>.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.

The same field was read twice

The H3XploR PoC's request_rce.txt posts this body:

POST /loginok.html HTTP/1.1
Host: target:5466
Content-Type: application/x-www-form-urlencoded
Cookie: client_lang=french

username=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=

URL decoding produces one buffer for the username field:

anonymous\x00]]\rlocal h = io.popen("ipconfig")\rlocal r = h:read("*a")\rh:close()\rprint(r)\r--

Wing FTP's HTTP handler passes that buffer to two consumers in sequence.

The 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.

The 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 --.

The 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 ]].

The resulting file looks approximately like this:

-- Session/<UID>.lua
username = [[anonymous^@]]
local h = io.popen("ipconfig")
local r = h:read("*a")
h:close()
print(r)
--]]
password = [[]]
-- (rest of session state)

^@ 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.

The NULL byte is where the parsers split

The 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.

Wing 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.

The 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.

This 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: the gate sees a name, the sink sees a payload, the buffer is the same. The sink itself is the 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.

The execution path

The 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/<UID>.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.

The 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.

Why a session is a Lua source file

The 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.

The 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.

PoC: H3XploR/Exploit_CVE-2025-47812, 0xS4N4TG/CVE-2025-47812. Original research: Julien (@MrTuxracer), What the NULL?! Wing FTP Server RCE.

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.