Last active
March 11, 2024 18:46
-
-
Save FlyTechVideos/2a9b260f0cd440fbe316241ffc8e48ac to your computer and use it in GitHub Desktop.
Sets all DWORD/QWORD registry values for which the user has permission to edit to 0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import winreg | |
VALUE_TO_WRITE = 0x69 | |
root_dict = { | |
'HKEY_CLASSES_ROOT': winreg.HKEY_CLASSES_ROOT, | |
'HKEY_CURRENT_USER': winreg.HKEY_CURRENT_USER, | |
'HKEY_LOCAL_MACHINE': winreg.HKEY_LOCAL_MACHINE, | |
'HKEY_USERS': winreg.HKEY_USERS, | |
'HKEY_CURRENT_CONFIG': winreg.HKEY_CURRENT_CONFIG | |
} | |
types_to_overwrite = [ | |
winreg.REG_DWORD, | |
winreg.REG_QWORD | |
] | |
def check_values(root, key, opened_key): | |
values_to_overwrite = [] | |
try: | |
i = 0 | |
while True: | |
value = winreg.EnumValue(opened_key, i) | |
if value[2] in types_to_overwrite: | |
values_to_overwrite.append((value[0], value[2])) | |
i += 1 | |
except: | |
pass | |
if len(values_to_overwrite) > 0: | |
try: | |
opened_write_key = winreg.OpenKey(root_dict[root], key, access=winreg.KEY_SET_VALUE) | |
for value_pair in values_to_overwrite: | |
winreg.SetValueEx(opened_write_key, value_pair[0], 0, int(value_pair[1]), VALUE_TO_WRITE) | |
except Exception as e: | |
print(f'PERMISSION DENIED: {e}') | |
pass | |
def traverse(root, key): | |
should_check_values = True | |
try: | |
opened_key = winreg.OpenKey(root_dict[root], key) | |
check_values(root, key, opened_key) | |
except Exception as e: | |
if 'WinError 5' in str(e): | |
print(f'{e}: Error 5 [no read permission]') | |
should_check_values = False # no need if i can't read them anyway | |
if should_check_values: | |
check_values(root, key, opened_key) | |
if key != '': | |
key += '\\' | |
try: | |
i = 0 | |
while True: | |
traverse(root, key + winreg.EnumKey(opened_key, i)) | |
i += 1 | |
except: | |
pass | |
def main(): | |
for root in root_dict.keys(): | |
traverse(root, "") | |
if __name__ == '__main__': | |
print('Safeguard against accidental execution. DO NOT EXECUTE THIS ON YOUR MAIN SYSTEM!') | |
exit(0) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No...... you can't restore it easily because it affected the HKCU, but HKLM is easy... But you can ensure it's OK if the script do not show any consoles yet (IDLE is counted as a console window, but more formatting). FlyTech have a video that delete all root key once.