2009-04-22 15:48:07 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
2012-07-12 21:38:03 +04:00
|
|
|
Copyright (c) 2006-2012 sqlmap developers (http://sqlmap.org/)
|
2010-10-15 03:18:29 +04:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
2009-04-22 15:48:07 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
from lib.core.common import randomStr
|
|
|
|
from lib.core.data import conf
|
|
|
|
from lib.core.data import logger
|
|
|
|
|
|
|
|
class Registry:
|
|
|
|
"""
|
|
|
|
This class defines methods to read and write Windows registry keys
|
|
|
|
"""
|
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
def _initVars(self, regKey, regValue, regType=None, regData=None, parse=False):
|
|
|
|
self._regKey = regKey
|
|
|
|
self._regValue = regValue
|
|
|
|
self._regType = regType
|
|
|
|
self._regData = regData
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
self._randStr = randomStr(lowercase=True)
|
|
|
|
self._batPathRemote = "%s/tmpr%s.bat" % (conf.tmpPath, self._randStr)
|
|
|
|
self._batPathLocal = os.path.join(conf.outputPath, "tmpr%s.bat" % self._randStr)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
if parse:
|
2012-12-06 17:14:19 +04:00
|
|
|
readParse = "FOR /F \"tokens=*\" %%A IN ('REG QUERY \"" + self._regKey + "\" /v \"" + self._regValue + "\"') DO SET value=%%A\r\nECHO %value%\r\n"
|
2009-04-22 15:48:07 +04:00
|
|
|
else:
|
2012-12-06 17:14:19 +04:00
|
|
|
readParse = "REG QUERY \"" + self._regKey + "\" /v \"" + self._regValue + "\""
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
self._batRead = (
|
2009-04-22 15:48:07 +04:00
|
|
|
"@ECHO OFF\r\n",
|
2013-01-10 18:02:28 +04:00
|
|
|
readParse,
|
|
|
|
)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
self._batAdd = (
|
2009-04-22 15:48:07 +04:00
|
|
|
"@ECHO OFF\r\n",
|
2013-01-10 18:02:28 +04:00
|
|
|
"REG ADD \"%s\" /v \"%s\" /t %s /d %s /f" % (self._regKey, self._regValue, self._regType, self._regData),
|
|
|
|
)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
self._batDel = (
|
2009-04-22 15:48:07 +04:00
|
|
|
"@ECHO OFF\r\n",
|
2013-01-10 18:02:28 +04:00
|
|
|
"REG DELETE \"%s\" /v \"%s\" /f" % (self._regKey, self._regValue),
|
|
|
|
)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
def _createLocalBatchFile(self):
|
|
|
|
self._batPathFp = open(self._batPathLocal, "w")
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2012-12-21 14:10:05 +04:00
|
|
|
if self._operation == "read":
|
2012-12-06 17:14:19 +04:00
|
|
|
lines = self._batRead
|
2012-12-21 14:10:05 +04:00
|
|
|
elif self._operation == "add":
|
2012-12-06 17:14:19 +04:00
|
|
|
lines = self._batAdd
|
2012-12-21 14:10:05 +04:00
|
|
|
elif self._operation == "delete":
|
2012-12-06 17:14:19 +04:00
|
|
|
lines = self._batDel
|
2009-04-22 15:48:07 +04:00
|
|
|
|
|
|
|
for line in lines:
|
2012-12-06 17:14:19 +04:00
|
|
|
self._batPathFp.write(line)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
self._batPathFp.close()
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
def _createRemoteBatchFile(self):
|
|
|
|
logger.debug("creating batch file '%s'" % self._batPathRemote)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
self._createLocalBatchFile()
|
|
|
|
self.writeFile(self._batPathLocal, self._batPathRemote, "text")
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
os.unlink(self._batPathLocal)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
def readRegKey(self, regKey, regValue, parse=False):
|
2012-12-21 14:10:05 +04:00
|
|
|
self._operation = "read"
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2012-12-21 14:10:05 +04:00
|
|
|
Registry._initVars(self, regKey, regValue, parse=parse)
|
2012-12-06 17:14:19 +04:00
|
|
|
self._createRemoteBatchFile()
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
logger.debug("reading registry key '%s' value '%s'" % (regKey, regValue))
|
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
data = self.evalCmd(self._batPathRemote)
|
2010-11-03 13:08:27 +03:00
|
|
|
|
2010-03-13 02:12:06 +03:00
|
|
|
if data and not parse:
|
2010-03-13 01:26:06 +03:00
|
|
|
pattern = ' '
|
|
|
|
index = data.find(pattern)
|
|
|
|
if index != -1:
|
|
|
|
data = data[index + len(pattern):]
|
2009-09-26 03:03:45 +04:00
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
self.delRemoteFile(self._batPathRemote)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
return data
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
def addRegKey(self, regKey, regValue, regType, regData):
|
2012-12-21 14:10:05 +04:00
|
|
|
self._operation = "add"
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2012-12-21 14:10:05 +04:00
|
|
|
Registry._initVars(self, regKey, regValue, regType, regData)
|
2012-12-06 17:14:19 +04:00
|
|
|
self._createRemoteBatchFile()
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
debugMsg = "adding registry key value '%s' " % self._regValue
|
|
|
|
debugMsg += "to registry key '%s'" % self._regKey
|
2009-04-22 15:48:07 +04:00
|
|
|
logger.debug(debugMsg)
|
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
self.execCmd(cmd=self._batPathRemote)
|
|
|
|
self.delRemoteFile(self._batPathRemote)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
def delRegKey(self, regKey, regValue):
|
2012-12-21 14:10:05 +04:00
|
|
|
self._operation = "delete"
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2012-12-21 14:10:05 +04:00
|
|
|
Registry._initVars(self, regKey, regValue)
|
2012-12-06 17:14:19 +04:00
|
|
|
self._createRemoteBatchFile()
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
debugMsg = "deleting registry key value '%s' " % self._regValue
|
|
|
|
debugMsg += "from registry key '%s'" % self._regKey
|
2009-04-22 15:48:07 +04:00
|
|
|
logger.debug(debugMsg)
|
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
self.execCmd(cmd=self._batPathRemote)
|
|
|
|
self.delRemoteFile(self._batPathRemote)
|