セキュリティ系の勉強・その他開発メモとか雑談. Twitter, ブログカテゴリ一覧
本ブログはあくまでセキュリティに関する情報共有の一環として作成したものであり,公開されているシステム等に許可なく実行するなど、違法な行為を助長するものではありません.

PowerShell難読化をだた試しながら読む(2) Binary Pulsarさんの記事

//

元記事はこちらなので、皆さんはぜひこちらを読んでください。
PowerShell難読化の基礎 (2) – Binary Pulsar

ドット記号によるスクリプト実行

ドット記号(.)を使用すると、アンパサンド記号(&)と同じ動きをする。

PS /> $comlet = "Get-Host"                                                      
PS /> . $comlet                                                                 


Name             : ConsoleHost
Version          : 6.0.4
InstanceId       : 4c891058-e3d5-48ec-ae4e-0e84674d999e
UI               : System.Management.Automation.Internal.Host.InternalHostUserI
                   nterface
CurrentCulture   : ja-JP
CurrentUICulture : ja-JP
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace



PS /> 
<br>
<br>
<br>

コマンドレット実行結果を利用

PowerShellの難読化では、意図的に一つしか結果が返ってこないコマンドレットを実行することにより、難読化に必要な文字列を取得するという手法がある。Get-commandは文字長が長く可読性があるので、エイリアスの"gcm"を使用すると良い。

PS> Get-command "*ke-E*"                                                      
CommandType     Name                                               Version    S
                                                                              o
                                                                              u
                                                                              r
                                                                              c
                                                                              e
-----------     ----                                               -------    -
Cmdlet          Invoke-Expression                                  3.1.0.0    M



PS /> .(gcm "*ke-E*") $comlet                                                   
Name             : ConsoleHost
Version          : 6.0.4
InstanceId       : 4c891058-e3d5-48ec-ae4e-0e84674d999e
UI               : System.Management.Automation.Internal.Host.InternalHostUserI
                   nterface
CurrentCulture   : ja-JP
CurrentUICulture : ja-JP
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

文字列"iex"の取得

前回の文字列から文字を抽出するやり方を同時に使用することが可能。Get-VariableはPowerShellで定義された変数を取得するためのコマンドレットで、gvにエイリアスされる。お手本ではMaximumDriveCountであったが、私のPowerShellMacOS上で走らせているためか、MaximumHistoryCountしか存在しなかった。ので、ExecutionContextで代用。

PS /> $comlet = "Get-Host"                                                      
PS /> gv "*onCont*"                                                             

Name                           Value                                           
----                           -----                                           
ExecutionContext               System.Management.Automation.EngineIntrinsics   


PS /> (gv "*onCont*" | %{$_.Name})[6,13,14] -Join ''                            
iex
PS /> .((gv "*onCont*" | %{$_.Name})[6,13,14] -Join '') $comlet                 


Name             : ConsoleHost
Version          : 6.0.4
InstanceId       : 4c891058-e3d5-48ec-ae4e-0e84674d999e
UI               : System.Management.Automation.Internal.Host.InternalHostUserI
                   nterface
CurrentCulture   : ja-JP
CurrentUICulture : ja-JP
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace



PS /> 

評価されない文字

挿入しても評価されない文字があるため、難読化に使用できる。バッククオート記号は、以下のエスケープシーケンスに一致しなければ、挿入しても文字列に影響を及ぼさない。

`0 NULL
`a 警告音
`b バックスペース
`f フォームフィード
`n 改行
`r キャリッジリターン
`t 水平タブ
`v 垂直タブ
http://binary-pulsar.azurewebsites.net/2018/09/08/ps-obfuscate-pt2/

よって、文字列自体を難読化させることが可能。(お手本では'e'の前にもバッククオートを挿入できていたが、私の環境ではできなかった。eの後ろのtが消えてしまう。"`ea"とやると、aの文字が消える。) 恐らくWindows環境なら可能なはず。

PS /> "`Get-`H`o`st" | iex                                                      


Name             : ConsoleHost
Version          : 6.0.4
InstanceId       : 4c891058-e3d5-48ec-ae4e-0e84674d999e
UI               : System.Management.Automation.Internal.Host.InternalHostUserI
                   nterface
CurrentCulture   : ja-JP
CurrentUICulture : ja-JP
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace



PS />