Agent: add lock to wmi tools

impacket libraries used for WMI are not designed for multithreading
This commit is contained in:
vakaris_zilius 2022-03-09 13:34:07 +00:00 committed by vakarisz
parent 16535e06c7
commit 3dc8ef606c
1 changed files with 8 additions and 1 deletions

View File

@ -1,4 +1,5 @@
import logging
import threading
from impacket.dcerpc.v5.dcom import wmi
from impacket.dcerpc.v5.dcom.wmi import DCERPCSessionError
@ -8,6 +9,11 @@ from impacket.dcerpc.v5.dtypes import NULL
logger = logging.getLogger(__name__)
# Due to the limitations of impacket library we should only run one WmiConnection at a time
# See comments in https://github.com/guardicore/monkey/pull/1766
lock = threading.Lock()
class AccessDeniedException(Exception):
def __init__(self, host, username, password, domain):
super(AccessDeniedException, self).__init__(
@ -77,6 +83,7 @@ class WmiTools(object):
def dcom_wrap(func):
def _wrapper(*args, **kwarg):
try:
with lock:
return func(*args, **kwarg)
finally:
WmiTools.dcom_cleanup()