diff --git a/.gitattributes b/.gitattributes index 3b284ae742cdc3192276f1041d394a9bf226845b..cadaae1c54699dba5ad8a6ff8a5a8d27033d3e06 100644 --- a/.gitattributes +++ b/.gitattributes @@ -188,9 +188,13 @@ libEXEIO/SConscript -text libEXEIO/SConstruct -text libEXEIO/include/exeio.h -text libEXEIO/include/exeio_elf.h -text +libEXEIO/include/exeio_pe.h -text libEXEIO/src/SConscript -text libEXEIO/src/SConstruct -text libEXEIO/src/exeio_src.cpp -text +libEXEIO/test/SConscript -text +libEXEIO/test/SConstruct -text +libEXEIO/test/main.cpp -text libIRDB/LICENSE.txt -text libIRDB/Makefile.in -text libIRDB/SConscript -text diff --git a/libEXEIO/SConscript b/libEXEIO/SConscript index 4440b8bb34cf1d2b31e414b30a40dec6b26155b4..604584b909c66881ab125837a6412526befc0890 100644 --- a/libEXEIO/SConscript +++ b/libEXEIO/SConscript @@ -3,3 +3,4 @@ import os Import('env') SConscript("src/SConscript") +SConscript("test/SConscript") diff --git a/libEXEIO/include/exeio.h b/libEXEIO/include/exeio.h index 4537352e67ba2db9b6d7fe08d07404fc42051bbf..09c5e011d08d9e04e2ab3ca5842bbba664bc2be7 100644 --- a/libEXEIO/include/exeio.h +++ b/libEXEIO/include/exeio.h @@ -12,7 +12,7 @@ namespace EXEIO { class exeio_t; // forward decl - typedef enum { ELF64, ELF32 } execlass_t; + typedef enum { ELF64, ELF32, PE32, PE64 } execlass_t; typedef int virtual_offset_t; diff --git a/libEXEIO/include/exeio_pe.h b/libEXEIO/include/exeio_pe.h new file mode 100644 index 0000000000000000000000000000000000000000..60eacb39c89edc4609b8dd6cccd888ff8c6f6c23 --- /dev/null +++ b/libEXEIO/include/exeio_pe.h @@ -0,0 +1,144 @@ +#ifndef EXEIO_PE_H +#define EXEIO_PE_H + +#include <iostream> +#include <vector> +#include <assert.h> +#include <fstream> +#include <pe_bliss.h> + + +class exeio_backend; +class exeio_section; + + +namespace EXEIO +{ + class exeio_pe_section_t : public exeio_section_t + { + public: + exeio_pe_section_t(const pe_bliss::section *the_s) : s(the_s) { assert(s); } + + bool isLoadable() const { return s->readable(); } + bool isExecutable() const { return s->executable(); } + bool isBSS() const { return s->empty(); } + const char* get_data() const { return s->get_raw_data().c_str(); } + std::string get_name() const { return s->get_name(); } + int get_size() const { return s->get_virtual_size(); } + EXEIO::virtual_offset_t get_address() const { return s->get_virtual_address(); } + bool mightContainStrings() const { assert(0); } + + private: + const pe_bliss::section *s; + }; + + class exeio_pe_backend_t : public exeio_backend_t + { + public: + exeio_pe_backend_t() : e(NULL), main(NULL), pe_sections(NULL) {} + ~exeio_pe_backend_t() + { + if(!main) + return; + // remove subclasses. + for(int i=0;i<main->sections.size(); ++i) + { + // copy each section into the main class' structure. + delete main->sections[i]; + } + + // remove the pe_bliss class. + delete e; + } + void load(exeio* the_main, char* filename) + { + main=the_main; + + std::ifstream pe_file(filename, std::ios::in | std::ios::binary); + if(!pe_file) + { + std::cerr << "Cannot open " << filename << std::endl; + assert(0); + exit(1); + } + + + e=new pe_bliss::pe_base(pe_bliss::pe_factory::create_pe(pe_file)); + assert(e); + + pe_sections=new pe_bliss::section_list(e->get_image_sections()); + + for(pe_bliss::section_list::const_iterator it = pe_sections->begin(); + it != pe_sections->end(); ++it) + { + const pe_bliss::section& s = *it; + main->sections.add_section(new EXEIO::exeio_pe_section_t(&s)); + } + + + } + virtual void dump_header(std::ostream& stream) + { + assert(e); + + std::cout << "EP : " <<std::hex<< e->get_ep() << std::endl; + std::cout << "EP section name: " << e->section_from_rva(e->get_ep()).get_name() << std::endl; + std::cout << "EP section data length: " << + e->section_data_length_from_rva(e->get_ep()) << std::endl; + + if(e->has_imports()) + { + std::cout << "Import section name: " << + e->section_from_directory(pe_bliss::pe_win + ::image_directory_entry_import).get_name() + << std::endl; + } + } + virtual void dump_section_headers(std::ostream& stream) + { + assert(e); + for(pe_bliss::section_list::const_iterator it = pe_sections->begin(); + it != pe_sections->end(); ++it) + { + const pe_bliss::section& s = *it; //Ð¡ÐµÐºÑ†Ð¸Ñ + std::cout << "Section [" << s.get_name() << "]" << std::endl //Ð˜Ð¼Ñ Ñекции + << "Characteristics: " << s.get_characteristics() << std::endl + << "Size of raw data: " << s.get_size_of_raw_data() << std::endl + << "Virtual address: " << s.get_virtual_address() << std::endl + << "Virtual size: " << s.get_virtual_size() << std::endl + << std::endl; + } + + } + virtual execlass_t get_class() + { + assert(e); + if(e->get_pe_type() == pe_bliss::pe_type_32) + return PE32; + if(e->get_pe_type() == pe_bliss::pe_type_64) + return PE64; + assert(0); + } + + // get entry point of function. + virtual virtual_offset_t get_entry() + { + assert(e); + return (virtual_offset_t)e->get_ep(); + } + + + virtual bool isDLL() { assert(0); } + + + + private: + pe_bliss::pe_base* e; + pe_bliss::section_list* pe_sections; + EXEIO::exeio* main; + }; + + +} + +#endif diff --git a/libEXEIO/src/SConscript b/libEXEIO/src/SConscript index 8e84e12f90eac54f1d76c848c63ffb094eedea34..edc6d28696c1abb771ea84a320fec0b423edd3e9 100644 --- a/libEXEIO/src/SConscript +++ b/libEXEIO/src/SConscript @@ -14,9 +14,11 @@ cpppath=''' . $SECURITY_TRANSFORMS_HOME/include/ $SECURITY_TRANSFORMS_HOME/libEXEIO/include + $SECURITY_TRANSFORMS_HOME/pebliss/trunk/pe_lib/ ''' #myenv.Append(CCFLAGS="-Wp,-w -Wall") +myenv.Append(CCFLAGS="-w") myenv=myenv.Clone(CPPPATH=Split(cpppath)) lib=myenv.Library(libname, Split(files)) diff --git a/libEXEIO/src/exeio_src.cpp b/libEXEIO/src/exeio_src.cpp index afecb4db349aa4331f784272f253ffb2d9386c77..fb59192a94afc3277db34d14e79b633cb8d54eea 100644 --- a/libEXEIO/src/exeio_src.cpp +++ b/libEXEIO/src/exeio_src.cpp @@ -1,15 +1,34 @@ +#include <istream> +#include <fstream> #include <exeio.h> #include <exeio_elf.h> +#include <exeio_pe.h> using namespace EXEIO; +using namespace pe_bliss; using namespace std; void exeio::load(char* filename) { - backend=new exeio_elf_backend_t(); + + ifstream instream(filename); + + if(!instream) + assert(0 && "Cannot open file"); + + + // check for elf magic number + if( instream.get()=='E' && instream.get()=='L' && instream.get()=='F' ) + { + backend=new exeio_elf_backend_t(); + } + // we assume it's ELF or PE. + else + backend=new exeio_pe_backend_t(); + backend->load(this, filename); } diff --git a/libEXEIO/test/SConscript b/libEXEIO/test/SConscript new file mode 100644 index 0000000000000000000000000000000000000000..a8581a4c89caab5fb8228f114fcd27dba4785d04 --- /dev/null +++ b/libEXEIO/test/SConscript @@ -0,0 +1,37 @@ +import os + +Import('env') +myenv=env +myenv.Replace(SECURITY_TRANSFORMS_HOME=os.environ['SECURITY_TRANSFORMS_HOME']) + + +libname="test" +files= ''' + main.cpp + ''' + +cpppath=''' + . + $SECURITY_TRANSFORMS_HOME/include/ + $SECURITY_TRANSFORMS_HOME/libEXEIO/include + $SECURITY_TRANSFORMS_HOME/libEXEIO/include + ''' +libpath=''' + $SECURITY_TRANSFORMS_HOME/lib/ + ''' + +#myenv.Append(CCFLAGS="-Wp,-w -Wall") +myenv.Append(CCFLAGS="-g") + +libs= ''' + EXEIO + PEBLISS + iconv + ''' + +myenv=myenv.Clone(CPPPATH=Split(cpppath), LIBS=Split(libs), LIBPATH=Split(libpath)) +lib=myenv.Program(libname, Split(files)) + +#install=myenv.Install("$SECURITY_TRANSFORMS_HOME/lib/", lib) +#Default(install) + diff --git a/libEXEIO/test/SConstruct b/libEXEIO/test/SConstruct new file mode 100644 index 0000000000000000000000000000000000000000..c0dd68a00d406b0148a93709cf916ad6d05f282c --- /dev/null +++ b/libEXEIO/test/SConstruct @@ -0,0 +1,6 @@ + + + +env=Environment() +Export('env') +SConscript("SConscript") diff --git a/libEXEIO/test/main.cpp b/libEXEIO/test/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..041d395f36ece81ab882115d7f097c0b19b200e7 --- /dev/null +++ b/libEXEIO/test/main.cpp @@ -0,0 +1,23 @@ +#include <iostream> +#include <exeio.h> + +using namespace std; +using namespace EXEIO; + + +int main(int argc, char* argv[]) +{ + if(argc!=2) + { + cout<<"Usage: "<<argv[0]<<": <exe>"<<endl; + } + + exeio *exep=new exeio; + + exep->load(argv[1]); + + EXEIO::dump::header(cout,*exep); + EXEIO::dump::section_headers(cout,*exep); + + return 0; +}