13 HANDLE file = CreateFile(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 = CreateFile(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 DWORD r_size = 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 = CreateFile(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;