This addresses a regression with sample 848092559:
LDB sig (Win.Virus.Virut-5898123-1) that uses 'NumberOfSections:3-3'
started matching on a PE that has 4 sections, but one is totally outside
of the file and gets removed. Previously, two of the ClamAV PE header
parsing implementations handled this case differently, and the NDB/LDB
matching code would be told there were 4 sections while the bytecode
and unpacking code would only see 3 sections. When consolidating the
PE header parsing code, I made it so that the section always gets
removed.
For now we just replicate the original behavior by providing a new
flag to the PE header parsing code. We should re-evaluate the effects
that this has later, once we have better tests for the bytecode API
and we have test samples for each of the hardcoded detection cases in
cli_scanpe.
Also, fixes some memory leaks based on the changes in my last commit x_x
// First, if a section exists totally outside of a file, remove the
// section from the list.
// TODO Document that this happens in the function documentation
/* First, if a section exists totally outside of a file, remove the
*sectionfromthelistorzerooutit'ssize.*/
if(section->rsz){/* Don't bother with virtual only sections */
if(section->raw>=fsize||section->uraw>=fsize){
cli_dbgmsg("cli_peheader: Broken PE file - Section %d starts or exists beyond the end of file (Offset@ %lu, Total filesize %lu)\n",section_pe_idx,(unsignedlong)section->raw,(unsignedlong)fsize);
cli_dbgmsg("cli_peheader: PE Section %d uraw+ursz extends past the end of the file by %lu bytes\n",section_pe_idx,(section->uraw+section->ursz)-fsize);
section->ursz=fsize-section->uraw;
/* If a section is truncated, adjust it's size value */
cli_dbgmsg("cli_peheader: PE Section %d uraw+ursz extends past the end of the file by %lu bytes\n",section_pe_idx,(section->uraw+section->ursz)-fsize);