Vulnerability in Standard Libraries of Multiple Programming Languages

Applications that run scripts in bat and cmd format on the Windows platform using standard process launch functions, susceptible vulnerability that allows you to execute your code if arguments are passed at startup without separate escaping of special characters. The vulnerability is codenamed BatBadBut and affects applications that use standard libraries in languages ​​such as Rust, PHP, Node.js, Python, Ruby, Go, Erlang and Haskell.

The issue is flagged as a vulnerability because it affects library functions such as Rust's Command::arg and Command::args, which are designed to directly pass arguments to a process without being processed by the command interpreter. The implication is that the application developer may not need to check the arguments since they are passed directly to the running process. If on Unix systems the arguments are passed to the process individually in an array, then on Windows, when using the CreateProcess API, the arguments are formatted as a single string, the parsing of which falls on the shoulders of the launched process.

When running bat and cmd scripts on the Windows platform, the CreateProcess() function implicitly calls the cmd.exe executable file, even if the application does not specify it when calling it. The cmd.exe program contains its own complex argument separating logic to separate its own arguments, the script being run, and the script's arguments. To protect against argument substitution on the Windows platform, standard programming language libraries use separate escaping handlers, which, as it turns out, can be bypassed by manipulating double quotes.

For example, in a program that calls the script './test.bat' with an argument based on data received from the user, an attacker can pass the value '”&calc.exe', which, when the script is run, will be expanded to the string 'C:WindowsSystem32 cmd.exe /c .test.bat “”&calc.exe”' and will launch the calc.exe process. The method also works when running scripts implicitly, when an executable file named “test” is launched without specifying an extension, and there is a file “test.bat” in one of the directories mentioned in the PATH environment variable.

Currently the fix is ​​already released (CVE-2024-24576) for the Rust standard library and was included in the Rust 1.77.2 update. Updates to eliminate the vulnerability for Node.js and PHP are in the process of being prepared (tags 8.2.18 and 8.3.5 have already been set, but releases have not yet been announced). The Python, Ruby, Go, Erlang and Haskell projects have so far limited themselves to including in the documentation a warning about the manifestation of a vulnerability in the absence of proper escaping of special characters.

In Rust 1.77.2, an additional check was added to the standard library that returns an error if there are special characters in the argument to the running script, which cannot be guaranteed to be safely escaped. For developers who independently implement escaping logic, the CommandExt::raw_arg method is provided, which completely disables escaping on the side of library calls.

What's hampering adding protection is that simply escaping double quotes is not enough, as the shell processes and expands variables such as “%PATH%”. For example, a quote can be substituted by manipulating the environment variable “%CMDCMDLINE%” by specifying '%CMDCMDLINE:~-1%&calc.exe' instead of '”&calc.exe'.

Thanks for reading: