--libcurl code injection via trigraphs
The --libcurl option in curl generates C source code that does not escape question mark characters, allowing trigraph sequences to inject arbitrary code. An attacker who controls curl command-line options could cause the generated code to execute commands when compiled with trigraph support enabled.
- Disclosed after 43 hours
Summary:
curl command --libcurl option can be tricked to generate C code that when compiled contains arbitrary code execution.
Steps To Reproduce:
curl --libcurl client.c --user-agent "??/\");char c[]={'i','d',' ','>','x',0},m[]={'r',0};fclose(popen(c,m));//" http://example.invalidgcc -trigraphs client.c -lcurl -o client./clientls -l x
Note: In this PoC older compiler is simulated by passing -trigraphs option to gcc.
To remedy this issue ? chars should be quoted to \? in the generated strings.
Impact
Code injection to generated source code.
However, the impact of this vulnerability is minimal due to difficultly in finding scenarios where it would be practically exploitable. To be even remotely plausible curl command should somehow be hooked into a system that uses --libcurl to generate, compile and finally execute the compiled code while also accepting external user input for the curl command options. This seems extremely unlikely to happen in real life.
Trigraph support has also largely been disabled by now (gcc and clang have it disabled by default at least).
I don't really mind if this is found to be "not a vulnerability" (or only self-exploitable). In this case just close this H1 ticket and create a regular GitHub issue / or fix it direct.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The generated C code does not escape question mark characters, so trigraph sequences can alter the code structure.
Impact
Code injection into generated source code is possible, but the impact is minimal because exploitation requires an unlikely setup where curl options are externally controlled and the generated code is compiled with trigraphs enabled.
What the attacker needed
Attacker controls curl command-line options; generated code compiled with trigraphs enabled; compiled code is executed
How it was found
- Run curl with --libcurl and a crafted user-agent containing trigraph sequences.
- Compile the generated C file with trigraph support enabled.
- Execute the compiled binary.
- Check that the injected command created a file.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When you use curl with the --libcurl option, it writes a small C program that repeats your request. If your input contains three question marks in a row, an old C compiler feature called trigraphs can turn them into a backslash, which then breaks out of a string and lets an attacker add their own code. This only matters if someone else can control the curl options and the generated code is compiled with trigraphs turned on.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.