def apply_enum_by_name(enum, member_name): member_value = enum.members[member_name].value for line in sark.lines(*sark.get_selection()): for operand in line.insn.operands: if operand.type.is_imm: if operand.imm == member_value: idc.OpEnumEx(line.ea, operand.n, enum.eid, enum.members[member_name].serial) elif operand.type.is_displ or operand.type.is_phrase: if operand.addr == member_value: idc.OpEnumEx(line.ea, operand.n, enum.eid, enum.members[member_name].serial)
def rename_constant(arg_ea, fct_name, arg_name, arg_enums): """ Rename constants to values from standard enumerations. """ instruction = idc.GetMnem(arg_ea) if instruction == 'push': op_num = 0 elif instruction == 'mov': op_num = 1 else: raise RenamingException('Constant: unhandled instruction ' + instruction) op_val = idc.GetOperandValue(arg_ea, op_num) # NULL if op_val == 0: targetid = idc.GetConstByName('NULL_{}_{}'.format(arg_name, fct_name)) serial = 0 enumid = idc.GetEnum(NULL_ENUM_NAME) constid = idc.GetConstEx(enumid, 0, serial, -1) while constid != idaapi.BADADDR: if constid == targetid: idc.OpEnumEx(arg_ea, op_num, enumid, serial) return serial = serial + 1 constid = idc.GetConstEx(enumid, 0, serial, -1) # All other constants op_type = idc.GetOpType(arg_ea, op_num) if op_type == idaapi.o_imm: # only one choice if len(arg_enums) == 1: enumid = idc.GetEnum(arg_enums[0]) idc.OpEnumEx(arg_ea, op_num, enumid, 0) return for enum in arg_enums: enumid = idc.GetEnum(enum) constid = get_constant_id(enumid, op_val) if constid == idaapi.BADADDR: # Not in this enum continue else: # Found the right enum idc.OpEnumEx(arg_ea, op_num, enumid, 0) return
def __call__(self): if self.op == 'hex': idc.OpHex(self.ea, self.n) if self.op == 'bin': idc.OpBinary(self.ea, self.n) if self.op == 'dec': idc.OpDecimal(self.ea, self.n) if self.op == 'chr': idc.OpChr(self.ea, self.n) if self.op == 'oct': idc.OpOctal(self.ea, self.n) if self.op == 'enum': idc.OpEnumEx(self.ea, self.n, self.extra['id'], self.extra['serial'])
def __call__(self): if self.op == 'hex': idc.OpHex(self.ea, self.n) if self.op == 'bin': idc.OpBinary(self.ea, self.n) if self.op == 'dec': idc.OpDecimal(self.ea, self.n) if self.op == 'chr': idc.OpChr(self.ea, self.n) if self.op == 'oct': idc.OpOctal(self.ea, self.n) if self.op == 'enum': id = idaapi.get_enum(self.extra['ename'].encode('utf-8')) idc.OpEnumEx(self.ea, self.n, id, self.extra['serial'])
def fix_names(self): """ Fix the table of imports and map enums to apis. """ start_addr = idc.AskAddr(idc.here(), "Enter table start address") ## check if address is within the base address and maximum address if (start_addr < idc.MinEA()) or (start_addr > idc.MaxEA()): idc.Warning("You have entered an invalid start address") idc.Exit self.start_addr = start_addr current_addr = self.start_addr #Current size of PoisonIvy IAT end_addr = current_addr + 568 # Walk the table 8 bytes at a time while current_addr <= end_addr: idc.MakeQword(current_addr) print "DEBUG: Current address - 0x%08x" % current_addr addr = idc.Qword(current_addr) print "DEBUG: address - 0x%08x" % addr if addr == -1: print "[!] Skipping address 0x%08x - 0x%08x" % (current_addr, addr) current_addr += 8 continue # Make the current address an offset idc.OpOff(current_addr,0,0) # We need to undefine the bytes incase IDA autoanalysis had converted an incorrect byte idc.MakeUnkn(addr, 1) # Create code at this address idc.MakeCode(addr) # Create function at the same address idc.MakeFunction(addr, addr+16) # Read the second operand at the address which should be the negative API address value imp_addr = idc.GetOperandValue(addr, 1) if imp_addr == -1: print "[!] Couldn't get operand at address - 0x%08x" % addr current_addr +=8 continue # try: # int_addr = int(imp_addr,16) # except ValueError as e: # print "[!] Failed on: %s - %s\n" % (imp_addr, e) # current_addr +=8 # continue # if we know about this value then let's do the work if imp_addr in self.enums.keys(): enum_id = self.enums[imp_addr] # Convert operand to enum idc.OpEnumEx(addr, 1, enum_id,0) const_id = idc.GetConstEx(enum_id, imp_addr, 0, -1) fn_name = "fn_"+idc.GetConstName(const_id) off_name = "ptr_"+idc.GetConstName(const_id) # Rename the function to the symbol name. # We append fn_ to the symbol for the function name # and ptr_ to the offset in the table. if not idc.MakeNameEx(addr, fn_name, idaapi.SN_NOWARN): print "[!] Failed to rename function %s at 0x%08x\n" % (fn_name, addr) if not idc.MakeNameEx(current_addr, off_name, idaapi.SN_NOWARN): print "[!] Failed to rename offset %s at 0x%08x\n" % (off_name,current_addr) current_addr += 8 return