-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.cpp
More file actions
1918 lines (1548 loc) · 74.2 KB
/
main.cpp
File metadata and controls
1918 lines (1548 loc) · 74.2 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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
#include <memory>
#include <array>
#include <Windows.h>
#include <Iphlpapi.h>
#include <intrin.h>
#include <comdef.h>
#include <Wbemidl.h>
#include <SetupAPI.h>
#include <devguid.h>
#include <initguid.h>
#include <dxgi.h>
#include <dxgi1_6.h>
#include <d3d11.h>
#include <ntddscsi.h>
#include <winioctl.h>
#include <atlbase.h>
#include <shlwapi.h>
#include <Wincrypt.h>
#include <sddl.h>
#include <bcrypt.h>
#include <ncrypt.h>
#include <winternl.h>
#include <ks.h>
#include <wbemcli.h>
// Define TPM-related structures and functions if not already defined
#ifndef TBS_SUCCESS
#define TBS_SUCCESS 0
typedef UINT32 TBS_RESULT;
typedef UINT32 TBS_HCONTEXT;
typedef struct TBS_CONTEXT_PARAMS {
UINT32 version;
BOOL asynchronous;
} TBS_CONTEXT_PARAMS;
#define TBS_CONTEXT_VERSION_ONE 1
#define TBS_CONTEXT_VERSION_TWO 2
// TPM Base Service function prototypes
extern "C" {
__declspec(dllimport) TBS_RESULT __stdcall Tbsi_Context_Create(
_In_ const TBS_CONTEXT_PARAMS* pContextParams,
_Out_ TBS_HCONTEXT* phContext
);
__declspec(dllimport) TBS_RESULT __stdcall Tbsi_Context_Close(
_In_ TBS_HCONTEXT hContext
);
__declspec(dllimport) TBS_RESULT __stdcall Tbsi_GetDeviceInfo(
_In_ UINT32 Size,
_Out_ PVOID Information
);
__declspec(dllimport) TBS_RESULT __stdcall Tbsi_GetRandom(
_In_ TBS_HCONTEXT hContext,
_Out_ BYTE* pRgbRandom,
_In_ UINT32 cbRandom
);
__declspec(dllimport) TBS_RESULT __stdcall Tbsi_GetTpmProperty(
_In_ TBS_HCONTEXT hContext,
_In_ UINT32 PropertyId,
_In_ UINT32 PropertySubId,
_In_ UINT32 Size,
_Out_ PBYTE PropertyValue
);
}
#endif
// TPM-related definitions
#define TPM_AVAILABLE
#define TPM_BASE 0x0
#define TPM_RESULT_COUNT 0x8
#define TPM_ORD_GetRandom 0x46
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "wbemuuid.lib")
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "setupapi.lib")
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "crypt32.lib")
#pragma comment(lib, "shlwapi.lib")
#pragma comment(lib, "tbs.lib")
#pragma comment(lib, "bcrypt.lib")
#pragma comment(lib, "ncrypt.lib")
// SMBIOS table access definitions
#define SMBIOS_HARDWARE_SECURITY 0x25
#define SMBIOS_SYSTEM_INFORMATION 0x1
#define SMBIOS_BIOS_INFORMATION 0x0
// IOCTL codes for disk operations
#ifndef IOCTL_STORAGE_QUERY_PROPERTY
#define IOCTL_STORAGE_QUERY_PROPERTY 0x2d1400
#endif
enum class IdType {
HARDWARE, // Physical hardware identifiers that typically won't change
SOFTWARE // Software-based identifiers that can change with reinstalls/resets
};
class HWIDGrabber {
private:
std::string GetStringFromBytes(const std::vector<BYTE>& bytes) {
std::stringstream ss;
for (size_t i = 0; i < bytes.size(); i++) {
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(bytes[i]);
if (i < bytes.size() - 1) ss << "-";
}
return ss.str();
}
std::string GetWMIProperty(IWbemClassObject* obj, const wchar_t* property) {
VARIANT vtProp;
HRESULT hr = obj->Get(property, 0, &vtProp, 0, 0);
std::string result;
if (SUCCEEDED(hr)) {
if (vtProp.vt == VT_BSTR && vtProp.bstrVal != nullptr) {
_bstr_t bstr_t(vtProp.bstrVal, false);
result = static_cast<const char*>(bstr_t);
}
VariantClear(&vtProp);
}
return result;
}
template<class T>
void SafeRelease(T** ppT) {
if (*ppT) {
(*ppT)->Release();
*ppT = nullptr;
}
}
// More secure hashing using BCrypt (modern Windows crypto API)
std::string BCryptHashString(const std::string& input) {
BCRYPT_ALG_HANDLE hAlg = NULL;
BCRYPT_HASH_HANDLE hHash = NULL;
NTSTATUS status = 0;
DWORD cbData = 0, cbHash = 0, cbHashObject = 0;
PBYTE pbHashObject = NULL;
PBYTE pbHash = NULL;
std::string hashStr = "";
std::stringstream ss;
// Open algorithm provider
status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_SHA256_ALGORITHM, NULL, 0);
if (!BCRYPT_SUCCESS(status)) {
goto Cleanup;
}
// Get hash object size
status = BCryptGetProperty(hAlg, BCRYPT_OBJECT_LENGTH, (PBYTE)&cbHashObject, sizeof(DWORD), &cbData, 0);
if (!BCRYPT_SUCCESS(status)) {
goto Cleanup;
}
pbHashObject = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbHashObject);
if (NULL == pbHashObject) {
goto Cleanup;
}
// Get hash length
status = BCryptGetProperty(hAlg, BCRYPT_HASH_LENGTH, (PBYTE)&cbHash, sizeof(DWORD), &cbData, 0);
if (!BCRYPT_SUCCESS(status)) {
goto Cleanup;
}
pbHash = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbHash);
if (NULL == pbHash) {
goto Cleanup;
}
// Create hash
status = BCryptCreateHash(hAlg, &hHash, pbHashObject, cbHashObject, NULL, 0, 0);
if (!BCRYPT_SUCCESS(status)) {
goto Cleanup;
}
// Hash data
status = BCryptHashData(hHash, (PUCHAR)input.c_str(), static_cast<ULONG>(input.length()), 0);
if (!BCRYPT_SUCCESS(status)) {
goto Cleanup;
}
// Finalize hash
status = BCryptFinishHash(hHash, pbHash, cbHash, 0);
if (!BCRYPT_SUCCESS(status)) {
goto Cleanup;
}
// Format hash as hex string
for (DWORD i = 0; i < cbHash; i++) {
ss << std::hex << std::setw(2) << std::setfill('0') << (int)pbHash[i];
}
hashStr = ss.str();
Cleanup:
if (hHash) BCryptDestroyHash(hHash);
if (hAlg) BCryptCloseAlgorithmProvider(hAlg, 0);
if (pbHashObject) HeapFree(GetProcessHeap(), 0, pbHashObject);
if (pbHash) HeapFree(GetProcessHeap(), 0, pbHash);
return hashStr;
}
// Get SMBIOS information directly for more reliable hardware identification
std::string GetSMBIOSData() {
DWORD smbiosDataSize = 0;
std::vector<BYTE> smbiosData;
std::stringstream result;
// Get size of SMBIOS data
DWORD smbiosSize = GetSystemFirmwareTable('RSMB', 0, NULL, 0);
if (smbiosSize > 0) {
smbiosData.resize(smbiosSize);
DWORD bytesRead = GetSystemFirmwareTable('RSMB', 0, smbiosData.data(), smbiosSize);
if (bytesRead > 0) {
// Raw SMBIOS data fingerprint - harder to spoof than WMI
result << "SMBIOS Data Signature: " << GetStringFromBytes(std::vector<BYTE>(smbiosData.begin(), smbiosData.begin() + 16)) << std::endl;
// Extract SMBIOS version
if (smbiosData.size() > 0x08) {
result << "SMBIOS Version: "
<< static_cast<int>(smbiosData[0x06]) << "."
<< static_cast<int>(smbiosData[0x07]) << std::endl;
}
// Process structures after header
BYTE* ptr = smbiosData.data() + 8; // Skip header
BYTE* endPtr = smbiosData.data() + smbiosSize;
while (ptr < endPtr) {
BYTE type = *ptr;
BYTE length = *(ptr + 1);
if (type == 0 && length < 4) break; // End of table
if (type == SMBIOS_SYSTEM_INFORMATION && length >= 8) {
result << "System Manufacturer: " << (const char*)(ptr + 4) << std::endl;
result << "System Product: " << (const char*)(ptr + 5) << std::endl;
result << "System Serial: " << (const char*)(ptr + 7) << std::endl;
}
// Find the string area ending (double NUL)
BYTE* strPtr = ptr + length;
while (strPtr < endPtr && !(*strPtr == 0 && *(strPtr + 1) == 0)) {
strPtr++;
}
// Move to the next structure
ptr = strPtr + 2;
}
}
}
return result.str();
}
// Get TPM identifier if available - extremely difficult to spoof
std::string GetTPMInfo() {
std::stringstream result;
TBS_CONTEXT_PARAMS contextParams;
TBS_HCONTEXT hContext = 0;
UINT32 tpmVersion = 0;
// Initialize the context parameters for TPM 2.0
contextParams.version = TBS_CONTEXT_VERSION_TWO;
contextParams.asynchronous = FALSE;
// Attempt to create a context with the TPM Base Services
HRESULT hr = Tbsi_Context_Create(&contextParams, &hContext);
if (SUCCEEDED(hr)) {
// Get TPM version
hr = Tbsi_GetDeviceInfo(sizeof(UINT32), &tpmVersion);
if (SUCCEEDED(hr)) {
result << "TPM Version: " << (tpmVersion == 1 ? "1.2" : "2.0") << std::endl;
// Get TPM properties (2.0 specific)
if (tpmVersion == 2) {
UINT32 tpmProps[2] = { 0 }; // Manufacturer and version info
UINT32 propSize = sizeof(tpmProps);
hr = Tbsi_GetTpmProperty(hContext, 1, TPM_BASE, propSize, (PBYTE)tpmProps);
if (SUCCEEDED(hr)) {
result << "TPM Manufacturer: 0x" << std::hex << tpmProps[0] << std::endl;
result << "TPM Version: 0x" << std::hex << tpmProps[1] << std::endl;
}
}
// Get a random value from the TPM as a unique identifier that's hardware-bound
BYTE randomBytes[32] = { 0 };
hr = Tbsi_GetRandom(hContext, randomBytes, sizeof(randomBytes));
if (SUCCEEDED(hr)) {
std::vector<BYTE> randomVec(randomBytes, randomBytes + sizeof(randomBytes));
result << "TPM Random ID: " << GetStringFromBytes(randomVec) << std::endl;
}
}
// Close TPM context
Tbsi_Context_Close(hContext);
}
else {
result << "TPM not available or accessible" << std::endl;
}
return result.str();
}
std::string ExecuteWMICCommand(const std::string& command) {
std::string result;
char buffer[128];
std::string cmd = "wmic " + command + " 2>&1";
FILE* pipe = _popen(cmd.c_str(), "r");
if (!pipe) {
return "Error executing WMIC command";
}
while (!feof(pipe)) {
if (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
result += buffer;
}
}
_pclose(pipe);
return result;
}
public:
std::string HashString(const std::string& input) {
return BCryptHashString(input);
}
// Get CPU Information with low-level access (HARDWARE)
std::string GetCPUInfo() {
int cpuInfo[4] = { 0 };
std::array<char, 64> brandString = { 0 };
__cpuid(cpuInfo, 0x80000002);
memcpy(brandString.data(), cpuInfo, sizeof(cpuInfo));
__cpuid(cpuInfo, 0x80000003);
memcpy(brandString.data() + 16, cpuInfo, sizeof(cpuInfo));
__cpuid(cpuInfo, 0x80000004);
memcpy(brandString.data() + 32, cpuInfo, sizeof(cpuInfo));
__cpuid(cpuInfo, 1);
DWORD cpuId = cpuInfo[0];
__cpuid(cpuInfo, 1);
DWORD signatureEax = cpuInfo[0]; // Contains stepping, model, family
DWORD featureFlags = cpuInfo[3]; // EDX features
// Get CPU microcode version (more specific info)
unsigned int microcode = 0;
// Get CPU thermal and power info (harder to spoof)
__cpuid(cpuInfo, 0x6);
DWORD thermalInfo = cpuInfo[0];
// Read CPU extended features
__cpuid(cpuInfo, 7);
DWORD extFeatures = cpuInfo[1]; // EBX: Extended Features
std::stringstream ss;
ss << "CPU: " << brandString.data() << std::endl;
ss << "CPU ID: " << std::hex << std::uppercase << cpuId << std::endl;
ss << "Signature: " << std::hex << std::uppercase << signatureEax << std::endl;
ss << "Features: " << std::hex << std::uppercase << featureFlags << std::endl;
ss << "Microcode: " << std::hex << std::uppercase << microcode << std::endl;
ss << "Thermal Info: " << std::hex << std::uppercase << thermalInfo << std::endl;
ss << "Extended Features: " << std::hex << std::uppercase << extFeatures;
return ss.str();
}
// Get CPU hardware identifier (stabilized version with deeper hardware access)
std::string GetCPUHardwareID() {
int cpuInfo[4] = { 0 };
std::array<char, 64> brandString = { 0 };
__cpuid(cpuInfo, 0x80000002);
memcpy(brandString.data(), cpuInfo, sizeof(cpuInfo));
__cpuid(cpuInfo, 0x80000003);
memcpy(brandString.data() + 16, cpuInfo, sizeof(cpuInfo));
__cpuid(cpuInfo, 0x80000004);
memcpy(brandString.data() + 32, cpuInfo, sizeof(cpuInfo));
// Get detailed processor information - family, model, stepping
__cpuid(cpuInfo, 1);
int family = ((cpuInfo[0] >> 8) & 0xF) + ((cpuInfo[0] >> 20) & 0xFF); // Base + Extended Family
int model = ((cpuInfo[0] >> 4) & 0xF) | ((cpuInfo[0] >> 12) & 0xF0); // Base + Extended Model
int stepping = cpuInfo[0] & 0xF;
DWORD signature = cpuInfo[0];
// Additional CPU features that are hardware-specific and difficult to spoof
DWORD features = cpuInfo[3]; // Standard features
// Extended processor info
__cpuid(cpuInfo, 7);
DWORD extFeatures = cpuInfo[1]; // EBX register contains newer features
// Format as a stable hardware ID string with extended info
std::stringstream cpuIdStream;
cpuIdStream << std::string(brandString.data()) << "-"
<< std::hex << std::uppercase
<< family << model << stepping << "-"
<< signature << "-"
<< features << "-"
<< extFeatures;
return cpuIdStream.str();
}
// Motherboard Information (HARDWARE)
std::string GetMotherboardInfo() {
HRESULT hres;
std::string result = "Motherboard Info (HARDWARE):\n";
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres)) return "Failed to initialize COM library";
IWbemLocator* pLoc = nullptr;
hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&pLoc);
if (FAILED(hres)) {
CoUninitialize();
return "Failed to create IWbemLocator object";
}
IWbemServices* pSvc = nullptr;
hres = pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc);
if (FAILED(hres)) {
pLoc->Release();
CoUninitialize();
return "Could not connect to WMI server";
}
hres = CoSetProxyBlanket(
pSvc,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE
);
if (FAILED(hres)) {
pSvc->Release();
pLoc->Release();
CoUninitialize();
return "Could not set proxy blanket";
}
IEnumWbemClassObject* pEnumerator = nullptr;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_BaseBoard"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator
);
if (FAILED(hres)) {
pSvc->Release();
pLoc->Release();
CoUninitialize();
return "Query for motherboard failed";
}
IWbemClassObject* pclsObj = nullptr;
ULONG uReturn = 0;
while (pEnumerator) {
hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (uReturn == 0) break;
std::string manufacturer = GetWMIProperty(pclsObj, L"Manufacturer");
std::string product = GetWMIProperty(pclsObj, L"Product");
std::string serialNumber = GetWMIProperty(pclsObj, L"SerialNumber");
result += " Manufacturer: " + manufacturer + "\n";
result += " Model: " + product + "\n";
result += " Serial Number: " + serialNumber + "\n";
pclsObj->Release();
}
// Get BIOS information
pEnumerator->Release();
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_BIOS"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator
);
if (SUCCEEDED(hres)) {
while (pEnumerator) {
hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (uReturn == 0) break;
std::string biosVersion = GetWMIProperty(pclsObj, L"Version");
std::string biosSerialNumber = GetWMIProperty(pclsObj, L"SerialNumber");
std::string manufacturerBIOS = GetWMIProperty(pclsObj, L"Manufacturer");
result += "BIOS Info (HARDWARE):\n";
result += " Manufacturer: " + manufacturerBIOS + "\n";
result += " Version: " + biosVersion + "\n";
result += " Serial Number: " + biosSerialNumber + "\n";
pclsObj->Release();
}
}
pEnumerator->Release();
pSvc->Release();
pLoc->Release();
CoUninitialize();
return result;
}
// Get motherboard hardware ID using direct SMBIOS access
std::string GetMotherboardHardwareID() {
HRESULT hres;
std::string motherboardId = "";
// First try using direct SMBIOS access (harder to spoof than WMI)
DWORD smbiosSize = GetSystemFirmwareTable('RSMB', 0, NULL, 0);
if (smbiosSize > 0) {
std::vector<BYTE> smbiosData(smbiosSize);
DWORD bytesRead = GetSystemFirmwareTable('RSMB', 0, smbiosData.data(), smbiosSize);
if (bytesRead > 0) {
// Process structures to find System Information (Type 1)
BYTE* ptr = smbiosData.data() + 8; // Skip header
BYTE* endPtr = smbiosData.data() + smbiosSize;
std::string manufacturer, product, serialNumber;
while (ptr < endPtr) {
BYTE type = *ptr;
BYTE length = *(ptr + 1);
if (type == 0 && length < 4) break; // End of table
if (type == SMBIOS_SYSTEM_INFORMATION && length >= 8) {
// Extract string pointers
BYTE manufacturerPtr = *(ptr + 4);
BYTE productPtr = *(ptr + 5);
BYTE serialPtr = *(ptr + 7);
// Find the string table
BYTE* strPtr = ptr + length;
// Extract strings
std::vector<std::string> strings;
std::string currentStr;
while (strPtr < endPtr) {
if (*strPtr == 0) {
strings.push_back(currentStr);
currentStr.clear();
// Check for end of string list (double null)
if (*(strPtr + 1) == 0) break;
}
else {
currentStr += *strPtr;
}
strPtr++;
}
// Assign values from string index
if (manufacturerPtr > 0 && manufacturerPtr <= strings.size())
manufacturer = strings[manufacturerPtr - 1];
if (productPtr > 0 && productPtr <= strings.size())
product = strings[productPtr - 1];
if (serialPtr > 0 && serialPtr <= strings.size())
serialNumber = strings[serialPtr - 1];
// Combine to form a unique ID
motherboardId = manufacturer + "-" + product + "-" + serialNumber;
break;
}
// Find the next structure
BYTE* strPtr = ptr + length;
while (strPtr < endPtr && !(*strPtr == 0 && *(strPtr + 1) == 0)) {
strPtr++;
}
ptr = strPtr + 2;
}
}
}
// Fall back to WMI if SMBIOS access didn't yield results
if (motherboardId.empty()) {
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres)) return "";
IWbemLocator* pLoc = nullptr;
hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&pLoc);
if (FAILED(hres)) {
CoUninitialize();
return "";
}
IWbemServices* pSvc = nullptr;
hres = pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc);
if (FAILED(hres)) {
pLoc->Release();
CoUninitialize();
return "";
}
hres = CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL,
RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
if (FAILED(hres)) {
pSvc->Release();
pLoc->Release();
CoUninitialize();
return "";
}
// Query motherboard data
IEnumWbemClassObject* pEnumerator = nullptr;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT Manufacturer, Product, SerialNumber FROM Win32_BaseBoard"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator
);
if (SUCCEEDED(hres)) {
IWbemClassObject* pclsObj = nullptr;
ULONG uReturn = 0;
if (pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn) == S_OK && uReturn > 0) {
std::string manufacturer = GetWMIProperty(pclsObj, L"Manufacturer");
std::string product = GetWMIProperty(pclsObj, L"Product");
std::string serialNumber = GetWMIProperty(pclsObj, L"SerialNumber");
// Combine to form a unique ID
motherboardId = manufacturer + "-" + product + "-" + serialNumber;
pclsObj->Release();
}
pEnumerator->Release();
}
// Cleanup
pSvc->Release();
pLoc->Release();
CoUninitialize();
}
return motherboardId;
}
// Hard Disk Information (HARDWARE)
std::string GetDiskInfo() {
std::string result = "Disk Information (HARDWARE):\n";
HRESULT hres;
// Initialize COM
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres)) return "Failed to initialize COM library";
// Initialize WMI
IWbemLocator* pLoc = nullptr;
hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&pLoc);
if (FAILED(hres)) {
CoUninitialize();
return "Failed to create IWbemLocator object";
}
// Connect to WMI
IWbemServices* pSvc = nullptr;
hres = pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc);
if (FAILED(hres)) {
pLoc->Release();
CoUninitialize();
return "Could not connect to WMI server";
}
// Set security levels
hres = CoSetProxyBlanket(
pSvc,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE
);
if (FAILED(hres)) {
pSvc->Release();
pLoc->Release();
CoUninitialize();
return "Could not set proxy blanket";
}
// Query disk drive data
IEnumWbemClassObject* pEnumerator = nullptr;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_DiskDrive"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator
);
if (FAILED(hres)) {
pSvc->Release();
pLoc->Release();
CoUninitialize();
return "Query for disk drives failed";
}
// Get the data
IWbemClassObject* pclsObj = nullptr;
ULONG uReturn = 0;
int diskIndex = 0;
while (pEnumerator) {
hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (uReturn == 0) break;
// Get properties
std::string model = GetWMIProperty(pclsObj, L"Model");
std::string serialNumber = GetWMIProperty(pclsObj, L"SerialNumber");
std::string diskInterface = GetWMIProperty(pclsObj, L"InterfaceType");
std::string sizeStr = GetWMIProperty(pclsObj, L"Size");
result += " Disk " + std::to_string(diskIndex++) + ":\n";
result += " Model: " + model + "\n";
result += " Serial: " + serialNumber + "\n";
result += " Interface: " + diskInterface + "\n";
if (!sizeStr.empty()) {
uint64_t sizeBytes = std::stoull(sizeStr);
uint64_t sizeGB = sizeBytes / (1024 * 1024 * 1024);
result += " Size: " + std::to_string(sizeGB) + " GB\n";
}
pclsObj->Release();
}
// Cleanup
pEnumerator->Release();
pSvc->Release();
pLoc->Release();
CoUninitialize();
return result;
}
// Get disk hardware ID using direct device I/O control
std::string GetDiskHardwareID() {
std::stringstream diskIdStream;
// Try direct I/O control first - much harder to spoof
for (char driveLetter = 'C'; driveLetter <= 'Z'; driveLetter++) {
std::string physicalDrivePath = "\\\\.\\PhysicalDrive0"; // Start with primary drive
// Open the physical drive
HANDLE hDrive = CreateFileA(
physicalDrivePath.c_str(),
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL
);
if (hDrive != INVALID_HANDLE_VALUE) {
// Get device properties using IOCTL
STORAGE_PROPERTY_QUERY query;
DWORD bytesReturned = 0;
char buffer[1024] = { 0 };
query.PropertyId = StorageDeviceProperty;
query.QueryType = PropertyStandardQuery;
if (DeviceIoControl(
hDrive,
IOCTL_STORAGE_QUERY_PROPERTY,
&query,
sizeof(query),
buffer,
sizeof(buffer),
&bytesReturned,
NULL)) {
STORAGE_DEVICE_DESCRIPTOR* deviceDesc = (STORAGE_DEVICE_DESCRIPTOR*)buffer;
// Extract model, serial number, and firmware from the descriptor
std::string vendor, product, serial;
if (deviceDesc->VendorIdOffset > 0 && buffer[deviceDesc->VendorIdOffset] != '\0') {
vendor = &buffer[deviceDesc->VendorIdOffset];
}
if (deviceDesc->ProductIdOffset > 0 && buffer[deviceDesc->ProductIdOffset] != '\0') {
product = &buffer[deviceDesc->ProductIdOffset];
}
if (deviceDesc->SerialNumberOffset > 0 && buffer[deviceDesc->SerialNumberOffset] != '\0') {
serial = &buffer[deviceDesc->SerialNumberOffset];
}
if (!product.empty() && !serial.empty()) {
diskIdStream << product << "-" << serial << ";";
}
}
// Try SCSI INQUIRY for additional details
SCSI_PASS_THROUGH_DIRECT sptd;
char inquiryBuffer[256] = { 0 };
ZeroMemory(&sptd, sizeof(SCSI_PASS_THROUGH_DIRECT));
sptd.Length = sizeof(SCSI_PASS_THROUGH_DIRECT);
sptd.CdbLength = 6;
sptd.DataIn = SCSI_IOCTL_DATA_IN;
sptd.DataTransferLength = sizeof(inquiryBuffer);
sptd.DataBuffer = inquiryBuffer;
sptd.TimeOutValue = 2;
// INQUIRY command
sptd.Cdb[0] = 0x12; // INQUIRY
sptd.Cdb[4] = sizeof(inquiryBuffer);
if (DeviceIoControl(
hDrive,
IOCTL_SCSI_PASS_THROUGH_DIRECT,
&sptd,
sizeof(SCSI_PASS_THROUGH_DIRECT),
&sptd,
sizeof(SCSI_PASS_THROUGH_DIRECT),
&bytesReturned,
NULL)) {
// Extract vendor and product data from inquiry response
char vendor[9] = { 0 };
char product[17] = { 0 };
memcpy(vendor, inquiryBuffer + 8, 8);
memcpy(product, inquiryBuffer + 16, 16);
// Trim spaces
std::string vendorStr(vendor);
std::string productStr(product);
if (!vendorStr.empty() && !productStr.empty()) {
diskIdStream << vendorStr << "-" << productStr << ";";
}
}
CloseHandle(hDrive);
}
break; // For now only check primary drive
}
// Fallback to WMI if direct access yields no results
if (diskIdStream.str().empty()) {
HRESULT hres;
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres)) return "";
IWbemLocator* pLoc = nullptr;
hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&pLoc);
if (FAILED(hres)) {
CoUninitialize();
return "";
}
IWbemServices* pSvc = nullptr;
hres = pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc);
if (FAILED(hres)) {
pLoc->Release();
CoUninitialize();
return "";
}
hres = CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL,
RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
if (FAILED(hres)) {
pSvc->Release();
pLoc->Release();
CoUninitialize();
return "";
}
IEnumWbemClassObject* pEnumerator = nullptr;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT Model, SerialNumber FROM Win32_DiskDrive"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator
);
if (SUCCEEDED(hres)) {
IWbemClassObject* pclsObj = nullptr;
ULONG uReturn = 0;
while (pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn) == S_OK && uReturn > 0) {
std::string model = GetWMIProperty(pclsObj, L"Model");
std::string serialNumber = GetWMIProperty(pclsObj, L"SerialNumber");
if (!serialNumber.empty()) {
diskIdStream << model << "-" << serialNumber << ";";
}
pclsObj->Release();
}
pEnumerator->Release();
}
pSvc->Release();
pLoc->Release();
CoUninitialize();
}
return diskIdStream.str();
}
// MAC Address Information (HARDWARE)
std::string GetMACAddresses() {
std::string result = "Network Adapters (HARDWARE):\n";
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
pAdapterInfo = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));
if (pAdapterInfo == NULL) return "Error allocating memory for adapter info";
// Make an initial call to GetAdaptersInfo to get the necessary size
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
free(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO*)malloc(ulOutBufLen);
if (pAdapterInfo == NULL) return "Error allocating memory for adapter info";
}
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
pAdapter = pAdapterInfo;
int i = 0;
while (pAdapter) {
result += " Adapter " + std::to_string(i++) + ":\n";
result += " Description: " + std::string(pAdapter->Description) + "\n";
// Format MAC address
std::stringstream macStream;
for (UINT j = 0; j < pAdapter->AddressLength; j++) {
if (j > 0) macStream << "-";
macStream << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(pAdapter->Address[j]);
}
result += " MAC Address: " + macStream.str() + "\n";
pAdapter = pAdapter->Next;
}
}
else {
result += " GetAdaptersInfo failed with error: " + std::to_string(dwRetVal) + "\n";
}
if (pAdapterInfo) free(pAdapterInfo);
return result;
}