-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathutil.cpp
85 lines (79 loc) · 2.47 KB
/
util.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
#include "util.h"
#include <iostream>
BYTE *buffer_payload(wchar_t *filename, OUT size_t &r_size)
{
HANDLE file = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(file == INVALID_HANDLE_VALUE) {
#ifdef _DEBUG
std::cerr << "Could not open file!" << std::endl;
#endif
return nullptr;
}
HANDLE mapping = CreateFileMapping(file, 0, PAGE_READONLY, 0, 0, 0);
if (!mapping) {
#ifdef _DEBUG
std::cerr << "Could not create mapping!" << std::endl;
#endif
CloseHandle(file);
return nullptr;
}
BYTE *dllRawData = (BYTE*) MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
if (dllRawData == nullptr) {
#ifdef _DEBUG
std::cerr << "Could not map view of file" << std::endl;
#endif
CloseHandle(mapping);
CloseHandle(file);
return nullptr;
}
r_size = GetFileSize(file, 0);
BYTE* localCopyAddress = (BYTE*) VirtualAlloc(NULL, r_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (localCopyAddress == NULL) {
std::cerr << "Could not allocate memory in the current process" << std::endl;
return nullptr;
}
memcpy(localCopyAddress, dllRawData, r_size);
UnmapViewOfFile(dllRawData);
CloseHandle(mapping);
CloseHandle(file);
return localCopyAddress;
}
void free_buffer(BYTE* buffer, size_t buffer_size)
{
if (buffer == NULL) return;
VirtualFree(buffer, buffer_size, MEM_DECOMMIT);
}
wchar_t* get_file_name(wchar_t *full_path)
{
size_t len = wcslen(full_path);
for (size_t i = len - 2; i >= 0; i--) {
if (full_path[i] == '\\' || full_path[i] == '/') {
return full_path + (i + 1);
}
}
return full_path;
}
wchar_t* get_directory(IN wchar_t *full_path, OUT wchar_t *out_buf, IN const size_t out_buf_size)
{
memset(out_buf, 0, out_buf_size);
memcpy(out_buf, full_path, out_buf_size);
wchar_t *name_ptr = get_file_name(out_buf);
if (name_ptr != nullptr) {
*name_ptr = '\0'; //cut it
}
return out_buf;
}
bool get_calc_path(LPWSTR lpwOutPath, DWORD szOutPath, bool isPayl32bit)
{
if (isPayl32bit) {
#ifdef _WIN64
ExpandEnvironmentStringsW(L"%SystemRoot%\\SysWoW64\\calc.exe", lpwOutPath, szOutPath);
#else
ExpandEnvironmentStringsW(L"%SystemRoot%\\system32\\calc.exe", lpwOutPath, szOutPath);
#endif
}
else {
ExpandEnvironmentStringsW(L"%SystemRoot%\\system32\\calc.exe", lpwOutPath, szOutPath);
}
return true;
}