2019-05-08 13:47:52 +03:00
|
|
|
#!/usr/bin/env python
|
2009-04-22 15:48:07 +04:00
|
|
|
|
|
|
|
"""
|
2020-01-01 15:25:15 +03:00
|
|
|
Copyright (c) 2006-2020 sqlmap developers (http://sqlmap.org/)
|
2017-10-11 15:50:46 +03:00
|
|
|
See the file 'LICENSE' for copying permission
|
2009-04-22 15:48:07 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
2020-08-10 22:27:38 +03:00
|
|
|
from lib.core.common import openFile
|
2009-04-22 15:48:07 +04:00
|
|
|
from lib.core.common import randomStr
|
|
|
|
from lib.core.data import conf
|
|
|
|
from lib.core.data import logger
|
2014-04-06 20:19:54 +04:00
|
|
|
from lib.core.enums import REGISTRY_OPERATION
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2019-05-29 17:42:04 +03:00
|
|
|
class Registry(object):
|
2009-04-22 15:48:07 +04:00
|
|
|
"""
|
|
|
|
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 = (
|
2018-03-13 15:45:42 +03:00
|
|
|
"@ECHO OFF\r\n",
|
|
|
|
readParse,
|
|
|
|
)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
self._batAdd = (
|
2018-03-13 15:45:42 +03:00
|
|
|
"@ECHO OFF\r\n",
|
|
|
|
"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 = (
|
2018-03-13 15:45:42 +03:00
|
|
|
"@ECHO OFF\r\n",
|
|
|
|
"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):
|
2020-08-10 22:27:38 +03:00
|
|
|
self._batPathFp = openFile(self._batPathLocal, "w")
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2014-04-06 20:19:54 +04:00
|
|
|
if self._operation == REGISTRY_OPERATION.READ:
|
2012-12-06 17:14:19 +04:00
|
|
|
lines = self._batRead
|
2014-04-06 20:19:54 +04:00
|
|
|
elif self._operation == REGISTRY_OPERATION.ADD:
|
2012-12-06 17:14:19 +04:00
|
|
|
lines = self._batAdd
|
2014-04-06 20:19:54 +04:00
|
|
|
elif self._operation == REGISTRY_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()
|
2013-02-14 17:18:33 +04:00
|
|
|
self.writeFile(self._batPathLocal, self._batPathRemote, "text", forceCheck=True)
|
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):
|
2014-04-06 20:19:54 +04:00
|
|
|
self._operation = REGISTRY_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):
|
2014-04-06 20:19:54 +04:00
|
|
|
self._operation = REGISTRY_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):
|
2014-04-06 20:19:54 +04:00
|
|
|
self._operation = REGISTRY_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)
|