forked from rapid7/metasploit-baseline-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_msf_host.py
107 lines (77 loc) · 3.02 KB
/
build_msf_host.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
import getopt
import glob
import json
import sys
import os
import packer
from tqdm import tqdm
from lib import packerMod
from lib import serverHelper
def build_base(packer_var_file, replace_existing, vmServer=None, prependString = ""):
TEMP_DIR="tmp"
vm_name = packer_var_file.strip("_packer.json")
temp_path = os.path.join("..", "..", TEMP_DIR, prependString + vm_name)
if not os.path.exists(temp_path):
os.makedirs(temp_path)
output = vm_name + "_vmware.box"
only = ['vmware-iso']
with open(os.path.join("..", "..", packer_var_file)) as packer_var_source:
packer_vars = json.load(packer_var_source)
packer_vars.update({
"vm_name": prependString + vm_name,
"output": os.path.join("..", "..", "box", output)
})
packerfile = "ubuntu.json"
packer_obj = packerMod(packerfile)
packer_obj.update_config(packer_vars)
if vmServer.get_esxi() is not None:
packer_vars.update(vmServer.get_config())
packer_obj.use_esxi_config()
packerfile = os.path.join(temp_path, vm_name + ".json")
packer_obj.save_config(packerfile)
out_file = os.path.join(temp_path, "output.log")
err_file = os.path.join(temp_path, "error.log")
p = packer.Packer(str(packerfile), only=only, vars=packer_vars,
out_iter=out_file, err_iter=err_file)
vm = vmServer.get_vm(prependString + vm_name)
if vm is not None:
if replace_existing:
vmServer.remove_vm(prependString + vm_name)
else:
return p # just return without exec since ret value is not checked anyways
p.build(parallel=True, debug=False, force=False)
if vmServer.get_esxi() is not None:
vm = vmServer.get_vm(prependString + vm_name)
if vm is not None:
vm.takeSnapshot(snapshotName='baseline')
return p
def main(argv):
prependString = ""
replace_vms = False
esxi_file = "esxi_config.json"
try:
opts, args = getopt.getopt(argv[1:], "c:hp:r", ["prependString="])
except getopt.GetoptError:
print argv[0] + ' -n <numProcessors>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print argv[0] + " [options]"
print '-c <file>, --esxiConfig=<file> use alternate hypervisor config file'
print '-p <string>, --prependString=<file> prepend string to the beginning of VM names'
print '-r, --replace replace existing msf_host'
sys.exit()
elif opt in ("-c", "--esxiConfig"):
esxi_file = arg
elif opt in ("-p", "--prependString"):
prependString = arg
elif opt in ("-r", "--replace"):
replace_vms = True
targets = glob.glob('msf_host_packer.json')
vm_server = serverHelper(esxi_file)
os.chdir("boxcutter/ubuntu")
for target in tqdm(targets):
build_base(target, replace_existing=replace_vms, vmServer=vm_server, prependString=prependString)
return True
if __name__ == "__main__":
main(sys.argv)