From 382b1353649472bc2088598b3b8b60c096c1e201 Mon Sep 17 00:00:00 2001 From: Peter LaFosse Date: Fri, 10 Nov 2017 17:42:28 -0500 Subject: Add 'relocatable' property to BinaryView class --- binaryninjaapi.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'binaryninjaapi.h') diff --git a/binaryninjaapi.h b/binaryninjaapi.h index a6276b00..5f3aace7 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -1088,6 +1088,7 @@ namespace BinaryNinja virtual uint64_t PerformGetEntryPoint() const { return 0; } virtual bool PerformIsExecutable() const { return false; } virtual BNEndianness PerformGetDefaultEndianness() const; + virtual bool PerformIsRelocatable() const; virtual size_t PerformGetAddressSize() const; virtual bool PerformSave(FileAccessor* file); @@ -1115,6 +1116,7 @@ namespace BinaryNinja static uint64_t GetEntryPointCallback(void* ctxt); static bool IsExecutableCallback(void* ctxt); static BNEndianness GetDefaultEndiannessCallback(void* ctxt); + static bool IsRelocatableCallback(void* ctxt); static size_t GetAddressSizeCallback(void* ctxt); static bool SaveCallback(void* ctxt, BNFileAccessor* file); @@ -1181,6 +1183,7 @@ namespace BinaryNinja void SetDefaultPlatform(Platform* platform); BNEndianness GetDefaultEndianness() const; + bool IsRelocatable() const; size_t GetAddressSize() const; bool IsExecutable() const; -- cgit v1.3.1 From 0cff612e91e59a343fad898daa6fa51ab525a789 Mon Sep 17 00:00:00 2001 From: Brian Potchik Date: Mon, 13 Nov 2017 17:09:13 -0500 Subject: Add GetInstructionAlignment Callback to Architecture. --- architecture.cpp | 20 ++++++++++++++++++++ binaryninjaapi.h | 3 +++ binaryninjacore.h | 2 ++ examples/x86_extension/src/x86_extension.cpp | 5 +++++ python/architecture.py | 13 ++++++++++++- python/examples/nes.py | 1 + 6 files changed, 43 insertions(+), 1 deletion(-) (limited to 'binaryninjaapi.h') diff --git a/architecture.cpp b/architecture.cpp index eb588394..17d02909 100644 --- a/architecture.cpp +++ b/architecture.cpp @@ -111,6 +111,13 @@ size_t Architecture::GetDefaultIntegerSizeCallback(void* ctxt) } +size_t Architecture::GetInstructionAlignmentCallback(void* ctxt) +{ + Architecture* arch = (Architecture*)ctxt; + return arch->GetInstructionAlignment(); +} + + size_t Architecture::GetMaxInstructionLengthCallback(void* ctxt) { Architecture* arch = (Architecture*)ctxt; @@ -443,6 +450,7 @@ void Architecture::Register(Architecture* arch) callbacks.getEndianness = GetEndiannessCallback; callbacks.getAddressSize = GetAddressSizeCallback; callbacks.getDefaultIntegerSize = GetDefaultIntegerSizeCallback; + callbacks.getInstructionAlignment = GetInstructionAlignmentCallback; callbacks.getMaxInstructionLength = GetMaxInstructionLengthCallback; callbacks.getOpcodeDisplayLength = GetOpcodeDisplayLengthCallback; callbacks.getAssociatedArchitectureByAddress = GetAssociatedArchitectureByAddressCallback; @@ -523,6 +531,12 @@ size_t Architecture::GetDefaultIntegerSize() const } +size_t Architecture::GetInstructionAlignment() const +{ + return 1; +} + + size_t Architecture::GetMaxInstructionLength() const { return BN_DEFAULT_NSTRUCTION_LENGTH; @@ -905,6 +919,12 @@ size_t CoreArchitecture::GetDefaultIntegerSize() const } +size_t CoreArchitecture::GetInstructionAlignment() const +{ + return BNGetArchitectureInstructionAlignment(m_object); +} + + size_t CoreArchitecture::GetMaxInstructionLength() const { return BNGetArchitectureMaxInstructionLength(m_object); diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 5f3aace7..4f62ef5f 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -1585,6 +1585,7 @@ namespace BinaryNinja static BNEndianness GetEndiannessCallback(void* ctxt); static size_t GetAddressSizeCallback(void* ctxt); static size_t GetDefaultIntegerSizeCallback(void* ctxt); + static size_t GetInstructionAlignmentCallback(void* ctxt); static size_t GetMaxInstructionLengthCallback(void* ctxt); static size_t GetOpcodeDisplayLengthCallback(void* ctxt); static BNArchitecture* GetAssociatedArchitectureByAddressCallback(void* ctxt, uint64_t* addr); @@ -1640,6 +1641,7 @@ namespace BinaryNinja virtual size_t GetAddressSize() const = 0; virtual size_t GetDefaultIntegerSize() const; + virtual size_t GetInstructionAlignment() const; virtual size_t GetMaxInstructionLength() const; virtual size_t GetOpcodeDisplayLength() const; @@ -1780,6 +1782,7 @@ namespace BinaryNinja virtual BNEndianness GetEndianness() const override; virtual size_t GetAddressSize() const override; virtual size_t GetDefaultIntegerSize() const override; + virtual size_t GetInstructionAlignment() const override; virtual size_t GetMaxInstructionLength() const override; virtual size_t GetOpcodeDisplayLength() const override; virtual Ref GetAssociatedArchitectureByAddress(uint64_t& addr) override; diff --git a/binaryninjacore.h b/binaryninjacore.h index 45e6310c..cb99958c 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -1003,6 +1003,7 @@ extern "C" BNEndianness (*getEndianness)(void* ctxt); size_t (*getAddressSize)(void* ctxt); size_t (*getDefaultIntegerSize)(void* ctxt); + size_t (*getInstructionAlignment)(void* ctxt); size_t (*getMaxInstructionLength)(void* ctxt); size_t (*getOpcodeDisplayLength)(void* ctxt); BNArchitecture* (*getAssociatedArchitectureByAddress)(void* ctxt, uint64_t* addr); @@ -1946,6 +1947,7 @@ extern "C" BINARYNINJACOREAPI BNEndianness BNGetArchitectureEndianness(BNArchitecture* arch); BINARYNINJACOREAPI size_t BNGetArchitectureAddressSize(BNArchitecture* arch); BINARYNINJACOREAPI size_t BNGetArchitectureDefaultIntegerSize(BNArchitecture* arch); + BINARYNINJACOREAPI size_t BNGetArchitectureInstructionAlignment(BNArchitecture* arch); BINARYNINJACOREAPI size_t BNGetArchitectureMaxInstructionLength(BNArchitecture* arch); BINARYNINJACOREAPI size_t BNGetArchitectureOpcodeDisplayLength(BNArchitecture* arch); BINARYNINJACOREAPI BNArchitecture* BNGetAssociatedArchitectureByAddress(BNArchitecture* arch, uint64_t* addr); diff --git a/examples/x86_extension/src/x86_extension.cpp b/examples/x86_extension/src/x86_extension.cpp index 9efcc119..076551ab 100644 --- a/examples/x86_extension/src/x86_extension.cpp +++ b/examples/x86_extension/src/x86_extension.cpp @@ -327,6 +327,11 @@ public: return LittleEndian; } + virtual size_t GetInstructionAlignment() const override + { + return 1; + } + virtual bool GetInstructionInfo(const uint8_t* data, uint64_t addr, size_t maxLen, InstructionInfo& result) override { return m_arch->GetInstructionInfo(data, addr, maxLen, result); diff --git a/python/architecture.py b/python/architecture.py index 72403fec..41a130bf 100644 --- a/python/architecture.py +++ b/python/architecture.py @@ -111,6 +111,7 @@ class Architecture(object): endianness = Endianness.LittleEndian address_size = 8 default_int_size = 4 + instr_alignment = 1 max_instr_length = 16 opcode_display_length = 8 regs = {} @@ -132,6 +133,7 @@ class Architecture(object): self.__dict__["endianness"] = Endianness(core.BNGetArchitectureEndianness(self.handle)) self.__dict__["address_size"] = core.BNGetArchitectureAddressSize(self.handle) self.__dict__["default_int_size"] = core.BNGetArchitectureDefaultIntegerSize(self.handle) + self.__dict__["instr_alignment"] = core.BNGetArchitectureInstructionAlignment(self.handle) self.__dict__["max_instr_length"] = core.BNGetArchitectureMaxInstructionLength(self.handle) self.__dict__["opcode_display_length"] = core.BNGetArchitectureOpcodeDisplayLength(self.handle) self.__dict__["stack_pointer"] = core.BNGetArchitectureRegisterName(self.handle, @@ -228,6 +230,7 @@ class Architecture(object): self._cb.getEndianness = self._cb.getEndianness.__class__(self._get_endianness) self._cb.getAddressSize = self._cb.getAddressSize.__class__(self._get_address_size) self._cb.getDefaultIntegerSize = self._cb.getDefaultIntegerSize.__class__(self._get_default_integer_size) + self._cb.getInstructionAlignment = self._cb.getInstructionAlignment.__class__(self._get_instruction_alignment) self._cb.getMaxInstructionLength = self._cb.getMaxInstructionLength.__class__(self._get_max_instruction_length) self._cb.getOpcodeDisplayLength = self._cb.getOpcodeDisplayLength.__class__(self._get_opcode_display_length) self._cb.getAssociatedArchitectureByAddress = \ @@ -385,7 +388,8 @@ class Architecture(object): def __setattr__(self, name, value): if ((name == "name") or (name == "endianness") or (name == "address_size") or - (name == "default_int_size") or (name == "regs") or (name == "get_max_instruction_length")): + (name == "default_int_size") or (name == "regs") or (name == "get_max_instruction_length") or + (name == "get_instruction_alignment")): raise AttributeError("attribute '%s' is read only" % name) else: try: @@ -420,6 +424,13 @@ class Architecture(object): log.log_error(traceback.format_exc()) return 4 + def __get_instruction_alignment(self, ctxt): + try: + return self.__class__.instr_alignment + except: + log.log_error(traceback.format_exc()) + return 1 + def _get_max_instruction_length(self, ctxt): try: return self.__class__.max_instr_length diff --git a/python/examples/nes.py b/python/examples/nes.py index e55a90b7..39544c9f 100644 --- a/python/examples/nes.py +++ b/python/examples/nes.py @@ -367,6 +367,7 @@ class M6502(Architecture): name = "6502" address_size = 2 default_int_size = 1 + instr_alignment = 1 max_instr_length = 3 regs = { "a": RegisterInfo("a", 1), -- cgit v1.3.1 From eb862da7472ed36e584a59329dbc56a4de7be017 Mon Sep 17 00:00:00 2001 From: Peter LaFosse Date: Fri, 24 Nov 2017 10:29:18 -0500 Subject: Adding API's to get Linux CA bundle info --- binaryninjaapi.cpp | 18 ++++++++++++++++++ binaryninjaapi.h | 3 +++ binaryninjacore.h | 2 ++ 3 files changed, 23 insertions(+) (limited to 'binaryninjaapi.h') diff --git a/binaryninjaapi.cpp b/binaryninjaapi.cpp index b774cf2b..f5eede03 100644 --- a/binaryninjaapi.cpp +++ b/binaryninjaapi.cpp @@ -344,3 +344,21 @@ string BinaryNinja::GetUniqueIdentifierString() BNFreeString(str); return result; } + + +string BinaryNinja::GetLinuxCADirectory() +{ + char* str = BNGetLinuxCADirectory(); + string result = str; + BNFreeString(str); + return result; +} + + +string BinaryNinja::GetLinuxCABundlePath() +{ + char* str = BNGetLinuxCABundlePath(); + string result = str; + BNFreeString(str); + return result; +} diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 4f62ef5f..e4ef4c43 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -3581,4 +3581,7 @@ namespace BinaryNinja bool IsArray() const; bool IsKeyValueStore() const; }; + + std::string GetLinuxCADirectory(); + std::string GetLinuxCABundlePath(); } diff --git a/binaryninjacore.h b/binaryninjacore.h index cb99958c..82198efd 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -3185,6 +3185,8 @@ extern "C" BINARYNINJACOREAPI BNMetadata* BNBinaryViewQueryMetadata(BNBinaryView* view, const char* key); BINARYNINJACOREAPI void BNBinaryViewRemoveMetadata(BNBinaryView* view, const char* key); + BINARYNINJACOREAPI char* BNGetLinuxCADirectory(); + BINARYNINJACOREAPI char* BNGetLinuxCABundlePath(); #ifdef __cplusplus } #endif -- cgit v1.3.1 From 448ffe2842b2b1e0d21f905403489d3f865672d8 Mon Sep 17 00:00:00 2001 From: Brian Potchik Date: Mon, 4 Dec 2017 13:12:06 -0500 Subject: Add MediumLevelIL Function Recognizer. --- binaryninjaapi.h | 2 ++ binaryninjacore.h | 1 + functionrecognizer.cpp | 18 ++++++++++++++++++ python/functionrecognizer.py | 15 +++++++++++++++ 4 files changed, 36 insertions(+) (limited to 'binaryninjaapi.h') diff --git a/binaryninjaapi.h b/binaryninjaapi.h index e4ef4c43..baeb5de6 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -2951,6 +2951,7 @@ namespace BinaryNinja class FunctionRecognizer { static bool RecognizeLowLevelILCallback(void* ctxt, BNBinaryView* data, BNFunction* func, BNLowLevelILFunction* il); + static bool RecognizeMediumLevelILCallback(void* ctxt, BNBinaryView* data, BNFunction* func, BNMediumLevelILFunction* il); public: FunctionRecognizer(); @@ -2959,6 +2960,7 @@ namespace BinaryNinja static void RegisterArchitectureFunctionRecognizer(Architecture* arch, FunctionRecognizer* recog); virtual bool RecognizeLowLevelIL(BinaryView* data, Function* func, LowLevelILFunction* il); + virtual bool RecognizeMediumLevelIL(BinaryView* data, Function* func, MediumLevelILFunction* il); }; class UpdateException: public std::exception diff --git a/binaryninjacore.h b/binaryninjacore.h index 82198efd..7a0e585e 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -1191,6 +1191,7 @@ extern "C" { void* context; bool (*recognizeLowLevelIL)(void* ctxt, BNBinaryView* data, BNFunction* func, BNLowLevelILFunction* il); + bool (*recognizeMediumLevelIL)(void* ctxt, BNBinaryView* data, BNFunction* func, BNMediumLevelILFunction* il); }; struct BNTypeParserResult diff --git a/functionrecognizer.cpp b/functionrecognizer.cpp index 32c7245c..34f1db80 100644 --- a/functionrecognizer.cpp +++ b/functionrecognizer.cpp @@ -38,11 +38,22 @@ bool FunctionRecognizer::RecognizeLowLevelILCallback(void* ctxt, BNBinaryView* d } +bool FunctionRecognizer::RecognizeMediumLevelILCallback(void* ctxt, BNBinaryView* data, BNFunction* func, BNMediumLevelILFunction* il) +{ + FunctionRecognizer* recog = (FunctionRecognizer*)ctxt; + Ref dataObj = new BinaryView(BNNewViewReference(data)); + Ref funcObj = new Function(BNNewFunctionReference(func)); + Ref ilObj = new MediumLevelILFunction(BNNewMediumLevelILFunctionReference(il)); + return recog->RecognizeMediumLevelIL(dataObj, funcObj, ilObj); +} + + void FunctionRecognizer::RegisterGlobalRecognizer(FunctionRecognizer* recog) { BNFunctionRecognizer reg; reg.context = recog; reg.recognizeLowLevelIL = RecognizeLowLevelILCallback; + reg.recognizeMediumLevelIL = RecognizeMediumLevelILCallback; BNRegisterGlobalFunctionRecognizer(®); } @@ -52,6 +63,7 @@ void FunctionRecognizer::RegisterArchitectureFunctionRecognizer(Architecture* ar BNFunctionRecognizer reg; reg.context = recog; reg.recognizeLowLevelIL = RecognizeLowLevelILCallback; + reg.recognizeMediumLevelIL = RecognizeMediumLevelILCallback; BNRegisterArchitectureFunctionRecognizer(arch->GetObject(), ®); } @@ -60,3 +72,9 @@ bool FunctionRecognizer::RecognizeLowLevelIL(BinaryView*, Function*, LowLevelILF { return false; } + + +bool FunctionRecognizer::RecognizeMediumLevelIL(BinaryView*, Function*, MediumLevelILFunction*) +{ + return false; +} diff --git a/python/functionrecognizer.py b/python/functionrecognizer.py index 8514a2ee..4ac304ca 100644 --- a/python/functionrecognizer.py +++ b/python/functionrecognizer.py @@ -36,6 +36,7 @@ class FunctionRecognizer(object): self._cb = core.BNFunctionRecognizer() self._cb.context = 0 self._cb.recognizeLowLevelIL = self._cb.recognizeLowLevelIL.__class__(self._recognize_low_level_il) + self._cb.recognizeMediumLevelIL = self._cb.recognizeMediumLevelIL.__class__(self._recognize_medium_level_il) @classmethod def register_global(cls): @@ -62,3 +63,17 @@ class FunctionRecognizer(object): def recognize_low_level_il(self, data, func, il): return False + + def _recognize_medium_level_il(self, ctxt, data, func, il): + try: + file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(data)) + view = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(data)) + func = function.Function(view, handle = core.BNNewFunctionReference(func)) + il = mediumlevelil.MediumLevelILFunction(func.arch, handle = core.BNNewMediumLevelILFunctionReference(il)) + return self.recognize_medium_level_il(view, func, il) + except: + log.log_error(traceback.format_exc()) + return False + + def recognize_medium_level_il(self, data, func, il): + return False -- cgit v1.3.1 From b207c76729df133c7b6fc4bc9f1abb6948ae6438 Mon Sep 17 00:00:00 2001 From: Peter LaFosse Date: Thu, 14 Dec 2017 18:46:10 -0500 Subject: Add autoDefined attribute to Sections and Segments --- binaryninjaapi.h | 2 ++ binaryninjacore.h | 2 ++ binaryview.cpp | 2 +- python/binaryview.py | 17 ++++++++++------- 4 files changed, 15 insertions(+), 8 deletions(-) (limited to 'binaryninjaapi.h') diff --git a/binaryninjaapi.h b/binaryninjaapi.h index baeb5de6..72f74c5a 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -1027,6 +1027,7 @@ namespace BinaryNinja uint64_t start, length; uint64_t dataOffset, dataLength; uint32_t flags; + bool autoDefined; }; struct Section @@ -1037,6 +1038,7 @@ namespace BinaryNinja uint64_t infoData; uint64_t align, entrySize; BNSectionSemantics semantics; + bool autoDefined; }; struct QualifiedNameAndType; diff --git a/binaryninjacore.h b/binaryninjacore.h index 1bb0c70a..245e6353 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -1531,6 +1531,7 @@ extern "C" uint64_t start, length; uint64_t dataOffset, dataLength; uint32_t flags; + bool autoDefined; }; enum BNSectionSemantics @@ -1551,6 +1552,7 @@ extern "C" uint64_t infoData; uint64_t align, entrySize; BNSectionSemantics semantics; + bool autoDefined; }; struct BNAddressRange diff --git a/binaryview.cpp b/binaryview.cpp index 0a082458..50011a0f 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -1724,6 +1724,7 @@ vector BinaryView::GetSegments() segment.dataOffset = segments[i].dataOffset; segment.dataLength = segments[i].dataLength; segment.flags = segments[i].flags; + segment.autoDefined = segments[i].autoDefined; result.push_back(segment); } @@ -1731,7 +1732,6 @@ vector BinaryView::GetSegments() return result; } - bool BinaryView::GetSegmentAt(uint64_t addr, Segment& result) { BNSegment segment; diff --git a/python/binaryview.py b/python/binaryview.py index 9f149ba8..6eb59db1 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -416,12 +416,13 @@ class BinaryViewType(object): class Segment(object): - def __init__(self, start, length, data_offset, data_length, flags): + def __init__(self, start, length, data_offset, data_length, flags, auto_defined): self.start = start self.length = length self.data_offset = data_offset self.data_length = data_length self.flags = flags + self.auto_defined = auto_defined @property def executable(self): @@ -450,7 +451,7 @@ class Segment(object): class Section(object): - def __init__(self, name, section_type, start, length, linked_section, info_section, info_data, align, entry_size, semantics): + def __init__(self, name, section_type, start, length, linked_section, info_section, info_data, align, entry_size, semantics, auto_defined): self.name = name self.type = section_type self.start = start @@ -461,6 +462,7 @@ class Section(object): self.align = align self.entry_size = entry_size self.semantics = SectionSemantics(semantics) + self.auto_defined = auto_defined @property def end(self): @@ -965,7 +967,7 @@ class BinaryView(object): result = [] for i in xrange(0, count.value): result.append(Segment(segment_list[i].start, segment_list[i].length, - segment_list[i].dataOffset, segment_list[i].dataLength, segment_list[i].flags)) + segment_list[i].dataOffset, segment_list[i].dataLength, segment_list[i].flags, segment_list[i].autoDefined)) core.BNFreeSegmentList(segment_list) return result @@ -979,7 +981,7 @@ class BinaryView(object): result[section_list[i].name] = Section(section_list[i].name, section_list[i].type, section_list[i].start, section_list[i].length, section_list[i].linkedSection, section_list[i].infoSection, section_list[i].infoData, section_list[i].align, section_list[i].entrySize, - section_list[i].semantics) + section_list[i].semantics, section_list[i].autoDefined) core.BNFreeSectionList(section_list, count.value) return result @@ -3353,7 +3355,7 @@ class BinaryView(object): if not core.BNGetSegmentAt(self.handle, addr, segment): return None result = Segment(segment.start, segment.length, segment.dataOffset, segment.dataLength, - segment.flags) + segment.flags, segment.autoDefined) return result def get_address_for_data_offset(self, offset): @@ -3386,7 +3388,7 @@ class BinaryView(object): result.append(Section(section_list[i].name, section_list[i].type, section_list[i].start, section_list[i].length, section_list[i].linkedSection, section_list[i].infoSection, section_list[i].infoData, section_list[i].align, section_list[i].entrySize, - section_list[i].semantics)) + section_list[i].semantics, section_list[i].autoDefined)) core.BNFreeSectionList(section_list, count.value) return result @@ -3395,7 +3397,8 @@ class BinaryView(object): if not core.BNGetSectionByName(self.handle, name, section): return None result = Section(section.name, section.type, section.start, section.length, section.linkedSection, - section.infoSection, section.infoData, section.align, section.entrySize, section.semantics) + section.infoSection, section.infoData, section.align, section.entrySize, section.semantics, + section_list.autoDefined) core.BNFreeSection(section) return result -- cgit v1.3.1