// patches file's PE header to make it compatible with WinXP void patchHeader(LPCSTR filename) { PeLib::PeFile* pef = PeLib::openPeFile(filename); pef->readMzHeader(); pef->readPeHeader(); PatchPeHeaderVisitor vis; pef->visit(vis); delete pef; }
void pack_pe(char * name) { std::string filename = name; PeLib::PeFile* pef = PeLib::openPeFile(filename); if (!pef) { std::cout << "Invalid PE File" << std::endl; return ; } pef->readMzHeader(); pef->readPeHeader(); dump(centerOutput("----------------------------------------------")); dump(centerOutput("PE Loaded")); dump(centerOutput("----------------------------------------------")); PeRebuilderVisitor v2; pef->visit(v2); delete pef; }
/** * Extracts all icons from a PE file. * @param filename Name of the PE file. **/ void extractIcons(const std::string& filename) { std::cout << "Opening file: " << filename << std::endl; PeLib::PeFile* pef = PeLib::openPeFile(filename); if (!pef) { std::cout << "Not a valid PE file" << std::endl; return; } try { pef->readMzHeader(); pef->readPeHeader(); pef->readResourceDirectory(); } catch(std::exception& e) { delete pef; std::cout << "Not a valid PE file." << std::endl << std::endl; return; } unsigned int icons = pef->resDir().getNumberOfResources(PeLib::PELIB_RT_ICON); unsigned int icongroups = pef->resDir().getNumberOfResources(PeLib::PELIB_RT_GROUP_ICON); if (icongroups == -1 || icons == -1) { std::cout << "No icons found." << std::endl << std::endl; delete pef; return; } std::cout << icons << " icons found in " << icongroups << " icon groups." << std::endl; std::vector<PeLib::byte> data; using PeLib::word; // Icons in ICO fils have a different header than icons in PE files. // The first six bytes never change for simple ICO files. char iconheader[22] = {0, 0, 1, 0, 1, 0}; unsigned int icongroupindex = pef->resDir().resourceTypeIdToIndex(PeLib::PELIB_RT_GROUP_ICON); for (unsigned int i=0;i<icongroups;i++) { std::cout << "Reading icon group " << i + 1 << "." << std::endl; // std::cout << "OH :" << pef->resDir().getResourceIdByIndex(icongroupindex, i) << std::endl; pef->resDir().getResourceData(PeLib::PELIB_RT_GROUP_ICON, pef->resDir().getResourceIdByIndex(icongroupindex, i), data); word entries = *(word*)(&data[4]); // Number of icons in icon group. for (int j=0;j<entries;j++) { std::cout << "Reading icon " << j + 1<< "." << std::endl; // Parts of the icon header can be copied from the PE icon to the ICO icon. std::copy(data.begin() + 6 + j * 14, data.begin() + 6 + j * 14 + 12, iconheader + 6); *(word*)(&iconheader[18]) = 0x16; // Read the icon data. std::vector<PeLib::byte> icondata; word iconid = *(word*)(&data[6 + 12 + j * 14]); pef->resDir().getResourceData(PeLib::PELIB_RT_ICON, iconid, icondata); std::string iconname = makeIconName(filename, iconid); std::ofstream file(iconname.c_str(), std::ios::binary); if (file) { std::cout << "Writing icon " << j + 1 << " to file " << iconname << "." << std::endl; // Write the icon header followed by the icon data. file.write(iconheader, 22); file.write(reinterpret_cast<char*>(&icondata[0]), icondata.size()); } else { std::cout << "Couldn't open file: " << iconname << ". Make sure the icons subdirectory exists." << std::endl; } } } delete pef; std::cout << std::endl; }