summaryrefslogtreecommitdiff
path: root/WinKeyRecover/old.cs
blob: d36aa95c2584a080b934a1c43fafee1d26552be3 (plain)
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
using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Runtime.InteropServices;


namespace WinKeyRecover
{
	class NativeMethods
	{
		[DllImport("kernel32.dll")]
		public static extern IntPtr LoadLibrary(string dllToLoad);

		[DllImport("kernel32.dll")]
        public static extern bool FreeLibrary(IntPtr hModule);

		[DllImport("kernel32.dll")]
		public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

		[UnmanagedFunctionPointer(CallingConvention.StdCall)]
		public delegate int PidGenX([MarshalAs(UnmanagedType.LPWStr)] string ProductKey, [MarshalAs(UnmanagedType.LPWStr)] string PkeyPath, [MarshalAs(UnmanagedType.LPWStr)] string MSPID, int UnknownUsage, IntPtr ProductID, IntPtr DigitalProductID, IntPtr DigitalProductID4);
	}


	class PKChecker
	{
		public string Check_productKey(string productKey, string fileXml, string fileDll, string mspid)
		{
			IntPtr hModule = NativeMethods.LoadLibrary(fileDll);
			string description;
			string result;
			byte[] array = new byte[50];
			byte[] array2 = new byte[164];
			byte[] array3 = new byte[1272];
			IntPtr intPtr = Marshal.AllocHGlobal(50);
			IntPtr intPtr2 = Marshal.AllocHGlobal(164);
			IntPtr intPtr3 = Marshal.AllocHGlobal(1272);
			array[0] = 50;
			array2[0] = 164;
			array3[0] = 248;
			array3[1] = 4;
			Marshal.Copy(array, 0, intPtr, 50);
			Marshal.Copy(array2, 0, intPtr2, 164);
			Marshal.Copy(array3, 0, intPtr3, 1272);
            int num = ((NativeMethods.PidGenX)Marshal.GetDelegateForFunctionPointer(NativeMethods.GetProcAddress(hModule, "PidGenX"), typeof(NativeMethods.PidGenX)))(productKey, fileXml, mspid, 0, intPtr, intPtr2, intPtr3);
			if (num == 0)
			{
				Marshal.Copy(intPtr3, array3, 0, array3.Length);
				string @string = GetString(array3, 136);
				string productDescription = GetProductDescription(fileXml, "{" + @string + "}");
				description = productDescription;
			}
			else
			{
				description = "Invalid";
			}
			Marshal.FreeHGlobal(intPtr2);
			Marshal.FreeHGlobal(intPtr3);
			NativeMethods.FreeLibrary(hModule);
			result = description;
			return result;
		}

		private static string GetString(byte[] bytes, int index)
		{
			int num = index;
			while (bytes[num] != 0 || bytes[num + 1] != 0)
			{
				num++;
			}
			return Encoding.ASCII.GetString(bytes, index, num - index).Replace("\0", "");
		}

		private static string GetProductDescription(string fileXml, string aid)
		{
			string result;
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(fileXml);
			Stream inStream = new MemoryStream(Convert.FromBase64String(xmlDocument.GetElementsByTagName("tm:infoBin")[0].InnerText));
			xmlDocument.Load(inStream);
			XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
			xmlNamespaceManager.AddNamespace("pkc", "http://www.microsoft.com/DRM/PKEY/Configuration/2.0");
			XmlNode xmlNode = xmlDocument.SelectSingleNode("/pkc:ProductKeyConfiguration/pkc:Configurations/pkc:Configuration[pkc:ActConfigId='" + aid + "']", xmlNamespaceManager);
			if (xmlNode == null)
			{
				xmlNode = xmlDocument.SelectSingleNode("/pkc:ProductKeyConfiguration/pkc:Configurations/pkc:Configuration[pkc:ActConfigId='" + aid.ToUpper() + "']", xmlNamespaceManager);
			}
			result = xmlNode.ChildNodes.Item(3).InnerText;
			return result;
		}
	}


	class Cli
	{
		public static void Intro(char[] validChars)
		{
			string validCharsString = string.Join(", ", validChars);
			string intro =
				  "\n    ###########################"
				+ "\n    # R�cup�rateur de license #"
				+ "\n    ###########################\n"
				+ "\nInstructions:"
				+ "\n- Renseigner la cl� en remplacant les characters manquants par des '*'."
				+ "\n- La cl� doit �tre renseign�e comme ceci: 'XXXXX-XXXXX-XXXXX-XXXXX-XXXXX'."
				+ "\n- Les characters autoris�s sont: " + validCharsString
				+ "\n- Un fichier nomm� 'cl�s_trouv�es.txt' contenant les cl�s sera cr�� sur votre bureau.\n";
			Console.WriteLine(intro);
		}

		public static string Get_key(char[] validChars)
		{
			string key;
			Console.Write("Entrez votre cl�: ");
			key = Console.ReadLine().ToUpper();
			return key;
		}

