Création d'une classe Code pour simplifier la lecture

This commit is contained in:
Horde Nicolas 2021-07-05 15:00:22 +02:00
parent f21ad8016f
commit 6203b579c8
1 changed files with 55 additions and 47 deletions

102
test.cpp
View File

@ -139,6 +139,15 @@ class Goal {
State goal; State goal;
}; };
class Code
{
public:
uint32_t address;
size_t size;
unsigned char *content;
bool assembled;
};
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// Fonctions diverses // Fonctions diverses
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -430,7 +439,7 @@ class Desassembler
{ {
public: public:
Desassembler(TextWindow *log); Desassembler(TextWindow *log);
std::vector<std::array<std::string, 5>> Desassemble(unsigned char* code,size_t codesize,uint32_t address); std::vector<std::array<std::string, 5>> Desassemble(Code *code);
private: private:
csh handle; csh handle;
cs_insn *insn; cs_insn *insn;
@ -455,10 +464,10 @@ Desassembler::Desassembler(TextWindow *log) : log(log)
log->append("Initialisation du désassembleur X86"); log->append("Initialisation du désassembleur X86");
} }
std::vector<std::array<std::string, 5>> Desassembler::Desassemble(unsigned char* code,size_t codesize,uint32_t address) std::vector<std::array<std::string, 5>> Desassembler::Desassemble(Code *code)
{ {
std::stringstream out; std::stringstream out;
srcsize=cs_disasm(handle, code, codesize, address, 0, &insn); srcsize=cs_disasm(handle, code->content, code->size, code->address, 0, &insn);
if (srcsize == 0) if (srcsize == 0)
log->append("Erreur de désassemblage"); log->append("Erreur de désassemblage");
else else
@ -488,18 +497,14 @@ class Assembler
{ {
public: public:
Assembler(TextWindow *log); Assembler(TextWindow *log);
unsigned char *Assemble(std::string source,uint32_t address); Code *Assemble(std::string source,uint32_t address);
size_t getCodesize();
private: private:
ks_engine *ks; ks_engine *ks;
ks_err err; ks_err err;
int err2; int err2;
TextWindow *log; TextWindow *log;
TextEditWindow *edit; TextEditWindow *edit;
size_t srcsize; Code *code = new Code;
size_t codesize;
std::string src;
unsigned char *code = new unsigned char[64*1024];
}; };
Assembler::Assembler(TextWindow *log) : log(log) Assembler::Assembler(TextWindow *log) : log(log)
@ -512,29 +517,27 @@ Assembler::Assembler(TextWindow *log) : log(log)
} }
else else
log->append("Initialisation de l'assembleur X86"); log->append("Initialisation de l'assembleur X86");
code->assembled=false;
} }
size_t Assembler::getCodesize() Code *Assembler::Assemble(std::string source,uint32_t address)
{
return codesize;
}
unsigned char *Assembler::Assemble(std::string source,uint32_t address)
{ {
std::stringstream out; std::stringstream out;
src=source; code->address=address;
srcsize=src.size(); size_t srcsize=source.size();
unsigned char src_char[srcsize+1]; unsigned char src_char[srcsize+1];
strcpy(reinterpret_cast<char*>(src_char), src.c_str()); strcpy(reinterpret_cast<char*>(src_char), source.c_str());
err2=ks_asm(ks, reinterpret_cast<const char*>(src_char), address, &code, &codesize, &srcsize); err2=ks_asm(ks, reinterpret_cast<const char*>(src_char), code->address, &code->content, &code->size, &srcsize);
if (err2 != KS_ERR_OK) if (err2 != KS_ERR_OK)
{ {
log->append("Erreur d'assemblage"); log->append("Erreur d'assemblage");
codesize=0; code->size=0;
code->assembled=false;
} }
else else
{ {
out << "Assemblage réussi, taille du code :" << codesize; out << "Assemblage réussi, taille du code :" << code->size;
code->assembled=true;
log->append(out.str()); log->append(out.str());
/*out.str(""); /*out.str("");
out.clear(); out.clear();
@ -546,7 +549,7 @@ unsigned char *Assembler::Assemble(std::string source,uint32_t address)
log->append(out.str()); log->append(out.str());
}*/ }*/
} }
return reinterpret_cast<unsigned char*>(code); return code;
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -556,8 +559,8 @@ class VMEngine
{ {
public: public:
VMEngine(TextWindow *log); VMEngine(TextWindow *log);
void Configure(State *init); void Configure(State *init,Code *code);
void Run(); void Run(uint32_t start, uint32_t stop);
private: private:
uc_engine *uc; uc_engine *uc;
uc_err err; uc_err err;
@ -576,7 +579,7 @@ VMEngine::VMEngine(TextWindow *log) : log(log)
log->append("Initialisation de l'ordinateur IA86"); log->append("Initialisation de l'ordinateur IA86");
} }
void VMEngine::Configure(State *init) void VMEngine::Configure(State *init, Code *code)
{ {
std::stringstream out; std::stringstream out;
out << "Configuration initiale de l'ordinateur IA86:\n "; out << "Configuration initiale de l'ordinateur IA86:\n ";
@ -662,17 +665,19 @@ void VMEngine::Configure(State *init)
else else
out << "EAX=" << std::uppercase << std::setfill('0') << std::setw(8) << std::hex << init->dump.regs.eax << " "; out << "EAX=" << std::uppercase << std::setfill('0') << std::setw(8) << std::hex << init->dump.regs.eax << " ";
log->append(out.str()); log->append(out.str());
uc_mem_map(uc, init->dump.regs.eip,code->size, UC_PROT_ALL);
if (uc_mem_write(uc, init->dump.regs.eip, code->content, code->size))
{
log->append("Erreur de copie mémoire dans la machine virtuelle");
return;
}
} }
void VMEngine::Run() void VMEngine::Run(uint32_t start, uint32_t stop)
{ {
err=uc_emu_start(uc, start, stop, 0, 0);
} }
/*uc_mem_map(uc, ADDRESS, 1 * 1024 * 1024, UC_PROT_ALL);
if (uc_mem_write(uc, ADDRESS, encode, sizecode)) {
error=uc_emu_start(uc, ADDRESS, ADDRESS + sizecode, 0, 0);*/
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// Classe Menu // Classe Menu
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -692,8 +697,7 @@ class Menu final : public finalcut::FDialog
void loadGoal(); void loadGoal();
private: private:
int scenario=0; int scenario=0;
unsigned char *code; Code *code = new Code();
bool compiled=false;
void configureFileMenuItems(); void configureFileMenuItems();
void initMenusCallBack (); void initMenusCallBack ();
void initMenus(); void initMenus();
@ -933,35 +937,39 @@ void Menu::loadGoal()
void Menu::compile() void Menu::compile()
{ {
code=asmer.Assemble(edit.get(),goals[scenario].init.dump.regs.eip); code=asmer.Assemble(edit.get(),goals[scenario].init.dump.regs.eip);
debug.set(unasmer.Desassemble(code,asmer.getCodesize(),goals[scenario].init.dump.regs.eip)); debug.set(unasmer.Desassemble(code));
compiled=(asmer.getCodesize()>0);
} }
void Menu::verify() void Menu::verify()
{ {
if (!compiled)
{
finalcut::FMessageBox::error(this, "Vous devez compiler le source d'abord !");
return;
}
} }
void Menu::exec() void Menu::exec()
{ {
verify(); if (!code->assembled)
vm.Configure(&goals[scenario].init); {
finalcut::FMessageBox::error(this, "Vous devez compiler le source d'abord !");
return;
}
vm.Configure(&goals[scenario].init,code);
} }
void Menu::trace() void Menu::trace()
{ {
verify(); if (!code->assembled)
} {
finalcut::FMessageBox::error(this, "Vous devez compiler le source d'abord !");
return;
}}
void Menu::step() void Menu::step()
{ {
verify(); if (!code->assembled)
{
} finalcut::FMessageBox::error(this, "Vous devez compiler le source d'abord !");
return;
}}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// Fonction Main // Fonction Main