|
|
|
@ -71,88 +71,115 @@ bool obf_init_from_buffer(pobfuscator_t obf, void* buffer, int buffer_size)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool obf_create_groups(pobfuscator_t obf, int group_size)
|
|
|
|
|
bool obf_create_groups(pobfuscator_t obf, int32_t group_size)
|
|
|
|
|
{
|
|
|
|
|
//remake cuz this shit broke as fuck
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//obf->groups.clear();
|
|
|
|
|
|
|
|
|
|
///*if (group_size < 24)
|
|
|
|
|
// return false;*/
|
|
|
|
|
|
|
|
|
|
//int cur_group_id = 0, cur_size_in_bytes = 0;
|
|
|
|
|
//pcode_link_t start = obf->code_start->next;
|
|
|
|
|
//for (pcode_link_t t = obf->code_start->next; t;)
|
|
|
|
|
//{
|
|
|
|
|
// pcode_link_t real_next = t->next;
|
|
|
|
|
// if (!(t->flags & CLFLAG_IS_GAGET) && !(t->flags & CLFLAG_IS_LABEL))
|
|
|
|
|
// {
|
|
|
|
|
// if (cur_size_in_bytes + t->raw_data_size /*+ END_OF_GROUP_GAGT_SIZE*/ > group_size)
|
|
|
|
|
// {
|
|
|
|
|
// std::string group_label_name = "Group";
|
|
|
|
|
// group_label_name.append(std::to_string(cur_group_id + 1));
|
|
|
|
|
// pcode_link_t lab = new code_link_t;
|
|
|
|
|
// lab->flags = CLFLAG_IS_LABEL;
|
|
|
|
|
// lab->label_name = group_label_name;
|
|
|
|
|
// lab->group = cur_group_id;
|
|
|
|
|
|
|
|
|
|
// pcode_link_t gadget = new code_link_t;
|
|
|
|
|
// gadget->flags = 0;
|
|
|
|
|
// gadget->label_name = "";
|
|
|
|
|
// gadget->raw_data = new unsigned char[6];
|
|
|
|
|
// gadget->raw_data_size = 6;
|
|
|
|
|
// gadget->group = cur_group_id;
|
|
|
|
|
// unsigned char gadget_data[] = { 0xFF, 0x25, 0x00, 0x00, 0x00, 0x00 };
|
|
|
|
|
// memcpy(gadget->raw_data, gadget_data, 6);
|
|
|
|
|
|
|
|
|
|
// pcode_link_t abs_addr = new code_link_t;
|
|
|
|
|
// abs_addr->flags = CLFLAG_IS_ABS_ADDR;
|
|
|
|
|
// abs_addr->label_name = group_label_name;
|
|
|
|
|
// abs_addr->raw_data = new unsigned char[8];
|
|
|
|
|
// abs_addr->raw_data_size = 8;
|
|
|
|
|
// abs_addr->group = cur_group_id;
|
|
|
|
|
|
|
|
|
|
// t->prev->next = gadget;
|
|
|
|
|
// gadget->next = abs_addr;
|
|
|
|
|
// abs_addr->next = lab;
|
|
|
|
|
// lab->next = t;// real_next;
|
|
|
|
|
|
|
|
|
|
// gadget->prev = t->prev;
|
|
|
|
|
// abs_addr->prev = gadget;
|
|
|
|
|
// lab->prev = abs_addr;
|
|
|
|
|
// t->prev = lab;
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
// printf("creating group %d\n", cur_group_id);
|
|
|
|
|
// obf->groups.emplace_back();
|
|
|
|
|
// obf->groups.back().size_in_bytes = cur_size_in_bytes + END_OF_GROUP_GAGT_SIZE;
|
|
|
|
|
// obf->groups.back().start = start;
|
|
|
|
|
// obf->groups.back().base_address = cur_group_id;
|
|
|
|
|
// cur_size_in_bytes = 0;
|
|
|
|
|
// cur_group_id++;
|
|
|
|
|
// start = t;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// cur_size_in_bytes += t->raw_data_size;
|
|
|
|
|
// t->group = cur_group_id;
|
|
|
|
|
// t = real_next;
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
//obf->groups.emplace_back();
|
|
|
|
|
//obf->groups.back().size_in_bytes = cur_size_in_bytes + 16;
|
|
|
|
|
//obf->groups.back().start = start;
|
|
|
|
|
//obf->groups.back().base_address = cur_group_id;
|
|
|
|
|
|
|
|
|
|
//return true;
|
|
|
|
|
int cur_group_id = 0;
|
|
|
|
|
int cur_offset = 0;
|
|
|
|
|
|
|
|
|
|
//assign instructions to groups
|
|
|
|
|
for (pcode_link_t t = obf->code_start->next; t; t = t->next)
|
|
|
|
|
{
|
|
|
|
|
if (!(t->flags & CLFLAG_IS_LABEL))
|
|
|
|
|
{
|
|
|
|
|
if (!(t->flags & CLFLAG_IS_GAGET))
|
|
|
|
|
{
|
|
|
|
|
if (cur_offset + t->raw_data_size > group_size)
|
|
|
|
|
{
|
|
|
|
|
++cur_group_id;
|
|
|
|
|
cur_offset = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cur_offset += t->raw_data_size;
|
|
|
|
|
t->group = cur_group_id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//assign labels to their proper groups
|
|
|
|
|
for (pcode_link_t t = obf->code_start->next; t; t = t->next)
|
|
|
|
|
{
|
|
|
|
|
if (t->flags & CLFLAG_IS_LABEL)
|
|
|
|
|
{
|
|
|
|
|
pcode_link_t t2 = t;
|
|
|
|
|
while (t2 && (t2->flags & CLFLAG_IS_LABEL)) { t2 = t2->next; }
|
|
|
|
|
if (t2) t->group = t2->group;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//create group descriptors
|
|
|
|
|
obf->groups.clear();
|
|
|
|
|
pcode_link_t start = obf->code_start->next;
|
|
|
|
|
cur_offset = 0;
|
|
|
|
|
pcode_link_t prev_meme = nullptr;
|
|
|
|
|
for (pcode_link_t t = obf->code_start->next; t; t = t->next)
|
|
|
|
|
{
|
|
|
|
|
if (start->group != t->group)
|
|
|
|
|
{
|
|
|
|
|
obf->groups.emplace_back(0, start, t->prev, cur_offset);
|
|
|
|
|
cur_offset = 0;
|
|
|
|
|
start = t;
|
|
|
|
|
}
|
|
|
|
|
cur_offset += t->raw_data_size;
|
|
|
|
|
prev_meme = t;
|
|
|
|
|
}
|
|
|
|
|
if (!prev_meme)
|
|
|
|
|
return false;
|
|
|
|
|
obf->groups.emplace_back(0, start, prev_meme, cur_offset);
|
|
|
|
|
|
|
|
|
|
//append jumps to next group onto end
|
|
|
|
|
for (uint32_t i = 0; i < obf->groups.size() - 1; i++)
|
|
|
|
|
{
|
|
|
|
|
pcode_group_t cur_group = &obf->groups[i];
|
|
|
|
|
pcode_group_t next_group = &obf->groups[i+1];
|
|
|
|
|
|
|
|
|
|
//add jump gadget to end of current group
|
|
|
|
|
pcode_link_t gadget = new code_link_t;
|
|
|
|
|
gadget->flags = CLFLAG_IS_GAGET;
|
|
|
|
|
gadget->label_name = "";
|
|
|
|
|
gadget->raw_data = new unsigned char[6];
|
|
|
|
|
gadget->raw_data_size = 6;
|
|
|
|
|
gadget->group = i;
|
|
|
|
|
unsigned char gadget_data[] = { 0xFF, 0x25, 0x00, 0x00, 0x00, 0x00 };
|
|
|
|
|
memcpy(gadget->raw_data, gadget_data, 6);
|
|
|
|
|
|
|
|
|
|
pcode_link_t abs_addr = new code_link_t;
|
|
|
|
|
abs_addr->flags = (CLFLAG_IS_GAGET | CLFLAG_IS_ABS_ADDR);
|
|
|
|
|
abs_addr->label_name = std::string("Group") + std::to_string(i + 1);
|
|
|
|
|
abs_addr->raw_data = new unsigned char[8];
|
|
|
|
|
abs_addr->raw_data_size = 8;
|
|
|
|
|
abs_addr->group = i;
|
|
|
|
|
|
|
|
|
|
pcode_link_t real_next = cur_group->end->next;
|
|
|
|
|
cur_group->end->next = gadget;
|
|
|
|
|
gadget->next = abs_addr;
|
|
|
|
|
abs_addr->next = real_next;
|
|
|
|
|
|
|
|
|
|
real_next->prev = abs_addr;
|
|
|
|
|
abs_addr->prev = gadget;
|
|
|
|
|
gadget->prev = cur_group->end;
|
|
|
|
|
|
|
|
|
|
cur_group->end = abs_addr;
|
|
|
|
|
|
|
|
|
|
//add label to beginning of next group
|
|
|
|
|
pcode_link_t next_group_label = new code_link_t;
|
|
|
|
|
next_group_label->flags = CLFLAG_IS_LABEL;
|
|
|
|
|
next_group_label->label_name = std::string("Group") + std::to_string(i + 1);
|
|
|
|
|
next_group_label->group = i + 1;
|
|
|
|
|
|
|
|
|
|
pcode_link_t real_prev = next_group->start->prev;
|
|
|
|
|
next_group->start->prev = next_group_label;
|
|
|
|
|
real_prev->next = next_group_label;
|
|
|
|
|
next_group_label->next = next_group->start;
|
|
|
|
|
next_group_label->prev = real_prev;
|
|
|
|
|
next_group->start = next_group_label;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void obf_replace_rel_jmps(pobfuscator_t obf)
|
|
|
|
|
|
|
|
|
|
{ // original_jump -------------------------.
|
|
|
|
|
// jmp 0x10(0xEB, 0x10) ------------------ | -----.
|
|
|
|
|
// jmp 0x0E(0xEB, 0x0E) ------------------ | -----.
|
|
|
|
|
// jmp qword ptr[rip] <----------------' |
|
|
|
|
|
// address here(8 bytes) |
|
|
|
|
|
// not taken branch code <-----------------------'
|
|
|
|
@ -204,7 +231,7 @@ void obf_replace_rel_jmps(pobfuscator_t obf)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool obf_replace_abs_jmps(pobfuscator_t obf)
|
|
|
|
|
bool obf_resolve_abs_addresses(pobfuscator_t obf)
|
|
|
|
|
{
|
|
|
|
|
for (pcode_link_t t = obf->code_start->next; t; t = t->next)
|
|
|
|
|
{
|
|
|
|
@ -273,7 +300,6 @@ bool obf_gen_all_labels(pobfuscator_t obf)
|
|
|
|
|
|
|
|
|
|
bool obf_gen_label(pobfuscator_t obf, pcode_link_t jmp, int32_t delta)
|
|
|
|
|
{
|
|
|
|
|
obf->current_label_id++;
|
|
|
|
|
pcode_link_t temp;
|
|
|
|
|
//when going positive, counting starts at NEXT instruction(excluding size of jmp)
|
|
|
|
|
//when negative, counting INCLUDES the size of the jmp instructrion
|
|
|
|
@ -316,7 +342,7 @@ bool obf_gen_label(pobfuscator_t obf, pcode_link_t jmp, int32_t delta)
|
|
|
|
|
//couldnt find label, adding new one
|
|
|
|
|
pcode_link_t new_label = new code_link_t;
|
|
|
|
|
new_label->flags = CLFLAG_IS_LABEL;
|
|
|
|
|
new_label->label_name = std::to_string(obf->current_label_id);
|
|
|
|
|
new_label->label_name = std::to_string(++obf->current_label_id);
|
|
|
|
|
jmp->label_name = new_label->label_name;
|
|
|
|
|
|
|
|
|
|
new_label->next = temp;
|
|
|
|
@ -328,22 +354,41 @@ bool obf_gen_label(pobfuscator_t obf, pcode_link_t jmp, int32_t delta)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#include <Windows.h>
|
|
|
|
|
|
|
|
|
|
void obf_dbg_print_code(pobfuscator_t obf)
|
|
|
|
|
{
|
|
|
|
|
HANDLE StdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
|
if (!StdHandle)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (pcode_link_t t = obf->code_start->next; t; t = t->next)
|
|
|
|
|
{
|
|
|
|
|
if (t->group % 2)
|
|
|
|
|
SetConsoleTextAttribute(StdHandle, 12);
|
|
|
|
|
else
|
|
|
|
|
SetConsoleTextAttribute(StdHandle, 14);
|
|
|
|
|
|
|
|
|
|
if (t->flags & CLFLAG_IS_REL_JUMP)
|
|
|
|
|
{
|
|
|
|
|
printf("\tJump to: %s ", t->label_name.data());
|
|
|
|
|
printf("\tRel jmp to: %s\t\t", t->label_name.data());
|
|
|
|
|
}
|
|
|
|
|
else if (t->flags & CLFLAG_IS_LABEL)
|
|
|
|
|
{
|
|
|
|
|
printf("Label: %s ", t->label_name.data());
|
|
|
|
|
//SetConsoleTextAttribute(StdHandle, 13);
|
|
|
|
|
printf("Label: %s \n", t->label_name.data());
|
|
|
|
|
}
|
|
|
|
|
else if (t->flags & CLFLAG_IS_ABS_ADDR)
|
|
|
|
|
{
|
|
|
|
|
printf("\tAbs jmp to: %s\t", t->label_name.data());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("\tRegular Instruction. ");
|
|
|
|
|
printf("\tInstruction: \t\t");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!(t->flags & CLFLAG_IS_LABEL))
|
|
|
|
|
{
|
|
|
|
|
obf_print_byte_array(t->raw_data, t->raw_data_size);
|
|
|
|
|