-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen-dod-profiles.py
executable file
·560 lines (474 loc) · 19.2 KB
/
gen-dod-profiles.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#!/usr/bin/env python3
from argparse import ArgumentParser, BooleanOptionalAction
from itertools import islice
import json
import re
import sys
import uuid
import yaml
can_ignore = ['at', 'ca', 'cp', 'ir', 'ma', 'mp', 'pe', 'pl', 'pm', 'ps', 'pt', 'ra']
pillars = ['Enabler', 'User', 'Device', 'Application & Workload', 'Data',
'Network & Environment', 'Automation & Orchestration',
'Visibility & Analytics']
mappings = { p: [] for p in pillars }
levels_by_id = { }
levels_by_name = { 'Target': [], 'Advanced': [] }
controls_by_id = { }
params_by_id = { }
baselines_by_name = { }
baselines_by_id = { }
seen_ids = { }
mapped_ids = { }
profile_uuid = str(uuid.uuid4())
import_uuid = str(uuid.uuid4())
html_preamble = '''<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>NIST Controls by DoD Pillar</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous">
<style>
dt { float: left; width: 3em; }
dd { margin-bottom: 0; }
.offcanvas { --bs-offcanvas-height: 60vh; }
th, td { padding-left: 1em; }
</style>
</head>
<body>
<div class="container-fluid border-bottom" style="padding-top: 1em; padding-bottom: 1em; margin-bottom: 1em;">
<button style="float: right;" type="button" class="btn btn-info" data-bs-toggle="modal" data-bs-target="#legend">
Legend
</button>
<header class="d-flex justify-content-center">
<h3>NIST SP 800-53 rev5 Controls by DoD pillar and NIST baseline</h3>
</header>
</div>
<div class="modal" id="legend" aria-hidden="true">
<div class="modal-dialog modal-xl">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalLabel">NIST Control Families</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="row">
<div class="col">
<dl>
<dt>ac</dt><dd>Access Control</dd>
<dt>at</dt><dd>Awareness and Training</dd>
<dt>au</dt><dd>Audit and Accountability</dd>
<dt>ca</dt><dd>Assessment, Authorization and Monitoring</dd>
<dt>cm</dt><dd>Configuration Management</dd>
<dt>cp</dt><dd>Contingency Planning</dd>
<dt>ia</dt><dd>Identification and Authentication</dd>
<dt>ir</dt><dd>Incident Response</dd>
<dt>ma</dt><dd>Maintenance</dd>
<dt>mp</dt><dd>Media Protection</dd>
</dl>
</div>
<div class="col">
<dl>
<dt>pe</dt><dd>Physical and Environmental Protection</dd>
<dt>pl</dt><dd>Planning</dd>
<dt>pm</dt><dd>Program Management</dd>
<dt>ps</dt><dd>Personnel Security</dd>
<dt>pt</dt><dd>Personally Identifiable Information Processing and Transparency</dd>
<dt>ra</dt><dd>Risk Assessment</dd>
<dt>sa</dt><dd>System and Services Acquisition</dd>
<dt>sc</dt><dd>System and Communications Protection</dd>
<dt>si</dt><dd>System and Information Integrity</dd>
<dt>sr</dt><dd>Supply Chain Risk Management</dd>
</dl>
</div>
</div>
</div>
</div>
</div>
</div>
'''
html_postamble = '''
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script>
<script>
const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))
const offcanvasElementList = document.querySelectorAll('.offcanvas')
const offcanvasList = [...offcanvasElementList].map(offcanvasEl => new bootstrap.Offcanvas(offcanvasEl))
</script>
</body>
</html>
'''
def get_profile(name, level, controls):
profile = {
'profile': {
'uuid': profile_uuid,
'metadata': { 'title': f"NIST SP 800-53 rev5 - DoD pillar {name}, {level}" },
'imports' : [
{
'href': f"#{import_uuid}",
'include-controls': [
{ 'with-ids': controls }
]
}
],
'back-matter': {
'resources': [
{
'uuid': import_uuid,
'description': 'NIST SP 800-53 Rev 5.1.1 Controls',
'rlinks': [
{ 'href' : 'NIST_SP-800-53_rev5_catalog.xml' }
]
}
]
}
}
}
return profile
def get_catalog(name, level, ids):
catalog = {
'catalog': {
'uuid': profile_uuid,
'metadata': { 'title': f"NIST SP 800-53 rev5 - DoD pillar {name}, {level}" },
'controls': [controls_by_id[id] for id in ids]
}
}
return catalog
def add_mappings(controls):
for c in controls:
id = c['id']
controls_by_id[id] = c
c['props_by_name'] = { prop['name'] : prop['value'] for prop in c['props'] }
for p in c['props']:
name = p['name']
value = p['value']
if name == 'pillar':
key = value
mappings[key].append(id)
elif name == 'type':
levels_by_id[id] = value
levels_by_name[value].append(id)
if 'params' in c:
for param in c['params']:
id = param['id']
if id in params_by_id:
print(f"Warning: duplicate parameter id {id}", file.sys.stderr)
params_by_id[id] = param
if 'controls' in c:
add_mappings(c['controls'])
def load_baseline(filename):
ids = []
with open(filename, 'r') as file:
profile = json.load(file).get('profile')
for i in profile['imports']:
for inc in i['include-controls']:
for id in inc['with-ids']:
if id not in seen_ids:
ids.append(id)
seen_ids[id] = id
return ids
def write_profile(dest, name, level, prefix, controls, resolve=False):
filename = f"{dest}/{prefix}-{name}-{level}.yaml"
filename = filename.replace(' & ', '-and-')
print(f"Writing {filename}", file=sys.stderr)
if resolve:
content = get_catalog(name, level, controls)
else:
content = get_profile(name, level, controls)
with open(filename, 'w') as file:
yaml.dump(content, file, sort_keys=False)
baseline_styles = ['primary', 'success', 'danger']
baseline_headings = ['Low', 'Moderate', 'High']
level_styles = { 'Target': 'primary', 'Advanced': 'success',
'Low': 'primary', 'Moderate': 'success', 'High': 'danger' }
def get_prop(id, name, default='unset'):
control = controls_by_id[id]
props = control['props_by_name']
return props[name] if name in props else default
def is_withdrawn(id):
status = get_prop(id, 'status')
return status == 'withdrawn'
def is_org(id):
scope = get_prop(id, 'implementation-level')
return scope == 'organization'
def generate_control(id, classes, guidance=False):
mapped_ids[id] = id
text = controls_by_id[id]['title']
label = id
if guidance:
return f'<a href="#{id}" class="{classes}" data-bs-toggle="offcanvas">{label}</a>'
else:
return f'<span class="{classes}" data-bs-toggle="tooltip" data-bs-title="{text}">{label}</span>'
param_pattern = re.compile(r'{{ insert: param, (\S+) }}')
def resolve_text(text):
def replace(m):
id = m.group(1)
if id not in params_by_id:
print(f"Warning: {id} not in this control", file=sys.stderr)
return id
param = params_by_id[id]
if 'label' in param:
repl = param['label']
elif 'select' in param:
repl = " | ".join(param['select']['choice'])
else:
repl = id
repl = resolve_text(repl)
return f'<span style="background-color: yellow;">< {repl} ></span>'
return param_pattern.sub(replace, text)
part_names = {
'statement' : 'Statement',
'guidance' : 'Guidance',
'assessment-objective': 'Assessment Objective',
'assessment-method' : 'Assessment Method',
}
linebreak_pattern = re.compile(r'\n\n', flags=re.M)
href_pattern = re.compile(r'\[([^\]]+)\]\(([^\)]+)\)')
def md_to_link(m):
label = m.group(1)
href = m.group(2)
return f'<a href="{href}" data-bs-toggle="offcanvas">{label}</a>'
def resolve_parts(parts, params, top=False):
html = []
for part in parts:
if top and part['name'] in part_names:
title = part_names[part['name']]
html.append(f'<h5 style="padding-top: 0.5em;">{title}</h5>')
if 'props' in part:
props = { p['name'] : p['value'] for p in part['props'] }
if 'label' in props:
label = props['label']
html.append(f'<b class="label">{label}</b>')
if 'prose' in part:
text = resolve_text(part['prose'])
text = linebreak_pattern.sub('<br/>', text)
text = href_pattern.sub(md_to_link, text)
html.append(f'<span class="text">{text}</span><br/>')
if 'parts' in part:
html.append('<div class="container-fluid">')
html.extend(resolve_parts(part['parts'], params))
html.append('</div>')
return html
def resolve_props(control):
items = []
nist_items = []
dod_items = []
for prop in control['props']:
if prop['name'] == 'pillar':
nist_items.append(prop['value'])
elif prop['name'] in ['activity', 'phase', 'tech', 'type']:
dod_items.append(prop['value'])
# DoD Activities
items.append('<div style="float: right;">')
items.append('<table>')
items.append('<tr><th>Activity</th><th>Phase</th><th>Scope</th><th>Maturity Level</th></tr>')
i = iter(dod_items)
while chunk := list(islice(i, 4)):
activity, phase, tech, type = chunk
items.append(f"<tr><td>{activity}</td><td>{phase}</td><td>{tech}</td><td>{type}</td></tr>")
items.append('</table>')
items.append('</div>')
# DoD Pillars
items.append(f'<h5 style="padding-top: 0.5em;">Pillars</h5>')
items.append('<p>')
items.append(', '.join(nist_items))
items.append('</p>')
# Links
items += resolve_links(control)
return "\n".join(items)
def resolve_links(control):
if 'links' not in control:
return []
items = []
items.append('<div>')
items.append('<h5>Links</h5>')
for link in control['links']:
if link['rel'] in [ 'required', 'related' ]:
text = link['href']
items.append(f'<a href="{text}" data-bs-toggle="offcanvas">{text}</a>')
items.append('</div>')
return items
def resolve_control(id):
control = controls_by_id[id]
if 'params' not in control:
params = []
else:
params = { param['id'] : param for param in control['params'] }
html = resolve_parts(control['parts'], params, top=True)
return html
def get_style(id):
if id in baselines_by_id:
baseline = baselines_by_id[id]
return level_styles[baseline]
else:
return 'secondary'
def generate_html(type, guidance=False):
html = []
html.append('<div class="container-fluid">')
html.append('<div class="row align-items-end">')
html.append('<div class="col"></div>')
for p in pillars:
html.append('<div class="col">')
html.append(f'<h5>{p}</h5>')
html.append('</div>')
html.append('</div>')
# controls in DoD maturity levels
if type == 'dod':
for name in ['Advanced', 'Target']:
ids = { id : id for id in levels_by_name[name] }
html.append('<div class="row align-items-end">')
html.append('<div class="col">')
html.append(f'<h5>DoD {name}</h5>')
html.append('</div>')
for p in pillars:
html.append('<div class="col">')
for id in mappings[p]:
if id in ids and not is_org(id):
style = get_style(id)
classes = f'badge bg-{style}-subtle text-{style}'
html.append(generate_control(id, classes, guidance))
html.append('</div>')
html.append('</div>')
else:
# controls in NIST baselines AND DoD pillars
for i, k in reversed(list(enumerate(baselines_by_name.keys()))):
style = baseline_styles[i]
head = baseline_headings[i]
classes = f'badge bg-{style}-subtle text-{style}'
baseline_ids = { id : id for id in baselines_by_name[k] }
html.append('<div class="row align-items-end">')
html.append('<div class="col">')
html.append(f'<h5>{head} Baseline</h5>')
html.append('</div>')
for p in pillars:
html.append('<div class="col">')
for id in mappings[p]:
if id in baseline_ids and not is_org(id):
html.append(generate_control(id, classes, guidance))
html.append('</div>')
html.append('</div>')
# controls only in DoD pillars
html.append('<div class="row align-items-end">')
html.append('<div class="col">')
html.append(f'<h5>No baseline</h5>')
html.append('</div>')
style = 'secondary'
classes = f'badge bg-{style}-subtle text-{style}'
for p in pillars:
html.append('<div class="col">')
for id in mappings[p]:
if id not in mapped_ids:
html.append(generate_control(id, classes, guidance))
html.append('</div>')
html.append('</div>')
html.append('</div>') # container-fluid
# controls only in NIST baselines
html.append('<div class="container-fluid" style="padding-top: 2em; padding-bottom: 1em;">')
html.append('<h5>Controls in NIST baselines that are not mapped to DoD pillars</h5>')
for i, k in reversed(list(enumerate(baselines_by_name.keys()))):
style = baseline_styles[i]
classes = f'badge bg-{style}-subtle text-{style}'
for id in baselines_by_name[k]:
if id not in mapped_ids:
html.append(generate_control(id, classes, guidance))
html.append('</div>')
# NIST controls not referenced by DoD or by NIST baselines
html.append('<div class="container-fluid" style="padding-bottom: 1em;">')
html.append('<h5>Controls not in any DoD pillars or NIST baselines</h5>')
style = 'secondary'
classes = f'badge bg-{style}-subtle text-{style}'
for id in controls_by_id.keys():
if id not in mapped_ids:
if id[:2] in can_ignore:
continue
if is_withdrawn(id):
continue
html.append(generate_control(id, classes, guidance))
html.append('</div>')
# Ignored NIST controls not referenced elsewhere
html.append('<div class="container-fluid" style="padding-bottom: 1em;">')
html.append('<h5>Ignored controls not referenced elsewhere</h5>')
style = 'secondary'
classes = f'badge bg-{style}-subtle text-{style}'
for id in controls_by_id.keys():
if id not in mapped_ids:
if is_withdrawn(id):
continue
html.append(generate_control(id, classes, guidance))
html.append('</div>')
# Guidance text popup div blocks
if guidance:
html.append('<div class="container">')
for id in controls_by_id.keys():
if is_withdrawn(id):
continue
control = controls_by_id[id]
title = control['title']
scope = get_prop(id, 'implementation-level')
prop_html = resolve_props(control)
baseline = f", {baselines_by_id[id]}" if id in baselines_by_id else ''
html.append(f'<div class="offcanvas offcanvas-bottom" id="{id}">')
html.append('<div class="offcanvas-header">')
html.append(f'<h4>{id.upper()} – {title} ({scope}{baseline})</h4>')
html.append('<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>')
html.append('</div>')
html.append('<div class="offcanvas-body">')
html.append(prop_html)
html.extend(resolve_control(id))
html.append('</div>')
html.append('</div>')
html.append('</div>')
return "\n".join(html)
def is_level(c, l):
return c in levels_by_id and levels_by_id[c] == l
def main(filename, dest, prefix, baselines, vis_type, resolve=False, visualize=False, guidance=False):
baselines = baselines or []
for i, b in enumerate(baselines):
ids = load_baseline(b)
head = baseline_headings[i]
baselines_by_name[head] = ids
for id in ids:
baselines_by_id[id] = head
print(f"Reading {filename}", file=sys.stderr)
with open(filename, 'r') as file:
catalog = json.load(file).get('catalog')
groups = catalog.get('groups')
for g in groups:
add_mappings(g['controls'])
if visualize:
pillars.pop(0) # skip the enablers column
print('Generating HTML', file=sys.stderr)
print(html_preamble)
print(generate_html(vis_type, guidance))
print(html_postamble)
else:
for p in pillars:
controls = mappings[p]
for l in ['Target', 'Advanced']:
subset = list(filter(lambda c: is_level(c, l), controls))
write_profile(dest, p, l, prefix, subset, resolve)
if __name__ == '__main__':
parser = ArgumentParser(description='Generate OSCAL profiles for each DoD pillar')
parser.add_argument('-d', '--dir', type=str, default='.',
help='Target directory for generated profiles')
parser.add_argument('-f', '--file', type=str, required=True,
help='The DoD annotated NIST controls in JSON')
parser.add_argument('-p', '--prefix', type=str, default='dod-profile',
help='Filename prefix for generated profiles')
parser.add_argument('-r', '--resolve', action=BooleanOptionalAction,
help='Include control definitions inline, not just as imports')
parser.add_argument('-b', '--baseline', type=str, action='append',
help='Include NIST baseline information')
parser.add_argument('-V', '--visualize', action=BooleanOptionalAction,
help='Generate a visualization of controls by pillar')
parser.add_argument('-g', '--guidance', action=BooleanOptionalAction,
help='Include guidance notes from the specifcation')
parser.add_argument('-t', '--vis-type', type=str, default='nist',
help='Type of report [ nist | dod ]')
args = parser.parse_args()
main(args.file, args.dir, args.prefix, args.baseline, args.vis_type,
resolve=args.resolve, visualize=args.visualize, guidance=args.guidance)