diff --git a/libEXEIO/include/exeio.h b/libEXEIO/include/exeio.h index 09c5e011d08d9e04e2ab3ca5842bbba664bc2be7..2da0c87070182cdae946700a239c9ca44b6aca49 100644 --- a/libEXEIO/include/exeio.h +++ b/libEXEIO/include/exeio.h @@ -14,8 +14,7 @@ namespace EXEIO typedef enum { ELF64, ELF32, PE32, PE64 } execlass_t; - typedef int virtual_offset_t; - + typedef uintptr_t virtual_offset_t; // create type to match elfio @@ -76,7 +75,7 @@ namespace EXEIO exeio_t(char* filename) { Init(); load(filename); } ~exeio_t() { delete backend; } - virtual void load(std::string filename) { load((char*)filename.c_str()); } + virtual void load(std::string filename) { load((char*)filename.c_str()); } // load the file virtual void load(char* fn); diff --git a/libEXEIO/include/exeio_pe.h b/libEXEIO/include/exeio_pe.h index 60eacb39c89edc4609b8dd6cccd888ff8c6f6c23..cc496d6ec10401ee394caa0732d1177f30e649e4 100644 --- a/libEXEIO/include/exeio_pe.h +++ b/libEXEIO/include/exeio_pe.h @@ -17,7 +17,7 @@ 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); } + exeio_pe_section_t(const pe_bliss::section *the_s, const pe_bliss::pe_base *the_b) : s(the_s),b(the_b) { assert(s); assert(b);} bool isLoadable() const { return s->readable(); } bool isExecutable() const { return s->executable(); } @@ -25,11 +25,15 @@ namespace EXEIO 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(); } + EXEIO::virtual_offset_t get_address() const { + EXEIO::virtual_offset_t base = b->get_image_base_64(); + return base + s->get_virtual_address(); + } bool mightContainStrings() const { assert(0); } private: const pe_bliss::section *s; + const pe_bliss::pe_base *b; }; class exeio_pe_backend_t : public exeio_backend_t @@ -72,7 +76,7 @@ namespace EXEIO it != pe_sections->end(); ++it) { const pe_bliss::section& s = *it; - main->sections.add_section(new EXEIO::exeio_pe_section_t(&s)); + main->sections.add_section(new EXEIO::exeio_pe_section_t(&s,e)); } diff --git a/libIRDB/include/core/address.hpp b/libIRDB/include/core/address.hpp index dd267e9aa1eb2a1ad738aa87b2b3761d3e725574..7495975ef7e6340ef08316f96ac1946710f24318 100644 --- a/libIRDB/include/core/address.hpp +++ b/libIRDB/include/core/address.hpp @@ -18,10 +18,12 @@ * */ +#include <stdint.h> + // // An address in a variant. // -typedef int virtual_offset_t; +typedef uintptr_t virtual_offset_t; class AddressID_t : public BaseObj_t { public: diff --git a/libIRDB/include/core/baseobj.hpp b/libIRDB/include/core/baseobj.hpp index b3d78a64cce6b34944a1d55a535ea1c90b85178c..06a2feb9427354b1a080805ee5a12cd81bccfcbd 100644 --- a/libIRDB/include/core/baseobj.hpp +++ b/libIRDB/include/core/baseobj.hpp @@ -19,8 +19,9 @@ */ +#include <stdint.h> -typedef int virtual_offset_t; +typedef uintptr_t virtual_offset_t; typedef int db_id_t; diff --git a/libIRDB/include/core/basetypes.hpp b/libIRDB/include/core/basetypes.hpp index aacca91c61a4a8b294565a4ef864ad4c6f7defbf..c458d202f4f11ee16a6691d57b3acd3dffabe9e3 100644 --- a/libIRDB/include/core/basetypes.hpp +++ b/libIRDB/include/core/basetypes.hpp @@ -18,8 +18,8 @@ * */ +#include <stdint.h> - -typedef int virtual_offset_t; +typedef uintptr_t virtual_offset_t; typedef int db_id_t; typedef int schema_version_t; diff --git a/libIRDB/include/utils.hpp b/libIRDB/include/utils.hpp index 2e507fe5c1592a8031926b09f12b545558324c2c..5a36002c01f20390b47e35ada38756567f05b125 100644 --- a/libIRDB/include/utils.hpp +++ b/libIRDB/include/utils.hpp @@ -19,12 +19,17 @@ */ - +#ifndef _IRDB_UTIL_ +#define _IRDB_UTIL_ #include <sstream> #include <map> #include <algorithm> #include <set> +#include <string> +#include <iostream> +#include <sstream> +#include <stdint.h> template <class T> inline std::string to_string (const T& t) @@ -74,3 +79,17 @@ inline S const& find_map_object( const std::map< T , S > &a_map, const T& key) return (*it).second; } + +typedef uintptr_t virtual_offset_t; + +template<class T> T strtoint(std::string s) +{ + std::stringstream str(s); + T off; + + str >> off; + + return off; +} + +#endif diff --git a/libIRDB/src/core/fileir.cpp b/libIRDB/src/core/fileir.cpp index d19411fdbdc90ffee18feaf32f4aa1149cc0f139..9aa6b986491d4b3116798a77acc668fb378bc0c9 100644 --- a/libIRDB/src/core/fileir.cpp +++ b/libIRDB/src/core/fileir.cpp @@ -54,6 +54,11 @@ static void UpdateEntryPoints(std::map<db_id_t,Instruction_t*> &insnMap) } +virtual_offset_t strtovo(std::string s) +{ + return strtoint<virtual_offset_t>(s); +} + // Create a Variant from the database FileIR_t::FileIR_t(const VariantID_t &newprogid, File_t* fid) : BaseObj_t(NULL) { @@ -322,13 +327,13 @@ std::map<db_id_t,AddressID_t*> FileIR_t::ReadAddrsFromDB { // address_id integer PRIMARY KEY, // file_id integer REFERENCES file_info, -// vaddress_offset text, +// vaddress_offset bigint, // doip_id integer DEFAULT -1 db_id_t aid=atoi(dbintr->GetResultColumn("address_id").c_str()); db_id_t file_id=atoi(dbintr->GetResultColumn("file_id").c_str()); - int vaddr=atoi(dbintr->GetResultColumn("vaddress_offset").c_str()); + virtual_offset_t vaddr=strtovo(dbintr->GetResultColumn("vaddress_offset")); db_id_t doipid=atoi(dbintr->GetResultColumn("doip_id").c_str()); AddressID_t *newaddr=new AddressID_t(aid,file_id,vaddr); diff --git a/tools/meds2pdb/SConscript b/tools/meds2pdb/SConscript index fe81af7fafc4108e3ac1d7ec939ddaf7f10e3f7d..db4b58d990b47ad16c6cf5e708ebfed62ea561af 100644 --- a/tools/meds2pdb/SConscript +++ b/tools/meds2pdb/SConscript @@ -9,6 +9,7 @@ myenv.Replace(SECURITY_TRANSFORMS_HOME=os.environ['SECURITY_TRANSFORMS_HOME']) cpppath=''' $SECURITY_TRANSFORMS_HOME/include $SECURITY_TRANSFORMS_HOME/xform + $SECURITY_TRANSFORMS_HOME/libEXEIO/include $SECURITY_TRANSFORMS_HOME/libIRDB/include $SECURITY_TRANSFORMS_HOME/libMEDSannotation/include $SECURITY_TRANSFORMS_HOME/beaengine/include diff --git a/tools/meds2pdb/meds2pdb.cpp b/tools/meds2pdb/meds2pdb.cpp index 3aac5bd90c26569e4d82bd3fad88051435f95201..f23b51208227ac99905432bdfe22fd498159ee19 100644 --- a/tools/meds2pdb/meds2pdb.cpp +++ b/tools/meds2pdb/meds2pdb.cpp @@ -115,7 +115,6 @@ void insert_instructions(int fileID, vector<wahoo::Instruction*> instructions, v int address_id = next_address_id++; - // insert into address table if (j != i) query += ","; query += "("; diff --git a/xform/SConscript b/xform/SConscript index 0bff1d5940d4b8314fa98f7077b1153054552432..a6558a57d31b436376d4bf82a68adff4ce2b63ac 100644 --- a/xform/SConscript +++ b/xform/SConscript @@ -22,6 +22,7 @@ files= ''' ''' cpppath=''' $SECURITY_TRANSFORMS_HOME/beaengine/include/ + $SECURITY_TRANSFORMS_HOME/libEXEIO/include/ $SECURITY_TRANSFORMS_HOME/include/ ''' diff --git a/xform/elfreader.cpp b/xform/elfreader.cpp index 7db6b76aa112f72580b96ae088e68bd871c29669..e5fac16a6ab3b4e01b54e2fdd48e0cf4b8e8a911 100644 --- a/xform/elfreader.cpp +++ b/xform/elfreader.cpp @@ -22,20 +22,30 @@ #include <string.h> #include "targ-config.h" +#include <stdio.h> + +/* #include "elfio/elfio.hpp" #include "elfio/elfio_dump.hpp" +*/ #include "elfreader.h" using namespace std; -using namespace ELFIO; +//using namespace ELFIO; +using namespace EXEIO; ElfReader::ElfReader(char *p_elfFile) { - m_reader=new elfio; -// ELFIO::GetInstance()->CreateELFI( &m_reader ); +// m_reader=new elfio; + m_reader=new EXEIO::exeio(p_elfFile); + assert(m_reader); + + EXEIO::dump::header(cout, *m_reader); + EXEIO::dump::section_headers(cout, *m_reader); // Initialize it +/* bool ok = m_reader->load( p_elfFile ); if ( ! ok ) { std::cerr << "Can't open file" << std::endl; @@ -64,6 +74,7 @@ ElfReader::ElfReader(char *p_elfFile) } std::cout << std::endl; +*/ } ElfReader::~ElfReader() diff --git a/xform/elfreader.h b/xform/elfreader.h index bcf58b179b7207da42a1aff6b48da933bfd99d52..23ae15bd471132ab08312d8c6898e72d8aea9fff 100644 --- a/xform/elfreader.h +++ b/xform/elfreader.h @@ -2,7 +2,8 @@ #define _elfreader_H_ #include <vector> -#include "elfio/elfio.hpp" +// #include "elfio/elfio.hpp" +#include "exeio.h" #include "targ-config.h" #include <assert.h> @@ -21,15 +22,16 @@ class ElfReader bool read(app_iaddr_t p_pc, unsigned p_numBytes, char* p_buf); char* getInstructionBuffer(app_iaddr_t p_pc); - bool isElf64() { assert(m_reader); return m_reader->get_class()==ELFCLASS64; } - - - - + bool isElf32() { assert(m_reader); return m_reader->get_class()==EXEIO::ELF32; } + bool isElf64() { assert(m_reader); return m_reader->get_class()==EXEIO::ELF64; } + bool isPe32() { assert(m_reader); return m_reader->get_class()==EXEIO::PE32; } + bool isPe64() { assert(m_reader); return m_reader->get_class()==EXEIO::PE64; } private: - ELFIO::elfio* m_reader; - std::vector < const ELFIO::section* > m_sections; +// ELFIO::elfio* m_reader; + EXEIO::exeio* m_reader; + +// std::vector < const ELFIO::section* > m_sections; }; diff --git a/xform/instruction_descriptor.cpp b/xform/instruction_descriptor.cpp index 9704848cd8aceadbf89a51f7b28659a898b5406a..36164b1be50057caa7da7a996f19192ceb8db290 100644 --- a/xform/instruction_descriptor.cpp +++ b/xform/instruction_descriptor.cpp @@ -18,7 +18,6 @@ * */ -#include <string.h> #include "instruction_descriptor.h" #include "function_descriptor.h" @@ -35,7 +34,6 @@ wahoo::Instruction::Instruction() m_stackRef = false; m_varStackRef = false; m_isVisited = false; -// m_data[0] = '\0'; m_data = NULL; } @@ -50,7 +48,6 @@ wahoo::Instruction::Instruction(app_iaddr_t p_address, int p_size, Function* p_f m_allocSite = false; m_deallocSite = false; m_stackRef = false; - // m_data[0] = '\0'; m_data = NULL; } @@ -86,10 +83,3 @@ void wahoo::Instruction::markVarStackRef() { m_varStackRef = true; } - -/* -void wahoo::Instruction::setData(void *data, int len) -{ - memcpy(m_data, data, len); -} -*/ diff --git a/xform/rewriter.cpp b/xform/rewriter.cpp index d06c56562631e7ef680cae545cf1432178412f6e..a7ed292b4185ff50d427ab03e5f2047380ec617e 100644 --- a/xform/rewriter.cpp +++ b/xform/rewriter.cpp @@ -109,7 +109,7 @@ void Rewriter::readAnnotationFile(char p_filename[]) else annot_type=size_type_u.type; -// fprintf(stderr,"main loop: addr 0x%x scope: %s\n", addr, scope); +// fprintf(stderr,"main loop: addr 0x%p scope: %s\n", addr, scope); /* if the size > 0, then this is a declaration of a variable */ if(strcmp(type,"FUNC")==0) @@ -230,7 +230,7 @@ void Rewriter::readAnnotationFile(char p_filename[]) } else if(strcmp(type,"INSTR")==0) { -//fprintf(stderr, "At 0x%x, handling %s -- scope:%s\n", addr, type, scope); +//fprintf(stderr, "INSTR: At %p, handling %s -- scope:%s\n", addr, type, scope); /* optimizing annotation about not needing heavyweight metadata update */ /* ignore for now */ @@ -667,11 +667,11 @@ void Rewriter::readElfFile(char p_filename[]) objdump=strdup("objdump"); sprintf(buf, "%s -d --prefix-addresses %s | grep \"^[0-9]\"", objdump, p_filename); FILE* pin=popen(buf, "r"); - int addr; + app_iaddr_t addr; assert(pin); - fscanf(pin, "%x", &addr); + fscanf(pin, "%p", &addr); fgets(buf,sizeof(buf),pin); do { @@ -697,6 +697,8 @@ void Rewriter::dissassemble() vector<wahoo::Instruction*> instructions=getAllInstructions(); + fprintf(stderr, "Rewriter::disassemble(): number of instructions: %d\n", instructions.size()); + for (int j = 0; j < instructions.size(); ++j) { wahoo::Instruction *instr = instructions[j]; @@ -707,11 +709,13 @@ void Rewriter::dissassemble() disasm.Options = NasmSyntax + PrefixedNumeral; - if(getElfReader()->isElf64()) + if(getElfReader()->isElf64() || getElfReader()->isPe64()) disasm.Archi = 64; + else disasm.Archi = 32; + disasm.EIP = (UIntPtr) getElfReader()->getInstructionBuffer(instr->getAddress()); disasm.VirtualAddr = instr->getAddress(); @@ -729,7 +733,7 @@ void Rewriter::dissassemble() } else { - cerr<<"BeaEngine has decided that instrution at "<<hex + cerr<<"BeaEngine has decided that instruction at "<<hex <<instr->getAddress()<<dec<<" is bogus."<<endl; /* bogus intruction, remove it */ m_instructions[instr->getAddress()]=NULL; @@ -873,33 +877,6 @@ map<wahoo::Function*, double> Rewriter::getFunctionCoverage(char *p_instructionF visitedInstructions.insert((app_iaddr_t) address); } -/* - - FILE *fp = fopen(p_instructionFile, "r"); - if (!fp) { - cerr << "File containing instructions visited not found:" << p_instructionFile << endl; - return coverage; - } - - cerr<<"func cover checkpoint2"<<endl; - - set<app_iaddr_t> visitedInstructions; - - while (!feof(fp)) - { - int address = 0; - fscanf(fp, "%x\n", &address); - - cerr<<"address = "<<address<<endl; - - visitedInstructions.insert((app_iaddr_t) address); - } - - cerr<<"func cover checkpoint3"<<endl; - - fclose(fp); -*/ - vector<wahoo::Instruction*> allInstructions = getAllInstructions(); for (int i = 0; i < allInstructions.size(); ++i)