From 3dc8ef606c6d52ac5b226f763f6ce2ae0d3facc0 Mon Sep 17 00:00:00 2001 From: vakaris_zilius Date: Wed, 9 Mar 2022 13:34:07 +0000 Subject: [PATCH] Agent: add lock to wmi tools impacket libraries used for WMI are not designed for multithreading --- monkey/infection_monkey/exploit/tools/wmi_tools.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/monkey/infection_monkey/exploit/tools/wmi_tools.py b/monkey/infection_monkey/exploit/tools/wmi_tools.py index b7eaf8675..99fda3141 100644 --- a/monkey/infection_monkey/exploit/tools/wmi_tools.py +++ b/monkey/infection_monkey/exploit/tools/wmi_tools.py @@ -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,7 +83,8 @@ class WmiTools(object): def dcom_wrap(func): def _wrapper(*args, **kwarg): try: - return func(*args, **kwarg) + with lock: + return func(*args, **kwarg) finally: WmiTools.dcom_cleanup()