libPeConv
A library to load, manipulate, dump PE files.
Loading...
Searching...
No Matches
file_util.cpp
Go to the documentation of this file.
1#include "peconv/file_util.h"
3#include "peconv/util.h"
4
5#include <fstream>
6#ifdef _DEBUG
7 #include <iostream>
8#endif
9
10//load file content using MapViewOfFile
11peconv::ALIGNED_BUF peconv::load_file(IN LPCTSTR filename, OUT size_t &read_size)
12{
13 HANDLE file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
14 if(file == INVALID_HANDLE_VALUE) {
15#ifdef _DEBUG
16 std::cerr << "Could not open file!" << std::endl;
17#endif
18 return nullptr;
19 }
20 HANDLE mapping = CreateFileMapping(file, 0, PAGE_READONLY, 0, 0, 0);
21 if (!mapping) {
22#ifdef _DEBUG
23 std::cerr << "Could not create mapping!" << std::endl;
24#endif
25 CloseHandle(file);
26 return nullptr;
27 }
28 BYTE *dllRawData = (BYTE*) MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
29 if (!dllRawData) {
30#ifdef _DEBUG
31 std::cerr << "Could not map view of file" << std::endl;
32#endif
33 CloseHandle(mapping);
34 CloseHandle(file);
35 return nullptr;
36 }
37 size_t r_size = GetFileSize(file, 0);
38 if (read_size != 0 && read_size <= r_size) {
39 r_size = read_size;
40 }
41 if (peconv::is_bad_read_ptr(dllRawData, r_size)) {
42 std::cerr << "[-] Mapping of " << filename << " is invalid!" << std::endl;
43 UnmapViewOfFile(dllRawData);
44 CloseHandle(mapping);
45 CloseHandle(file);
46 return nullptr;
47 }
48 peconv::UNALIGNED_BUF localCopyAddress = peconv::alloc_unaligned(r_size);
49 if (localCopyAddress != nullptr) {
50 memcpy(localCopyAddress, dllRawData, r_size);
51 read_size = r_size;
52 } else {
53 read_size = 0;
54#ifdef _DEBUG
55 std::cerr << "Could not allocate memory in the current process" << std::endl;
56#endif
57 }
58 UnmapViewOfFile(dllRawData);
59 CloseHandle(mapping);
60 CloseHandle(file);
61 return localCopyAddress;
62}
63
64//load file content using ReadFile
65peconv::ALIGNED_BUF peconv::read_from_file(IN LPCTSTR in_path, IN OUT size_t &read_size)
66{
67 HANDLE file = CreateFile(in_path, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
68 if (file == INVALID_HANDLE_VALUE) {
69#ifdef _DEBUG
70 std::cerr << "Cannot open the file for reading!" << std::endl;
71#endif
72 return nullptr;
73 }
74 DWORD r_size = GetFileSize(file, 0);
75 if (read_size != 0 && read_size <= r_size) {
76 r_size = MASK_TO_DWORD(read_size);
77 }
79 if (buffer == nullptr) {
80#ifdef _DEBUG
81 std::cerr << "Allocation has failed!" << std::endl;
82#endif
83 return nullptr;
84 }
85 DWORD out_size = 0;
86 if (!ReadFile(file, buffer, r_size, &out_size, nullptr)) {
87#ifdef _DEBUG
88 std::cerr << "Reading failed!" << std::endl;
89#endif
90 peconv::free_file(buffer);
91 buffer = nullptr;
92 read_size = 0;
93 } else {
94 read_size = r_size;
95 }
96 CloseHandle(file);
97 return buffer;
98}
99
100//save the given buffer into a file
101bool peconv::dump_to_file(IN LPCTSTR out_path, IN PBYTE dump_data, IN size_t dump_size)
102{
103 if (!out_path || !dump_data || !dump_size) return false;
104
105 HANDLE file = CreateFile(out_path, GENERIC_WRITE, FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
106 if (file == INVALID_HANDLE_VALUE) {
107#ifdef _DEBUG
108 std::cerr << "Cannot open the file for writing!" << std::endl;
109#endif
110 return false;
111 }
112 DWORD written_size = 0;
113 bool is_dumped = false;
114 if (WriteFile(file, dump_data, (DWORD) dump_size, &written_size, nullptr)) {
115 is_dumped = true;
116 }
117#ifdef _DEBUG
118 else {
119 std::cerr << "Failed to write to the file : " << out_path << std::endl;
120 }
121#endif
122 CloseHandle(file);
123 return is_dumped;
124}
125
126//free the buffer allocated by load_file/read_from_file
131
132std::string peconv::get_file_name(IN const std::string str)
133{
134 size_t found = str.find_last_of("/\\");
135 if (found == std::string::npos) {
136 return str;
137 }
138 return str.substr(found + 1);
139}
140
141std::string peconv::get_directory_name(IN const std::string str)
142{
143 size_t found = str.find_last_of("/\\");
144 if (found == std::string::npos) {
145 return "";
146 }
147 return str.substr(0, found);
148}
149
150size_t peconv::find_extension_pos(IN const std::string str)
151{
152 size_t len = str.length();
153 size_t ext_pos = len;
154 for (size_t k = len; k != 0; k--) {
155 size_t i = k - 1;
156 char c = str[i];
157 if (c == '.') {
158 ext_pos = i;
159 break;
160 }
161 }
162 return ext_pos;
163}
Definitions of the used buffer types. Functions for their allocation and deallocation.
#define MASK_TO_DWORD(val)
Definition buffer_util.h:12
Functions related to operations on files. Wrappers for read/write.
bool dump_to_file(IN LPCTSTR path, IN PBYTE dump_data, IN size_t dump_size)
peconv::UNALIGNED_BUF load_file(IN LPCTSTR filename, OUT size_t &r_size)
Definition file_util.cpp:11
PBYTE ALIGNED_BUF
Definition buffer_util.h:46
UNALIGNED_BUF alloc_unaligned(size_t buf_size)
std::string get_directory_name(IN const std::string full_path)
bool is_bad_read_ptr(LPCVOID areaStart, SIZE_T areaSize)
Definition util.cpp:150
void free_file(IN peconv::UNALIGNED_BUF buffer)
peconv::UNALIGNED_BUF read_from_file(IN LPCTSTR path, IN OUT size_t &read_size)
Definition file_util.cpp:65
PBYTE UNALIGNED_BUF
Definition buffer_util.h:41
size_t find_extension_pos(IN const std::string str)
std::string get_file_name(IN const std::string full_path)
void free_unaligned(UNALIGNED_BUF section_buffer)
Miscellaneous utility functions.