		public static void Found_keys(List<List<string>> keysFound)
		{
			string found =
			  "\n    ###########################"
			+ "\n    # R�cup�rateur de license #"
			+ "\n    ###########################\n"
			+ "\nCl�s trouv�es:\n";
			Console.Clear();
			Console.WriteLine(found);
			for (int j = 0; j < keysFound.Count; j++)
			{
				Console.WriteLine("[>] " + keysFound[j][0] + " Version: " + keysFound[j][1]);
			}
			Console.WriteLine();
		}

		public static void End()
		{
			Console.WriteLine("\n    ###########################");
			Console.WriteLine("Appuyer sur 'Entrer' pour quitter.");
			Console.ReadLine();
			System.Environment.Exit(0);
		}
	}


	class Program
	{
		private static bool Key_isvalid(char[] validChars, string key)
		{
			bool validKey = true;
			if (key != string.Empty && key.Length == 29 && key.Contains('*'))
			{
				for (int i = 0; i < key.Length; i++)
				{
					if (key[i] != '-')
					{
						if (!validChars.Contains(key[i]) && key[i] != '*')
						{
							validKey = false;
						}
					}
				}
			}
			else
			{
				validKey = false;
			}
			if (!validKey)
			{
				Console.WriteLine("Cl� invalide.");
			}
			else
            {
				Console.WriteLine();
            }
			return validKey;
		}

		private static int Missing_count(string key)
		{
			int missing = 0;
			for (int i = 0; i < key.Length; i++)
			{
				if (key[i] == '*')
				{
					missing++;
				}
			}
			return missing;
		}

		private static List<int> Missing_positions(string key)
		{
			List<int> missingPosition = new List<int>();
			for (int i = 0; i < key.Length; i++)
			{
				if (key[i] == '*')
				{
					missingPosition.Add(i);
				}
			}
			return missingPosition;
		}

		private static string Replace_missing(char[] key, List<int> missingPos, string patternFinal)
		{
			char[] keyTest = new List<char>(key).ToArray();
			for (int j = 0; j < patternFinal.Length; j++)
			{
				keyTest[missingPos[j]] = patternFinal[j];
			}
			return new string(keyTest);
		}

		private static void Save_to_file(char[] key, List<List<string>> keysFound)
		{
			string fileName = Environment.GetEnvironmentVariable("userprofile") + "\\Desktop\\cl�s_trouv�es.txt";
			StreamWriter writer = new StreamWriter(fileName);
			writer.WriteLine("Cl� initiale: " + new string(key) + "\n");
			writer.WriteLine("Cl�s trouv�es: ");
			for (int i = 0; i < keysFound.Count; i++)
			{
				writer.WriteLine(keysFound[i][0] + " Version: " + keysFound[i][1]);
			}
			writer.Close();
			Console.WriteLine(new string(key));
		}

		private static void Start(char[] validChars, char[] key, List<int> missingPos,
			string pattern, int missing, List<List<string>> keysFound, string fileXml, string fileDll, string mspid)
		{
			if (missing == 1)
			{
				PKChecker pkc = new PKChecker();
				_ = Parallel.For(0, validChars.Length, i =>
				{
					string patternFinal = pattern + validChars[i];
					string keyTest = Replace_missing(key, missingPos, patternFinal);
					Console.Write("[+] Travail sur: " + keyTest + "\r");
					string resultat = pkc.Check_productKey(keyTest, fileXml, fileDll, mspid);
					if (resultat != "Invalid")
					{
						keysFound.Add(new List<string>() { keyTest, resultat });
						Save_to_file(key, keysFound);
						Cli.Found_keys(keysFound);
					}
				});
			}
			else
			{
				for (int i = 0; i < validChars.Length; i++)
				{
					string patternNew = pattern + validChars[i];
					Start(validChars, key, missingPos, patternNew, missing - 1, keysFound, fileXml, fileDll, mspid);
				}
			}
		}

		public static void Main()
		{
			bool validKey = false;
			string key = string.Empty;
			string mspid = "00000";
			string fileDll = ".\\pidgenx2.dll";
			string fileXml = ".\\Win10pkeyconfig.xrm-ms";
			List<List<string>> keysFound = new List<List<string>>();
			char[] validChars = new char[]
			{
			'B', 'C', 'D', 'F', 'G',
			'H', 'J', 'K', 'M', 'N',
			'P', 'Q', 'R', 'T', 'V',
			'W', 'X', 'Y', '2', '3',
			'4', '6', '7', '8', '9'
			};
			Cli.Intro(validChars);
            while (!validKey)
            {
                key = Cli.Get_key(validChars);
                validKey = Key_isvalid(validChars, key);
            }
			var watch = System.Diagnostics.Stopwatch.StartNew();
			int missing = Missing_count(key);
			List<int> missingPosition = Missing_positions(key);
            Start(validChars, key.ToCharArray(), missingPosition, string.Empty, missing, keysFound, fileXml, fileDll, mspid);
			watch.Stop();
			Console.WriteLine("Temps: " + watch.Elapsed + " ");
			Cli.End();
		}
	}
}

/*
 * Run 1: 12.98
 * Run 2: 13.53
 * Run 3: 12.72
 * Run 4: 13.04
 */