简体   繁体   中英

using winreg to write long binary data

I have a long binary data which I am able to successfully write to registry using .reg file but now I am trying to use python to achieve the same result. But I couldn't make it work.
Here is.reg file script

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,69,00,00,00,00,00,21,e0,00,00,6c,e0,\
  00,00,6d,e0,00,00,11,e0,00,00,6b,e0,00,00,40,e0,00,00,42,e0,00,00,3b,e0,00,\
  00,3e,e0,00,00,3c,e0,00,00,3f,e0,00,00,58,e0,00,00,07,e0,00,00,41,e0,00,00,\
  57,e0,00,00,43,e0,00,00,23,e0,00,00,3d,e0,00,00,08,e0,00,00,3b,00,00,00,44,\
  00,00,00,57,00,00,00,58,00,00,00,64,00,00,00,65,00,00,00,66,00,00,00,67,00,\
  00,00,68,00,00,00,69,00,00,00,6a,00,00,00,3c,00,00,00,6b,00,00,00,6c,00,00,\
  00,6d,00,00,00,6e,00,00,00,6f,00,00,00,3d,00,00,00,3e,00,00,00,3f,00,00,00,\
  40,00,00,00,41,00,00,00,42,00,00,00,43,00,00,00,13,e0,00,00,14,e0,00,00,12,\
  e0,00,00,20,e0,00,00,19,e0,00,00,22,e0,00,00,10,e0,00,00,24,e0,00,00,2e,e0,\
  00,00,30,e0,00,00,7d,00,00,00,56,e0,00,00,5d,e0,00,00,46,e0,00,00,f2,e0,00,\
  00,f1,e0,00,00,47,e0,00,00,52,e0,00,00,56,00,00,00,38,00,00,00,1d,00,00,00,\
  2a,00,00,00,5b,e0,00,00,51,e0,00,00,49,e0,00,00,37,e0,00,00,38,e0,00,00,38,\
  e0,00,00,1d,e0,00,00,36,00,00,00,5c,e0,00,00,46,00,00,00,5f,e0,00,00,63,e0,\
  00,00,62,e0,00,00,64,e0,00,00,6e,e0,00,00,6f,e0,00,00,70,e0,00,00,71,e0,00,\
  00,72,e0,00,00,73,e0,00,00,74,e0,00,00,75,e0,00,00,76,e0,00,00,77,e0,00,00,\
  78,e0,00,00,79,e0,00,00,7a,e0,00,00,7b,e0,00,00,7c,e0,00,00,7d,e0,00,00,7e,\
  e0,00,00,7f,e0,00,00,6a,e0,00,00,66,e0,00,00,69,e0,00,00,32,e0,00,00,67,e0,\
  00,00,65,e0,00,00,68,e0,00,00,00,00

and here is python code that I am trying;

import struct
import winreg
try:
    Registrykey= winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SYSTEM\\CurrentControlSet\\Control\\Keyboard Layout", 0, winreg.KEY_ALL_ACCESS)
    x = struct.pack('c', "000000000000000069000000000021e000006ce000006de0000011e000006be0000040e0000042e000003be000003ee000003ce000003fe0000058e0000007e0000041e0000057e0000043e0000023e000003de0000008e000003b0000004400000057000000580000006400000065000000660000006700000068000000690000006a0000003c0000006b0000006c0000006d0000006e0000006f0000003d0000003e0000003f0000004000000041000000420000004300000013e0000014e0000012e0000020e0000019e0000022e0000010e0000024e000002ee0000030e000007d00000056e000005de0000046e00000f2e00000f1e0000047e0000052e0000056000000380000001d0000002a0000005be0000051e0000049e0000037e0000038e0000038e000001de00000360000005ce00000460000005fe0000063e0000062e0000064e000006ee000006fe0000070e0000071e0000072e0000073e0000074e0000075e0000076e0000077e0000078e0000079e000007ae000007be000007ce000007de000007ee000007fe000006ae0000066e0000069e0000032e0000067e0000065e0000068e000000000")
    winreg.SetValueEx(Registrykey,"Scancode Map", 1, winreg.REG_BINARY, x)
    winreg.CloseKey(Registrykey)
except Exception as e:
    print (e)

I have tried storing data in various format and also have tried using different format string for struct but just getting errors. Could someone explain what I am not understanding here. thanks for taking your time...

Here's a solution that worked for me. I am using Python 3.10.5 on Windows 10 64-bit.

Convert the data payload to bytes from your reg file, such as b'\x00\x00\x00...and so on' , using bytes() built-in function ( Example ).

Once you have that, just use winreg module to write the data to the registry.

Remember to elevate your Python script since you are writing to HKEY_LOCAL_MACHINE . Otherwise you'll receive an access denied error:

import winreg

# You can convert registry payload to a list of integers first using list() function,
# then feed the list into bytes() function
# See https://stackoverflow.com/a/71965319/11653053 for example
bytesData = bytes(reg_payload_sequence)

# Use context manager to automatically close HKEY handle
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Keyboard Layout", access=winreg.KEY_ALL_ACCESS) as hkey:
    # bytesData variable contains your registry payload as a bytes object
    winreg.SetValueEx(hkey, "Scancode Map", 0, winreg.REG_BINARY, bytesData)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM