Skip to content

Instantly share code, notes, and snippets.

@0x410c
Created July 24, 2017 05:44
Show Gist options
  • Save 0x410c/75c03bcadae5ca8401e7b901aaeec458 to your computer and use it in GitHub Desktop.
Save 0x410c/75c03bcadae5ca8401e7b901aaeec458 to your computer and use it in GitHub Desktop.
generating hardware id on windows systems, c++ 54-7-2017
// generating hardware id on windows systems, c++ 54-7-2017
std::string getHDDSerial()
{
DWORD disk_serialINT;
GetVolumeInformationA(NULL, NULL, NULL, &disk_serialINT, NULL, NULL, NULL, NULL);
return std::to_string(disk_serialINT);
}
std::string getMac()
{
char data[4096];
ZeroMemory(data, 4096);
unsigned long len = 4000;
PIP_ADAPTER_INFO pinfo = (PIP_ADAPTER_INFO)data;
char sbuf[20];
std::string sret = "";
DWORD ret = GetAdaptersInfo(pinfo, &len);
if (ret != ERROR_SUCCESS)
{
return NULL;
}
for (int k = 0; k < 5; k++) {
sprintf_s(sbuf, "%02X-", pinfo->Address[k]);
sret += sbuf;
}
sprintf_s(sbuf, "%02X", pinfo->Address[5]);
sret += sbuf;
return(sret);
}
char* HashMD5(char* data, DWORD *result)
{
DWORD dwStatus = 0;
DWORD cbHash = 16;
int i = 0;
HCRYPTPROV cryptProv;
HCRYPTHASH cryptHash;
BYTE hash[16];
char *hex = "0123456789abcdef";
char *strHash;
strHash = (char*)malloc(500);
memset(strHash, '\0', 500);
if (!CryptAcquireContext(&cryptProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
{
dwStatus = GetLastError();
printf("CryptAcquireContext failed: %d\n", dwStatus);
*result = dwStatus;
return NULL;
}
if (!CryptCreateHash(cryptProv, CALG_MD5, 0, 0, &cryptHash))
{
dwStatus = GetLastError();
printf("CryptCreateHash failed: %d\n", dwStatus);
CryptReleaseContext(cryptProv, 0);
*result = dwStatus;
return NULL;
}
if (!CryptHashData(cryptHash, (BYTE*)data, strlen(data), 0))
{
dwStatus = GetLastError();
printf("CryptHashData failed: %d\n", dwStatus);
CryptReleaseContext(cryptProv, 0);
CryptDestroyHash(cryptHash);
*result = dwStatus;
return NULL;
}
if (!CryptGetHashParam(cryptHash, HP_HASHVAL, hash, &cbHash, 0))
{
dwStatus = GetLastError();
printf("CryptGetHashParam failed: %d\n", dwStatus);
CryptReleaseContext(cryptProv, 0);
CryptDestroyHash(cryptHash);
*result = dwStatus;
return NULL;
}
for (i = 0; i < cbHash; i++)
{
strHash[i * 2] = hex[hash[i] >> 4];
strHash[(i * 2) + 1] = hex[hash[i] & 0xF];
}
CryptReleaseContext(cryptProv, 0);
CryptDestroyHash(cryptHash);
return strHash;
}
std::string getHardwareID()
{
DWORD err;
GetPrimaryIp();
std::string hd = getHDDSerial() + ":" + getMac();
std::string hash = HashMD5((char*)hd.c_str(), &err);
return hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment