---
title: "3. Command injections and identifying filtering"
date: 2024-12-05
draft: false
---


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)

- indicates something in the request triggered a security mechanism (or some other mechanism aside from the frontend validation) 
- err could be displayed in various ways, in this case we rendered in the browser it's displayed in the field output would typically be returned
	- this means it was detected + prevented by the PHP webapplication itself 
		- **If the error msg displayed a different page, with information like our IP and our request, this may indicate it was denied by WAF!**
- It's likely the webapplication detected a blacklisted character, or command. 

---

**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. 
- both linux / win accept commands with tabs btwn args and they are executed the same. 
Tab Url-encoded: `%09` 


![](/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 `\` 
- necessary to specify directories in both linux/windows
- several techniques can be used to produce any character we want while avoiding the use of blacklisted chars

**LINUX**
EX1) 
- Replacing slashes, or any other character is through linux env vars like we did with `${IFS}` 
	- While IFS replaces with space, there's no such env var for slashes or semicolons. However these chars may be used in an envvar and we can specify start/length of our string to exactly match this character. 
- Take for example the `$PATH` envvar 
	- `echo ${PATH}` -> `/usr/local/bin:/usr/bin:/bin:/usr/games`
	- If we start at `0` index and only take length of 1 we end up with `/` which we can use in payload. 
	- `echo ${PATH:0:1}`  prints -> `/` 
- The same general practice applies to all other envvars. 
	- `$HOME` and `$PWD` 
- For a semi-colon:
	- `echo ${LS_COLORS:10:1}` -> `;` 

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` 
- the character `\` is on `92` ; before it is `[` on `91` 

![](/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)
