-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMarkdownReportGenerator.cs
More file actions
398 lines (335 loc) · 13.9 KB
/
MarkdownReportGenerator.cs
File metadata and controls
398 lines (335 loc) · 13.9 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
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
using System.Text;
namespace NetworkOptimizer.Reports;
/// <summary>
/// Markdown report generator for network audit reports
/// Generates reports suitable for wikis, ticketing systems, and version control
/// </summary>
public class MarkdownReportGenerator
{
private readonly BrandingOptions _branding;
public MarkdownReportGenerator(BrandingOptions? branding = null)
{
_branding = branding ?? BrandingOptions.OzarkConnect();
}
/// <summary>
/// Generate Markdown report and save to file
/// </summary>
public void GenerateReport(ReportData data, string outputPath)
{
var markdown = GenerateMarkdown(data);
File.WriteAllText(outputPath, markdown);
}
/// <summary>
/// Generate Markdown report as string
/// </summary>
public string GenerateMarkdown(ReportData data)
{
var sb = new StringBuilder();
// Header
ComposeHeader(sb, data);
// Threat Intelligence Summary
if (data.ThreatSummary != null && data.ThreatSummary.TotalEvents > 0)
{
ComposeThreatSummary(sb, data);
}
// Network Reference
ComposeNetworkReference(sb, data);
// Executive Summary
ComposeExecutiveSummary(sb, data);
// Action Items
ComposeActionItems(sb, data);
// Separator
sb.AppendLine();
sb.AppendLine("---");
sb.AppendLine();
// Switch Details
ComposeSwitchDetails(sb, data);
// Port Security Coverage Summary
ComposePortSecuritySummary(sb, data);
// Footer
ComposeFooter(sb, data);
return sb.ToString();
}
private void ComposeHeader(StringBuilder sb, ReportData data)
{
sb.AppendLine($"# {data.ClientName} Security Audit Report");
sb.AppendLine();
sb.AppendLine($"**Generated:** {data.GeneratedAt:MMMM dd, yyyy}");
sb.AppendLine();
}
private void ComposeNetworkReference(StringBuilder sb, ReportData data)
{
sb.AppendLine("## Network Reference");
sb.AppendLine();
if (!data.Networks.Any())
{
sb.AppendLine("*No networks configured*");
sb.AppendLine();
return;
}
sb.AppendLine("| Network | VLAN | Subnet |");
sb.AppendLine("|---------|------|--------|");
var sortedNetworks = data.Networks.OrderBy(n => n.VlanId).ToList();
foreach (var network in sortedNetworks)
{
var vlanStr = network.VlanId == 1
? $"{network.VlanId} (native)"
: network.VlanId.ToString();
sb.AppendLine($"| {network.Name} | {vlanStr} | {network.Subnet} |");
}
sb.AppendLine();
}
private void ComposeExecutiveSummary(StringBuilder sb, ReportData data)
{
sb.AppendLine("## Executive Summary");
sb.AppendLine();
// Security Posture Rating
var ratingText = data.SecurityScore.Rating switch
{
SecurityRating.Excellent => "**Overall Security Posture: EXCELLENT ✓**",
SecurityRating.Good => "**Overall Security Posture: GOOD ✓**",
SecurityRating.Fair => "**Overall Security Posture: FAIR ⚠**",
SecurityRating.NeedsWork => "**Overall Security Posture: NEEDS ATTENTION ✗**",
_ => "**Overall Security Posture: UNKNOWN**"
};
sb.AppendLine(ratingText);
sb.AppendLine();
// Hardening measures
if (data.HardeningNotes.Any())
{
sb.AppendLine("Hardening measures already in place:");
foreach (var note in data.HardeningNotes)
{
sb.AppendLine($"- {note}");
}
sb.AppendLine();
}
// Topology notes
if (data.TopologyNotes.Any())
{
sb.AppendLine("Network topology notes:");
foreach (var note in data.TopologyNotes)
{
sb.AppendLine($"- {note}");
}
sb.AppendLine();
}
}
private void ComposeActionItems(StringBuilder sb, ReportData data)
{
sb.AppendLine("## Action Items");
sb.AppendLine();
if (!data.CriticalIssues.Any() && !data.RecommendedImprovements.Any())
{
sb.AppendLine("**No action items — all checks passed.** ✓");
sb.AppendLine();
return;
}
// Critical Issues
if (data.CriticalIssues.Any())
{
sb.AppendLine($"### 🔴 Critical ({data.CriticalIssues.Count})");
sb.AppendLine();
sb.AppendLine("| Device | Port | Issue | Action |");
sb.AppendLine("|--------|------|-------|--------|");
foreach (var issue in data.CriticalIssues)
{
var portText = issue.PortIndex.HasValue
? $"{issue.PortIndex} ({issue.PortName})"
: issue.PortName;
sb.AppendLine($"| {issue.SwitchName} | {portText} | {issue.Message} | {issue.RecommendedAction} |");
}
sb.AppendLine();
}
// Recommended Improvements
if (data.RecommendedImprovements.Any())
{
sb.AppendLine($"### 🟡 Recommended ({data.RecommendedImprovements.Count})");
sb.AppendLine();
sb.AppendLine("| Device | Port | Issue |");
sb.AppendLine("|--------|------|-------|");
foreach (var issue in data.RecommendedImprovements)
{
var portText = issue.PortIndex.HasValue
? $"{issue.PortIndex} ({issue.PortName})"
: issue.PortName;
sb.AppendLine($"| {issue.SwitchName} | {portText} | {issue.Message} |");
}
sb.AppendLine();
}
}
private void ComposeSwitchDetails(StringBuilder sb, ReportData data)
{
foreach (var switchDevice in data.Switches)
{
var switchType = switchDevice.IsGateway ? "Gateway" : "Switch";
sb.AppendLine($"## [{switchType}] {switchDevice.Name} ({switchDevice.ModelName})");
sb.AppendLine();
// Check if any port has isolation
var hasIsolation = switchDevice.Ports.Any(p => p.Isolation);
// Build table header
if (hasIsolation)
{
sb.AppendLine("| Port | Name | Link | Forward | Native VLAN | PoE | Port Sec | Isolation | Status |");
sb.AppendLine("|------|------|------|---------|-------------|-----|----------|-----------|--------|");
}
else
{
sb.AppendLine("| Port | Name | Link | Forward | Native VLAN | PoE | Port Sec | Status |");
sb.AppendLine("|------|------|------|---------|-------------|-----|----------|--------|");
}
// Port rows
foreach (var port in switchDevice.Ports)
{
var (status, statusType) = port.GetStatus(switchDevice.MaxCustomMacAcls > 0);
var nativeVlan = port.NativeVlan.HasValue && !string.IsNullOrEmpty(port.NativeNetwork)
? $"{port.NativeNetwork} ({port.NativeVlan})"
: port.Forward == "disabled" ? "—" : "";
var forward = port.Forward == "customize" ? "custom" : port.Forward;
if (hasIsolation)
{
sb.AppendLine($"| {port.PortIndex} | {port.Name} | {port.GetLinkStatus()} | {forward} | {nativeVlan} | {port.GetPoeStatus()} | {port.GetPortSecurityStatus()} | {port.GetIsolationStatus()} | {status} |");
}
else
{
sb.AppendLine($"| {port.PortIndex} | {port.Name} | {port.GetLinkStatus()} | {forward} | {nativeVlan} | {port.GetPoeStatus()} | {port.GetPortSecurityStatus()} | {status} |");
}
}
sb.AppendLine();
// Notes
if (switchDevice.MaxCustomMacAcls == 0)
{
sb.AppendLine($"*Note: {switchDevice.ModelName} doesn't support MAC ACLs*");
sb.AppendLine();
}
// Excluded networks note
var portsWithExclusions = switchDevice.Ports.Where(p => p.ExcludedNetworks.Any()).ToList();
if (portsWithExclusions.Any())
{
foreach (var port in portsWithExclusions)
{
var excluded = string.Join(", ", port.ExcludedNetworks);
sb.AppendLine($"*Port {port.PortIndex} excludes: {excluded}*");
}
sb.AppendLine();
}
}
}
private void ComposePortSecuritySummary(StringBuilder sb, ReportData data)
{
sb.AppendLine("## Port Security Coverage Summary");
sb.AppendLine();
sb.AppendLine("| Switch | Total | Disabled | MAC Restricted | Unprotected Active |");
sb.AppendLine("|--------|-------|----------|----------------|-------------------|");
foreach (var switchDevice in data.Switches)
{
var macStr = switchDevice.MaxCustomMacAcls > 0
? switchDevice.MacRestrictedPorts.ToString()
: "0 (no ACL support)";
sb.AppendLine($"| {switchDevice.Name} | {switchDevice.TotalPorts} | {switchDevice.DisabledPorts} | {macStr} | {switchDevice.UnprotectedActivePorts} |");
}
sb.AppendLine();
}
private void ComposeThreatSummary(StringBuilder sb, ReportData data)
{
var threat = data.ThreatSummary!;
sb.AppendLine("## Threat Intelligence");
sb.AppendLine();
sb.AppendLine($"*{threat.TimeRange}*");
sb.AppendLine();
// Summary stats
sb.AppendLine("| Total Events | Blocked | Detected | Unique Sources |");
sb.AppendLine("|-------------|---------|----------|----------------|");
sb.AppendLine($"| {threat.TotalEvents:N0} | {threat.TotalBlocked:N0} | {threat.TotalDetected:N0} | {threat.UniqueSourceIps:N0} |");
sb.AppendLine();
// Kill chain
if (threat.ByKillChain.Any())
{
sb.AppendLine("### Kill Chain Distribution");
sb.AppendLine();
foreach (var stage in threat.ByKillChain.OrderByDescending(k => k.Value))
{
var pct = threat.TotalEvents > 0 ? (double)stage.Value / threat.TotalEvents * 100 : 0;
sb.AppendLine($"- **{stage.Key}**: {stage.Value:N0} ({pct:F0}%)");
}
sb.AppendLine();
}
// Top sources
if (threat.TopSources.Any())
{
sb.AppendLine("### Top Threat Sources");
sb.AppendLine();
sb.AppendLine("| IP Address | Country | ASN | Events |");
sb.AppendLine("|-----------|---------|-----|--------|");
foreach (var source in threat.TopSources)
{
sb.AppendLine($"| {source.Ip} | {source.CountryCode ?? "-"} | {source.AsnOrg ?? "-"} | {source.EventCount:N0} |");
}
sb.AppendLine();
// Suppressed-count footnote
if (threat.SuppressedEventCount > 0)
{
var infraCount = threat.InfrastructureSources.Sum(s => s.EventCount);
var trustedCount = threat.TrustedUserSources.Sum(s => s.EventCount);
var parts = new List<string>();
if (infraCount > 0) parts.Add($"{infraCount:N0} from known infrastructure");
if (trustedCount > 0) parts.Add($"{trustedCount:N0} from trusted user devices");
sb.AppendLine($"*Excludes {string.Join(" and ", parts)} (see categorized tables below).*");
sb.AppendLine();
}
}
// Known Infrastructure Activity
if (threat.InfrastructureSources.Any())
{
sb.AppendLine("### Known Infrastructure Activity");
sb.AppendLine();
sb.AppendLine("| IP Address | Label | Country | Events |");
sb.AppendLine("|-----------|-------|---------|--------|");
foreach (var source in threat.InfrastructureSources)
{
sb.AppendLine($"| {source.Ip} | {source.Label ?? "-"} | {source.CountryCode ?? "-"} | {source.EventCount:N0} |");
}
sb.AppendLine();
}
// Trusted User Activity
if (threat.TrustedUserSources.Any())
{
sb.AppendLine("### Trusted User Activity");
sb.AppendLine();
sb.AppendLine("| IP Address | Label | Country | Events |");
sb.AppendLine("|-----------|-------|---------|--------|");
foreach (var source in threat.TrustedUserSources)
{
sb.AppendLine($"| {source.Ip} | {source.Label ?? "-"} | {source.CountryCode ?? "-"} | {source.EventCount:N0} |");
}
sb.AppendLine();
}
// Exposed services
if (threat.ExposedServices.Any())
{
sb.AppendLine("### Exposed Services Under Attack");
sb.AppendLine();
sb.AppendLine("| Port | Service | Forward To | Threats | Sources |");
sb.AppendLine("|------|---------|-----------|---------|---------|");
foreach (var svc in threat.ExposedServices)
{
sb.AppendLine($"| {svc.Port} | {svc.ServiceName} | {svc.ForwardTarget} | {svc.ThreatCount:N0} | {svc.UniqueSourceIps:N0} |");
}
sb.AppendLine();
}
}
private void ComposeFooter(StringBuilder sb, ReportData data)
{
sb.AppendLine("---");
sb.AppendLine();
if (!string.IsNullOrEmpty(_branding.CustomFooter))
{
sb.AppendLine(_branding.CustomFooter);
}
else if (_branding.ShowProductAttribution)
{
sb.AppendLine($"*Generated by {_branding.ProductName} for {_branding.CompanyName} | {data.GeneratedAt:yyyy-MM-dd HH:mm}*");
}
sb.AppendLine();
}
}