13 HANDLE file = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
14 if(file == INVALID_HANDLE_VALUE) {
16 std::cerr <<
"Could not open file!" << std::endl;
20 HANDLE mapping = CreateFileMapping(file, 0, PAGE_READONLY, 0, 0, 0);
23 std::cerr <<
"Could not create mapping!" << std::endl;
28 BYTE *dllRawData = (BYTE*) MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
31 std::cerr <<
"Could not map view of file" << std::endl;
37 size_t r_size = GetFileSize(file, 0);
38 if (read_size != 0 && read_size <= r_size) {
42 std::cerr <<
"[-] Mapping of " << filename <<
" is invalid!" << std::endl;
43 UnmapViewOfFile(dllRawData);
49 if (localCopyAddress !=
nullptr) {
50 memcpy(localCopyAddress, dllRawData, r_size);
55 std::cerr <<
"Could not allocate memory in the current process" << std::endl;
58 UnmapViewOfFile(dllRawData);
61 return localCopyAddress;
67 HANDLE file = CreateFileA(in_path, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
68 if (file == INVALID_HANDLE_VALUE) {
70 std::cerr <<
"Cannot open the file for reading!" << std::endl;
74 size_t r_size =
static_cast<size_t>(GetFileSize(file, 0));
75 if (read_size != 0 && read_size <= r_size) {
79 if (buffer ==
nullptr) {
81 std::cerr <<
"Allocation has failed!" << std::endl;
86 if (!ReadFile(file, buffer, r_size, &out_size,
nullptr)) {
88 std::cerr <<
"Reading failed!" << std::endl;
103 if (!out_path || !dump_data || !dump_size)
return false;
105 HANDLE file = CreateFileA(out_path, GENERIC_WRITE, FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
106 if (file == INVALID_HANDLE_VALUE) {
108 std::cerr <<
"Cannot open the file for writing!" << std::endl;
112 DWORD written_size = 0;
113 bool is_dumped =
false;
114 if (WriteFile(file, dump_data, (DWORD) dump_size, &written_size,
nullptr)) {
119 std::cerr <<
"Failed to write to the file : " << out_path << std::endl;
134 size_t found = str.find_last_of(
"/\\");
135 if (found == std::string::npos) {
138 return str.substr(found + 1);
143 size_t found = str.find_last_of(
"/\\");
144 if (found == std::string::npos) {
147 return str.substr(0, found);
Definitions of the used buffer types. Functions for their allocation and deallocation.
Functions related to operations on files. Wrappers for read/write.
bool free_aligned(ALIGNED_BUF buffer, size_t buffer_size=0)
ALIGNED_BUF alloc_aligned(size_t buffer_size, DWORD protect, ULONGLONG desired_base=NULL)
void free_file(IN peconv::ALIGNED_BUF buffer)
std::string get_directory_name(IN const std::string full_path)
bool free_pe_buffer(ALIGNED_BUF buffer, size_t buffer_size=0)
peconv::ALIGNED_BUF read_from_file(IN const char *path, IN OUT size_t &read_size)
ALIGNED_BUF alloc_pe_buffer(size_t buffer_size, DWORD protect, ULONGLONG desired_base=NULL)
peconv::ALIGNED_BUF load_file(IN const char *filename, OUT size_t &r_size)
bool is_bad_read_ptr(LPCVOID areaStart, SIZE_T areaSize)
bool dump_to_file(IN const char *path, IN PBYTE dump_data, IN size_t dump_size)
std::string get_file_name(IN const std::string full_path)
Miscellaneous utility functions.