Skip to content

Instantly share code, notes, and snippets.

@ldmsys
Created June 4, 2023 02:33
Show Gist options
  • Save ldmsys/2fa1fcebc75ad69b0a626dd67d748e03 to your computer and use it in GitHub Desktop.
Save ldmsys/2fa1fcebc75ad69b0a626dd67d748e03 to your computer and use it in GitHub Desktop.
typedef LONG(WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
/* Ref: https://learn.microsoft.com/ko-kr/windows-server/get-started/kms-client-activation-keys */
void kms_activation(const char* server_addr) {
HMODULE hMod = GetModuleHandleW(L"ntdll.dll");
if (!hMod) {
fprintf(stderr, "Error: Loading ntdll.dll failed successfully");
return;
}
RtlGetVersionPtr fxPtr = (RtlGetVersionPtr)GetProcAddress(hMod, "RtlGetVersion");
int M, m, b;
if (fxPtr != NULL) {
RTL_OSVERSIONINFOW rovi = { 0 };
rovi.dwOSVersionInfoSize = sizeof(rovi);
if (fxPtr(&rovi) == 0) { // NO_ERROR
printf("Windows version: %ld.%ld build %ld\n", rovi.dwMajorVersion, rovi.dwMinorVersion, rovi.dwBuildNumber);
M = rovi.dwMajorVersion; m = rovi.dwMinorVersion; b = rovi.dwBuildNumber;
}
else {
fprintf(stderr, "Error: Getting Windows version failed successfully");
return;
}
} else {
fprintf(stderr, "Error: Getting Windows version failed successfully");
return;
}
DWORD dwType;
if (GetProductInfo(6, 2, 0, 0, &dwType))
printf("Windows edition: %lu\n", dwType);
else {
fprintf(stderr, "Error: Getting Windows edition failed successfully");
return;
}
const char* kmsKey = NULL;
if (M == 10 && m == 0) { // Windows 10/11 Series
switch (dwType) {
case PRODUCT_PROFESSIONAL: kmsKey = "W269N-WFGWX-YVC9B-4J6C9-T83GX"; break;
case PRODUCT_PROFESSIONAL_N: kmsKey = "MH37W-N47XK-V7XM9-C7227-GCQG9"; break;
case PRODUCT_PRO_WORKSTATION: kmsKey = "NRG8B-VKK3Q-CXVCJ-9G2XF-6Q84J"; break;
case PRODUCT_PRO_WORKSTATION_N: kmsKey = "9FNHH-K3HBT-3W4TD-6383H-6XYWF"; break;
case PRODUCT_EDUCATION: kmsKey = "6TP4R-GNPTD-KYYHQ-7B7DP-J447Y"; break;
case PRODUCT_EDUCATION_N: kmsKey = "YVWGF-BXNMC-HTQYQ-CPQ99-66QFC"; break;
case PRODUCT_ENTERPRISE: kmsKey = "NPPR9-FWDCX-D2C8J-H872K-2YT43"; break;
case PRODUCT_ENTERPRISE_N: kmsKey = "DPH2V-TTNVB-4X9Q3-TJR4H-KHJW4"; break;
case PRODUCT_ENTERPRISEG: kmsKey = "YYVX9-NTFWV-6MDM3-9PT4T-4M68B"; break;
case PRODUCT_ENTERPRISEGN: kmsKey = "44RPN-FTY23-9VTTB-MP9BX-T84FV"; break;
case PRODUCT_ENTERPRISE_S:
if (b == 10240 /* LTSB 2015 */) kmsKey = "44RPN-FTY23-9VTTB-MP9BX-T84FV";
else if (b == 14393 /* LTSB 2016 */) kmsKey = "DCPHK-NFMTC-H88MJ-PFHPY-QJ4BJ";
else if (b == 17763 /* LTSC 2019 */) kmsKey = "M7XTQ-FN8P6-TTKYV-9D4CC-J462D";
break;
case PRODUCT_ENTERPRISE_S_N:
if (b == 10240 /* LTSB 2015 */) kmsKey = "2F77B-TNFGY-69QQF-B8YKP-D69TJ";
else if (b == 14393 /* LTSB 2016 */) kmsKey = "QFFDN-GRT3P-VKWWX-X7T3R-8B639";
else if (b == 17763 /* LTSC 2019 */) kmsKey = "92NFX-8DJQP-P6BBQ-THF9C-7CG2H";
break;
case PRODUCT_STANDARD_A_SERVER_CORE: kmsKey = "N2KJX-J94YW-TQVFB-DG9YT-724CC"; break;
case PRODUCT_DATACENTER_A_SERVER_CORE: kmsKey = "6NMRW-2C8FM-D24W7-TQWMY-CWH2D"; break;
case PRODUCT_STANDARD_SERVER:
if (b == 14393 /* Server 2016 */) kmsKey = "WC2BQ-8NRM3-FDDYY-2BFGV-KHKQY";
else if (b == 17763 /* Server 2019 */) kmsKey = "N69G4-B89J2-4G8F4-WWYCC-J464C";
else if (b == 20348 /* Server 2022 */) kmsKey = "VDYBN-27WPP-V4HQT-9VMD4-VMK7H";
break;
case PRODUCT_DATACENTER_SERVER:
if (b == 14393 /* Server 2016 */) kmsKey = "CB7KF-BWN84-R7R2Y-793K2-8XDDG";
else if (b == 17763 /* Server 2019 */) kmsKey = "WMDGN-G9PQG-XVVXX-R3X43-63DFG";
else if (b == 20348 /* Server 2022 */) kmsKey = "WX4NM-KYWYW-QJJR4-XV3QB-6VM33";
break;
}
} else if (M == 6 && m == 3) { //Windows 8.1 Series
switch (dwType) {
case PRODUCT_PROFESSIONAL: kmsKey = "GCRJD-8NW9H-F2CDX-CCM8D-9D6T9"; break;
case PRODUCT_PROFESSIONAL_N: kmsKey = "HMCNV-VVBFX-7HMBH-CTY9B-B4FXY"; break;
case PRODUCT_ENTERPRISE: kmsKey = "MHF9N-XY6XB-WVXMC-BTDCT-MKKG7"; break;
case PRODUCT_ENTERPRISE_N: kmsKey = "TT4HM-HN7YT-62K67-RGRQJ-JFFXW"; break;
case PRODUCT_STANDARD_SERVER: kmsKey = "D2N9P-3P6X9-2R39C-7RTCD-MDVJX"; break;
case PRODUCT_DATACENTER_SERVER: kmsKey = "W3GGN-FT8W3-Y4M27-J84CP-Q3VJ9"; break;
}
} else if (M == 6 && m == 2) { //Windows 8 Series
switch (dwType) {
case PRODUCT_PROFESSIONAL: kmsKey = "NG4HW-VH26C-733KW-K6F98-J8CK4"; break;
case PRODUCT_PROFESSIONAL_N: kmsKey = "XCVCF-2NXM9-723PB-MHCB7-2RYQQ"; break;
case PRODUCT_ENTERPRISE: kmsKey = "32JNW-9KQ84-P47T8-D8GGY-CWCK7"; break;
case PRODUCT_ENTERPRISE_N: kmsKey = "JMNMF-RHW7P-DMY6X-RF3DR-X2BQT"; break;
case PRODUCT_STANDARD_SERVER: kmsKey = "BN3D2-R7TKB-3YPBD-8DRP2-27GG4"; break;
case PRODUCT_DATACENTER_SERVER: kmsKey = "48HP8-DN98B-MYWDG-T2DCC-8W83P"; break;
}
}
if(kmsKey == NULL) { // Undefined behavior
fprintf(stderr, "Error: Finding KMS Key failed successfully");
return;
}
FILE* f;
if ((f = fopen("C:\\DONTDO.TXT", "r")) != NULL) { // Don't Use KMS!!
fclose(f);
printf("KMS Key: %s. Stopping!", kmsKey);
return;
}
char cmdbuf[255];
sprintf(cmdbuf, "cscript %%windir%%\\system32\\slmgr.vbs /ipk %s", kmsKey);
system(cmdbuf);
sprintf(cmdbuf, "cscript %%windir%%\\system32\\slmgr.vbs /skms %s", server_addr);
system(cmdbuf);
strcpy(cmdbuf, "cscript %%windir%%\\system32\\slmgr.vbs /ato");
system(cmdbuf);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment