Move .NET method invocation logging to after the needed type conversion is done for method arguments#24853
Closed
daxian-dbw wants to merge 2 commits intoPowerShell:masterfrom
Closed
Move .NET method invocation logging to after the needed type conversion is done for method arguments#24853daxian-dbw wants to merge 2 commits intoPowerShell:masterfrom
daxian-dbw wants to merge 2 commits intoPowerShell:masterfrom
Conversation
Member
Author
|
/azp run |
|
Azure Pipelines successfully started running 4 pipeline(s). |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
Member
Author
|
/azp run PowerShell-CI-macos, PowerShell-CI-Linux, PowerShell-CI-Windows, PowerShell-CI-static-analysis, PSResourceGet ACR, PowerShell-Windows-Packaging-CI |
|
Azure Pipelines successfully started running 1 pipeline(s). |
5 tasks
Member
Author
|
Superseded by #25022 |
Contributor
|
📣 Hey @daxian-dbw, how did we do? We would love to hear your feedback with the link below! 🗣️ 🔗 https://aka.ms/PSRepoFeedback |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Summary
This PR is to fix a MSRC report about a bug in the .NET method logging implementation. The report is not considered a vulnerability, so I'm submitting the fix in GitHub.
The .NET method invocation logging is currently called before PowerShell does any of the needed type conversion for the method arguments, and it results in inaccurate information being logged to AMSI.
The fix is to move .NET method invocation logging to after the needed type conversion is done for method arguments.
The following is the comparison between before and after the fix when turning on the console logging for the AMSI notification feature:
Before the fix -- it logs
.GetType(<Foo>)After the fix -- it logs
.GetType(<System.Management.Automation.AmsiUtils>)PR Context
PowerShell 7.3 added a new feature that will log any .NET method calls and provide it to the IAntimalwareProvider2.Notify call. As an example running the following will result in the string below being provided to the AMSI provider to check before running
This allows antimalware providers registered with AMSI to be able to detect the runtime values provided to .NET methods even if they are obfuscated in the code and missed by any static analysis of the scriptblock text being run.
A bug in the implementation means that the provided args in the LogMemberInvocation method that builds the string to provide to AMSI are the ones before they are casted when actually invoking them.
For any .NET method that accepts a string we can now provide a custom class with a
ToStringmethod that returns the value we want to provide to the .NET call but the AMSI notify string value will just be the class name. With this we can now bypass the checks that may be in place to stop things like the below which would typically be picked up by AMSI due to the presence of the keywordAmsiUtilsin the string.Repro Steps
Run the following PowerShell script
The type name string provided at runtime to
GetTypeisSystem.Management.Automation.AmsiUtilsbut AMSI will see the following in the member invocation log, masking the true intention of the call.