Lua加密更进一步
Lua AST base crypt
该方案会直接修改lua库中luac和unluac相关的逻辑,这种方式会比文件加密有更复杂的加密逻辑,也增加破解难度
核心方法(包括但不限于,这里的String只是举例)
- ldump.c中的DumpString
- lundump.c中的LoadString
他俩是成对的,如何的加密,就要对应的解密
static void DumpString (const TString *s, DumpState *D) {
if (s == NULL)
DumpByte(0, D);
else {
#if LUAC_COMPATIBLE_FORMAT
uint32_t size = tsslen(s) + 1;
#else
size_t size = tsslen(s) + 1; /* include trailing '\0' */
#endif
char *str = getstr(s);
if (size < 0xFF)
DumpByte(cast_int(size), D);
else {
DumpByte(0xFF, D);
DumpVar(size, D);
}
block_encrypt(str,size - 1); // crypt
DumpVector(str, size - 1, D); /* no need to save '\0' */
block_decrypt(str,size - 1); // decrypt 这里务必反解回去,因为后面还要用
}
}
static TString *LoadString (LoadState *S) {
#if LUAC_COMPATIBLE_FORMAT
uint32_t size = LoadByte(S);
#else
size_t size = LoadByte(S);
#endif
if (size == 0xFF)
LoadVar(S, size);
if (size == 0)
return NULL;
else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */
char buff[LUAI_MAXSHORTLEN];
LoadVector(S, buff, size);
block_decrypt(buff,size); //这里
return luaS_newlstr(S->L, buff, size);
}
else { /* long string */
TString *ts = luaS_createlngstrobj(S->L, size);
char* ret = getstr(ts);
LoadVector(S,ret, size); /* load directly in final place */
block_decrypt(ret,size); //和这里
return ts;
}
}
void block_encrypt(char* b, size_t s)
{
size_t i;
for(i=1;i<s;++i)
{
xxxx 逐字符
}
}
void block_decrypt(char* b,size_t s)
{
size_t i;
for(i=(s>0?s-1:0);i>0;--i)
{
xxxx 逐字符解密
}
}