forked from netwrix/pingcastle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPingCastleReportHelper.cs
195 lines (184 loc) · 6.01 KB
/
PingCastleReportHelper.cs
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
//
// Copyright (c) Ping Castle. All rights reserved.
// https://www.pingcastle.com
//
// Licensed under the Non-Profit OSL. See LICENSE file in the project root for full license information.
//
using PingCastle.Healthcheck;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace PingCastle.Data
{
public class PingCastleReportHelper<T> where T : IPingCastleReport
{
public static PingCastleReportCollection<T> LoadXmls(string Xmls, DateTime maxfiltervalue)
{
var output = new PingCastleReportCollection<T>();
int files = 0;
foreach (string filename in Directory.GetFiles(Xmls, PingCastleFactory.GetFilePatternForLoad<T>(), SearchOption.AllDirectories))
{
try
{
files++;
T data = DataHelper<T>.LoadXml(filename);
// taking the more recent report
if (data.GenerationDate > maxfiltervalue)
{
Trace.WriteLine("File " + filename + " ignored because generation date " + data.GenerationDate.ToString("u") + " is after the consolidation date " + maxfiltervalue.ToString("u"));
continue;
}
output.Add(data);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Unable to load the file " + filename + " (" + ex.Message + ")");
Console.ResetColor();
Trace.WriteLine("Unable to load the file " + filename + " (" + ex.Message + ")");
Trace.WriteLine(ex.StackTrace);
}
}
Console.WriteLine("Reports loaded: " + output.Count + " - on a total of " + files + " valid files");
output.EnrichInformation();
return output;
}
public static PingCastleReportHistoryCollection<T> LoadHistory(string Xmls, DateTime maxfiltervalue)
{
var output = new PingCastleReportHistoryCollection<T>();
int files = 0;
foreach (string filename in Directory.GetFiles(Xmls, "*ad_hc_*.xml", SearchOption.AllDirectories))
{
try
{
files++;
var data = DataHelper<T>.LoadXml(filename);
// taking the more recent report
if (data.GenerationDate > maxfiltervalue)
{
Trace.WriteLine("File " + filename + " ignored because generation date " + data.GenerationDate.ToString("u") + " is after the consolidation date " + maxfiltervalue.ToString("u"));
continue;
}
output.Add(data);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Unable to load the file " + filename + " (" + ex.Message + ")");
Console.ResetColor();
Trace.WriteLine("Unable to load the file " + filename + " (" + ex.Message + ")");
Trace.WriteLine(ex.StackTrace);
}
}
Console.WriteLine("Reports loaded: " + output.Count + " - on a total of " + files + " valid files");
return output;
}
public static PingCastleReportCollection<HealthcheckData> TransformReportsToDemo(PingCastleReportCollection<HealthcheckData> consolidation)
{
string rotKey = GenerateRandomRotKey();
var output = new PingCastleReportCollection<HealthcheckData>();
foreach (HealthcheckData data in consolidation)
{
HealthcheckData demoreport = TransformReportToDemo(rotKey, data);
output.Add(demoreport);
}
return output;
}
public static HealthcheckData TransformReportToDemo(string rotKey, HealthcheckData healthcheckData)
{
healthcheckData.DomainFQDN = TransformFQDNToDemo(rotKey, healthcheckData.DomainFQDN);
healthcheckData.ForestFQDN = TransformFQDNToDemo(rotKey, healthcheckData.ForestFQDN);
healthcheckData.NetBIOSName = TransformFQDNToDemo(rotKey, healthcheckData.NetBIOSName);
if (healthcheckData.Trusts != null)
{
foreach (HealthCheckTrustData trust in healthcheckData.Trusts)
{
trust.TrustPartner = TransformFQDNToDemo(rotKey, trust.TrustPartner);
if (trust.KnownDomains != null)
{
foreach (var di in trust.KnownDomains)
{
di.DnsName = TransformFQDNToDemo(rotKey, di.DnsName);
di.ForestName = TransformFQDNToDemo(rotKey, di.ForestName);
if (!String.IsNullOrEmpty(di.NetbiosName))
di.NetbiosName = TransformFQDNToDemo(rotKey, di.NetbiosName.ToLowerInvariant());
}
}
}
}
if (healthcheckData.ReachableDomains != null)
{
foreach (var di in healthcheckData.ReachableDomains)
{
if (di.DnsName.Equals(di.NetbiosName, StringComparison.InvariantCultureIgnoreCase))
{
di.NetbiosName = TransformFQDNToDemo(rotKey, di.NetbiosName.ToLowerInvariant());
di.DnsName = di.NetbiosName;
}
else
{
di.NetbiosName = TransformFQDNToDemo(rotKey, di.NetbiosName.ToLowerInvariant());
di.DnsName = TransformFQDNToDemo(rotKey, di.DnsName);
}
di.ForestName = TransformFQDNToDemo(rotKey, di.ForestName);
}
}
return healthcheckData;
}
public static string TransformFQDNToDemo(string rotKey, string source)
{
if (String.IsNullOrEmpty(source))
return null;
StringBuilder sb = new StringBuilder(source.Length);
source = source.ToLowerInvariant();
for (int i = 0; i < source.Length; i++)
{
char c = source[i];
if (c >= 97 && c <= 122)
{
int j = c + rotKey[source.Length - 1 - i] - 97;
if (j > 122) j -= 26;
sb.Append((char)j);
}
else
{
sb.Append(c);
}
}
return sb.ToString();
}
public static string TransformNameToDemo(string rotKey, string source)
{
if (String.IsNullOrEmpty(source))
return null;
StringBuilder sb = new StringBuilder(source.Length);
source = source.ToLowerInvariant();
for (int i = 0; i < source.Length; i++)
{
char c = source[i];
if (c >= 97 && c <= 122)
{
int j = c + rotKey[i] - 97;
if (j > 122) j -= 26;
sb.Append((char)j);
}
else
{
sb.Append(c);
}
}
return sb.ToString();
}
public static string GenerateRandomRotKey()
{
string refstring = "abcdefghijklmnopqrstuvwxyz";
var randomString = new StringBuilder();
var random = new Random();
for (int i = 0; i < 100; i++)
randomString.Append(refstring[random.Next(refstring.Length)]);
return randomString.ToString();
}
}
}