def show_ctree_graph(create_subgraph=False): w = ida_kernwin.get_current_widget() if ida_kernwin.get_widget_type(w) == ida_kernwin.BWN_PSEUDOCODE: vu = ida_hexrays.get_widget_vdui(w) if vu: vu.get_current_item(ida_hexrays.USE_MOUSE) focusitem = vu.item.e if vu.item.is_citem() else None sub = None if create_subgraph: if not focusitem: return sub = "subgraph %x" % focusitem.obj_id # create graphviewer cg = cfunc_graph_t(focusitem, HRDevHelper.config, close_open=True, subtitle=sub) # build graph for current function gb = graph_builder_t(cg, None if create_subgraph else vu.cfunc) gb.apply_to(focusitem if create_subgraph else vu.cfunc.body, vu.cfunc.body) # show graph cg.Show() # set zoom and dock position cg.zoom_and_dock(w) return
def activate(self, ctx): obj = FILTERS[self.name] obj.set_activated(not obj.is_activated()) vu = hr.get_widget_vdui(ctx.widget) if vu: vu.refresh_view(not obj.is_activated()) return 1
def activate(self, ctx): global DO_FILTER DO_FILTER = not DO_FILTER vu = ida_hexrays.get_widget_vdui(ctx.widget) if vu: vu.refresh_ctext() return 1
def update(self, ctx): vu = ida_hexrays.get_widget_vdui(ctx.widget) if vu is None: return ida_kernwin.AST_DISABLE_FOR_WIDGET if IsPtrSizedLvar(vu): return ida_kernwin.AST_ENABLE return ida_kernwin.AST_DISABLE
def _highlight_hexrays(self, lines_out, widget, lines_in): """ Highlight lines in the given Hex-Rays window according to the synchronized addresses. """ vdui = ida_hexrays.get_widget_vdui(widget) if self._hexrays_addresses or self._hexrays_origin: self._highlight_lines(lines_out, set([vdui.cpos.lnnum]), lines_in)
def _highlight_hexrays(self, lines_out, widget, lines_in): """ TODO """ vdui = ida_hexrays.get_widget_vdui(widget) if self._hexrays_addresses or self._hexrays_origin: self._highlight_lines(lines_out, set([vdui.cpos.lnnum]), lines_in)
def get_lines_rendering_info_ev(self, out, widget, rin): wt = ida_kernwin.get_widget_type(widget) if wt == ida_kernwin.BWN_PSEUDOCODE: vu = ida_hexrays.get_widget_vdui(widget) if vu: cf = vu.cfunc if cf.entry_ea not in self.funcs: return vusync = self.funcs[cf.entry_ea] ea = self.ea if ea in vusync: slist = vusync.get_items(ea) for section_lines in rin.sections_lines: for line in section_lines: lnnum = ida_kernwin.place_t.as_simpleline_place_t(line.at).n for sync_info in slist: ix, iy, ilen = sync_info if lnnum == iy: e = ida_kernwin.line_rendering_output_entry_t(line) e.bg_color = COLOR e.cpx = ix e.nchars = ilen e.flags |= ida_kernwin.LROEF_CPS_RANGE out.entries.push_back(e) return
def acquire_pseudocode_vdui(addr): """ Acquires a IDA HexRays vdui pointer, which is a pointer to a pseudocode view that contains the cfunc which describes the code on the screen. Using this function optimizes the switching of code views by using in-place switching if a view is already present. @param addr: @return: """ func = ida_funcs.get_func(addr) if not func: return None names = ["Pseudocode-%c" % chr(ord("A") + i) for i in range(5)] for name in names: widget = ida_kernwin.find_widget(name) if not widget: continue vu = ida_hexrays.get_widget_vdui(widget) break else: vu = ida_hexrays.open_pseudocode(func.start_ea, False) if func.start_ea != vu.cfunc.entry_ea: target_cfunc = idaapi.decompile(func.start_ea) vu.switch_to(target_cfunc, False) return vu
def refresh_pseudocode_view(): """Refreshes the pseudocode view in IDA.""" names = ["Pseudocode-%c" % chr(ord("A") + i) for i in range(5)] for name in names: widget = ida_kernwin.find_widget(name) if widget: vu = ida_hexrays.get_widget_vdui(widget) vu.refresh_view(True)
def activate(self, ctx): vu = ida_hexrays.get_widget_vdui(ctx.widget) if not vu or not self.sel: print("No vdui? Strange, since this action should be enabled only for pseudocode views.") return 0 form = XrefsForm(self.sel) form.Show() return 1
def _commit_changes(self): vu = ida_hexrays.get_widget_vdui(self.parent_widget) if vu: vu.refresh_ctext() # "refresh_ctext()" took away the focus, take it back kw.activate_widget(kw.find_widget(self.title), True) self.SetFocusedField(self.cbEditable) return True return False
def activate(self, ctx): global IS_ACTIVATED IS_ACTIVATED = not IS_ACTIVATED vu = ida_hexrays.get_widget_vdui(ctx.widget) if vu: vu.refresh_ctext() kw.msg("[%s] %sactivated.\n" % (PLUGIN_NAME, "" if IS_ACTIVATED else "de")) return 1
def activate(self, ctx): # type: (...) -> int vu = hr.get_widget_vdui(ctx.widget) for t in transformers: t.run(vu, vu.cfunc.body, None) vu.cfunc.verify(1, True) vu.cfunc.remove_unused_labels() CleanupVisitor().clean_up(vu.cfunc.body, None) # DebugVisitor().visit(vu.cfunc.body, None) vu.refresh_ctext() return 1
def activate(self, ctx): vu = ida_hexrays.get_widget_vdui(ctx.widget) # ---------------------------------------------- # Do something with the vdui (vu) print "Analyzing decompiled code..." cv = char_converter_visitor_t() cv.apply_to(vu.cfunc.body, None) vu.refresh_ctext() return 1
def activate(self, ctx): vu = ida_hexrays.get_widget_vdui(ctx.widget) if vu: loc = ida_moves.lochist_entry_t() if ida_kernwin.get_custom_viewer_location(loc, ctx.widget): coord = pseudo_line_t(vu.cfunc.entry_ea, _place_to_line_number(loc.place())) if coord in self.hooks.marked_lines.keys(): del self.hooks.marked_lines[coord] else: self.hooks.marked_lines[coord] = self.COLOR_KEY ida_kernwin.refresh_custom_viewer(ctx.widget)
def refresh_pseudocode_view(ea): """Refreshes the pseudocode view in IDA.""" names = ["Pseudocode-%c" % chr(ord("A") + i) for i in range(5)] for name in names: widget = ida_kernwin.find_widget(name) if widget: vu = ida_hexrays.get_widget_vdui(widget) # Check if the address is in the same function func_ea = vu.cfunc.entry_ea func = ida_funcs.get_func(func_ea) if ida_funcs.func_contains(func, ea): vu.refresh_view(True)
def get_lines_rendering_info(self, out, widget, rin): vu = ida_hexrays.get_widget_vdui(widget) if vu: entry_ea = vu.cfunc.entry_ea for section_lines in rin.sections_lines: for line in section_lines: coord = pseudo_line_t(entry_ea, _place_to_line_number(line.at)) color = self.marked_lines.get(coord, None) if color is not None: e = ida_kernwin.line_rendering_output_entry_t(line) e.bg_color = color out.entries.push_back(e)
def activate(self, ctx): vu = ida_hexrays.get_widget_vdui(ctx.widget) vu.get_current_item(ida_hexrays.USE_KEYBOARD) highlight = vu.item.e if vu.item.is_citem() else None cg = cfunc_graph_t(highlight) gb = graph_builder_t(cg) gb.apply_to(vu.cfunc.body, None) import tempfile fname = tempfile.mktemp(suffix=".gdl") cg.gen_gdl(fname) ida_gdl.display_gdl(fname) return 1
def dump_ctree_to_lambda(create_subgraph=False): w = ida_kernwin.get_current_widget() if ida_kernwin.get_widget_type(w) == ida_kernwin.BWN_PSEUDOCODE: vu = ida_hexrays.get_widget_vdui(w) if vu: vu.get_current_item(ida_hexrays.USE_MOUSE) focusitem = vu.cfunc.body if create_subgraph: focusitem = vu.item.e if vu.item.is_citem() else None if focusitem: gd = graph_dumper_t() gd.apply_to(focusitem, vu.cfunc.body) lines = "(%s)" % " and\n".join(gd.lines) print("%s\n%x:\n%s" % ("-" * 80, ida_kernwin.get_screen_ea(), lines))
def activate(self, ctx): # Get current function address print 'activate' curfunc = idc.get_func_attr(idc.get_screen_ea(), idc.FUNCATTR_START) self.cu.execute('select * from xref where y == %d' % curfunc) print 'find calls' while True: r = self.cu.fetchone() if r is None: break i, x, y, z, k = r addxref(x, y, z, i, k) vu = ida_hexrays.get_widget_vdui(ctx.widget) vu.refresh_view(True) return 1
def activate(self, ctx): print("Suggesting variable names...") ea = idaapi.get_screen_ea() vuu = ida_hexrays.get_widget_vdui(ctx.widget) if ea is None: idaapi.warning("Current function not found.") else: f = StringIO() with jsonlines.Writer(f) as writer: try: info, cfunc = func(ea, vuu) # We must set the working directory to the dire dir to open the model correctly os.chdir(dire_dir) p = subprocess.Popen([RUN_ONE, '--model', MODEL], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, encoding=sys.getdefaultencoding()) #print(info) writer.write(info) comm = p.communicate(input=f.getvalue()) json_results = comm[0] stderr = comm[1] if p.returncode != 0: print(stderr) raise ValueError("Variable prediction failed") results = json.loads(json_results) best_results = results[0][0] #print("best: ", best_results) tuples = map( lambda x: (varnames[x[0]] if x[0] in varnames else x[0], x[1]['new_name']), best_results.items()) FinalRename(dict(tuples), cfunc, vuu).apply_to(cfunc.body, None) # Force the UI to update #vuu.refresh_ctext() except ida_hexrays.DecompilationFailure: idaapi.warning("Decompilation failed") except ValueError as e: idaapi.warning( str(e) + ". See output window for more details.") return 1
def update(self, ctx): if ctx.widget_type != ida_kernwin.BWN_PSEUDOCODE: return ida_kernwin.AST_DISABLE_FOR_WIDGET vu = ida_hexrays.get_widget_vdui(ctx.widget) vu.get_current_item(ida_hexrays.USE_KEYBOARD) item = vu.item self.sel = None if item.citype == ida_hexrays.VDI_EXPR and item.it.to_specific_type.opname in ('obj', 'memref', 'memptr'): # if an expression is selected. verify that it's either a cot_obj, cot_memref or cot_memptr self.sel = item.it.to_specific_type elif item.citype == ida_hexrays.VDI_FUNC: # if the function itself is selected, show xrefs to it. self.sel = item.f return ida_kernwin.AST_ENABLE if self.sel else ida_kernwin.AST_DISABLE
def rename_func(): """rename function, suggests current identifier as function name""" name = _get_identifier() if name: str = ida_kernwin.ask_str(name, -1, "Rename function") if str: f = ida_funcs.get_func(ida_kernwin.get_screen_ea()) if f: if ida_name.set_name(f.start_ea, str, ida_name.SN_NOCHECK): cv = ida_kernwin.get_current_viewer() if ida_kernwin.get_widget_type( cv) == ida_kernwin.BWN_PSEUDOCODE: vd = ida_hexrays.get_widget_vdui(cv) if vd: vd.refresh_view(True) return
def run(self, arg): w = ida_kernwin.get_current_widget() if ida_kernwin.get_widget_type(w) == ida_kernwin.BWN_PSEUDOCODE: vu = ida_hexrays.get_widget_vdui(w) vu_title = ida_kernwin.get_widget_title(w) if vu: vu.get_current_item(ida_hexrays.USE_KEYBOARD) highlight = vu.item.e if vu.item.is_citem() else None # create graphviewer cg = cfunc_graph_t(highlight, True) # build graph for current function gb = graph_builder_t(cg) gb.apply_to(vu.cfunc.body, None) # show graph cg.Show() # set zoom and dock position cg.zoom_and_dock(vu_title, ZOOM, DOCK_POSITION)
def run(self, arg): w = ida_kernwin.get_current_widget() if ida_kernwin.get_widget_type(w) == ida_kernwin.BWN_PSEUDOCODE: vu = ida_hexrays.get_widget_vdui(w) vu_title = ida_kernwin.get_widget_title(w) if vu: vu.get_current_item(ida_hexrays.USE_KEYBOARD) focusitem = vu.item.e if vu.item.is_citem() else None # create graphviewer cg = cfunc_graph_t(focusitem, self.config, close_open=True) # build graph for current function gb = graph_builder_t(cg) gb.apply_to(vu.cfunc.body, None) # show graph cg.Show() # set zoom and dock position cg.zoom_and_dock(vu_title, self.config["options"]["zoom"], self.config["options"]["dockpos"])
def activate(self, ctx): vu = ida_hexrays.get_widget_vdui(ctx.widget) if not IsPtrSizedLvar(vu): return 1 lvar = vu.item.get_lvar() name = ida_kernwin.ask_str( "", ida_kernwin.HIST_IDENT, "Please enter the API name for which to set the type") if name is None: return 1 ptrTif = GetTypeSignature(name) if ptrTif is None: ida_kernwin.warning("Could not get type for \"%s\"" % name) return 1 ChangeVariableType(vu.cfunc.entry_ea, lvar, ptrTif) vu.cfunc.refresh_func_ctext() return 1
def get_all_vdui(): """ Return every visible vdui_t (Hex-Rays window). """ found = {} # TODO: A-Z.. eh good enough for widget_title in ["Pseudocode-%c" % chr(0x41+i) for i in range(0, 26)]: # try to find the hexrays widget of the given name widget = ida_kernwin.find_widget(widget_title) if not widget: continue # make sure the widget looks in-use vdui = ida_hexrays.get_widget_vdui(widget) if not (vdui and vdui.visible): continue found[widget_title] = vdui return found
def activate(self, ctx): # get the current item vu = ida_hexrays.get_widget_vdui(ctx.widget) vu.get_current_item(ida_hexrays.USE_KEYBOARD) # REGION1, will be referenced later # check that the current item is a union field if not vu.item.is_citem(): ida_kernwin.warning("Please position the cursor on a union member") return 0 e = vu.item.e while True: op = e.op if op != ida_hexrays.cot_memptr and op != ida_hexrays.cot_memref: ida_kernwin.warning( "Please position the cursor on a union member") return 0 e = e.x if op == ida_hexrays.cot_memptr: if e.type.is_union(): break else: if ida_typeinf.remove_pointer(e.type).is_union(): break if not e.type.is_udt(): ida_kernwin.warning( "Please position the cursor on a union member") return 0 # END REGION1 # REGION2 # calculate the member offset off = 0 e = vu.item.e while True: e2 = e.x tif = ida_typeinf.remove_pointer(e2.type) if not tif.is_union(): off += e.m e = e2 if e2.op != ida_hexrays.cot_memptr and e2.op != ida_hexrays.cot_memref: break if not e2.type.is_udt(): break # END REGION2 # REGION3 # go up and collect more member references (in order to calculate the final offset) p = vu.item.e while True: p2 = vu.cfunc.body.find_parent_of(p) if p2.op == ida_hexrays.cot_memptr: break if p2.op == ida_hexrays.cot_memref: e2 = p2.cexpr tif = ida_typeinf.remove_pointer(e2.x.type) if not tif.is_union(): off += e2.m p = p2 continue if p2.op == ida_hexrays.cot_ref: # handle &a.b + N (this expression may appear if the user previously selected # a wrong field) delta = 0 add = vu.cfunc.body.find_parent_of(p2) if add.op == ida_hexrays.cot_cast: add = vu.cfunc.body.find_parent_of(add) if add.op == ida_hexrays.cot_add and add.y.op == ida_hexrays.cot_num: delta = add.y.numval() objsize = add.type.get_ptrarr_objsize() nbytes = delta * objsize off += nbytes # we could use helpers like WORD/BYTE/... to calculate a more precise offset # if ( p2->op == cot_call && (e2->exflags & EXFL_LVALUE) != 0 ) break # END REGION3 # REGION4 ea = vu.item.e.ea # the item itself may be unaddressable. # TODO: find its addressable parent if ea == ida_idaapi.BADADDR: ida_kernwin.warning("Sorry, the current item is not addressable") return 0 # END REGION4 # REGION5 # prepare the text representation for the item, # use the neighborhoods of cursor line = ida_lines.tag_remove( ida_kernwin.get_custom_viewer_curline(vu.ct, False)) line_len = len(line) x = max(0, vu.cpos.x - 10) l = min(10, line_len - vu.cpos.x) + 10 line = line[x:x + l] # END REGION5 # REGION6 ops = ida_hexrays.ui_stroff_ops_t() op = ops.push_back() op.offset = off op.text = line # END REGION6 # REGION7 class set_union_sel_t(ida_hexrays.ui_stroff_applicator_t): def __init__(self, ea): ida_hexrays.ui_stroff_applicator_t.__init__(self) self.ea = ea def apply(self, opnum, path, top_tif, spath): typename = ida_typeinf.print_tinfo('', 0, 0, ida_typeinf.PRTYPE_1LINE, top_tif, '', '') ida_kernwin.msg("User selected %s of type %s\n" % (spath, typename)) if path.empty(): return False vu.cfunc.set_user_union_selection(self.ea, path) vu.cfunc.save_user_unions() return True # END REGION7 # REGION8 su = set_union_sel_t(ea) res = ida_hexrays.select_udt_by_offset(None, ops, su) if res != 0: # regenerate ctree vu.refresh_view(True) # END REGION8 return 1
def activate(self, ctx): if load_cfg(reload=True): vu = ida_hexrays.get_widget_vdui(ctx.widget) if vu: vu.refresh_ctext() return 1
def activate(self, ctx): udc_exit.toggle_install(); udc_exit_group.toggle_install(); vu = ida_hexrays.get_widget_vdui(ctx.widget) vu.refresh_view(True) return 1