Load the specified PE from disk.
| 15 | |
| 16 | // Load the specified PE from disk. |
| 17 | cobf_error cobf::load_pe() |
| 18 | { |
| 19 | // Check if already loaded. |
| 20 | if (!this->pe_rawf.empty()) return cobf_error::COBF_PE_LOADED; |
| 21 | |
| 22 | // Get an access to the file. |
| 23 | cobf_error ret_err = cobf_error::COBF_NO_ERROR; |
| 24 | HANDLE h_file = CreateFileA( |
| 25 | this->pe_path.c_str(), // The PE file path. |
| 26 | GENERIC_READ, // Access options. |
| 27 | FILE_SHARE_READ, // The share mode. |
| 28 | NULL, // Security attributes. |
| 29 | OPEN_EXISTING, // Disposition. |
| 30 | FILE_ATTRIBUTE_NORMAL, // File attributes. |
| 31 | NULL // The template handle. |
| 32 | ); |
| 33 | |
| 34 | // Check the return. |
| 35 | if (h_file == INVALID_HANDLE_VALUE) |
| 36 | { |
| 37 | // Cannot open the specified file. |
| 38 | return cobf_error::COBF_CANNOT_OPEN_FILE; |
| 39 | }; |
| 40 | |
| 41 | // Needed local variables. |
| 42 | PIMAGE_DOS_HEADER dos_hdr; |
| 43 | PIMAGE_NT_HEADERS nt_hdrs; |
| 44 | PIMAGE_IMPORT_DESCRIPTOR p_imports; |
| 45 | size_t imports_size; |
| 46 | |
| 47 | // Getting the size. |
| 48 | LARGE_INTEGER f_size; |
| 49 | if (!GetFileSizeEx(h_file, &f_size)) |
| 50 | { |
| 51 | // Cannot get the file size. |
| 52 | ret_err = cobf_error::COBF_CANNOT_GET_SIZE; |
| 53 | goto LOAD_FINISH; |
| 54 | }; |
| 55 | |
| 56 | // Resizing the raw file array. |
| 57 | this->pe_rawf.resize((size_t)f_size.QuadPart); |
| 58 | |
| 59 | // Reading the file. |
| 60 | DWORD r_size; |
| 61 | if (!ReadFile( |
| 62 | h_file, // The PE file handle. |
| 63 | this->pe_rawf.data(), // The data of the vector. |
| 64 | (DWORD)this->pe_rawf.size(),// Size to read. |
| 65 | &r_size, // Read bytes. |
| 66 | NULL // Overlapped struct. |
| 67 | )) |
| 68 | { |
| 69 | // Cannot read. |
| 70 | ret_err = cobf_error::COBF_CANNOT_READ_FILE; |
| 71 | goto LOAD_FINISH; |
| 72 | }; |
| 73 | |
| 74 | // Get the dos header. |
no test coverage detected