33# See the file 'docs/LICENSE' for copying permission.
44
55import logging
6- from ctypes import byref , c_void_p , sizeof
7-
6+ from ctypes import byref , c_bool , c_void_p , sizeof
87from lib .api .process import Process
98from lib .common .abstracts import Auxiliary
109from lib .common .defines import KERNEL32 , PROCESSENTRY32 , TH32CS_SNAPPROCESS
1110from lib .common .exceptions import CuckooError
1211
1312log = logging .getLogger (__name__ )
13+
1414INVALID_HANDLE_VALUE_PTR = c_void_p (- 1 ).value
1515
1616# Ensure snapshot handle is not truncated on 64-bit.
1717KERNEL32 .CreateToolhelp32Snapshot .restype = c_void_p
18+ # Ensure bool return types are not sign-extended on 64-bit.
19+ KERNEL32 .Process32First .restype = c_bool
20+ KERNEL32 .Process32Next .restype = c_bool
1821
1922
2023class TLSDumpMasterSecrets (Auxiliary ):
@@ -32,22 +35,33 @@ def __init__(self, options, config):
3235 def start (self ):
3336 proc_info = PROCESSENTRY32 ()
3437 proc_info .dwSize = sizeof (PROCESSENTRY32 )
38+
3539 snapshot = KERNEL32 .CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS , 0 )
3640 if snapshot in (None , INVALID_HANDLE_VALUE_PTR ):
3741 log .warning ("Failed to create process snapshot" )
42+ del self .options ["tlsdump" ]
3843 return
44+
3945 flag = KERNEL32 .Process32First (snapshot , byref (proc_info ))
4046 pid = 0
47+
4148 while flag :
42- if proc_info .sz_exeFile == b"lsass.exe" :
49+ exename = proc_info .sz_exeFile
50+ if isinstance (exename , bytes ):
51+ exename = exename .decode ("utf-8" , errors = "replace" )
52+ if exename == "lsass.exe" :
4353 pid = proc_info .th32ProcessID
4454 log .info ("lsass.exe found, pid %d" , pid )
45- flag = 0
55+ break
4656 flag = KERNEL32 .Process32Next (snapshot , byref (proc_info ))
57+
4758 KERNEL32 .CloseHandle (snapshot )
59+
4860 if not pid :
4961 log .warning ("Unable to find lsass.exe process" )
62+ del self .options ["tlsdump" ]
5063 return
64+
5165 try :
5266 p = Process (options = self .options , config = self .config , pid = pid )
5367 filepath = p .get_filepath ()
@@ -56,8 +70,6 @@ def start(self):
5670 if "process access denied" in e .message :
5771 log .warning ("You're not running the Agent as Administrator" )
5872 else :
59- log .warning (
60- "An unknown error occurred while trying to inject into the lsass.exe process to dump TLS master secrets: %s" ,
61- e ,
62- )
73+ log .warning ("An unknown error occurred while trying to inject into the lsass.exe process to dump TLS master secrets: %s" , e )
74+
6375 del self .options ["tlsdump" ]
0 commit comments