Summery
This note consolidates all my Command Injection knowledge
Code review
It’s important to review and really understand the code you look through for the command injection. I like to read the code a view times to really understand it:
- Read it briefly, try to understand it’s concept
- Look for vulnerable functions
- Write step-by-step what the code does
Vulnerable functions
The most commonly seen functions that can be used to system code execution are in my expirience exec(), eval(), system(), input(), request(), popen() (in that order). There are also other functions, also depending on the lanugage at hand. You can find some of them in Vulnerable functions note.
The basic premise is that you look for vulnerable functions and if you find one, you also check if it takes any input that you can actually influance. So for exmaple, if a vulnerable function reads your payload’s filename, you can make it so the file name is something like ;echo <REV SHELL> | base64 -d | bash'.
Similarly to SQL Injection, flexibility and the skill to look, understand and see possible injections is very important - patience is advised.
Exploitation
As in all injection attacks, if you find some weak and possibly vulnerable code, you likely need to stop the expected behavior and start your injection. To do so you can use some special characters:
| Injection Operator | Injection Character | URL-Encoded Character | Executed Command |
|---|---|---|---|
| Semicolon | ; | %3b | Both |
| New Line | \n | %0a | Both |
| Background | & | %26 | Both (second output generally shown first) |
| Pipe | | | %7c | Both (only second output is shown) |
| AND | && | %26%26 | Both (only if first succeeds) |
| OR | | | %7c%7c | Second (only if first fails) |
| Sub-Shell | `` | %60%60 | Both (Linux-only) |
| Sub-Shell | $() | %24%28%29 | Both (Linux-only) |
| You might also find some additional operators and guidance in Injections. |
Filters
Sometimes you might encounter some filters that will try to block you from executing your payloads. Bypassing file upload filters hold some information about basic filters, also HacktheBox has many notes on that very subject.
I might expend this note to not relay so much on other notes.