def main(): """ Enumerate all installed products, go through all components and check if client refences point to valid products. Remove references to non-existing products if the user allowed it. """ hkey_components = OpenKey(HKEY_LOCAL_MACHINE, COMPONENTS_KEY, 0, KEY_ALL_ACCESS) missing_products = get_missing_products(hkey_components) print('Missing products refer the following components:') for product_guid in sorted(missing_products.keys()): if product_guid[1:] == '0' * 31: continue print('Product', transpose_guid(product_guid) + ':') for component_guid, component_file in missing_products[product_guid]: print(' ' + transpose_guid(component_guid), '=', component_file) print('Remove all references to product', transpose_guid(product_guid) + '? [y/n]') if strtobool(raw_input().lower()): for component_guid, component_file in missing_products[ product_guid]: hkey_component = OpenKey(hkey_components, component_guid, 0, KEY_ALL_ACCESS) print( 'Removing reference in ' + transpose_guid(component_guid), '=', component_file) DeleteValue(hkey_component, product_guid) CloseKey(hkey_component) else: print('Cancelled removal of product', transpose_guid(product_guid)) CloseKey(hkey_components)
def change_key(cd_key): cd_key = cd_key.upper() key_pattern = re.compile(("[A-Z0-9]{5}-" "[A-Z0-9]{5}-" "[A-Z0-9]{5}-" "[A-Z0-9]{5}-" "[A-Z0-9]{5}")) if not key_pattern.match(cd_key): print( "The CD Key seems invalid.\nPlease input a key in the form " "XXXXX-XXXXX-XXXXX-XXXXX-XXXXX") return False cd_key = cd_key.replace("-", "") reg_key = OpenKey(HKEY_LOCAL_MACHINE, (r'SOFTWARE\Microsoft\Windows NT' '\CurrentVersion\WPAEvents'), 0, KEY_ALL_ACCESS) try: _ = QueryValueEx(reg_key, 'OOBETimer')[0] # noqa DeleteValue(reg_key, 'OOBETimer') except WindowsError: pass win = wmi.WMI() ret_code = None for instance in win.win32_WindowsProductActivation(): ret_code = instance.SetProductKey(cd_key)[0] return not ret_code
def test_delete(self): from _winreg import OpenKey, KEY_ALL_ACCESS, DeleteValue, DeleteKey key = OpenKey(self.root_key, self.test_key_name, 0, KEY_ALL_ACCESS) sub_key = OpenKey(key, "sub_key", 0, KEY_ALL_ACCESS) for name, value, type in self.test_data: DeleteValue(sub_key, name) DeleteKey(key, "sub_key")
def remove_registry_startup(name='Updater'): try: key = OpenKey(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS) DeleteValue(key, name) return True except: return False finally: CloseKey(key)
ip_address, ip_mask, ip_gateway, "1", ] subprocess.Popen(args).wait() else: sock.close() h = CreateKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS) if s.vmmode == 'normal': # In normal mode we remove the entry in Run from the registry. DeleteValue(h, 'Agent') else: # In bird mode we modify it so that the agent is aware that no new # IP address has to be assigned and goes straight to listening for # the host to connect. settings = dict(vmmode=s.vmmode) value = 'C:\\Python27\\Pythonw.exe "%s" %s' % ( os.path.abspath(__file__), json.dumps(settings).encode('base64')) SetValueEx(h, 'Agent', 0, REG_SZ, value) h.Close() try: if not BIND_IP: BIND_IP = socket.gethostbyname(socket.gethostname())
def delete_key(name, user=True): key = OpenKey(HKEY_CURRENT_USER, 'Environment', 0, KEY_ALL_ACCESS) DeleteValue(key, name) CloseKey(key) SendMessage(win32con.HWND_BROADCAST, win32con.WM_SETTINGCHANGE, 0, 'Environment')