-
Notifications
You must be signed in to change notification settings - Fork 7
/
vcm.py
executable file
·307 lines (227 loc) · 9.38 KB
/
vcm.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python
from urllib.parse import urlparse
import click
import os
import time
import configparser
from subprocess import call
import re
GLOBAL_CONFIG_LOCATION = '~/.vcm'
DEFAULT_NMAP_SETTINGS = ["-sV", "-p-"]
global_config = None
# TODO: Stuff to automate later
# brew install testssl
# download openssl binary and store it in default location
# brew install nmap
# brew install nikto
class VcmGlobalConfig:
open_ssl_binary = '/usr/bin/openssl' # default - can be overridden in global config file.
def __init__(self):
pass
def read_global_vcm(self):
global global_config
print(f"Reading global config from {GLOBAL_CONFIG_LOCATION}")
read_config = configparser.RawConfigParser()
global_config_filename = os.path.expanduser(GLOBAL_CONFIG_LOCATION)
read_config.read(global_config_filename)
self.open_ssl_binary = read_config.get('GlobalSettings', 'openssl_binary')
def write_global_vcm(self):
print(f"Creating global config file with defaults in {GLOBAL_CONFIG_LOCATION}")
global global_config
global_config = configparser.RawConfigParser()
global_config.add_section('GlobalSettings')
global_config.set('GlobalSettings', 'openssl_binary', self.open_ssl_binary)
global_config_file = os.path.expanduser(GLOBAL_CONFIG_LOCATION)
with open(global_config_file, 'w') as configfile:
try:
global_config.write(configfile)
except configparser.Error as ex:
print(f"Error writing config file: {global_config_file} : {ex.message}")
return
class VcmProjectConfig:
local_folder = ''
remote_folder = ''
project_name = ''
targets = []
target_urls = []
# derived directories
artifacts_folder = ''
def __init__(self):
pass
def read_project_vcm(self):
project_config = configparser.RawConfigParser()
project_filename = os.path.join(os.getcwd(), '.vcm')
cf = project_config.read(project_filename)
if len(cf) == 0:
raise Exception(f"Unable to read config file: {project_filename}")
self.remote_folder = project_config.get('ProjectSettings', 'remote_path')
self.local_folder = project_config.get('ProjectSettings', 'local_path')
self.artifacts_folder = os.path.join(self.local_folder, 'artifacts')
url_targets = re.split(",", project_config.get('ProjectSettings', 'url_targets'))
for t in url_targets:
stripped_target = t.strip()
# The requirement is for targets to have a scheme - even if you're just
# using nmap
if len(stripped_target) > 0:
target_url = urlparse(stripped_target)
self.target_urls.append(target_url)
if not bool(target_url.scheme):
raise ValueError(
f"URL found without scheme: {stripped_target}. Please note, schemes are required for all URLs")
self.targets.append(t)
def write_project_vcm(self, project_name, local_folder, remote_folder, url_targets):
project_config = configparser.RawConfigParser()
project_config.add_section('ProjectSettings')
project_config.set('ProjectSettings', 'project_name', project_name)
project_config.set('ProjectSettings', 'local_path', os.path.join(local_folder, ''))
project_config.set('ProjectSettings', 'remote_path', os.path.join(remote_folder, ''))
project_config.set('ProjectSettings', 'url_targets', url_targets)
project_vmc_filename = os.path.join(local_folder, '.vcm')
with open(project_vmc_filename, 'w') as configfile:
try:
project_config.write(configfile)
except configparser.Error as ex:
print(f"Error writing config file: {project_vmc_filename} : {ex.message}")
return
@click.group()
def vcm():
global global_config
global_config = VcmGlobalConfig()
if os.path.isfile(os.path.expanduser(GLOBAL_CONFIG_LOCATION)):
global_config.read_global_vcm()
else:
global_config.write_global_vcm()
pass
###
# Folder and project management
###
def create_folder(folder):
if not os.path.exists(folder):
try:
os.makedirs(folder)
except OSError as ex:
print(f"Error creating folder: {folder} : {ex.strerror}")
return
@vcm.command()
def create():
# create a config file .vcm and ask for: project name, root dir name, remote directory, urls (csv)
project_name = click.prompt('Project Name', type=str)
local_folder = click.prompt('Local Path', type=str, default=os.path.join(os.getcwd(), project_name))
remote_folder = click.prompt('Remote Path', type=str)
url_targets = click.prompt('URL Targets (CSV)', type=str)
create_folder(local_folder)
for folder in ['reports', 'artifacts', 'logistics']:
create_folder(os.path.join(local_folder, folder))
project_config = VcmProjectConfig()
project_config.write_project_vcm(project_name, local_folder, remote_folder, url_targets)
@vcm.command()
def push():
# ensure the remote dir is mounted
project_config = VcmProjectConfig()
project_config.read_project_vcm()
# do an rsync -ah from local to remote
if click.confirm('Sync local (%s) to remote (%s)?' % (project_config.local_folder, project_config.remote_folder)):
args = ["rsync", "-ah", "--progress", project_config.local_folder, project_config.remote_folder]
call(args)
@vcm.command()
def pull():
# ensure the remote dir is mounted
# do an rsync -ah from remote to local
project_config = VcmProjectConfig()
project_config.read_project_vcm()
# do an rsync -ah from local to remote
if click.confirm('Sync remote (%s) to local (%s)?' % (project_config.remote_folder, project_config.local_folder)):
args = ["rsync", "-ah", "--progress", project_config.remote_folder, project_config.local_folder]
call(args)
###
# Running testing tools
###
@vcm.group()
def run():
pass
@run.command()
def nmap():
try:
project_config = VcmProjectConfig()
project_config.read_project_vcm()
except ValueError as ex:
print(ex)
return
# We only need the netloc of the full url - strip the rest out
nmap_targets = []
for t in project_config.targets:
nmap_targets.append(urlparse(t).netloc)
if not click.confirm('Run nmap against the following targets: %s' % ', '.join(nmap_targets)):
return
args = ["nmap"]
args.extend(DEFAULT_NMAP_SETTINGS)
for t in nmap_targets:
args.append(t)
args.append("-oA")
args.append(os.path.join(project_config.artifacts_folder, f'nmap_{time.time()}'))
call(args)
@run.command()
def nikto():
try:
project_config = VcmProjectConfig()
project_config.read_project_vcm()
except ValueError as ex:
print(ex)
return
if not click.confirm('Run nikto against the following targets: %s' % ', '.join(project_config.targets)):
return
# Nikto takes multiple hosts from a file
# BUT bear in mind advice from: https://github.com/sullo/nikto/wiki/Basic-Testing
# ie run scans separately so that memory is freed each time.
for t in project_config.targets:
output_filename = os.path.join(project_config.artifacts_folder,
f"nikto_{urlparse(t).netloc}_{time.time()}.html")
try:
# nikto -h https://www.test.com -ssl -Format html -output .
args = ["nikto", "-h", t, '-ssl', '-Format', 'html', '-output', output_filename]
print(args)
call(args)
except Exception as ex:
print(f"Error writing nikto output to: {output_filename} : {ex}")
@run.command()
def testssl():
try:
project_config = VcmProjectConfig()
project_config.read_project_vcm()
except ValueError as ex:
print(ex)
return
https_targets = []
for t in project_config.targets:
https_targets.append('https://' + urlparse(t).netloc)
if not click.confirm('Run testssl against the following targets: %s' % ', '.join(https_targets)):
return
for t in https_targets:
output_filename = os.path.join(project_config.artifacts_folder, f"testssl_{urlparse(t).netloc}_{time.time()}.txt")
try:
args = ["testssl.sh", "--openssl", global_config.open_ssl_binary, "--logfile", output_filename, t]
print(args)
call(args)
except Exception as ex:
print(f"Error writing testssl output to: {output_filename} : {ex}")
@run.command()
def dirb():
try:
project_config = VcmProjectConfig()
project_config.read_project_vcm()
except ValueError as ex:
print(ex)
return
if not click.confirm('Run dirb against the following targets: %s' % ', '.join(project_config.targets)):
return
for t in project_config.targets:
output_filename = os.path.join(project_config.artifacts_folder,
'dirb_' + str(project_config.targets.index(t))) + '.txt'
try:
# dirb url -o output.txt
args = ["dirb", t, '-o', output_filename]
call(args)
except Exception as ex:
print(f"Error writing dirb output to: {output_filename} : {ex}")
if __name__ == '__main__':
vcm()