Skip to content

Commit 9c485d4

Browse files
authored
Merge pull request #429 from gdesmar/undeclared_next_section_virtual_address
Fix undeclared next_section_virtual_address usage
2 parents 26e2584 + fdfe543 commit 9c485d4

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

pefile.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,7 @@ def __init__(self, *args, **kwargs):
11451145
self.VirtualAddress_adj = None
11461146
self.section_min_addr = None
11471147
self.section_max_addr = None
1148+
self.index_in_file = None
11481149

11491150
def get_PointerToRawData_adj(self):
11501151
if self.PointerToRawData_adj is None and self.PointerToRawData is not None:
@@ -3679,20 +3680,8 @@ def parse_sections(self, offset, max_offset=0x10000000):
36793680
# Set the section's flags according to the Characteristics member
36803681
set_flags(section, section.Characteristics, section_flags)
36813682

3682-
if section.__dict__.get(
3683-
"IMAGE_SCN_MEM_WRITE", False
3684-
) and section.__dict__.get("IMAGE_SCN_MEM_EXECUTE", False):
3685-
if section.Name.rstrip(b"\x00") == b"PAGE" and self.is_driver():
3686-
# Drivers can have a PAGE section with those flags set without
3687-
# implying that it is malicious
3688-
pass
3689-
else:
3690-
self.__warnings.append(
3691-
f"Suspicious flags set for section {i}. "
3692-
"Both IMAGE_SCN_MEM_WRITE and IMAGE_SCN_MEM_EXECUTE are set. "
3693-
"This might indicate a packed executable."
3694-
)
3695-
3683+
# Set the section's original index as it may change after being sorted by VirtualAddress
3684+
section.index_in_file = i
36963685
self.sections.append(section)
36973686

36983687
# Sort the sections by their VirtualAddress and add a field to each of them
@@ -3707,6 +3696,21 @@ def parse_sections(self, offset, max_offset=0x10000000):
37073696
idx + 1
37083697
].VirtualAddress
37093698

3699+
for section in self.sections:
3700+
if section.__dict__.get(
3701+
"IMAGE_SCN_MEM_WRITE", False
3702+
) and section.__dict__.get("IMAGE_SCN_MEM_EXECUTE", False):
3703+
if section.Name.rstrip(b"\x00") == b"PAGE" and self.is_driver():
3704+
# Drivers can have a PAGE section with those flags set without
3705+
# implying that it is malicious
3706+
pass
3707+
else:
3708+
self.__warnings.append(
3709+
f"Suspicious flags set for section {section.index_in_file}. "
3710+
"Both IMAGE_SCN_MEM_WRITE and IMAGE_SCN_MEM_EXECUTE are set. "
3711+
"This might indicate a packed executable."
3712+
)
3713+
37103714
if self.FILE_HEADER.NumberOfSections > 0 and self.sections:
37113715
return (
37123716
offset + self.sections[0].sizeof() * self.FILE_HEADER.NumberOfSections

0 commit comments

Comments
 (0)