Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposing IMAGE_RESOURCE_DIRECTORY fields and removing Characteristics… #390

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions libyara/modules/pe.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ typedef struct _PE
YR_OBJECT* object;
IMPORTED_DLL* imported_dlls;
uint32_t resources;
uint32_t resources_dir;

} PE;

Expand Down Expand Up @@ -472,10 +473,46 @@ int _pe_iterate_resources(
{
int result = RESOURCE_ITERATOR_FINISHED;

// A few sanity checks to avoid corrupt files
// The directory entry should be populated even if the sanity checks aren't
// satisfied to allow for signatures on malformed files.

if (resource_dir->Characteristics != 0 ||
resource_dir->NumberOfNamedEntries > 32768 ||
set_integer(
resource_dir->Characteristics,
pe->object,
"resources_dir[%i].characteristics",
pe->resources_dir);
set_integer(
resource_dir->TimeDateStamp,
pe->object,
"resources_dir[%i].time_date_stamp",
pe->resources_dir);
set_integer(
resource_dir->MajorVersion,
pe->object,
"resources_dir[%i].major_version",
pe->resources_dir);
set_integer(
resource_dir->MinorVersion,
pe->object,
"resources_dir[%i].minor_version",
pe->resources_dir);
set_integer(
resource_dir->NumberOfNamedEntries,
pe->object,
"resources_dir[%i].number_of_named_entries",
pe->resources_dir);
set_integer(
resource_dir->NumberOfIdEntries,
pe->object,
"resources_dir[%i].number_of_id_entries",
pe->resources_dir);
pe->resources_dir += 1;

// A few sanity checks to avoid corrupt files. Characteristics of 0 shouldn't
// be used for sanity check as valid resource entries can have arbitrary
// values of this field.

if (resource_dir->NumberOfNamedEntries > 32768 ||
resource_dir->NumberOfIdEntries > 32768)
{
return result;
Expand Down Expand Up @@ -1296,6 +1333,7 @@ void pe_parse_header(
(void*) pe);

set_integer(pe->resources, pe->object, "number_of_resources");
set_integer(pe->resources_dir, pe->object, "number_of_resources_dir");

section = IMAGE_FIRST_SECTION(pe->header);

Expand Down Expand Up @@ -1836,6 +1874,15 @@ begin_declarations;
declare_string("language_string");
end_struct_array("resources");
declare_integer("number_of_resources");
begin_struct_array("resources_dir");
declare_integer("characteristics");
declare_integer("time_date_stamp");
declare_integer("major_version");
declare_integer("minor_version");
declare_integer("number_of_named_entries");
declare_integer("number_of_id_entries");
end_struct_array("resources_dir");
declare_integer("number_of_resources_dir");

#if defined(HAVE_LIBCRYPTO)
begin_struct_array("signatures");
Expand Down Expand Up @@ -2136,6 +2183,7 @@ int module_load(
pe->header = pe_header;
pe->object = module_object;
pe->resources = 0;
pe->resources_dir = 0;

module_object->data = pe;

Expand Down