pan-unit42 / dotnetfile

MIT License
97 stars 16 forks source link

Handle of PathLike objects for DotNetPEParser #4

Closed mariesosa closed 1 year ago

mariesosa commented 1 year ago

Summary

Fix to handle path objects such as pathlib.Path in DotNetPEParser, which currently raise an error (see below).

Previous behavior

>>>from pathlib import Path
>>>import dotnetfile
>>>filepath = Path("<path_2_file>")
>>>dotnetfile.DotNetPE(filepath)
~/.virtualenvs/dotnet/lib/python3.7/site-packages/dotnetfile-0.1.0-py3.7.egg/dotnetfile/dotnetfile.py in __init__(self, path)
    161 class DotNetPE(DotNetPEParser):
    162     def __init__(self, path):
--> 163         super().__init__(path)
    164         self.AntiMetadataAnalysis = AntiMetadataAnalysis(self)
    165         self.Cor20Header = Cor20Header(self)

~/.virtualenvs/dotnet/lib/python3.7/site-packages/dotnetfile-0.1.0-py3.7.egg/dotnetfile/parser.py in __init__(self, file_ref, parse, log_level, *args, **kwargs)
     77         }
     78 
---> 79         if not self.is_dotnet_file():
     80             raise CLRFormatError('File is not a .NET assembly.')
     81 

~/.virtualenvs/dotnet/lib/python3.7/site-packages/dotnetfile-0.1.0-py3.7.egg/dotnetfile/parser.py in is_dotnet_file(self)
    153         dotnet_data_dir = DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR']
    154 
--> 155         if self.is_dotnet_data_directory_hidden():
    156             result = True
    157         else:

~/.virtualenvs/dotnet/lib/python3.7/site-packages/dotnetfile-0.1.0-py3.7.egg/dotnetfile/parser.py in is_dotnet_data_directory_hidden(self)
    123 
    124         dotnet_dir_number = DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR']
--> 125         number_of_rva_and_sizes = self.OPTIONAL_HEADER.NumberOfRvaAndSizes
    126 
    127         try:

AttributeError: 'DotNetPE' object has no attribute 'OPTIONAL_HEADER'

This error was due to a bad handling of variable file_ref used to instantiate DotNetPEParser.

TheEnergyStory commented 1 year ago

Thanks for your contribution and excuse the very long delay! This has been added to version 0.2.4