MCPcopy Create free account
hub / github.com/connorjaydunn/BinaryShield / addSection

Method addSection

src/pe.cpp:202–231  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

200}
201
202bool 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
233DWORD PE::getNewSectionVirtualAddress()
234{

Callers

nothing calls this directly

Calls 1

alignFunction · 0.85

Tested by

no test coverage detected