Windows PowerShell ISE - Remote Code Execution 2019-05-03 21:05:16

[+] Credits: John Page (aka hyp3rlinx)
[+] Website: hyp3rlinx.altervista.org
[+] Source: http://hyp3rlinx.altervista.org/advisories/WINDOWS-POWERSHELL-ISE-FILENAME-PARSING-FLAW-RCE-0DAY.txt
[+] ISR: ApparitionSec
[+] Zero Day Initiative Program


Windows PowerShell ISE


[Vulnerability Type]
Filename Parsing Flaw Remote Code Execution 0day


[Security Issue]
Windows PowerShell ISE will execute wrongly supplied code when debugging specially crafted PowerShell scripts that contain
array brackets as part of the filename. This can result in ISE executing attacker supplied scripts pointed to by the filename
and not the &quot;trusted&quot; PS file currently loaded and being viewed by a user in the host application. This undermines the integrity of
PowerShell ISE allowing potential unexpected remote code execution.

In PowerShell brackets are used to access array elements.

PS C:\&gt; $a=1..10
PS C:\&gt; $a[4]

However, when brackets are used as part of the filename it can be used to hijack the currently loaded file in place of another malicious file.
That file must contain a single matching char value which is also found in our specially crafted filename.

Requirements are both files must reside in the same directory. Example, if a file named [HelloWorldTutoria1].ps1 resides alongside a
file named 1.ps1 it will create a script hijacking condition. Note, the last letter is a number &quot;1&quot; not a lowercase &quot;L&quot;.

Other things I discovered playing with PS filenames is we can target scripts using a single alphabetic or numeric char and certain symbols.
PowerShell scripts with only a single quote also work, [Pwned'].ps1 will load and execute ===&gt; '.ps1 if debugged from the vuln ISE application.

These chars also get the job done:
&quot;$&quot; &quot;_&quot; &quot;#&quot; &quot;^&quot; plus any single case insensitive letter a-z or numbers 0-9, [Hello_World].ps1 ====&gt; _.ps1

[Hello].ps1 will execute this instead =====&gt; h.ps1

Dashes &quot;-&quot; throw the following error: &quot;The specified wildcard character pattern is not valid: [Hello-World].ps1&quot; when pointing to
another PS file named -.ps1 and seems to treat it sort of like a meta-character.

[pw3d].ps1 &lt;===== expected to execute

3.ps1 &lt;===== actually executed

This exploits the trust between PowerShell ISE and the end user. So scripts debugged local or over a network share display &quot;trusted&quot; code
in ISE that is expected to run. However, when the user debugs the script a different script gets executed.
Interestingly, that second script does NOT get loaded into PowerShell ISE upon execution, so a user may not see anything amiss.

User interaction is required for a successful attack to occur and obviously running any unknown PowerShell script can be dangerous.
Again, this exploit takes advantage of &quot;trust&quot; where users can see and read the code and will trust it as everything looks just fine and
yet ... still they get PWNED!.

Tested successfully on Win7/10

Long live user interaction! lol...

[POC Video URL]

set-executionpolicy unrestricted -force

PS scripts over Network shares may get 'RemoteSigned' security policy issue so run below cmd.

set-executionpolicy unrestricted -force process
Choose 'R' to run once.

Below Python script will create two .ps1 files to demonstrate the vulnerable condition.
Examine the code, what does it say? it reads... Write-output &quot;Hello World!&quot;... now Run it...

BAM! other PS script executes!.

#PowerShell ISE 0day Xploit
#ZDI CVSS: 7.0

fname1=&quot;[HelloWorldTutoria1].ps1&quot; #Expected code to run is 'HelloWorld!'
fname2=&quot;1.ps1&quot; #Actual code executed is calc.exe for Poc
evil_code=&quot;start calc.exe&quot; #Edit to suit your needs.
payload1='Write-Output &quot;Hello World!&quot;'
payload2=evil_code+&quot;\n&quot;+'Write-Output &quot;Hello World!&quot;'

def mk_ps_hijack_script():
global c
if c&lt;2:

if __name__==&quot;__main__&quot;:
print &quot;PowerShell ISE Xploit 0day Files Created!&quot;
print &quot;Discovery by hyp3rlinx&quot;
print &quot;ZDI-CAN-8005&quot;

[Network Access]


[Disclosure Timeline]
ZDI Case opened : 2019-02-06
Case officially contracted to ZDI : 2019-02-20

Vendor Disclosure : 2019-03-01
submitted to the vendor as ZDI-CAN-8005.

April 25, 2019 : MSRC response (as per ZDI): &quot;we've determined that this doesn't meet the bar for servicing via a security update.
we have opened a bug with the PowerShell team, and this is something they may address in a future release as a form of defense-in-depth.&quot;

ZDI also indicated they too will close the case.
May 1, 2019 : Public Disclosure

[+] Disclaimer