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

Ensure filename is not null when loggin WDAC ETW events #20910

Merged
merged 1 commit into from Dec 13, 2023

Conversation

jborean93
Copy link
Collaborator

PR Summary

Ensures the filename value used in WDAC ETW events is set to an empty string if the input value is null. This ensures the value matches up with the manifest defined which has the following properties for this task allowing ETW consumers to be able to consume this event.

PR Context

The manifest defines the properties for this event as

<template tid="WDACQueryTobeusedwhenoperationisjustexecutingamethodArgs_V1">
    <data name="QueryName" inType="win:UnicodeString" />
    <data name="FileName" inType="win:UnicodeString" />
    <data name="QuerySuccess" inType="win:Int32" />
    <data name="QuerySResult" inType="win:Int32" />
</template>

When running as it is now and the FileName is null we get the following event in the event log

image

The raw XML for the event is

<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
  <Provider Name="PowerShellCore" Guid="{f90714a8-5509-434a-bf6d-b1624c8a19a2}" /> 
  <EventID>16386</EventID> 
  <Version>1</Version> 
  <Level>4</Level> 
  <Task>131</Task> 
  <Opcode>20</Opcode> 
  <Keywords>0x0</Keywords> 
  <TimeCreated SystemTime="2023-12-13T08:27:51.4381401Z" /> 
  <EventRecordID>3</EventRecordID> 
  <Correlation ActivityID="{b8f2cbfe-2d7f-0001-0ea6-f4b87f2dda01}" /> 
  <Execution ProcessID="8876" ThreadID="7492" /> 
  <Channel /> 
  <Computer>SERVER2022.domain.test</Computer> 
  <Security /> 
  </System>
<ProcessingErrorData>
  <ErrorCode>15005</ErrorCode> 
  <DataItemName>QuerySResult</DataItemName> 
  <EventPayload>57006C00640070004700650074004C006F0063006B0064006F0077006E0050006F006C0069006300790000000000001000000080</EventPayload> 
  </ProcessingErrorData>
  </Event>

The ErrorCode represents ERROR_EVT_INVALID_EVENT_DATA and the EventPayload shows the raw value which when formatted with Format-Hex shows

   Label: Byte[] (System.Byte[]) <318C4389>

          Offset Bytes                                           Ascii
                 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
          ------ ----------------------------------------------- -----
0000000000000000 57 00 6C 00 64 00 70 00 47 00 65 00 74 00 4C 00 W l d p G e t L
0000000000000010 6F 00 63 00 6B 00 64 00 6F 00 77 00 6E 00 50 00 o c k d o w n P
0000000000000020 6F 00 6C 00 69 00 63 00 79 00 00 00 00 00 00 10 o l i c y      �
0000000000000030 00 00 00 80                                        �

We can see the first value for QueryName is the Unicode NULL terminated string WldpGetLockdownPolicy. The next string value is going to consume the 00 00 as the empty string NULL terminator for FileName. This only leaves 6 bytes left for the QuerySuccess and QuerySResult which is invalid causing the error. The problem here is there is actually no value for FileName, there should be an extra 2 0 bytes between the first and third value.

With this PR we can see that the event is now decoded properly

image

<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
  <Provider Name="PowerShellCore" Guid="{f90714a8-5509-434a-bf6d-b1624c8a19a2}" /> 
  <EventID>16386</EventID> 
  <Version>1</Version> 
  <Level>4</Level> 
  <Task>131</Task> 
  <Opcode>20</Opcode> 
  <Keywords>0x0</Keywords> 
  <TimeCreated SystemTime="2023-12-13T08:37:17.8895239Z" /> 
  <EventRecordID>373</EventRecordID> 
  <Correlation ActivityID="{b8f2cbfe-2d7f-0001-c7b4-f4b87f2dda01}" /> 
  <Execution ProcessID="5352" ThreadID="14300" /> 
  <Channel /> 
  <Computer>SERVER2022.domain.test</Computer> 
  <Security /> 
  </System>
<EventData>
  <Data Name="QueryName">WldpGetLockdownPolicy</Data> 
  <Data Name="FileName" /> 
  <Data Name="QuerySuccess">268435456</Data> 
  <Data Name="QuerySResult">-2147483648</Data> 
  </EventData>
  </Event>

There's probably other events affected but this is just what I came across when testing things out and only needed a simple fix.

PR Checklist

This PR has 2 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Small
Size       : +1 -1
Percentile : 0.8%

Total files changed: 1

Change summary by file extension:
.cs : +1 -1

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@iSazonov
Copy link
Collaborator

@SteveL-MSFT Who replaced Paul? Who could review this?

Copy link
Member

@daxian-dbw daxian-dbw left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the fix @jborean93!

@daxian-dbw daxian-dbw merged commit 43333bc into PowerShell:master Dec 13, 2023
38 checks passed
Copy link
Contributor

microsoft-github-policy-service bot commented Dec 13, 2023

📣 Hey @jborean93, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

@jborean93 jborean93 deleted the etw-wdac-null branch December 13, 2023 20:20
@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Dec 14, 2023
VindSkyggen pushed a commit to VindSkyggen/PowerShell that referenced this pull request Dec 26, 2023
dkattan pushed a commit to dkattan/PowerShell that referenced this pull request Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BackPort-7.4.x-Done CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Extra Small
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants