-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui1_support.py
210 lines (185 loc) · 10.3 KB
/
gui1_support.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Support module generated by PAGE version 4.18
# in conjunction with Tcl version 8.6
# Nov 11, 2018 03:57:06 PM CET platform: Windows NT
import sys, gui1, subprocess, threading, time, psutil, base64, re
MEMORY_LIMIT_PERCENT=60
CPU_LIMIT_PERCENT=30
thread_limit=10
block=False
run_block=False
first_run=True
No_Cred=False
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
try:
import ttk
py3 = False
except ImportError:
import tkinter.ttk as ttk
py3 = True
def set_Tk_var():
global wrn
wrn = tk.StringVar()
global combobox
combobox = 'tk.StringVar()'
def main():
global CREATE_NO_WINDOW,block,hosts_parsed,run_block,querry,user,passw,CHECK_PER,sqlcmd_mode,first_run,selection,No_Cred,opts
CREATE_NO_WINDOW = 0x08000000
CHECK_PER=10
Checked=False
if not run_block:
selection=w.TCombobox1.get()
if selection=="sqlcmd":
querry=w.Scrolledtext2.get(0.0,"end").replace('\n', ' ')
elif selection=="Invoke-Command" or selection=="Invoke-WmiMethod":
querry=w.Scrolledtext2.get(0.0,"end").replace('\n', ';')
elif selection=="PSEXEC":
querry=w.Scrolledtext2.get(0.0,"end").replace('\n', ';')
querry=w.Scrolledtext2.get(0.0,"end").replace('powershell', 'powershell -inputformat none')
querry=querry[:-1]
user=w.Entry1.get().replace(' ', '').replace('\n', '')
passw=w.Entry2.get().replace(' ', '').replace('\n', '').encode('base64')
hosts=w.Scrolledtext1.get(0.0,"end").encode("ascii")
opts=w.Entry3.get()
sqlcmd_mode=False
if len(hosts)>0 and len(querry)>0 and "-Querry-" not in querry and "-Hosts-" not in hosts:
if selection=="sqlcmd" and len(passw)>0 and len(user)>0 and "Username" not in user and "Username" not in passw.decode('base64'):
Checked=True
elif selection=="Invoke-Command" or selection=="Invoke-WmiMethod" or selection=="PSEXEC":
Checked=True
No_Cred=False
if len(passw)==0 or len(user)==0 or "Username" in user or "Username" in passw.decode('base64'):
No_Cred=True
if Checked:
first_run=False
run_block=True
w.TProgressbar1['value']=0
w.Scrolledtext3.delete(1.0,"end")
w.Button1.configure(text="Running")
hosts_parsed=hosts.splitlines()
if selection=="sqlcmd" and re.search("xp_cmdshell",querry,re.IGNORECASE):
querry=querry.replace('xp_cmdshell', 'exec xp_cmdshell')
querry=querry.replace('"', '""')
sqlcmd_mode=True
threading.Thread(target=check_system).start()
threading.Thread(target=launcher).start()
def launcher():
global hosts_parsed,iterations,CHECK_PER,block,thread_limit
iterations=0
for line in hosts_parsed:
if len(line)>0:
if block or threading.activeCount()>thread_limit:
while threading.activeCount()>6:
time.sleep(1)
thread = threading.Thread(target=procedure,args=(line.splitlines()))
thread.start()
else:
pass
if iterations%20==0 and iterations!=0 and len(hosts_parsed)!=(iterations+1):
time.sleep(0.2)
iterations+=1
w.TProgressbar1['value']=(float(float(iterations)/float(len(hosts_parsed))))*100
def procedure(dest):
global iterations,hosts_parsed,run_block,querry,user,passw,sqlcmd_mode,selection,No_Cred,opts
try:
if selection=="sqlcmd":
if sqlcmd_mode:
if "Opts.(Default)" in opts or len(opts)<1:
opts="-l 10 -t 30"
w.Entry3.delete(first=0,last=100)
w.Entry3.insert('insert',"Opts.(Default)")
out = subprocess.check_output("chcp 65001>NUL && @echo ON && sqlcmd -S "+dest+" -U "+user+" -P "+passw.decode('base64')+" -Q "+'"'+querry+'"'+" -s "+'"'+'|'+'"'+
" "+opts+" && exit",shell=True, bufsize=-1 , stderr=subprocess.STDOUT, stdin=subprocess.PIPE, close_fds=False, creationflags=CREATE_NO_WINDOW).decode("utf-8")
else:
if "Opts.(Default)" in opts or len(opts)<1:
opts="-y 32 -Y 32 -l 10 -t 60"
w.Entry3.delete(first=0,last=100)
w.Entry3.insert('insert',"Opts.(Default)")
out = subprocess.check_output("chcp 65001>NUL && @echo ON && sqlcmd -S "+dest+" -U "+user+" -P "+passw.decode('base64')+" -Q "+'"'+"SET NOCOUNT ON;"+querry+'"'+" -s "+'"'+'|'+'"'+
" "+opts+" && exit",shell=True, bufsize=-1 ,stderr=subprocess.STDOUT, stdin=subprocess.PIPE, close_fds=False, creationflags=CREATE_NO_WINDOW).decode("utf-8")
elif selection=="Invoke-Command":
if "Opts.(Default)" in opts or len(opts)<1:
opts=""
w.Entry3.delete(first=0,last=100)
w.Entry3.insert('insert',"Opts.(Default)")
if No_Cred:
out = subprocess.check_output("chcp 65001>NUL && powershell -ExecutionPolicy RemoteSigned -Command "+'"'+
"Invoke-Command -ComputerName "+dest+" -ScriptBlock {"+querry+"}"+" "+opts+
'"',shell=True, bufsize=-1 , stderr=subprocess.STDOUT, stdin=subprocess.PIPE, close_fds=False, creationflags=CREATE_NO_WINDOW).decode("utf-8")
else:
out = subprocess.check_output("chcp 65001>NUL && powershell -ExecutionPolicy RemoteSigned -Command "+'"'+"$Password = '"+passw.decode('base64')+
"';$pass = ConvertTo-SecureString -AsPlainText $Password -Force;$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList "+user+",$pass;"+
"Invoke-Command -ComputerName "+dest+" -Credential $Cred -ScriptBlock {"+querry+"}"+" "+opts+
'"',shell=True, bufsize=-1 , stderr=subprocess.STDOUT, stdin=subprocess.PIPE, close_fds=False, creationflags=CREATE_NO_WINDOW).decode("utf-8")
elif selection=="Invoke-WmiMethod":
if "Opts.(Default)" in opts or len(opts)<1:
opts=""
w.Entry3.delete(first=0,last=100)
w.Entry3.insert('insert',"Opts.(Default)")
if No_Cred:
out = subprocess.check_output("chcp 65001>NUL && powershell -ExecutionPolicy RemoteSigned -Command "+'"'+
r"$proc = Invoke-WmiMethod -class Win32_process -name Create -ArgumentList 'CMD.EXE /c "+querry+r" > C:\temp\result.txt && exit' -ComputerName '"+dest+
"'"+" "+opts+r";$Process = Get-Process -ID $proc.processid;$Process.WaitForExit();Get-Content \\"+dest+r"\C$\temp\result.txt"+
'"',shell=True, bufsize=-1 , stderr=subprocess.STDOUT, stdin=subprocess.PIPE, close_fds=False, creationflags=CREATE_NO_WINDOW).decode("utf-8")
else:
out = subprocess.check_output("chcp 65001>NUL && powershell -ExecutionPolicy RemoteSigned -Command "+'"'+"$Password ='"+passw.decode('base64')+
r"';$pass = ConvertTo-SecureString -AsPlainText $Password -Force;$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList "+user+",$pass;"+
r"$proc = Invoke-WmiMethod -class Win32_process -name Create -ArgumentList 'CMD.EXE /c "+querry+r" > C:\temp\result.txt' -ComputerName '"+dest+
"'"+" "+opts+r"' -Credential $Cred;$Process = Get-Process -ID $proc.processid;$Process.WaitForExit();Get-Content \\"+dest+r"\C$\temp\result.txt -Credential $Cred;"+
'"',shell=True, bufsize=-1 , stderr=subprocess.STDOUT, stdin=subprocess.PIPE, close_fds=False, creationflags=CREATE_NO_WINDOW).decode("utf-8")
elif selection=="PSEXEC":
if "Opts.(Default)" in opts or len(opts)<1:
opts="-s"
w.Entry3.delete(first=0,last=100)
w.Entry3.insert('insert',"Opts.(Default)")
if No_Cred:
out = subprocess.check_output(r"chcp 65001>NUL && .\PSEXEC /accepteula \\"+dest+" "+opts+" cmd /c "+'"'+querry+'"'+" && exit",shell=True, bufsize=-1 , stderr=subprocess.STDOUT, stdin=subprocess.PIPE, close_fds=False, creationflags=CREATE_NO_WINDOW).decode("utf-8")
else:
out = subprocess.check_output(r"chcp 65001>NUL && .\PSEXEC /accepteula \\"+dest+" -u "+user+" -p "+passw.decode('base64')+" "+opts+" cmd /c "+'"'+querry+'"'+" && exit",shell=True, bufsize=-1 , stderr=subprocess.STDOUT, stdin=subprocess.PIPE, close_fds=False, creationflags=CREATE_NO_WINDOW).decode("utf-8")
else:
destroy_window()
w.Scrolledtext3.insert("end",'\n'+"++++++++++++++++++++++++++++++++++++++++"+'\n'+"--------------------"+"Output from: "+dest+'\n'+out)
except subprocess.CalledProcessError as e:
w.Scrolledtext3.insert("end",'\n'+"++++++++++++++++++++++++++++++++++++++++"+'\n'+"--------------------"+"Output from: "+dest+ " (ERROR!)"+'\n'+e.output)
except Exception as e:
if hasattr(e, 'message'):
w.Scrolledtext3.insert("end",'\n'+"++++++++++++++++++++++++++++++++++++++++"+'\n'+"--------------------"+"Output from: "+dest+ " (ERROR!)"+'\n'+getattr(e, 'message', repr(e)))
else:
w.Scrolledtext3.insert("end",'\n'+"++++++++++++++++++++++++++++++++++++++++"+'\n'+"--------------------"+"Output from: "+dest+ " (ERROR!)"+'\n'+"Unknown error !!!")
if iterations>=len(hosts_parsed):
w.Button1.configure(text='''GO''')
w.Scrolledtext3.see("end")
w.TProgressbar1['value']=0
run_block=False
else:
run_block=True
def check_system():
global run_block,block,thread_limit
while run_block:
current_cpu=psutil.cpu_percent(interval=0.2, percpu=False)
if psutil.virtual_memory().percent<MEMORY_LIMIT_PERCENT and current_cpu<CPU_LIMIT_PERCENT:
block=False
thread_limit+=2
else:
block=True
thread_limit-=2
def init(top, gui, *args, **kwargs):
global w, top_level, root
w = gui
top_level = top
root = top
def destroy_window():
# Function which closes the window.
global top_level,run_block,hosts_parsed
hosts_parsed = []
run_block=False
top_level.destroy()
top_level = None
if __name__ == '__main__':
import gui1.py
gui1.py.vp_start_gui()