能力值:
( LV3,RANK:30 )
|
-
-
11 楼
已经有人干掉保护了,放出 KeyGenMe 源代码。
另外这个 KeyGenMe 的主要思想是保护算法,我将来不用它保护注册码……
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef enum _SVM_STATUS {
SVM_SUCCESS_NORMAL,
SVM_SUCCESS_EXIT,
SVM_ERR_OUT_OF_CODE_RANGE,
SVM_ERR_UNKNOWN_INSTRUCTION,
SVM_ERR_INTEGER_DIVIDE_BY_ZERO,
SVM_ERR_INTEGER_OVERFLOW
} SVM_STATUS;
#define SVM_REG_COUNT (UINT8_MAX + 1)
#define SVM_REG_SIZE (sizeof(uint32_t) * SVM_REG_COUNT)
#define SVM_REG_PC 0
typedef struct _SVM_CONTEXT SVM_CONTEXT;
typedef uint32_t (*SVM_Handler_Proc)(SVM_CONTEXT *);
typedef void (*SVM_Invoke_Proc)(SVM_CONTEXT *);
struct _SVM_CONTEXT {
uint32_t *lpRegister;
uint8_t *lpCodeChunk;
uint32_t nCodeLength;
SVM_STATUS nStatus;
SVM_Invoke_Proc lpInvoke;
};
static uint8_t *SVM_ReadCode(SVM_CONTEXT *lpThis, uint32_t nIndex, uint32_t nLength)
{
if (nIndex + nLength > lpThis->nCodeLength)
{
lpThis->nStatus = SVM_ERR_OUT_OF_CODE_RANGE;
return NULL;
}
return lpThis->lpCodeChunk + nIndex;
}
static uint8_t *SVM_ReadCodeAtPC(SVM_CONTEXT *lpThis, uint32_t nLength)
{
return SVM_ReadCode(lpThis, lpThis->lpRegister[SVM_REG_PC], nLength);
}
static uint32_t SVM_Handler_Nop(SVM_CONTEXT *lpThis)
{
return 1;
}
static uint32_t SVM_Handler_Exit(SVM_CONTEXT *lpThis)
{
lpThis->nStatus = SVM_SUCCESS_EXIT;
return 1;
}
static uint32_t SVM_Handler_Move(SVM_CONTEXT *lpThis)
{
uint8_t *lpOperand = SVM_ReadCodeAtPC(lpThis, 3);
if (NULL == lpOperand) { return 0; }
lpThis->lpRegister[lpOperand[1]] = lpThis->lpRegister[lpOperand[2]];
return 3;
}
static uint32_t SVM_Handler_Add(SVM_CONTEXT *lpThis)
{
uint8_t *lpOperand = SVM_ReadCodeAtPC(lpThis, 4);
uint32_t nAdd1, nAdd2;
if (NULL == lpOperand) { return 0; }
nAdd1 = lpThis->lpRegister[lpOperand[2]];
nAdd2 = lpThis->lpRegister[lpOperand[3]];
lpThis->lpRegister[lpOperand[1]] = nAdd1 + nAdd2;
return 4;
}
static uint32_t SVM_Handler_Mul(SVM_CONTEXT *lpThis)
{
uint8_t *lpOperand = SVM_ReadCodeAtPC(lpThis, 5);
uint64_t nResult, nMul1, nMul2;
if (NULL == lpOperand) { return 0; }
nMul1 = lpThis->lpRegister[lpOperand[3]];
nMul2 = lpThis->lpRegister[lpOperand[4]];
nResult = nMul1 * nMul2;
lpThis->lpRegister[lpOperand[1]] = (uint32_t)(nResult >> 32);
lpThis->lpRegister[lpOperand[2]] = (uint32_t)(nResult);
return 5;
}
static uint32_t SVM_Handler_Div(SVM_CONTEXT *lpThis)
{
uint8_t *lpOperand = SVM_ReadCodeAtPC(lpThis, 6);
uint64_t nDividend, nDivisor, nQuotient, nRemainder;
uint32_t nHigh, nLow;
if (NULL == lpOperand) { return 0; }
nHigh = lpThis->lpRegister[lpOperand[3]];
nLow = lpThis->lpRegister[lpOperand[4]];
nDividend = ((uint64_t)nHigh << 32) | (uint64_t)nLow;
nDivisor = (uint64_t)lpThis->lpRegister[lpOperand[5]];
if (0 == nDivisor)
{
lpThis->nStatus = SVM_ERR_INTEGER_DIVIDE_BY_ZERO;
return 0;
}
nQuotient = nDividend / nDivisor;
nRemainder = nDividend % nDivisor;
if (nQuotient > UINT32_MAX)
{
lpThis->nStatus = SVM_ERR_INTEGER_OVERFLOW;
return 0;
}
lpThis->lpRegister[lpOperand[1]] = (uint32_t)nQuotient;
lpThis->lpRegister[lpOperand[2]] = (uint32_t)nRemainder;
return 6;
}
static uint32_t SVM_Handler_Nor(SVM_CONTEXT *lpThis)
{
uint8_t *lpOperand = SVM_ReadCodeAtPC(lpThis, 4);
uint32_t nNor1, nNor2;
if (NULL == lpOperand) { return 0; }
nNor1 = lpThis->lpRegister[lpOperand[2]];
nNor2 = lpThis->lpRegister[lpOperand[3]];
lpThis->lpRegister[lpOperand[1]] = ~(nNor1 | nNor2);
return 4;
}
static uint32_t SVM_Handler_Shl(SVM_CONTEXT *lpThis)
{
uint8_t *lpOperand = SVM_ReadCodeAtPC(lpThis, 4);
uint32_t nShift1, nShift2;
if (NULL == lpOperand) { return 0; }
nShift1 = lpThis->lpRegister[lpOperand[2]];
nShift2 = lpThis->lpRegister[lpOperand[3]];
lpThis->lpRegister[lpOperand[1]] = nShift1 << (nShift2 & 0x1F);
return 4;
}
static uint32_t SVM_Handler_Shr(SVM_CONTEXT *lpThis)
{
uint8_t *lpOperand = SVM_ReadCodeAtPC(lpThis, 4);
uint32_t nShift1, nShift2;
if (NULL == lpOperand) { return 0; }
nShift1 = lpThis->lpRegister[lpOperand[2]];
nShift2 = lpThis->lpRegister[lpOperand[3]];
lpThis->lpRegister[lpOperand[1]] = nShift1 >> (nShift2 & 0x1F);
return 4;
}
static uint32_t SVM_Handler_ZTest(SVM_CONTEXT *lpThis)
{
uint8_t *lpOperand = SVM_ReadCodeAtPC(lpThis, 3);
if (NULL == lpOperand) { return 0; }
if (0 == lpThis->lpRegister[lpOperand[2]])
{
lpThis->lpRegister[lpOperand[1]] = 0;
return 3;
}
lpThis->lpRegister[lpOperand[1]] = 0xFFFFFFFF;
return 3;
}
static uint32_t SVM_Handler_LoadC(SVM_CONTEXT *lpThis)
{
uint8_t *lpOperand = SVM_ReadCodeAtPC(lpThis, 3);
uint32_t nIndex, *lpResult;
if (NULL == lpOperand) { return 0; }
nIndex = lpThis->lpRegister[lpOperand[2]];
lpResult = (uint32_t *)SVM_ReadCode(lpThis, nIndex,
sizeof(uint32_t) / sizeof(uint8_t));
if (NULL == lpResult) { return 0; }
lpThis->lpRegister[lpOperand[1]] = *lpResult;
return 3;
}
static uint32_t SVM_Handler_Invk(SVM_CONTEXT *lpThis)
{
(*lpThis->lpInvoke)(lpThis);
return 1;
}
static uint32_t SVM_Handler_LoadR(SVM_CONTEXT *lpThis)
{
uint8_t *lpOperand = SVM_ReadCodeAtPC(lpThis, 3);
uint32_t nIndex, nResult;
if (NULL == lpOperand) { return 0; }
nIndex = lpThis->lpRegister[lpOperand[2]];
nResult = lpThis->lpRegister[(uint8_t)nIndex];
lpThis->lpRegister[lpOperand[1]] = nResult;
return 3;
}
static uint32_t SVM_Handler_StoreR(SVM_CONTEXT *lpThis)
{
uint8_t *lpOperand = SVM_ReadCodeAtPC(lpThis, 3);
uint32_t nIndex, nData;
if (NULL == lpOperand) { return 0; }
nIndex = lpThis->lpRegister[lpOperand[1]];
nData = lpThis->lpRegister[lpOperand[2]];
lpThis->lpRegister[(uint8_t)nIndex] = nData;
return 3;
}
static uint32_t SVM_Handler_LImm(SVM_CONTEXT *lpThis)
{
uint8_t *lpOperand = SVM_ReadCodeAtPC(lpThis, 6);
uint32_t *lpResult;
if (NULL == lpOperand) { return 0; }
lpResult = (uint32_t *)(&lpOperand[2]);
lpThis->lpRegister[lpOperand[1]] = *lpResult;
return 6;
}
typedef enum _SVM_HANDLER_LIST {
SVM_HANDLER_NOP,
SVM_HANDLER_EXIT,
SVM_HANDLER_MOVE,
SVM_HANDLER_ADD,
SVM_HANDLER_MUL,
SVM_HANDLER_DIV,
SVM_HANDLER_NOR,
SVM_HANDLER_SHL,
SVM_HANDLER_SHR,
SVM_HANDLER_ZTEST,
SVM_HANDLER_LOADC,
SVM_HANDLER_INVK,
SVM_HANDLER_LOADR,
SVM_HANDLER_STORER,
SVM_HANDLER_LIMM,
SVM_HANDLER_TOTAL_COUNT
} SVM_HANDLER_LIST;
static const SVM_Handler_Proc
SVM_Dispatcher[SVM_HANDLER_TOTAL_COUNT] = {
&SVM_Handler_Nop,
&SVM_Handler_Exit,
&SVM_Handler_Move,
&SVM_Handler_Add,
&SVM_Handler_Mul,
&SVM_Handler_Div,
&SVM_Handler_Nor,
&SVM_Handler_Shl,
&SVM_Handler_Shr,
&SVM_Handler_ZTest,
&SVM_Handler_LoadC,
&SVM_Handler_Invk,
&SVM_Handler_LoadR,
&SVM_Handler_StoreR,
&SVM_Handler_LImm
};
static void SVM_Execute(SVM_CONTEXT *lpThis)
{
uint32_t nLastPC, nInstLength;
uint8_t *lpOpcode;
for (; NULL != (lpOpcode = SVM_ReadCodeAtPC(lpThis, 1));)
{
nLastPC = lpThis->lpRegister[SVM_REG_PC];
if (*lpOpcode >= SVM_HANDLER_TOTAL_COUNT)
{
lpThis->nStatus = SVM_ERR_UNKNOWN_INSTRUCTION;
break;
}
nInstLength = (*SVM_Dispatcher[*lpOpcode])(lpThis);
if (SVM_SUCCESS_NORMAL != lpThis->nStatus)
break;
if (nLastPC == lpThis->lpRegister[SVM_REG_PC])
lpThis->lpRegister[SVM_REG_PC] += nInstLength;
}
}
#define CODE_LENGTH 203
const uint8_t g_VMCodes[CODE_LENGTH] = {
// Data 0x0000C203 And R194 = 0:
SVM_HANDLER_ADD, 194, 0x00, 0x00,
// R9 = 0x3E8B8105:
SVM_HANDLER_LIMM, 15, 0x8D, 0x2A, 0x57, 0x2C,
SVM_HANDLER_ADD, 9, 15, 252,
// R8 = R1(Input):
SVM_HANDLER_MOVE, 8, 1,
SVM_HANDLER_NOP,
// Loop: R10 = R8 * 0xC203:
SVM_HANDLER_NOP,
SVM_HANDLER_LOADC, 14, 194,
SVM_HANDLER_MUL, 17, 10, 8, 14,
// R11 = GetSalt(R9):
SVM_HANDLER_MOVE, 254, 9,
SVM_HANDLER_INVK,
SVM_HANDLER_ADD, 12, 10, 17,
SVM_HANDLER_MOVE, 11, 255,
// R12 = R10 ^ R11:
SVM_HANDLER_NOR, 13, 11, 194,
SVM_HANDLER_MUL, 16, 14, 15, 17,
SVM_HANDLER_NOR, 14, 10, 13,
SVM_HANDLER_MOVE, 17, 194,
SVM_HANDLER_MUL, 16, 15, 17, 14,
SVM_HANDLER_ADD, 17, 16, 12,
SVM_HANDLER_ADD, 16, 14, 15,
SVM_HANDLER_NOR, 17, 14, 15,
SVM_HANDLER_MOVE, 15, 16,
SVM_HANDLER_MOVE, 14, 16,
SVM_HANDLER_NOR, 15, 10, 194,
SVM_HANDLER_LIMM, 17, 16, 11, 13, 15,
SVM_HANDLER_NOR, 16, 15, 11,
SVM_HANDLER_ADD, 13, 17, 15,
SVM_HANDLER_MOVE, 15, 17,
SVM_HANDLER_NOR, 17, 14, 16,
SVM_HANDLER_NOR, 12, 17, 194,
SVM_HANDLER_MUL, 17, 15, 11, 12,
SVM_HANDLER_ADD, 16, 17, 15,
SVM_HANDLER_NOR, 15, 12, 17,
// R8 = R12:
SVM_HANDLER_MOVE, 16, 194,
SVM_HANDLER_ADD, 8, 12, 16,
SVM_HANDLER_MOVE, 254, 17,
SVM_HANDLER_INVK,
SVM_HANDLER_ADD, 16, 255, 17,
// R9 += 1:
SVM_HANDLER_MOVE, 15, 9,
SVM_HANDLER_SHL, 16, 194, 15,
SVM_HANDLER_ADD, 9, 15, 253,
SVM_HANDLER_SHR, 15, 16, 9,
// R12 = 0xC1747EF6:
SVM_HANDLER_LIMM, 11, 0xF6, 0x7E, 0x74, 0xC1,
SVM_HANDLER_MOVE, 12, 11,
SVM_HANDLER_NOR, 11, 15, 9,
// R12 += R9:
SVM_HANDLER_ADD, 13, 9, 12,
SVM_HANDLER_ADD, 12, 13, 194,
// R12 &= 0x80000000:
SVM_HANDLER_LIMM, 11, 0xFF, 0xFF, 0xFF, 0x7F,
SVM_HANDLER_NOR, 13, 12, 194,
SVM_HANDLER_NOR, 12, 13, 11,
// if (R12 != 0) goto loop:
SVM_HANDLER_ZTEST, 17, 12,
SVM_HANDLER_NOR, 16, 17, 194,
SVM_HANDLER_NOR, 13, 16, 251,
SVM_HANDLER_ADD, 0, 0, 13,
// R2(Output) = R8:
SVM_HANDLER_MOVE, 2, 8,
SVM_HANDLER_EXIT
};
static void GetSalt(SVM_CONTEXT *lpThis)
{
uint32_t r = 0;
switch (lpThis->lpRegister[0xFE])
{
case 0x3E8B8105: r = 0x03B7A707; break;
case 0x3E8B8106: r = 0x63FF1554; break;
case 0x3E8B8107: r = 0xC23F35EF; break;
case 0x3E8B8108: r = 0x7A5F1A89; break;
case 0x3E8B8109: r = 0xA62CB6AE; break;
}
lpThis->lpRegister[0xFF] = r;
}
uint32_t g_VMRegister[SVM_REG_COUNT];
__declspec(noinline) void SVM_Init(SVM_CONTEXT *lpThis)
{
uint8_t Rand[4];
int i, j;
lpThis->lpRegister = g_VMRegister;
lpThis->nStatus = SVM_SUCCESS_NORMAL;
lpThis->lpCodeChunk = (uint8_t *)g_VMCodes;
lpThis->nCodeLength = CODE_LENGTH;
lpThis->lpInvoke = &GetSalt;
srand((unsigned int)time(NULL));
for (i = 0; i < SVM_REG_COUNT; i++)
{
for (j = 0; j < 4; j++)
Rand[j] = (uint8_t)rand();
lpThis->lpRegister[i] = *(uint32_t *)Rand;
}
lpThis->lpRegister[SVM_REG_PC] = 0;
lpThis->lpRegister[0xFD] = 1;
lpThis->lpRegister[0xFC] = 0x12345678;
lpThis->lpRegister[0xFB] = 176;
}
#if 0
void SVM_Debug_Print(SVM_CONTEXT *lpThis)
{
int i;
for (i = 0; i < 256; i++)
{
if (i % 6 == 0) printf("\r\n %.3d : ", i);
printf("0x%.8X ", lpThis->lpRegister[i]);
}
printf("status = %ld\r\n", lpThis->nStatus);
printf("codelen = %ld.\r\n", CODE_LENGTH);
}
#endif
int __cdecl main(void)
{
SVM_CONTEXT svmc;
char szKey[12];
char szInput[12];
SVM_Init(&svmc);
printf("Seed = %.8X", svmc.lpRegister[1]);
printf(", please enter your key.\r\nKey : ");
scanf("%11s", szInput);
SVM_Execute(&svmc);
if (SVM_SUCCESS_EXIT == svmc.nStatus)
{
_ultoa(svmc.lpRegister[2], szKey, 16);
szInput[11] = szKey[11] = '\0';
if (_stricmp(szInput, szKey) == 0)
{
printf("------------------\r\n");
printf("Your key is right!\r\n");
printf("Good job !!!!!!!!!\r\n");
printf("------------------\r\n");
}
else
{
printf("Your Key is wrong!\r\n");
printf("Right Key = %s.\r\n", szKey);
}
}
else
{
printf("Error !\r\n");
}
printf("Press any key to exit.\r\n");
_getch();
return 0;
}
注册机中的核心算法:
#include <stdint.h>
#define ROUND 5
const uint32_t g_Salt[ROUND] = {
0x03B7A707, 0x63FF1554, 0xC23F35EF,
0x7A5F1A89, 0xA62CB6AE
};
uint32_t GetKey(uint32_t nSeed)
{
int i;
for (i = 0; i < ROUND; i++)
nSeed = (nSeed * 49667) ^ g_Salt[i];
return nSeed;
}
-------------------- 以上 --------------------
|