-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_detfile.py
119 lines (94 loc) · 2.92 KB
/
generate_detfile.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
import sys
import json
SKEL = """'''
This is an auto generated detfile build from a topology file: {filename}
{info}
https://github.com/RITRedteam/Topology-Generator
https://github.com/micahjmartin/detcord
'''
# pylint: disable=import-error, invalid-name, unused-wildcard-import
import os
from detcord import action, display
if os.path.exists('actions'):
from actions import *
env = dict()
env['user'] = 'root'
env['pass'] = 'changeme'
env['hosts'] = [] # DYNAMICALLY GENERATED IN build_hosts()
env['silent'] = False # https://github.com/micahjmartin/detcord/wiki/Detfile-Environment#silent
env['threading'] = False
{hosts}
{teams}
@action
def test(host):
'''Print the hostname of the box'''
display(host.run("command hostname"))
def on_detcord_begin(detfile="", hosts=[], actions=[], threading=False):
pass
def on_detcord_action(host="", action="", return_value=None):
pass
def on_detcord_action_fail(host="", action="", exception=None):
pass
def on_detcord_end(detfile=""):
pass
def build_hosts():
'''Build the hosts for the ENV dynamically
'''
global env
env['hosts'] = []
for team in TEAMS:
for ip in HOSTS:
new = ip.replace("x", str(team))
env['hosts'].append(new)
build_hosts()
"""
def get_hosts(data):
hosts = []
for network in data['networks']:
netip = network['ip']
for nethost in network['hosts']:
host = nethost.copy()
if nethost['ip'].lower() == "dhcp":
host['ip'] = "dhcp"
else:
host['ip'] = ".".join((netip, nethost['ip']))
hosts += [host]
return hosts
def build_detfile(filename, data):
# Build the hosts section
hosts = "HOSTS = [\n"
for host in get_hosts(data):
if host['ip'].lower() == 'dhcp':
continue
# Skip windows hosts
if 'win' in host.get('os', '').lower():
continue
hosts += "\t'{}',".format(host['ip'])
comment = []
if host.get('name', False): comment += [host['name']]
if host.get('os', False): comment += [host['os']]
comment = " - ".join(comment)
if comment:
hosts += " # " + comment
hosts += '\n'
hosts += "]"
# Build the teams
teams = 'TEAMS = ' + str(data['teams'])
# Information about the competition
info = []
if data.get('name', False): info += [data['name']]
if data.get('date', False): info += [data['date']]
info = " - ".join(info)
return SKEL.format(filename=filename, info=info, teams=teams, hosts=hosts)
def main():
try:
config = sys.argv[1]
with open(config) as fil:
config = json.load(fil)
except (IndexError) as E:
print("USAGE: {} <topology>".format(sys.argv[0]))
quit(1)
with open("detfile.py", 'w') as fil:
fil.write(build_detfile(sys.argv[1], config))
if __name__ == "__main__":
main()