WindowsXPKg/src/xp.cpp

253 lines
7.3 KiB
C++
Raw Normal View History

/*
Windows XP CD Key Verification/Generator v0.03
by z22
Compile with OpenSSL libs, modify to suit your needs.
2019-08-22 23:35:39 +03:00
http://gnuwin32.sourceforge.net/packages/openssl.htm
2019-08-21 18:29:48 +03:00
History:
0.03 Stack corruptionerror on exit fixed (now pkey is large enough)
More Comments added
0.02 Changed name the *.cpp;
Fixed minor bugs & Make it compilable on VC++
0.01 First version compilable MingW
2019-08-21 18:29:48 +03:00
*/
2019-08-21 18:29:48 +03:00
2023-06-01 19:24:07 +03:00
#include "header.h"
2023-06-01 16:09:22 +03:00
/* Unpacks the Windows XP Product Key. */
2023-06-04 16:32:05 +03:00
void unpackXP(QWORD (&pRaw)[2], DWORD &pSerial, DWORD &pHash, QWORD &pSignature) {
2023-06-01 16:09:22 +03:00
// We're assuming that the quantity of information within the product key is at most 114 bits.
// log2(24^25) = 114.
// Serial = Bits [0..30] -> 31 bits
2023-06-04 15:06:51 +03:00
pSerial = FIRSTNBITS(pRaw[0], 31);
2023-06-01 16:09:22 +03:00
// Hash (e) = Bits [31..58] -> 28 bits
2023-06-04 16:32:05 +03:00
pHash = NEXTNBITS(pRaw[0], 28, 31);
2023-06-01 16:09:22 +03:00
// Signature (s) = Bits [59..113] -> 55 bits
2023-06-04 16:32:05 +03:00
pSignature = FIRSTNBITS(pRaw[1], 51) << 5 | NEXTNBITS(pRaw[0], 5, 59);
2019-08-21 18:29:48 +03:00
}
2023-06-01 16:09:22 +03:00
/* Packs the Windows XP Product Key. */
2023-06-04 16:32:05 +03:00
void packXP(QWORD (&pRaw)[2], DWORD pSerial, DWORD pHash, QWORD pSignature) {
pRaw[0] = FIRSTNBITS(pSignature, 5) << 59 | FIRSTNBITS(pHash, 28) << 31 | pSerial;
// sig is 56 bits long, 5 of them are used -> 51 bits remaining
pRaw[1] = NEXTNBITS(pSignature, 51, 5);
2019-08-21 18:29:48 +03:00
}
2023-06-01 16:09:22 +03:00
/* Verify Product Key */
2023-06-04 14:54:22 +03:00
bool verifyXPKey(EC_GROUP *eCurve, EC_POINT *generator, EC_POINT *publicKey, char (&cdKey)[25]) {
2023-06-01 16:09:22 +03:00
BN_CTX *context = BN_CTX_new();
2023-06-01 16:09:22 +03:00
// Convert Base24 CD-key to bytecode.
2023-06-04 16:32:05 +03:00
QWORD bKey[2]{};
2023-06-04 15:17:41 +03:00
DWORD pID, checkHash;
QWORD sig = 0;
2019-08-21 18:29:48 +03:00
unbase24((BYTE *)bKey, cdKey);
2023-06-01 16:09:22 +03:00
// Extract data, hash and signature from the bytecode.
2023-06-04 14:54:22 +03:00
unpackXP(bKey, pID, checkHash, sig);
2019-08-21 18:29:48 +03:00
2023-06-01 16:09:22 +03:00
// e = Hash
// s = Signature
BIGNUM *e, *s;
2023-06-01 16:09:22 +03:00
// Put hash word into BigNum e.
e = BN_new();
BN_set_word(e, checkHash);
2023-06-01 16:09:22 +03:00
// Reverse signature and create a new BigNum s.
2023-06-04 15:17:41 +03:00
endian((BYTE *)&sig, sizeof(sig));
s = BN_bin2bn((BYTE *)&sig, sizeof(sig), nullptr);
2023-06-01 16:09:22 +03:00
// Create x and y.
BIGNUM *x = BN_new();
BIGNUM *y = BN_new();
2023-06-01 16:09:22 +03:00
// Create 2 new points on the existing elliptic curve.
EC_POINT *u = EC_POINT_new(eCurve);
EC_POINT *v = EC_POINT_new(eCurve);
2023-06-01 16:09:22 +03:00
// EC_POINT_mul calculates r = generator * n + q * m.
// v = s * generator + e * (-publicKey)
2023-06-01 16:09:22 +03:00
// u = generator * s
EC_POINT_mul(eCurve, u, nullptr, generator, s, context);
2023-06-01 16:09:22 +03:00
// v = publicKey * e
EC_POINT_mul(eCurve, v, nullptr, publicKey, e, context);
2023-06-01 16:09:22 +03:00
// v += u
EC_POINT_add(eCurve, v, u, v, context);
2023-06-01 16:09:22 +03:00
// EC_POINT_get_affine_coordinates() sets x and y, either of which may be nullptr, to the corresponding coordinates of p.
// x = v.x; y = v.y;
EC_POINT_get_affine_coordinates(eCurve, v, x, y, context);
2023-06-04 13:31:24 +03:00
BYTE buf[FIELD_BYTES], md[SHA_DIGEST_LENGTH], t[4];
DWORD newHash;
2023-06-01 16:09:22 +03:00
SHA_CTX hContext;
// h = First32(SHA-1(pID || v.x || v.y)) >> 4
SHA1_Init(&hContext);
// Chop Product ID into 4 bytes.
t[0] = (pID & 0xff); // First 8 bits
t[1] = (pID & 0xff00) >> 8; // Second 8 bits
t[2] = (pID & 0xff0000) >> 16; // Third 8 bits
t[3] = (pID & 0xff000000) >> 24; // Fourth 8 bits
// Hash chunk of data.
SHA1_Update(&hContext, t, sizeof(t));
// Empty buffer, place v.x in little-endian.
memset(buf, 0, FIELD_BYTES);
BN_bn2bin(x, buf);
endian(buf, FIELD_BYTES);
// Hash chunk of data.
SHA1_Update(&hContext, buf, FIELD_BYTES);
// Empty buffer, place v.y in little-endian.
memset(buf, 0, FIELD_BYTES);
BN_bn2bin(y, buf);
endian(buf, FIELD_BYTES);
// Hash chunk of data.
SHA1_Update(&hContext, buf, FIELD_BYTES);
// Store the final message from hContext in md.
SHA1_Final(md, &hContext);
// h = First32(SHA-1(pID || v.x || v.y)) >> 4
newHash = (md[0] | (md[1] << 8) | (md[2] << 16) | (md[3] << 24)) >> 4;
newHash &= 0xfffffff;
BN_free(e);
BN_free(s);
BN_free(x);
BN_free(y);
BN_CTX_free(context);
EC_POINT_free(u);
EC_POINT_free(v);
// If we managed to generate a key with the same hash, the key is correct.
return newHash == checkHash;
2019-08-21 18:29:48 +03:00
}
2023-06-01 16:09:22 +03:00
/* Generate a valid Product Key. */
2023-06-04 14:54:22 +03:00
void generateXPKey(EC_GROUP *eCurve, EC_POINT *generator, BIGNUM *order, BIGNUM *privateKey, DWORD pRaw, char (&pKey)[25]) {
2023-06-01 16:09:22 +03:00
EC_POINT *r = EC_POINT_new(eCurve);
BN_CTX *ctx = BN_CTX_new();
BIGNUM *c = BN_new();
BIGNUM *s = BN_new();
BIGNUM *x = BN_new();
BIGNUM *y = BN_new();
2023-06-04 16:32:05 +03:00
QWORD bKey[2]{};
2023-06-01 16:09:22 +03:00
do {
2023-06-04 15:17:41 +03:00
DWORD hash = 0;
QWORD sig = 0;
2023-06-01 16:09:22 +03:00
2023-06-04 16:32:05 +03:00
memset(bKey, 0, 2 * sizeof(QWORD));
2023-06-01 16:09:22 +03:00
// Generate a random number c consisting of 384 bits without any constraints.
BN_rand(c, FIELD_BITS, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY);
// r = generator * c;
EC_POINT_mul(eCurve, r, nullptr, generator, c, ctx);
// x = r.x; y = r.y;
EC_POINT_get_affine_coordinates(eCurve, r, x, y, ctx);
SHA_CTX hContext;
2023-06-04 13:31:24 +03:00
BYTE md[SHA_DIGEST_LENGTH]{}, buf[FIELD_BYTES]{}, t[4]{};
2023-06-01 16:09:22 +03:00
// h = (First-32(SHA1(pRaw, r.x, r.y)) >> 4
SHA1_Init(&hContext);
// Chop Raw Product Key into 4 bytes.
2023-06-04 14:54:22 +03:00
t[0] = (pRaw & 0xff);
t[1] = (pRaw & 0xff00) >> 8;
t[2] = (pRaw & 0xff0000) >> 16;
t[3] = (pRaw & 0xff000000) >> 24;
2023-06-01 16:09:22 +03:00
// Hash chunk of data.
SHA1_Update(&hContext, t, sizeof(t));
// Empty buffer, place r.x in little-endian
memset(buf, 0, FIELD_BYTES);
BN_bn2bin(x, buf);
endian(buf, FIELD_BYTES);
// Hash chunk of data.
SHA1_Update(&hContext, buf, FIELD_BYTES);
// Empty buffer, place r.y in little-endian.
memset(buf, 0, FIELD_BYTES);
BN_bn2bin(y, buf);
endian(buf, FIELD_BYTES);
// Hash chunk of data.
SHA1_Update(&hContext, buf, FIELD_BYTES);
// Store the final message from hContext in md.
SHA1_Final(md, &hContext);
// h = (First-32(SHA1(pRaw, r.x, r.y)) >> 4
hash = (md[0] | (md[1] << 8) | (md[2] << 16) | (md[3] << 24)) >> 4;
hash &= 0xfffffff;
/* s = privateKey * hash + c; */
// s = privateKey;
BN_copy(s, privateKey);
// s *= hash;
BN_mul_word(s, hash);
// BN_mod_add() adds a to b % m and places the non-negative result in r.
// s = |s + c % order|;
BN_mod_add(s, s, c, order, ctx);
// Convert s from BigNum back to bytecode and reverse the endianness.
2023-06-04 15:17:41 +03:00
BN_bn2bin(s, (BYTE *)&sig);
endian((BYTE *)&sig, BN_num_bytes(s));
2023-06-01 16:09:22 +03:00
// Pack product key.
2023-06-04 14:54:22 +03:00
packXP(bKey, pRaw, hash, sig);
//printf("PID: %.8X\nHash: %.8X\nSig: %.8X %.8X\n", pRaw[0], hash, sig[1], sig[0]);
2023-06-04 15:17:41 +03:00
std::cout << " PID: " << std::hex << std::setw(8) << std::setfill('0') << pRaw << std::endl
<< "Hash: " << std::hex << std::setw(8) << std::setfill('0') << hash << std::endl
<< " Sig: " << std::hex << std::setw(8) << std::setfill('0') << sig << std::endl
<< std::endl;
2023-06-04 16:32:05 +03:00
} while (bKey[1] >= (1ULL << 50));
2023-06-01 16:09:22 +03:00
// ↑ ↑ ↑
2023-06-04 16:32:05 +03:00
// bKey[1] can't be longer than 50 bits, else the signature part will make
2023-06-01 16:09:22 +03:00
// the CD-key longer than 25 characters.
// Convert the key to Base24.
base24(pKey, (BYTE *)bKey);
2023-06-01 16:09:22 +03:00
BN_free(c);
BN_free(s);
BN_free(x);
BN_free(y);
BN_CTX_free(ctx);
EC_POINT_free(r);
}