|
| 1 | +/* |
| 2 | + * Copyright 2021-present by Nedim Sabic Sabic |
| 3 | + * https://www.fibratus.io |
| 4 | + * All Rights Reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package evasion |
| 20 | + |
| 21 | +import ( |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + "unsafe" |
| 25 | + |
| 26 | + "github.com/rabbitstack/fibratus/pkg/event" |
| 27 | + "github.com/rabbitstack/fibratus/pkg/sys" |
| 28 | + "github.com/rabbitstack/fibratus/pkg/util/va" |
| 29 | + log "github.com/sirupsen/logrus" |
| 30 | + "golang.org/x/sys/windows" |
| 31 | +) |
| 32 | + |
| 33 | +var syscallStubs = map[event.Type]string{ |
| 34 | + event.CreateProcess: "NtCreateUserProcess", |
| 35 | + event.CreateThread: "NtCreateThreadEx", |
| 36 | + event.TerminateThread: "NtTerminateThread", |
| 37 | + event.RegCreateKey: "NtCreateKey", |
| 38 | + event.RegDeleteKey: "NtDeleteKey", |
| 39 | + event.RegSetValue: "NtSetValueKey", |
| 40 | + event.RegDeleteValue: "NtDeleteValueKey", |
| 41 | + event.SetThreadContext: "NtSetContextThread", |
| 42 | + event.OpenProcess: "NtOpenProcess", |
| 43 | + event.OpenThread: "NtOpenThread", |
| 44 | + event.VirtualAlloc: "NtAllocateVirtualMemory", |
| 45 | + event.CreateFile: "NtCreateFile", |
| 46 | + event.DeleteFile: "NtDeleteFile", |
| 47 | + event.RenameFile: "NtSetInformationFile", |
| 48 | + event.CreateSymbolicLinkObject: "NtCreateSymbolicLinkObject", |
| 49 | +} |
| 50 | + |
| 51 | +const syscallStubLength = 23 |
| 52 | + |
| 53 | +// indirectSyscall evasion refers to executing the syscall instruction by |
| 54 | +// diverting the execution flow into a legitimate, clean ntdll stub that |
| 55 | +// performs the syscall on process behalf. |
| 56 | +// |
| 57 | +// This achieves code origin legitimacy, since the execution lands in .text |
| 58 | +// of a signed Microsoft module (ntdll.dll). Stack frames look identical to |
| 59 | +// a normal API call, which achieves call stack normalization. |
| 60 | +type indirectSyscall struct { |
| 61 | + offsets map[event.Type]uintptr // stores expected syscall stub offsets |
| 62 | +} |
| 63 | + |
| 64 | +func NewIndirectSyscall() Evasion { |
| 65 | + return &indirectSyscall{} |
| 66 | +} |
| 67 | + |
| 68 | +func (i *indirectSyscall) tryResolveSyscallStubOffsets(e *event.Event) error { |
| 69 | + if i.offsets != nil { |
| 70 | + return nil |
| 71 | + } |
| 72 | + |
| 73 | + var ntdllBase va.Address |
| 74 | + if e.PS != nil { |
| 75 | + for _, mod := range e.PS.Modules { |
| 76 | + if mod.IsNTDLL() { |
| 77 | + ntdllBase = mod.BaseAddress |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if ntdllBase.IsZero() { |
| 83 | + return nil |
| 84 | + } |
| 85 | + |
| 86 | + var handle windows.Handle |
| 87 | + if err := windows.GetModuleHandleEx(sys.ModuleHandleFromAddress, (*uint16)(unsafe.Pointer(ntdllBase.Uintptr())), &handle); err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + defer windows.Close(handle) |
| 91 | + |
| 92 | + i.offsets = make(map[event.Type]uintptr) |
| 93 | + |
| 94 | + for evt, stub := range syscallStubs { |
| 95 | + addr, err := windows.GetProcAddress(handle, stub) |
| 96 | + if err != nil { |
| 97 | + log.Warnf("unable to get procedure address for %s: %v", evt, err) |
| 98 | + continue |
| 99 | + } |
| 100 | + i.offsets[evt] = addr - ntdllBase.Uintptr() |
| 101 | + log.Debugf("syscall stub %s resolved to address %x and offset %d", evt, addr, i.offsets[evt]) |
| 102 | + } |
| 103 | + |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +func (i *indirectSyscall) Eval(e *event.Event) (bool, error) { |
| 108 | + if err := i.tryResolveSyscallStubOffsets(e); err != nil { |
| 109 | + return false, err |
| 110 | + } |
| 111 | + if e.Callstack.IsEmpty() { |
| 112 | + return false, nil |
| 113 | + } |
| 114 | + |
| 115 | + frame := e.Callstack.FinalUserspaceFrame() |
| 116 | + if frame == nil { |
| 117 | + return false, nil |
| 118 | + } |
| 119 | + |
| 120 | + if frame.IsUnbacked() { |
| 121 | + return false, nil |
| 122 | + } |
| 123 | + |
| 124 | + sym := frame.Symbol |
| 125 | + mod := filepath.Base(strings.ToLower(frame.Module)) |
| 126 | + |
| 127 | + if mod != "ntdll.dll" { |
| 128 | + // only check ntdll syscall stubs |
| 129 | + return false, nil |
| 130 | + } |
| 131 | + |
| 132 | + // eliminate common false positives (there are |
| 133 | + // many other false positives that can be directly |
| 134 | + // tuned in the rules) |
| 135 | + switch { |
| 136 | + case e.IsCreateProcess() && sym == "ZwDeviceIoControlFile" && e.Callstack.ContainsSymbol("AttachConsole"): |
| 137 | + return false, nil |
| 138 | + case e.IsCreateThread() && (sym == "ZwSetInformationWorkerFactory" || sym == "ZwReleaseWorkerFactoryWorker"): |
| 139 | + return false, nil |
| 140 | + case e.IsOpenThread() && sym == "ZwAlpcOpenSenderThread": |
| 141 | + return false, nil |
| 142 | + case e.IsOpenProcess() && sym == "ZwAlpcOpenSenderProcess": |
| 143 | + return false, nil |
| 144 | + case e.IsCreateFile() && (sym == "ZwOpenFile" || sym == "NtOpenFile" || sym == "ZwQueryAttributesFile" || sym == "ZwQueryFullAttributesFile" || sym == "ZwQueryInformationByName" || sym == "ZwQuerySystemInformation"): |
| 145 | + return false, nil |
| 146 | + case e.IsDeleteFile() && (sym == "ZwSetInformationFile" && (e.Callstack.ContainsSymbol("DeleteFileA") || e.Callstack.ContainsSymbol("DeleteFileW"))): |
| 147 | + return false, nil |
| 148 | + case e.IsRegCreateKey() && sym == "ZwDeviceIoControlFile" && e.Callstack.ContainsSymbol("DllUnregisterServer"): |
| 149 | + return false, nil |
| 150 | + } |
| 151 | + |
| 152 | + exp, ok := i.offsets[e.Type] |
| 153 | + if !ok { |
| 154 | + return false, nil |
| 155 | + } |
| 156 | + curr := frame.Addr.Dec(uint64(frame.ModuleAddress)).Uintptr() |
| 157 | + |
| 158 | + //nolint:staticcheck |
| 159 | + return !(curr > exp && curr <= exp+syscallStubLength), nil |
| 160 | +} |
| 161 | + |
| 162 | +func (*indirectSyscall) Type() Type { return IndirectSyscall } |
0 commit comments