1+ import logging
12from gevent import sleep
23from impacket .dcerpc .v5 import transport , scmr
34from impacket .smbconnection import *
5+ from impacket .smb import SMB_DIALECT
46from cme .helpers import gen_random_string
57
68class SMBEXEC :
7- KNOWN_PROTOCOLS = {
8- '139/SMB' : (r'ncacn_np:%s[\pipe\svcctl]' , 139 ),
9- '445/SMB' : (r'ncacn_np:%s[\pipe\svcctl]' , 445 ),
10- }
119
12- def __init__ (self , host , protocol , username = '' , password = '' , domain = '' , hashes = None , share = None ):
10+ def __init__ (self , host , protocol , username = '' , password = '' , domain = '' , hashes = None , share = None , port = 445 ):
1311 self .__host = host
12+ self .__port = port
1413 self .__username = username
1514 self .__password = password
1615 self .__serviceName = gen_random_string ()
1716 self .__domain = domain
1817 self .__lmhash = ''
1918 self .__nthash = ''
2019 self .__share = share
21- self .__output = ' \\ Windows \\ Temp \\ ' + gen_random_string ()
22- self .__batchFile = '%TEMP% \\ ' + gen_random_string () + '.bat'
20+ self .__output = None
21+ self .__batchFile = None
2322 self .__outputBuffer = ''
2423 self .__shell = '%COMSPEC% /Q /c '
2524 self .__retOutput = False
@@ -40,20 +39,17 @@ def __init__(self, host, protocol, username = '', password = '', domain = '', ha
4039 if self .__password is None :
4140 self .__password = ''
4241
43- protodef = SMBEXEC .KNOWN_PROTOCOLS ['{}/SMB' .format (protocol )]
44- port = protodef [1 ]
45-
46- stringbinding = protodef [0 ] % self .__host
47-
42+ stringbinding = 'ncacn_np:%s[\pipe\svcctl]' % self .__host
43+ logging .debug ('StringBinding %s' % stringbinding )
4844 self .__rpctransport = transport .DCERPCTransportFactory (stringbinding )
49- self .__rpctransport .set_dport (port )
50-
45+ self .__rpctransport .set_dport (self . __port )
46+ #self.__rpctransport.setRemoteHost(self.__host)
5147 if hasattr (self .__rpctransport ,'preferred_dialect' ):
5248 self .__rpctransport .preferred_dialect (SMB_DIALECT )
5349 if hasattr (self .__rpctransport , 'set_credentials' ):
5450 # This method exists only for selected protocol sequences.
5551 self .__rpctransport .set_credentials (self .__username , self .__password , self .__domain , self .__lmhash , self .__nthash )
56- #rpctransport.set_kerberos(self.__doKerberos)
52+ #rpctransport.set_kerberos(self.__doKerberos, self.__kdcHost )
5753
5854 self .__scmr = self .__rpctransport .get_dce_rpc ()
5955 self .__scmr .connect ()
@@ -76,7 +72,10 @@ def execute(self, command, output=False):
7672 return self .__outputBuffer
7773
7874 def cd (self , s ):
75+ ret_state = self .__retOutput
76+ self .__retOutput = False
7977 self .execute_remote ('cd ' )
78+ self .__retOutput = ret_state
8079
8180 def get_output (self ):
8281
@@ -96,13 +95,18 @@ def output_callback(data):
9695 sleep (2 )
9796
9897 def execute_remote (self , data ):
98+ self .__output = '\\ Windows\\ Temp\\ ' + gen_random_string ()
99+ self .__batchFile = '%TEMP%\\ ' + gen_random_string () + '.bat'
100+
99101 if self .__retOutput :
100102 command = self .__shell + 'echo ' + data + ' ^> ' + self .__output + ' 2^>^&1 > ' + self .__batchFile + ' & ' + self .__shell + self .__batchFile
101103 else :
102104 command = self .__shell + 'echo ' + data + ' 2^>^&1 > ' + self .__batchFile + ' & ' + self .__shell + self .__batchFile
103105
104106 command += ' & ' + 'del ' + self .__batchFile
105107
108+ logging .debug ('Executing command: ' + command )
109+
106110 resp = scmr .hRCreateServiceW (self .__scmr , self .__scHandle , self .__serviceName , self .__serviceName , lpBinaryPathName = command )
107111 service = resp ['lpServiceHandle' ]
108112
0 commit comments