-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathvulnlab.py
More file actions
93 lines (70 loc) · 2.28 KB
/
Copy pathvulnlab.py
File metadata and controls
93 lines (70 loc) · 2.28 KB
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
#!/usr/bin/env python3
# vulnlab.py
# https://github.com/timkent/vulnlab
# depends on: python3-flask python3-pyvmomi
from flask import Flask
from pyVim import connect
from pyVmomi import vim
# details for your ESX host:
host = "x.x.x.x"
user = "vulnlab"
password = "sethere"
port = 443
# name of (this) machine to be excluded
control_vm = "vulnlab"
def main():
# ignore invalid cert on ESX box
import ssl
_create_unverified_https_context = ssl._create_unverified_context
ssl._create_default_https_context = _create_unverified_https_context
vm_list = get_vm_list()
app = Flask(__name__)
@app.route("/")
def index():
result = '''<!DOCTYPE html>
<html>
<head>
<title>VulnLab</title>
</head>
<body>
<h1>Reset</h1>
'''
for name, uuid in sorted(vm_list.items()):
result += ' <a href="/reset/' + name + '">' + name + '</a><br>\n'
result += ''' </body>
</html>
'''
return result, 200
@app.route("/reset/<string:vm_name>")
def reset(vm_name):
reset_vm(vm_list, vm_name)
return 'OK\n', 200
# start the web server
app.run(host="0.0.0.0", port=5000)
def get_vm_list():
# make connection
service_instance = connect.SmartConnect(host=host, user=user, pwd=password, port=int(port))
# create list of machines
content = service_instance.RetrieveContent()
containerView = content.viewManager.CreateContainerView(content.rootFolder, [vim.VirtualMachine], True)
children = containerView.view
vm_list = {}
for child in children:
summary = child.summary
if not summary.config.name == control_vm:
vm_list[summary.config.name] = summary.config.uuid
connect.Disconnect(service_instance)
return vm_list
def reset_vm(vm_list, target_name):
# make sure target is in list
target_uuid = vm_list[target_name]
if target_uuid:
# make connection
service_instance = connect.SmartConnect(host=host, user=user, pwd=password, port=int(port))
target_vm = service_instance.content.searchIndex.FindByUuid(None, target_uuid, True)
if target_vm:
# print(target_vm.runtime.powerState)
target_vm.ResetVM_Task()
connect.Disconnect(service_instance)
if __name__ == "__main__":
main()