-
Notifications
You must be signed in to change notification settings - Fork 10
/
tools.cpp
155 lines (114 loc) · 2.76 KB
/
tools.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "includes.hpp"
DWORD GetProcId(const wchar_t* proc_name)
{
DWORD proc_id = NULL;
HANDLE h_snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
PROCESSENTRY32 p_entry = { 0 };
p_entry.dwSize = sizeof(p_entry);
if (h_snap == INVALID_HANDLE_VALUE)
{
ERRLOG("CreateToolhelp32Snapshot: % d\n", GetLastError());
return NULL;
}
if (Process32First(h_snap, &p_entry))
{
do
{
if (!wcscmp(proc_name, p_entry.szExeFile))
{
proc_id = p_entry.th32ProcessID;
break;
}
} while (Process32Next(h_snap, &p_entry));
}
CloseHandle(h_snap);
return proc_id;
}
DWORD GetOwnModuleFullPathW(fs::path& mod_name_path)
{
wchar_t mod_name_buf[MAX_PATH] = { 0 };
DWORD mod_name_len = GetModuleFileNameW(g_h_current_module, mod_name_buf, sizeof(mod_name_buf) / sizeof(mod_name_buf[0]));
if (!mod_name_len || GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
return 0;
}
mod_name_path = mod_name_buf;
return mod_name_len;
}
bool VerifyDLL(const wchar_t* file_path, WORD desired_machine)
{
if (!file_path)
{
return false;
}
std::fstream file(file_path, std::ios::in | std::ios::binary | std::ios::ate);
if (!file.good())
{
return false;
}
DWORD file_size = (DWORD)file.tellg();
if (!file_size || file_size < PAGE_SIZE)
{
file.close();
return false;
}
BYTE* file_raw = new BYTE[PAGE_SIZE];
if (!file_raw)
{
file.close();
return false;
}
file.seekg(0, std::ios::beg);
file.read((char*)file_raw, PAGE_SIZE);
file.close();
IMAGE_DOS_HEADER* dos_header = nullptr;
IMAGE_NT_HEADERS* pe_header = nullptr;
IMAGE_FILE_HEADER* file_header = nullptr;
dos_header = (IMAGE_DOS_HEADER*)file_raw;
if (dos_header->e_magic != IMAGE_DOS_SIGNATURE || dos_header->e_lfanew > PAGE_SIZE)
{
delete[PAGE_SIZE] file_raw;
return false;
}
pe_header = (IMAGE_NT_HEADERS*)(file_raw + dos_header->e_lfanew);
if (pe_header->Signature != IMAGE_NT_SIGNATURE)
{
delete[PAGE_SIZE] file_raw;
return false;
}
file_header = &pe_header->FileHeader;
if (!(file_header->Machine & desired_machine) || !(file_header->Characteristics & IMAGE_FILE_DLL))
{
delete[PAGE_SIZE] file_raw;
return false;
}
delete[PAGE_SIZE] file_raw;
return true;
}
bool IsNativeProcess(HANDLE h_proc)
{
BOOL wow64 = FALSE;
IsWow64Process(h_proc, &wow64);
return (wow64 == FALSE);
}
DWORD IsElevatedProcess(HANDLE h_proc)
{
HANDLE h_token = 0;
if (!OpenProcessToken(h_proc, TOKEN_QUERY, &h_token))
{
return -1;
}
TOKEN_ELEVATION te = { 0 };
DWORD size_out = 0;
if (!GetTokenInformation(h_token, TOKEN_INFORMATION_CLASS::TokenElevation, &te, sizeof(te), &size_out))
{
CloseHandle(h_token);
return -1;
}
CloseHandle(h_token);
return te.TokenIsElevated != 0;
}
int _random(int begin, int end)
{
return begin + rand() % (end - begin + 1);
}