sqlmap/lib/takeover/registry.py

119 lines
3.7 KiB
Python
Raw Normal View History

2019-05-08 13:47:52 +03:00
#!/usr/bin/env python
"""
2024-01-04 01:11:52 +03:00
Copyright (c) 2006-2024 sqlmap developers (https://sqlmap.org/)
2017-10-11 15:50:46 +03:00
See the file 'LICENSE' for copying permission
"""
import os
2020-08-10 22:27:38 +03:00
from lib.core.common import openFile
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
2019-05-29 17:42:04 +03:00
class Registry(object):
"""
This class defines methods to read and write Windows registry keys
"""
def _initVars(self, regKey, regValue, regType=None, regData=None, parse=False):
self._regKey = regKey
self._regValue = regValue
self._regType = regType
self._regData = regData
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)
if parse:
readParse = "FOR /F \"tokens=*\" %%A IN ('REG QUERY \"" + self._regKey + "\" /v \"" + self._regValue + "\"') DO SET value=%%A\r\nECHO %value%\r\n"
else:
readParse = "REG QUERY \"" + self._regKey + "\" /v \"" + self._regValue + "\""
self._batRead = (
"@ECHO OFF\r\n",
readParse,
)
self._batAdd = (
"@ECHO OFF\r\n",
"REG ADD \"%s\" /v \"%s\" /t %s /d %s /f" % (self._regKey, self._regValue, self._regType, self._regData),
)
self._batDel = (
"@ECHO OFF\r\n",
"REG DELETE \"%s\" /v \"%s\" /f" % (self._regKey, self._regValue),
)
def _createLocalBatchFile(self):
2020-08-10 22:27:38 +03:00
self._batPathFp = openFile(self._batPathLocal, "w")
2014-04-06 20:19:54 +04:00
if self._operation == REGISTRY_OPERATION.READ:
lines = self._batRead
2014-04-06 20:19:54 +04:00
elif self._operation == REGISTRY_OPERATION.ADD:
lines = self._batAdd
2014-04-06 20:19:54 +04:00
elif self._operation == REGISTRY_OPERATION.DELETE:
lines = self._batDel
for line in lines:
self._batPathFp.write(line)
self._batPathFp.close()
def _createRemoteBatchFile(self):
logger.debug("creating batch file '%s'" % self._batPathRemote)
self._createLocalBatchFile()
self.writeFile(self._batPathLocal, self._batPathRemote, "text", forceCheck=True)
os.unlink(self._batPathLocal)
def readRegKey(self, regKey, regValue, parse=False):
2014-04-06 20:19:54 +04:00
self._operation = REGISTRY_OPERATION.READ
2012-12-21 14:10:05 +04:00
Registry._initVars(self, regKey, regValue, parse=parse)
self._createRemoteBatchFile()
logger.debug("reading registry key '%s' value '%s'" % (regKey, regValue))
data = self.evalCmd(self._batPathRemote)
2010-03-13 02:12:06 +03:00
if data and not parse:
pattern = ' '
index = data.find(pattern)
if index != -1:
data = data[index + len(pattern):]
self.delRemoteFile(self._batPathRemote)
return data
def addRegKey(self, regKey, regValue, regType, regData):
2014-04-06 20:19:54 +04:00
self._operation = REGISTRY_OPERATION.ADD
2012-12-21 14:10:05 +04:00
Registry._initVars(self, regKey, regValue, regType, regData)
self._createRemoteBatchFile()
debugMsg = "adding registry key value '%s' " % self._regValue
debugMsg += "to registry key '%s'" % self._regKey
logger.debug(debugMsg)
self.execCmd(cmd=self._batPathRemote)
self.delRemoteFile(self._batPathRemote)
def delRegKey(self, regKey, regValue):
2014-04-06 20:19:54 +04:00
self._operation = REGISTRY_OPERATION.DELETE
2012-12-21 14:10:05 +04:00
Registry._initVars(self, regKey, regValue)
self._createRemoteBatchFile()
debugMsg = "deleting registry key value '%s' " % self._regValue
debugMsg += "from registry key '%s'" % self._regKey
logger.debug(debugMsg)
self.execCmd(cmd=self._batPathRemote)
self.delRemoteFile(self._batPathRemote)