aboutsummaryrefslogtreecommitdiff
path: root/Patcher.cpp
blob: cefeeb17d94d3d0ff24c8165f37410b798eb1e51 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "Patcher.h"

void Patcher::PatchBytes(BYTE* address, BYTE* shellcode, const int shellCodeSize, bool modifyPageProtection, DWORD newPageProtection, bool restorePageProtection)
{
	DWORD oldProtection;
	if (modifyPageProtection)
	{
		//apply new page protection
		VirtualProtect(address, shellCodeSize, newPageProtection, &oldProtection);
	}
	else
	{
		//there is nothing to restore if page protection wasn't changed
		restorePageProtection = false;
	}

	//apply patch
	memcpy(address, shellcode, shellCodeSize);

	if (restorePageProtection)
	{
		//restore page protection
		VirtualProtect(address, shellCodeSize, oldProtection, &oldProtection);
	}
}

void Patcher::NOPBytes(BYTE* address, const int amount, bool modifyPageProtection, DWORD newPageProtection, bool restorePageProtection)
{
	DWORD oldProtection;
	if (modifyPageProtection)
	{
		//apply new page protection
		VirtualProtect(address, amount, newPageProtection, &oldProtection);
	}
	else
	{
		//there is nothing to restore if page protection wasn't changed
		restorePageProtection = false;
	}

	//apply patch
	for (int i = 0; i < amount; i++)
	{
		address[i] = 0x90; //NOP
	}

	if (restorePageProtection)
	{
		//restore page protection
		VirtualProtect(address, amount, oldProtection, &oldProtection);
	}
}

//Writes a 1 byte value using local endianess
void Patcher::Patch(BYTE* address, int8_t value)
{
	DWORD oldProtection;

	//apply new page protection
	VirtualProtect(address, sizeof(value), PAGE_EXECUTE_READWRITE, &oldProtection);

	//write value
	*(int8_t*)address = value;

	//restore page protection
	VirtualProtect(address, sizeof(value), oldProtection, &oldProtection);
}

//Writes a 2 byte value using local endianess
void Patcher::Patch(BYTE* address, int16_t value)
{
	DWORD oldProtection;

	//apply new page protection
	VirtualProtect(address, sizeof(value), PAGE_EXECUTE_READWRITE, &oldProtection);

	//write value
	*(int16_t*)address = value;

	//restore page protection
	VirtualProtect(address, sizeof(value), oldProtection, &oldProtection);
}

//Writes a 4 byte value using local endianess
void Patcher::Patch(BYTE* address, int32_t value)
{
	DWORD oldProtection;

	//apply new page protection
	VirtualProtect(address, sizeof(value), PAGE_EXECUTE_READWRITE, &oldProtection);

	//write value
	*(int32_t*)address = value;

	//restore page protection
	VirtualProtect(address, sizeof(value), oldProtection, &oldProtection);
}

//Writes a 8 byte value using local endianess
void Patcher::Patch(BYTE* address, int64_t value)
{
	DWORD oldProtection;

	//apply new page protection
	VirtualProtect(address, sizeof(value), PAGE_EXECUTE_READWRITE, &oldProtection);

	//write value
	*(int64_t*)address = value;

	//restore page protection
	VirtualProtect(address, sizeof(value), oldProtection, &oldProtection);
}