3. Command injections and identifying filtering

Method of injection mitigation is utilizing blacklisted characters + words on the backend to detect injection attempts and deny the request if any request contained them. On top of this, WAFs apply here too, and may have a broader scope + various methods of injection detection to prevent various other attacks like SQLi or XSS.

Command injection Filter/WAF Detection

![](/img/Pasted image 20241205114217.png)

Let's attempt a simple command injection here with BURP.

To review, front-end (easily bypassed) detection looks like this: ![](/img/Pasted image 20241205114300.png)

With WAF/ filter applied we get error text like the following: ![](/img/Pasted image 20241205114357.png)


Bypassing blacklisted operators

In our example %0a (the newline character) isn't blacklisted, as it may be needed in the payload itself. We know new-line chars work to append commands in both linux/windows. Let's use it as our injection operator.

![](/img/Pasted image 20241205124216.png) However: ![](/img/Pasted image 20241205124300.png) Spaces are often blacklisted! This is the same behavior if we only specify 127.0.0.1%0a+ using + as a space.

In such a scenario, Tab character can be used instead.

![](/img/Pasted image 20241205124503.png)

In Linux the $IFS env var may also work, its' default value is a space and a tab, which would work between command args. If we use ${IFS} where the spaces should be, the variable should be automatically replaced with a space and our command would work. 127.0.0.1%0a${IFS}

Brace Expansion

Many other ways to bypass space filters. Bash Brace Expansion is another method. It automatically adds spaces between arguments wrapped between braces. for example: {ls,-la} executes ls -la without spaces. we can use the same method for cmd injection filter bypasses.

https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Command%20Injection#bypass-without-space ![](/img/Pasted image 20241205125112.png)

Other blacklisted characters Very commonly blacklisted characters also include / and \

LINUX EX1)

So, let's try to use environment variables to add a semi-colon and a space to our payload (127.0.0.1${LS_COLORS:10:1}${IFS})

(it works protip)


Windows

The same exact concept works in Windows. i.e. echo a Windows var %HOMEPATH% -> \Users\htb-student Specify starting position and negative end position. echo %HOMEPATH:~6,-11%

Above is in a command prompt. Same applies to powershell.

$env:HOMEPATH[0] -> \ $env:PROGRAMFILES[10] -> (space char)

![](/img/Pasted image 20241205142829.png)

PS get-childitem

Get-ChildItem Env: returns all environment variables + picks one of them to produce a character we need.


Character shifting to bypass character blacklisting

In linux, the following command shifts the character we pass by 1. echo $(tr '!-}' '"-~'<<<[) Validate this with man ascii

![](/img/Pasted image 20241205143220.png)

Able to use powershell to achieve this same result in Windows though the command may be quite longer than linux


All of the above put together to find the name of the user in /home directory: ![](/img/Pasted image 20241205145243.png)