fortra / impacket

Impacket is a collection of Python classes for working with network protocols.
https://www.coresecurity.com
Other
13.37k stars 3.56k forks source link

winregistry.Registry: handle destruction of uninitialized instance #1765

Closed DidierA closed 2 months ago

DidierA commented 3 months ago

If Registry() is called and initialization fails (for instance, the given file does not exist), the not initialized instanced will be destroyed, and __del__() calls close() which calls self.fd.close(), raising an AttributeError. This fixes this situation.

Sample code to reproduce

from impacket.winregistry import Registry

try:
    reg = Registry('This file does not exist', isRemote = False)
except FileNotFoundError:
    pass

expected result

nothing

Actual result

Exception ignored in: <function Registry.__del__ at 0x7706ebe50a40>
Traceback (most recent call last):
  File "/home/didier/Tools/impacket/impacket/winregistry.py", line 183, in __del__
    self.close()
  File "/home/didier/Tools/impacket/impacket/winregistry.py", line 180, in close
    self.fd.close()
    ^^^^^^^
AttributeError: 'Registry' object has no attribute 'fd'

Actual result after fix

nothing

gabrielg5 commented 2 months ago

merged! thank you @DidierA