This repository has been archived by the owner on Sep 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
436 lines (418 loc) · 15.9 KB
/
Program.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
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
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Management;
using System.Collections;
namespace HID
{
internal class Program
{
[DllImport("iphlpapi.dll", CharSet = CharSet.Ansi)]
public static extern int GetAdaptersInfo(IntPtr intptr0, ref long long0);
[StructLayout(LayoutKind.Sequential)]
internal struct IpAddrString
{
internal IntPtr Next;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x10)]
internal string IpAddress;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x10)]
internal string IpMask;
internal uint Context;
}
internal struct IpAdapterInfo
{
internal const int MaxAdapterDescriptionLength = 0x80;
internal const int MaxAdapterNameLength = 0x100;
internal const int MaxAdapterAddressLength = 8;
internal IntPtr Next;
internal uint ComboIndex;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
internal string AdapterName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x84)]
internal string Description;
internal uint AddressLength;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
internal byte[] Address;
internal uint Index;
internal OldInterfaceType Type;
internal bool DhcpEnabled;
internal IntPtr CurrentIpAddress;
internal IpAddrString IpAddressList;
internal IpAddrString GatewayList;
internal IpAddrString DhcpServer;
[MarshalAs(UnmanagedType.Bool)]
internal bool HaveWins;
internal IpAddrString PrimaryWinsServer;
internal IpAddrString SecondaryWinsServer;
internal uint LeaseObtained;
internal uint LeaseExpires;
}
internal enum OldInterfaceType
{
Ethernet = 6,
Fddi = 15,
Loopback = 0x18,
Ppp = 0x17,
Slip = 0x1c,
TokenRing = 9,
Unknown = 0
}
private static void Main()
{
var value = GenerateHwid(true, false, true, false);
Console.WriteLine(value);
Console.ReadKey();
}
internal static string GetMac()
{
var text = string.Empty;
try
{
var value = (long)Marshal.SizeOf(typeof(IpAdapterInfo));
var intPtr = Marshal.AllocHGlobal(new IntPtr(value));
var adaptersInfo = GetAdaptersInfo(intPtr, ref value);
if (adaptersInfo == 111)
{
intPtr = Marshal.ReAllocHGlobal(intPtr, new IntPtr(value));
adaptersInfo = GetAdaptersInfo(intPtr, ref value);
}
if (adaptersInfo == 0)
{
var ptr = intPtr;
var ipAdapterInfo = (IpAdapterInfo)Marshal.PtrToStructure(ptr, typeof(IpAdapterInfo));
for (var i = 0; i < (long)(ulong)ipAdapterInfo.AddressLength; i++)
{
text = string.Concat(text, ipAdapterInfo.Address[i].ToString("X2"));
}
Marshal.FreeHGlobal(intPtr);
}
else
{
Marshal.FreeHGlobal(intPtr);
}
}
catch
{
// ignored
}
if (text == string.Empty)
{
text = "";
}
return text;
}
private static string GetProcessorId()
{
var text = string.Empty;
try
{
var managementClass = new ManagementClass("Win32_Processor");
var instances = managementClass.GetInstances();
using (var enumerator = instances.GetEnumerator())
{
while (enumerator.MoveNext())
{
var managementObject = (ManagementObject)enumerator.Current;
if (text != string.Empty) continue;
try
{
text = managementObject.Properties["ProcessorId"].Value.ToString();
if (text.Length != 0)
{
break;
}
text = string.Empty;
}
catch
{
// ignored
}
}
}
}
catch
{
// ignored
}
return text;
}
private static string GetMacAddress()
{
var text = string.Empty;
try
{
text = GetMac();
if (text.Length == 0)
{
text = string.Empty;
var managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
var instances = managementClass.GetInstances();
using (var enumerator = instances.GetEnumerator())
{
while (enumerator.MoveNext())
{
var managementObject = (ManagementObject)enumerator.Current;
if (text != string.Empty)
{
break;
}
try
{
if (managementObject["IPEnabled"] != null && (bool)managementObject["IPEnabled"] && managementObject["MacAddress"] != null && managementObject["MacAddress"].ToString().Length > 0)
{
text = managementObject["MacAddress"].ToString().ToUpper();
text = text.Replace(":", "");
}
}
catch
{
// ignored
}
}
}
}
}
catch
{
// ignored
}
return text;
}
private static string GetMotherBoardId()
{
try
{
return string.Concat(GetProduct(), "-", GetManufacturer(), "-", GetSerialNumber());
}
catch
{
return string.Empty;
}
}
private static string GetManufacturer()
{
string result;
try
{
var text = string.Empty;
var managementClass = new ManagementClass("Win32_BaseBoard");
var instances = managementClass.GetInstances();
using (var enumerator = instances.GetEnumerator())
{
while (enumerator.MoveNext())
{
var managementObject = (ManagementObject)enumerator.Current;
try
{
if (text == string.Empty && managementObject.Properties["Manufacturer"].Value != null)
{
text = managementObject.Properties["Manufacturer"].Value.ToString();
if (text.Length != 0)
{
break;
}
text = string.Empty;
}
}
catch
{
// ignored
}
}
}
result = text;
}
catch
{
result = string.Empty;
}
return result;
}
private static string GetSerialNumber()
{
string result;
try
{
var text = string.Empty;
var managementClass = new ManagementClass("Win32_BaseBoard");
var instances = managementClass.GetInstances();
using (var enumerator = instances.GetEnumerator())
{
while (enumerator.MoveNext())
{
var managementObject = (ManagementObject)enumerator.Current;
try
{
if (text == string.Empty && managementObject.Properties["SerialNumber"].Value != null)
{
text = managementObject.Properties["SerialNumber"].Value.ToString();
if (text.Length != 0)
{
break;
}
text = string.Empty;
}
}
catch
{
// ignored
}
}
}
result = text;
}
catch
{
result = string.Empty;
}
return result;
}
private static string GetProduct()
{
string result;
try
{
var text = string.Empty;
var managementClass = new ManagementClass("Win32_BaseBoard");
var instances = managementClass.GetInstances();
using (var enumerator = instances.GetEnumerator())
{
while (enumerator.MoveNext())
{
var managementObject = (ManagementObject)enumerator.Current;
try
{
if (managementObject.Properties["Product"].Value != null && text == string.Empty)
{
text = managementObject.Properties["Product"].Value.ToString();
if (text.Length != 0)
{
break;
}
text = string.Empty;
}
}
catch
{
// ignored
}
}
}
result = text;
}
catch
{
result = string.Empty;
}
return result;
}
private static string GetDiskId()
{
try
{
var arrayList = new ArrayList();
var managementObjectSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
using (var enumerator = managementObjectSearcher.Get().GetEnumerator())
{
while (enumerator.MoveNext())
{
var managementObject = (ManagementObject)enumerator.Current;
if (managementObject["DeviceID"] == null || managementObject["InterfaceType"] == null ||
managementObject["InterfaceType"].ToString() == "USB" ||
managementObject["InterfaceType"].ToString() == "1394") continue;
var flag = !(managementObject["MediaType"] != null && managementObject["MediaType"].ToString() == "Removable Media");
if (!flag) continue;
var obj = managementObject["SerialNumber"];
if (obj != null && obj.ToString().Trim() != string.Empty && obj.ToString()[0] != Convert.ToChar(31))
{
return obj.ToString().Trim();
}
arrayList.Add(managementObject["DeviceID"].ToString());
}
}
managementObjectSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
var managementObjectCollection = managementObjectSearcher.Get();
foreach (string b in arrayList)
{
using (var enumerator3 = managementObjectCollection.GetEnumerator())
{
while (enumerator3.MoveNext())
{
var managementObject2 = (ManagementObject)enumerator3.Current;
if (managementObject2["Tag"] == null) continue;
var a = managementObject2["Tag"].ToString();
if (a != b || managementObject2["SerialNumber"] == null) continue;
var obj2 = managementObject2["SerialNumber"];
if (obj2 != null && obj2.ToString() != string.Empty && obj2.ToString()[0] != Convert.ToChar(31))
{
return obj2.ToString().Trim().Replace(" ", "");
}
break;
}
}
}
}
catch
{
return string.Empty;
}
return string.Empty;
}
internal static string GenerateHwid(bool proccessor, bool mac, bool motherboard, bool disk)
{
var text = "";
RSACryptoServiceProvider.UseMachineKeyStore = true;
var md5 = MD5.Create();
if (proccessor)
{
var array = md5.ComputeHash(Encoding.Unicode.GetBytes(GetProcessorId()));
text += array[3].ToString("X2");
text = text + array[11].ToString("X2") + "-";
}
else
{
text = "85C1-";
}
if (mac)
{
var array2 = md5.ComputeHash(Encoding.Unicode.GetBytes(GetMacAddress()));
text += array2[3].ToString("X2");
text = text + array2[11].ToString("X2") + "-";
}
else
{
var array3 = md5.ComputeHash(Encoding.Unicode.GetBytes(text));
text += array3[15].ToString("X2");
text = text + array3[7].ToString("X2") + "-";
}
if (motherboard)
{
var array4 = md5.ComputeHash(Encoding.Unicode.GetBytes(GetMotherBoardId()));
text += array4[3].ToString("X2");
text = text + array4[11].ToString("X2") + "-";
}
else
{
var array5 = md5.ComputeHash(Encoding.Unicode.GetBytes(text));
text += array5[2].ToString("X2");
text = text + array5[14].ToString("X2") + "-";
}
if (disk)
{
var array6 = md5.ComputeHash(Encoding.Unicode.GetBytes(GetDiskId()));
text += array6[3].ToString("X2");
text = text + array6[11].ToString("X2") + "-";
}
else
{
var array7 = md5.ComputeHash(Encoding.Unicode.GetBytes(text));
text += array7[1].ToString("X2");
text = text + array7[9].ToString("X2") + "-";
}
var array8 = md5.ComputeHash(Encoding.Unicode.GetBytes(text));
text += array8[1].ToString("X2");
return text + array8[9].ToString("X2");
}
}
}