xD
master
James 3 years ago
parent 77c11fa473
commit 07d4738ec0

@ -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 int cur_group_id = 0;
int cur_offset = 0;
//obf->groups.clear(); //assign instructions to groups
for (pcode_link_t t = obf->code_start->next; t; t = t->next)
///*if (group_size < 24) {
// return false;*/ if (!(t->flags & CLFLAG_IS_LABEL))
{
//int cur_group_id = 0, cur_size_in_bytes = 0; if (!(t->flags & CLFLAG_IS_GAGET))
//pcode_link_t start = obf->code_start->next; {
//for (pcode_link_t t = obf->code_start->next; t;) if (cur_offset + t->raw_data_size > group_size)
//{ {
// pcode_link_t real_next = t->next; ++cur_group_id;
// if (!(t->flags & CLFLAG_IS_GAGET) && !(t->flags & CLFLAG_IS_LABEL)) cur_offset = 0;
// { }
// if (cur_size_in_bytes + t->raw_data_size /*+ END_OF_GROUP_GAGT_SIZE*/ > group_size) }
// { cur_offset += t->raw_data_size;
// std::string group_label_name = "Group"; t->group = cur_group_id;
// 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; //assign labels to their proper groups
// lab->group = cur_group_id; for (pcode_link_t t = obf->code_start->next; t; t = t->next)
{
// pcode_link_t gadget = new code_link_t; if (t->flags & CLFLAG_IS_LABEL)
// gadget->flags = 0; {
// gadget->label_name = ""; pcode_link_t t2 = t;
// gadget->raw_data = new unsigned char[6]; while (t2 && (t2->flags & CLFLAG_IS_LABEL)) { t2 = t2->next; }
// gadget->raw_data_size = 6; if (t2) t->group = t2->group;
// gadget->group = cur_group_id; }
// unsigned char gadget_data[] = { 0xFF, 0x25, 0x00, 0x00, 0x00, 0x00 }; }
// memcpy(gadget->raw_data, gadget_data, 6);
//create group descriptors
// pcode_link_t abs_addr = new code_link_t; obf->groups.clear();
// abs_addr->flags = CLFLAG_IS_ABS_ADDR; pcode_link_t start = obf->code_start->next;
// abs_addr->label_name = group_label_name; cur_offset = 0;
// abs_addr->raw_data = new unsigned char[8]; pcode_link_t prev_meme = nullptr;
// abs_addr->raw_data_size = 8; for (pcode_link_t t = obf->code_start->next; t; t = t->next)
// abs_addr->group = cur_group_id; {
if (start->group != t->group)
// t->prev->next = gadget; {
// gadget->next = abs_addr; obf->groups.emplace_back(0, start, t->prev, cur_offset);
// abs_addr->next = lab; cur_offset = 0;
// lab->next = t;// real_next; start = t;
}
// gadget->prev = t->prev; cur_offset += t->raw_data_size;
// abs_addr->prev = gadget; prev_meme = t;
// lab->prev = abs_addr; }
// t->prev = lab; if (!prev_meme)
return false;
// obf->groups.emplace_back(0, start, prev_meme, cur_offset);
// printf("creating group %d\n", cur_group_id); //append jumps to next group onto end
// obf->groups.emplace_back(); for (uint32_t i = 0; i < obf->groups.size() - 1; i++)
// obf->groups.back().size_in_bytes = cur_size_in_bytes + END_OF_GROUP_GAGT_SIZE; {
// obf->groups.back().start = start; pcode_group_t cur_group = &obf->groups[i];
// obf->groups.back().base_address = cur_group_id; pcode_group_t next_group = &obf->groups[i+1];
// cur_size_in_bytes = 0;
// cur_group_id++; //add jump gadget to end of current group
// start = t; pcode_link_t gadget = new code_link_t;
// } gadget->flags = CLFLAG_IS_GAGET;
// } gadget->label_name = "";
gadget->raw_data = new unsigned char[6];
// cur_size_in_bytes += t->raw_data_size; gadget->raw_data_size = 6;
// t->group = cur_group_id; gadget->group = i;
// t = real_next; unsigned char gadget_data[] = { 0xFF, 0x25, 0x00, 0x00, 0x00, 0x00 };
//} memcpy(gadget->raw_data, gadget_data, 6);
//obf->groups.emplace_back(); pcode_link_t abs_addr = new code_link_t;
//obf->groups.back().size_in_bytes = cur_size_in_bytes + 16; abs_addr->flags = (CLFLAG_IS_GAGET | CLFLAG_IS_ABS_ADDR);
//obf->groups.back().start = start; abs_addr->label_name = std::string("Group") + std::to_string(i + 1);
//obf->groups.back().base_address = cur_group_id; abs_addr->raw_data = new unsigned char[8];
abs_addr->raw_data_size = 8;
//return true; 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) void obf_replace_rel_jmps(pobfuscator_t obf)
{ // original_jump -------------------------. { // original_jump -------------------------.
// jmp 0x10(0xEB, 0x10) ------------------ | -----. // jmp 0x0E(0xEB, 0x0E) ------------------ | -----.
// jmp qword ptr[rip] <----------------' | // jmp qword ptr[rip] <----------------' |
// address here(8 bytes) | // address here(8 bytes) |
// not taken branch code <-----------------------' // 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) 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) bool obf_gen_label(pobfuscator_t obf, pcode_link_t jmp, int32_t delta)
{ {
obf->current_label_id++;
pcode_link_t temp; pcode_link_t temp;
//when going positive, counting starts at NEXT instruction(excluding size of jmp) //when going positive, counting starts at NEXT instruction(excluding size of jmp)
//when negative, counting INCLUDES the size of the jmp instructrion //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 //couldnt find label, adding new one
pcode_link_t new_label = new code_link_t; pcode_link_t new_label = new code_link_t;
new_label->flags = CLFLAG_IS_LABEL; 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; jmp->label_name = new_label->label_name;
new_label->next = temp; new_label->next = temp;
@ -328,22 +354,41 @@ bool obf_gen_label(pobfuscator_t obf, pcode_link_t jmp, int32_t delta)
return true; return true;
} }
#include <Windows.h>
void obf_dbg_print_code(pobfuscator_t obf) 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) 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) 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) 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 else
{ {
printf("\tRegular Instruction. "); printf("\tInstruction: \t\t");
} }
if (!(t->flags & CLFLAG_IS_LABEL)) if (!(t->flags & CLFLAG_IS_LABEL))
{ {
obf_print_byte_array(t->raw_data, t->raw_data_size); obf_print_byte_array(t->raw_data, t->raw_data_size);

@ -19,34 +19,37 @@ extern "C"
typedef struct _code_link_t typedef struct _code_link_t
{ {
_code_link_t* next; _code_link_t* next;
_code_link_t* prev; _code_link_t* prev;
uint32_t flags; uint32_t flags;
int group; int32_t group;
std::string label_name; std::string label_name;
xed_decoded_inst_t instruction; xed_decoded_inst_t instruction;
unsigned char* raw_data; unsigned char* raw_data;
unsigned int raw_data_size; unsigned int raw_data_size;
}code_link_t, * pcode_link_t; }code_link_t, * pcode_link_t;
typedef struct _code_group_t typedef struct _code_group_t
{ {
uint64_t base_address; uint64_t base_address;
pcode_link_t start; pcode_link_t start;
int size_in_bytes; pcode_link_t end;
int32_t size_in_bytes;
_code_group_t(uint64_t ba = 0, pcode_link_t s = nullptr, pcode_link_t e = nullptr, int32_t si = 0)
: base_address(ba), start(s), end(e), size_in_bytes(si) {};
}code_group_t, *pcode_group_t; }code_group_t, *pcode_group_t;
typedef struct _obfuscator_t typedef struct _obfuscator_t
{ {
pcode_link_t code_start; pcode_link_t code_start;
pcode_link_t code_end; pcode_link_t code_end;
std::vector<code_group_t> groups; std::vector<code_group_t> groups;
int group_size; int32_t group_size;
int current_label_id; int32_t current_label_id;
xed_machine_mode_enum_t machine_mode; xed_machine_mode_enum_t machine_mode;
xed_address_width_enum_t addr_width; xed_address_width_enum_t addr_width;
}obfuscator_t, *pobfuscator_t; }obfuscator_t, *pobfuscator_t;
typedef void* (*FnAllocateMem)(unsigned long size); typedef void* (*FnAllocateMem)(unsigned long size);
@ -58,13 +61,13 @@ void obf_one_time_please();
bool obf_init_from_buffer(pobfuscator_t obf, void* buffer, int buffer_size); bool obf_init_from_buffer(pobfuscator_t obf, void* buffer, int buffer_size);
//creates the groups of instructions based on number of bytes //creates the groups of instructions based on number of bytes
bool obf_create_groups(pobfuscator_t obf, int group_size); bool obf_create_groups(pobfuscator_t obf, int32_t group_size);
//replaces all relative jumps with the abs jump gadget //replaces all relative jumps with the abs jump gadget
void obf_replace_rel_jmps(pobfuscator_t obf); void obf_replace_rel_jmps(pobfuscator_t obf);
//replaces address in the abs jmp stub with the right address of the given label. //replaces address in the abs jmp stub with the right address of the given label.
bool obf_replace_abs_jmps(pobfuscator_t obf); bool obf_resolve_abs_addresses(pobfuscator_t obf);
//return number of bytes needed to store given group //return number of bytes needed to store given group
size_t obf_get_group_size(pobfuscator_t obf, int group_id); size_t obf_get_group_size(pobfuscator_t obf, int group_id);

@ -18,21 +18,23 @@
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
unsigned char buffer[] = { 0x48, 0x33, 0xC0, 0x48, 0x33, 0xC0, 0xEB, 0x08, 0x48, 0x33, 0xC0, 0x7E, 0x03, 0x48, 0x33, 0xC0, 0x48, 0x33, 0xC0 };//{ 0x48, 0x33, 0xC0, 0x48, 0x33, 0xC0, 0xEB, 0xFB, 0x48, 0x33, 0xC0, 0x7E, 0xF6, 0xC3 }; unsigned char buffer[] = { 0x48, 0x33, 0xC0, 0x48, 0x33, 0xC0, 0xEB, 0x08, 0x48, 0x33, 0xC0, 0x7E, 0x03, 0x48, 0x33, 0xC0, 0x48, 0x33, 0xC0, 0x48, 0x33, 0xC0, 0x48, 0x33, 0xC0, 0xEB, 0x08, 0x48, 0x33, 0xC0, 0x7E, 0x03, 0x48, 0x33, 0xC0, 0x48, 0x33, 0xC0 };//{ 0x48, 0x33, 0xC0, 0x48, 0x33, 0xC0, 0xEB, 0xFB, 0x48, 0x33, 0xC0, 0x7E, 0xF6, 0xC3 };
unsigned int buffer_size = sizeof(buffer); unsigned int buffer_size = sizeof(buffer);
obfuscator_t obf; obfuscator_t obf;
obf_one_time_please(); obf_one_time_please();
obf_init_from_buffer(&obf, buffer, buffer_size); obf_init_from_buffer(&obf, buffer, buffer_size);
obf_gen_all_labels(&obf); obf_gen_all_labels(&obf);
obf_replace_rel_jmps(&obf);
obf_dbg_print_code(&obf); obf_create_groups(&obf, 10);
obf_resolve_abs_addresses(&obf);
printf("\n\n"); printf("\n\n");
obf_replace_rel_jmps(&obf);
obf_dbg_print_code(&obf); obf_dbg_print_code(&obf);
printf("%u is num of groups.\n", obf.groups.size());
/*obf_create_groups(&obf, 10);
for (int i = 0; i < obf.groups.size(); i++)
/*for (int i = 0; i < obf.groups.size(); i++)
{ {
printf("\nprinting group %d \n", i); printf("\nprinting group %d \n", i);
obf_dbg_print_group(&obf, i); obf_dbg_print_group(&obf, i);

@ -1,12 +1,11 @@
c:\$fanta\shellcodeobfuscator\shellcodeobfuscator\x64\debug\vc142.pdb c:\$fanta\shellcode-obfuscator\shellcodeobfuscator\x64\debug\vc142.pdb
c:\$fanta\shellcodeobfuscator\shellcodeobfuscator\x64\debug\vc142.idb
c:\$fanta\shellcodeobfuscator\shellcodeobfuscator\x64\debug\main.obj
c:\$fanta\shellcodeobfuscator\shellcodeobfuscator\x64\debug\obfuscator.obj
c:\$fanta\shellcodeobfuscator\x64\debug\shellcodeobfuscator.exe
c:\$fanta\shellcodeobfuscator\x64\debug\shellcodeobfuscator.ilk
c:\$fanta\shellcodeobfuscator\x64\debug\shellcodeobfuscator.pdb
c:\$fanta\shellcode-obfuscator\shellcodeobfuscator\x64\debug\vc142.idb c:\$fanta\shellcode-obfuscator\shellcodeobfuscator\x64\debug\vc142.idb
c:\$fanta\shellcode-obfuscator\shellcodeobfuscator\x64\debug\main.obj
c:\$fanta\shellcode-obfuscator\shellcodeobfuscator\x64\debug\obfuscator.obj
c:\$fanta\shellcode-obfuscator\x64\debug\shellcodeobfuscator.exe
c:\$fanta\shellcode-obfuscator\x64\debug\shellcodeobfuscator.ilk c:\$fanta\shellcode-obfuscator\x64\debug\shellcodeobfuscator.ilk
c:\$fanta\shellcode-obfuscator\x64\debug\shellcodeobfuscator.pdb
c:\$fanta\shellcode-obfuscator\shellcodeobfuscator\x64\debug\obfuscator.new.obj.enc
c:\$fanta\shellcode-obfuscator\shellcodeobfuscator\x64\debug\shellcod.ad60371b.tlog\cl.command.1.tlog c:\$fanta\shellcode-obfuscator\shellcodeobfuscator\x64\debug\shellcod.ad60371b.tlog\cl.command.1.tlog
c:\$fanta\shellcode-obfuscator\shellcodeobfuscator\x64\debug\shellcod.ad60371b.tlog\cl.read.1.tlog c:\$fanta\shellcode-obfuscator\shellcodeobfuscator\x64\debug\shellcod.ad60371b.tlog\cl.read.1.tlog
c:\$fanta\shellcode-obfuscator\shellcodeobfuscator\x64\debug\shellcod.ad60371b.tlog\cl.write.1.tlog c:\$fanta\shellcode-obfuscator\shellcodeobfuscator\x64\debug\shellcod.ad60371b.tlog\cl.write.1.tlog

@ -1,2 +1,6 @@
 main.cpp  Obfuscator.cpp
C:\$Fanta\shellcode-obfuscator\ShellcodeObfuscator\Obfuscator.cpp(86,39): warning C4018: '>': signed/unsigned mismatch
C:\$Fanta\shellcode-obfuscator\ShellcodeObfuscator\Obfuscator.cpp(418,20): warning C4018: '<': signed/unsigned mismatch
C:\$Fanta\shellcode-obfuscator\ShellcodeObfuscator\Obfuscator.cpp(280): warning C4715: 'obf_get_group_size': not all control paths return a value
C:\$Fanta\shellcode-obfuscator\ShellcodeObfuscator\Obfuscator.cpp(72): warning C4715: 'obf_init_from_buffer': not all control paths return a value
ShellcodeObfuscator.vcxproj -> C:\$Fanta\shellcode-obfuscator\x64\Debug\ShellcodeObfuscator.exe ShellcodeObfuscator.vcxproj -> C:\$Fanta\shellcode-obfuscator\x64\Debug\ShellcodeObfuscator.exe

Binary file not shown.
Loading…
Cancel
Save