pefile icon indicating copy to clipboard operation
pefile copied to clipboard

Fail to write file if only timestamps are updated.

Open s3c opened this issue 4 years ago • 0 comments

When running the following code in Python 3.75:

import sys
import pefile

pe = pefile.PE(sys.argv[1])
pe.FILE_HEADER.TimeDateStamp = 0
pe.DIRECTORY_ENTRY_EXPORT.struct.TimeDateStamp = 0
pe.DIRECTORY_ENTRY_RESOURCE.struct.TimeDateStamp = 0
pe.write(filename=sys.argv[1])

I get a error associated with writing to a file with an open handle:

Traceback (most recent call last): File "scripts\update_timestamp.py", line 15, in pe.write(filename=out_filename) File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\pefile.py", line 2284, in write f = open(filename, 'wb+') OSError: [Errno 22] Invalid argument: 'test.exe'

However, when adding the following before the write, it works as expected:

pe.OPTIONAL_HEADER.CheckSum = pe.generate_checksum()

I'm guessing the file is held open by something unless specific fields are edited?

s3c avatar Oct 18 '19 10:10 s3c