| 200 | } |
| 201 | |
| 202 | bool PE::addSection(std::string name, DWORD flags, std::vector<BYTE> bytes) |
| 203 | { |
| 204 | // get next section's file offset |
| 205 | DWORD newSectionFO = getNewSectionFileOffset(); |
| 206 | |
| 207 | // create IMAGE_SECTION_HEADER for new section |
| 208 | PIMAGE_SECTION_HEADER pNewSectionHeader = &pSectionHeader[pNtHeader->FileHeader.NumberOfSections]; |
| 209 | pNewSectionHeader->Characteristics = flags; |
| 210 | pNewSectionHeader->Misc.VirtualSize = align(bytes.size(), pNtHeader->OptionalHeader.SectionAlignment); |
| 211 | pNewSectionHeader->SizeOfRawData = align(bytes.size(), pNtHeader->OptionalHeader.FileAlignment); |
| 212 | pNewSectionHeader->VirtualAddress = getNewSectionVirtualAddress(); |
| 213 | pNewSectionHeader->PointerToRawData = newSectionFO; |
| 214 | CopyMemory(pNewSectionHeader->Name, name.c_str(), min(name.size(), IMAGE_SIZEOF_SHORT_NAME)); |
| 215 | |
| 216 | // increase section count in the file header, and update size of image |
| 217 | pNtHeader->FileHeader.NumberOfSections += 1; |
| 218 | pNtHeader->OptionalHeader.SizeOfImage += pNewSectionHeader->Misc.VirtualSize; |
| 219 | |
| 220 | // resize our bytes vector to fit our new section bytes in |
| 221 | this->bytes.resize(newSectionFO + pNewSectionHeader->SizeOfRawData); |
| 222 | |
| 223 | // actually copy the section bytes into our bytes vector |
| 224 | std::copy(bytes.begin(), bytes.end(), this->bytes.begin() + newSectionFO); |
| 225 | |
| 226 | // parse section headers again (likely not required as we only store pointers to headers) |
| 227 | if (!parseHeaders()) |
| 228 | return 0; |
| 229 | |
| 230 | return 1; |
| 231 | } |
| 232 | |
| 233 | DWORD PE::getNewSectionVirtualAddress() |
| 234 | { |