Python keystone.KsError() Examples
The following are 6
code examples of keystone.KsError().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
keystone
, or try the search function
.
Example #1
Source File: assembler.py From chiasm-shell with MIT License | 6 votes |
def _set_arch(self, arch, *modes): """ Try and set the current architecture """ try: a = self.valid_archs[''.join(['KS_ARCH_', arch.upper()])] if a is None: l.error("Invalid architecture selected - run lsarch for valid options") return False ms = [self.modes[''.join(['KS_MODE_', m.upper()])] for m in modes] except KeyError: l.error("ERROR: Invalid architecture or mode string specified") return False try: _ks = ks.Ks(a, sum(ms)) self._arch = (arch, modes) l.debug("Architecture set to %s, mode(s): %s", arch, ', '.join(modes)) self._ks = _ks except ks.KsError as e: l.error("ERROR: %s", e) return False return True
Example #2
Source File: assembler.py From chiasm-shell with MIT License | 5 votes |
def default(self, line): """ Default behaviour - if no other commands are detected, try and assemble the current input according to the currently set architecture. :param line: Current line's text to try and assemble. """ try: encoding, dummy_insn_count = self._ks.asm(line) self._last_encoding = encoding l.info("".join('\\x{:02x}'.format(opcode) for opcode in encoding)) except ks.KsError as e: l.error("ERROR: %s", e)
Example #3
Source File: __base__.py From deen with Apache License 2.0 | 5 votes |
def process(self, data): super(AsmBase, self).process(data) try: encoding, count = self.ks.asm(data.decode()) except keystone.KsError as e: self.error = e self.log.error(self.error) self.log.debug(self.error, exc_info=True) return b'' return bytearray(encoding)
Example #4
Source File: __base__.py From deen with Apache License 2.0 | 5 votes |
def _interactive_assembly(self): # Import readline module to make arrow keys # and command history work in the interactive # mode. import readline prompt = self.display_name + ' > ' if self.args.revert: prompt = 'dsm:' + prompt else: prompt = 'asm:' + prompt while True: try: data = input(prompt) if not data: continue try: if self.args.revert: try: data = codecs.decode(data, 'hex') except Exception: self.write_to_stdout(b'Invalid hex encoding') continue output = self.unprocess(data) if not self.args.plain: output = self._syntax_highlighting(output) self.write_to_stdout(output, nonewline=True) else: encoding, count = self.ks.asm(data) if self.args.raw: output = bytes(bytearray(encoding)) else: output = codecs.encode(bytearray(encoding), 'hex') self.write_to_stdout(output) except keystone.KsError as e: self.log.error(e) self.log.debug(e, exc_info=True) self.write_to_stdout(str(e).encode()) except (KeyboardInterrupt, EOFError): return b''
Example #5
Source File: pe.py From crave with GNU General Public License v3.0 | 5 votes |
def patch_code(self, instructions='ret;',va=0): """ put instruction(s), at the end of the basic block specified""" #TODO: get capstone instruction at the end of the basic_block try: k = ks.Ks(ks.KS_ARCH_X86, ks.KS_MODE_32) encoding, count = k.asm(instructions, va+self.OPTIONAL_HEADER.ImageBase) except ks.KsError as e: l.error("Error! %s", e) raise if not self.set_bytes_at_rva(va, ''.join(map(chr, encoding))): raise Exception('Cannot patch bytes at %x!', va)
Example #6
Source File: assembler.py From dash with MIT License | 4 votes |
def RelaxInstructions(self, store, list_of_fixups, labels): """ Relax assembly instructions as necessary. Args: store: An AssemblyStore instance. list_of_fixups: a dictionary of instructions and their indices that need to be adjusted. labels: A dictionary of label names to addresses. Returns: N / A Side Effects: This will adjust the assembled instructions using wider versions when a labels address is beyond the range of the short forms (e.g. jumps and calls on x86) """ done_relaxing = False #last rounds label addresses last_rounds_label_addresses = labels while not done_relaxing: for row in store.GetRowsIterator(): if row.index not in list_of_fixups: continue mnemonic, label = list_of_fixups[row.index] label_addr_st = hex(labels[label]).replace('L', '') mnemonic = mnemonic.replace(label, label_addr_st) try: encoded_bytes, inst_count = self.assembler.asm(mnemonic, addr=row.address) if inst_count != 1: raise AssemblerError("mnemonic in row %d contains multiple statements" % row.index) opcode_str = "".join(["%02x" % byte for byte in encoded_bytes]) row.opcode = binascii.unhexlify(opcode_str) store.UpdateRow(row.index, row) except keystone.KsError as exc: LOGGER.error(str(exc)) store.SetErrorAtIndex(row.index) break # collect the labels updated addresses for row in store.GetRowsIterator(): if row.label != '': labels[row.label.upper()] = row.address # check to see if any labels differ at all if yes then continue # relaxing if labels == last_rounds_label_addresses: done_relaxing = True for row in store.GetRowsIterator(): self.CheckOpcodeBytes(row, store) else: last_rounds_label_addresses = labels