2022-03-21 22:47:46 +00:00
|
|
|
#ifndef LAIKA_BOX_H
|
|
|
|
#define LAIKA_BOX_H
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
2022-04-28 16:07:11 +00:00
|
|
|
#include "laika.h"
|
2022-04-28 23:10:15 +00:00
|
|
|
#include "lmem.h"
|
2022-03-21 22:47:46 +00:00
|
|
|
#include "lvm.h"
|
2022-04-28 23:10:15 +00:00
|
|
|
#include "lsodium.h"
|
|
|
|
|
2022-05-09 21:41:01 +00:00
|
|
|
#define LAIKA_BOX_SCRATCH_SIZE 128
|
2022-04-28 23:10:15 +00:00
|
|
|
#define LAIKA_BOX_HEAPSIZE 256
|
2022-05-09 21:41:01 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
LAIKA_BOX_UNLOCKED_INDX, /* for output */
|
|
|
|
LAIKA_BOX_SCRATCH_INDX, /* for misc. scratch work the vm needs to do (hold keys, etc.) */
|
|
|
|
LAIKA_BOX_DATA_INDX /* for input */
|
|
|
|
};
|
2022-03-21 22:47:46 +00:00
|
|
|
|
|
|
|
/* Laika Box:
|
|
|
|
Laika Boxes are obfuscated storage mediums where data is only in memory for a very short amount of time.
|
|
|
|
Of course, this can be bypassed with a simple debugger and setting a breakpoint right after the data is 'unlocked',
|
|
|
|
but the game of obfuscation isn't to prevent the data from being seen, it's to slow the reverse engineer down.
|
|
|
|
|
|
|
|
2 main APIs are exposed here, laikaB_unlock() & laikaB_lock(). Both of which are inlined to make it more painful
|
|
|
|
for the reverse engineer to quickly dump boxes from memory, forcing them to set breakpoints across the executable.
|
|
|
|
Each box has its own VM, with it's own deobfuscation routine. This makes static analysis a painful route for string
|
2022-05-11 02:03:23 +00:00
|
|
|
dumping. These apis, while can be used directly, are abstracted through macros with the pre-built boxes.
|
2022-05-09 21:41:01 +00:00
|
|
|
|
2022-05-11 02:03:23 +00:00
|
|
|
Use LAIKA_BOX_SKID_START & LAIKA_BOX_SKID_END for quick and dirty usage. The data macros in `lboxconfig.h` are passed
|
|
|
|
to these, which are generated by VMBoxGen (`tools/vmboxgen`). This will be extended in the future with more boxes and such,
|
|
|
|
however for the time being only LAIKA_BOX_SKID_* is implemented.
|
2022-03-21 22:47:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct sLaikaB_box {
|
2022-04-28 23:10:15 +00:00
|
|
|
uint8_t unlockedData[LAIKA_BOX_HEAPSIZE];
|
2022-05-09 21:41:01 +00:00
|
|
|
uint8_t scratch[LAIKA_BOX_SCRATCH_SIZE];
|
2022-04-28 23:10:15 +00:00
|
|
|
uint8_t code[LAIKA_VM_CODESIZE];
|
2022-03-21 22:47:46 +00:00
|
|
|
};
|
|
|
|
|
2022-05-11 01:28:54 +00:00
|
|
|
/* ==============================================[[ Box Var API ]]=============================================== */
|
|
|
|
|
|
|
|
#define LAIKA_BOX_STARTVAR(type, ident, box, data) \
|
|
|
|
uint8_t __data##ident[LAIKA_VM_CODESIZE] = data; \
|
2022-05-09 21:41:01 +00:00
|
|
|
type ident; \
|
|
|
|
struct sLaikaB_box __box##ident = box; \
|
|
|
|
laikaB_unlock(&__box##ident, __data##ident); \
|
|
|
|
ident = (type)__box##ident.unlockedData;
|
|
|
|
|
|
|
|
#define LAIKA_BOX_ENDVAR(ident) \
|
2022-05-11 01:28:54 +00:00
|
|
|
laikaB_lock(&__box##ident);
|
2022-05-09 21:41:01 +00:00
|
|
|
|
2022-05-11 01:28:54 +00:00
|
|
|
#ifdef LAIKA_OBFUSCATE
|
|
|
|
# define LAIKA_BOX_SKID_START(type, ident, strmacro) \
|
|
|
|
LAIKA_BOX_STARTVAR(type, ident, LAIKA_BOX_SKID(KEY_##strmacro), DATA_##strmacro)
|
|
|
|
# define LAIKA_BOX_SKID_END(ident) \
|
|
|
|
LAIKA_BOX_ENDVAR(ident)
|
|
|
|
#else /* disable obfuscations */
|
|
|
|
# define LAIKA_BOX_SKID_START(type, ident, strmacro) \
|
|
|
|
type ident = strmacro;
|
|
|
|
# define LAIKA_BOX_SKID_END(ident) ((void)0) /* no-op */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* ==============================================[[ Laika Boxes ]]=============================================== */
|
2022-05-09 21:41:01 +00:00
|
|
|
|
2022-05-11 02:03:23 +00:00
|
|
|
/* BOX_SKID decodes null-terminated strings using a provided xor _key. aptly named lol */
|
2022-05-01 19:41:32 +00:00
|
|
|
#define LAIKA_BOX_SKID(_key) { \
|
|
|
|
.unlockedData = {0}, /* reserved */ \
|
|
|
|
.code = { /* stack layout: \
|
|
|
|
[0] - unlockedData (ptr) \
|
|
|
|
[1] - data (ptr) \
|
|
|
|
[2] - key (uint8_t) \
|
|
|
|
[3] - working data (uint8_t) \
|
|
|
|
*/ \
|
2022-05-08 06:45:36 +00:00
|
|
|
LAIKA_MAKE_VM_IAB(OP_LOADCONST, 0, LAIKA_BOX_UNLOCKED_INDX), \
|
|
|
|
LAIKA_MAKE_VM_IAB(OP_LOADCONST, 1, LAIKA_BOX_DATA_INDX), \
|
2022-05-01 19:41:32 +00:00
|
|
|
LAIKA_MAKE_VM_IAB(OP_PUSHLIT, 2, _key), \
|
|
|
|
/* LOOP_START */ \
|
|
|
|
LAIKA_MAKE_VM_IAB(OP_READ, 3, 1), /* load data into working data */ \
|
|
|
|
LAIKA_MAKE_VM_IABC(OP_XOR, 3, 3, 2), /* xor data with key */ \
|
|
|
|
LAIKA_MAKE_VM_IAB(OP_WRITE, 0, 3), /* write data to unlockedData */ \
|
|
|
|
LAIKA_MAKE_VM_IA(OP_INCPTR, 0), \
|
|
|
|
LAIKA_MAKE_VM_IA(OP_INCPTR, 1), \
|
|
|
|
LAIKA_MAKE_VM_IAB(OP_TESTJMP, 3, -17), /* exit loop on null terminator */ \
|
|
|
|
OP_EXIT \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2022-05-11 01:28:54 +00:00
|
|
|
/* ==============================================[[ Raw Box API ]]=============================================== */
|
|
|
|
|
2022-04-28 23:10:15 +00:00
|
|
|
LAIKA_FORCEINLINE void* laikaB_unlock(struct sLaikaB_box *box, void *data) {
|
2022-04-30 21:21:29 +00:00
|
|
|
struct sLaikaV_vm vm = {
|
|
|
|
/* boxes have 2 reserved constants, [0] for the output, [1] for the input */
|
|
|
|
.constList = {
|
2022-05-08 06:45:36 +00:00
|
|
|
[LAIKA_BOX_UNLOCKED_INDX] = LAIKA_MAKE_VM_PTR(box->unlockedData),
|
2022-05-09 21:41:01 +00:00
|
|
|
[LAIKA_BOX_SCRATCH_INDX] = LAIKA_MAKE_VM_PTR(box->scratch),
|
2022-05-08 06:45:36 +00:00
|
|
|
[LAIKA_BOX_DATA_INDX] = LAIKA_MAKE_VM_PTR(data),
|
2022-04-30 21:21:29 +00:00
|
|
|
},
|
|
|
|
.code = {0},
|
|
|
|
.stack = {0},
|
|
|
|
.pc = 0
|
|
|
|
};
|
2022-03-21 22:47:46 +00:00
|
|
|
|
2022-04-30 21:21:29 +00:00
|
|
|
memcpy(vm.code, box->code, LAIKA_VM_CODESIZE);
|
2022-04-28 23:10:15 +00:00
|
|
|
laikaV_execute(&vm);
|
|
|
|
return (void*)box->unlockedData;
|
|
|
|
}
|
2022-03-21 22:47:46 +00:00
|
|
|
|
2022-04-28 23:10:15 +00:00
|
|
|
/* safely zeros the unlockedData using libsodium's api for clearing sensitive data from memory */
|
2022-05-14 18:24:20 +00:00
|
|
|
LAIKA_FORCEINLINE void laikaB_lock(struct sLaikaB_box *box) {
|
2022-04-28 23:10:15 +00:00
|
|
|
sodium_memzero(box->unlockedData, LAIKA_BOX_HEAPSIZE);
|
2022-05-09 21:41:01 +00:00
|
|
|
sodium_memzero(box->scratch, LAIKA_BOX_SCRATCH_SIZE);
|
2022-03-21 22:47:46 +00:00
|
|
|
}
|
2022-05-14 18:24:20 +00:00
|
|
|
|
2022-05-11 02:03:23 +00:00
|
|
|
/* include KEY_* & DATA_* macros for each obfuscated string */
|
2022-05-11 01:28:54 +00:00
|
|
|
#include "lboxconfig.h"
|
|
|
|
|
2022-03-21 22:47:46 +00:00
|
|
|
#endif
|