<commit_before>﻿using System;
using O365.Security.ETW;

namespace hiddentreasure_etw_demo
{
    public static class PowerShellMethodExecution
    {
        public static UserTrace CreateTrace()
        {
            var filter = new EventFilter(Filter
                .EventIdIs(7937)
                .And(UnicodeString.Contains("Payload", "Started"))
                .And(UnicodeString.Contains("ContextInfo", "Command Type = Function"))
                .And(UnicodeString.Contains("ContextInfo", "Command Name = prompt").op_LogicalNot())
                .And(UnicodeString.Contains("ContextInfo", "Command Name = PSConsoleHostReadline").op_LogicalNot()));

            filter.OnEvent += (IEventRecord r) => {
                var method = r.GetUnicodeString("ContextInfo");
                Console.WriteLine($"Method executed:\n{method}");
            };

            var provider = new Provider("Microsoft-Windows-PowerShell");
            provider.AddFilter(filter);

            var trace = new UserTrace();
            trace.Enable(provider);
            return trace;
        }
    }
}
<commit_msg>Pointer to PowerShellMethodAuditor code.<commit_after>﻿using System;
using O365.Security.ETW;

namespace hiddentreasure_etw_demo
{
    public static class PowerShellMethodExecution
    {
        public static UserTrace CreateTrace()
        {
            // For a more thorough example of how to implement this detection,
            // have a look at https://github.com/zacbrown/PowerShellMethodAuditor
            var filter = new EventFilter(Filter
                .EventIdIs(7937)
                .And(UnicodeString.Contains("Payload", "Started"))
                .And(UnicodeString.Contains("ContextInfo", "Command Type = Function"))
                .And(UnicodeString.Contains("ContextInfo", "Command Name = prompt").op_LogicalNot())
                .And(UnicodeString.Contains("ContextInfo", "Command Name = PSConsoleHostReadline").op_LogicalNot()));

            filter.OnEvent += (IEventRecord r) => {
                var method = r.GetUnicodeString("ContextInfo");
                Console.WriteLine($"Method executed:\n{method}");
            };

            var provider = new Provider("Microsoft-Windows-PowerShell");
            provider.AddFilter(filter);

            var trace = new UserTrace();
            trace.Enable(provider);
            return trace;
        }
    }
}

{'fraction_non_alphanumeric': 0.10934891485809682, 'fraction_numerical': 0.005843071786310518, 'mean_word_length': 2.26566757493188, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1456436', 'n_tokens_mistral': 618, 'n_tokens_neox': 596, 'n_words': 125}