Skip to content

Commit abd0e3b

Browse files
committed
add helpers preparing vm snapshots in basic states
prepVMs.py will `enable_uac` and `disable_smb1` these are some basic prep options hopefully useful for metasploit payloads.
1 parent 0db64d6 commit abd0e3b

File tree

4 files changed

+137
-2
lines changed

4 files changed

+137
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/answer_files/windows/Autounattend.xml
66
/esxi_config.json
77
/*.log
8+
/helpers/*.log

answer_files/windows/Autounattend_x64.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@
321321
<HelpCustomized>false</HelpCustomized>
322322
</OEMInformation>
323323
<ComputerName>WindowsX64</ComputerName>
324-
<TimeZone>Pacific Standard Time</TimeZone>
324+
<TimeZone>Central Standard Time</TimeZone>
325325
<RegisteredOwner/>
326326
</component>
327327
<component xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-ServerManager-SvrMgrNc" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">

answer_files/windows/Autounattend_x86.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@
316316
</OEMInformation>
317317
<!-- Rename computer here. -->
318318
<ComputerName>WindowsX86</ComputerName>
319-
<TimeZone>Pacific Standard Time</TimeZone>
319+
<TimeZone>Central Standard Time</TimeZone>
320320
<RegisteredOwner/>
321321
</component>
322322
<component xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-ServerManager-SvrMgrNc" processorArchitecture="x86" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">

helpers/prepVMs.py

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import argparse
2+
import json
3+
import multiprocessing
4+
import os
5+
import signal
6+
import time
7+
from tqdm import tqdm
8+
import vm_automation
9+
10+
WINDOWS_REQUIRED = "Win"
11+
UAC_ENABLE_COMMAND = ['cmd.exe',
12+
'/k',
13+
'%SystemRoot%\System32\\reg.exe',
14+
'ADD',
15+
'HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System',
16+
'/v',
17+
'EnableLUA',
18+
'/t',
19+
'REG_DWORD',
20+
'/d',
21+
'1',
22+
'/f']
23+
DISABLE_SMB1_COMMAND = ['cmd.exe',
24+
'/k',
25+
'%SystemRoot%\System32\\reg.exe',
26+
'ADD',
27+
'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters',
28+
'/v',
29+
'SMB1',
30+
'/t',
31+
'REG_DWORD',
32+
'/d',
33+
'0',
34+
'/f']
35+
36+
37+
38+
def get_vm_server(config_file):
39+
if os.path.isfile(config_file):
40+
with open(config_file) as config_file_handle:
41+
config_map = json.load(config_file_handle)
42+
if config_map['HYPERVISOR_TYPE'].lower() == "esxi":
43+
vmServer = vm_automation.esxiServer.createFromConfig(config_map, 'esxi_autoamtion.log')
44+
vmServer.connect()
45+
if config_map['HYPERVISOR_TYPE'].lower() == "workstation":
46+
vmServer = vm_automation.workstationServer(config_map, 'workstation_automation.log')
47+
return vmServer
48+
return None
49+
50+
51+
def enable_uac(vm_config, vm_name, command):
52+
schedule_delay = 30
53+
vm_server = get_vm_server(config_file=vm_config)
54+
vm_server.enumerateVms()
55+
for vm in vm_server.vmList:
56+
if vm_name == vm.vmName:
57+
vm.powerOn()
58+
vm_ready = False
59+
while vm_ready is False:
60+
vm_ready = vm_server.waitForVmsToBoot([vm])
61+
vm.setUsername('vagrant')
62+
vm.setPassword('vagrant')
63+
vm.runCmdOnGuest(command)
64+
time.sleep(schedule_delay)
65+
vm.vmObject.ShutdownGuest()
66+
time.sleep(10)
67+
vm.powerOff()
68+
69+
70+
def main():
71+
parser = argparse.ArgumentParser()
72+
parser.add_argument("-k", "--keyword", help="VM search parameter")
73+
parser.add_argument("-a", "--action", help="action [enable_uac|disable_smb1]")
74+
parser.add_argument("hypervisorConfig", help="json hypervisor config")
75+
76+
args = parser.parse_args()
77+
78+
validActions = ['enable_uac', 'disable_smb1']
79+
80+
prefix = args.keyword
81+
82+
if args.action.lower() not in validActions:
83+
print('INVALID ACTION')
84+
if args.action.lower() == 'enable_uac':
85+
command = UAC_ENABLE_COMMAND
86+
elif args.action.lower() == 'disable_smb1':
87+
command = UAC_ENABLE_COMMAND
88+
89+
vm_server = get_vm_server(config_file=args.hypervisorConfig)
90+
if vm_server is None:
91+
print ("Failed to connect to VM environment")
92+
exit(1)
93+
94+
vm_list = []
95+
vm_server.enumerateVms()
96+
for vm in vm_server.vmList:
97+
if prefix in vm.vmName and WINDOWS_REQUIRED in vm.vmName:
98+
vm_list.append(vm.vmName)
99+
100+
original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
101+
102+
pool = None
103+
try:
104+
pool = multiprocessing.Pool(3)
105+
106+
signal.signal(signal.SIGINT, original_sigint_handler)
107+
108+
results = []
109+
for vm_name in vm_list:
110+
pool.apply_async(enable_uac, [args.hypervisorConfig, vm_name, command], callback=results.append)
111+
112+
with tqdm(total=len(vm_list)) as progress:
113+
current_len = 0
114+
while len(results) < len(vm_list):
115+
if (len(results) > current_len):
116+
progress.update(len(results) - current_len)
117+
current_len = len(results)
118+
time.sleep(5)
119+
progress.update(len(results))
120+
121+
except KeyboardInterrupt:
122+
print("User cancel received, terminating all task")
123+
if pool is not None:
124+
pool.terminate()
125+
126+
print("Processing complete " + str(len(vm_list)) + " systems updated")
127+
if pool is not None:
128+
pool.close()
129+
pool.join()
130+
131+
132+
133+
if __name__ == "__main__":
134+
main()

0 commit comments

Comments
 (0)