[原创] IDAPython 系列 —— 画出两个函数的交叉引用图
2023-4-15 16:33:12 Author: bbs.pediy.com(查看原文) 阅读量:8 收藏

第一次在看雪平台投稿,还是有点忐忑的,一直想分享些 idapython 的内容,网上现在的内容太少,都只是教了一些基础的用法。官方的文档和例子又比较散。希望我的分享能让大家更加快乐的逆向吧,哈哈!

这是这个系列的第一篇,分享一下如何用IDAPython 画出两个函数之间的交叉引用图,可视化一直是我很喜欢的一个东西,逆向的目的就是理解代码嘛,可视化是理解代码的一个很重要的工具。

第二步,我们要向 IDA 中添加 action,通过 action 来执行我们的 find_cross_refs函数,首先我们了解一下 IDA 中 action 的概念:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

import idaapi

from .hookers import global_hooker_manager

from typing import Optional

class ActionManager(object):

    def __init__(self):

        self.__actions = []

    def register(self, action):

        self.__actions.append(action)

        idaapi.register_action(

                idaapi.action_desc_t(action.name, action.description, action, action.hotkey)

            )

        if isinstance(action, HexRaysPopupAction):

            global_hooker_manager.register(HexRaysPopupRequestHandler(action))

    def initialize(self):

        pass

    def finalize(self):

        for action in self.__actions:

            idaapi.unregister_action(action.name)

action_manager = ActionManager()

class Action(idaapi.action_handler_t):

    description: Optional[str] = None

    hotkey: Optional[str] = None

    def __init__(self):

        super(Action, self).__init__()

    @property

    def name(self):

        return "HexRaysPyTools:" + type(self).__name__

    def activate(self, ctx: idaapi.action_ctx_base_t):

        raise NotImplementedError

    def update(self, ctx: idaapi.action_ctx_base_t):

        raise NotImplementedError

class HexRaysPopupAction(Action):

    def __init__(self):

        super(HexRaysPopupAction, self).__init__()

    def activate(self, ctx: idaapi.action_ctx_base_t):

        raise NotImplementedError

    def check(self, hx_view):

        raise NotImplementedError

    def update(self, ctx: idaapi.action_ctx_base_t):

        if ctx.widget_type == idaapi.BWN_PSEUDOCODE:

            return idaapi.AST_ENABLE_FOR_WIDGET

        return idaapi.AST_DISABLE_FOR_WIDGET

class HexRaysPopupRequestHandler(idaapi.Hexrays_Hooks):

    def __init__(self, action):

        super().__init__()

        self.__action = action

    def populating_popup(self, widget, popup_handle, vu):

        if self.__action.check(vu):

            idaapi.attach_action_to_popup(widget, popup_handle, self.__action.name, None)

        return 0

代码里面其实还用了很多 sark 库的函数,大家可以自己学习其用法,这个库是专门对 idapython 的 api 做封装的,比较 pythonic,非常好用,我自己也给这个库添加了很多新的功能。


文章来源: https://bbs.pediy.com/thread-276867.htm
如有侵权请联系:admin#unsafe.sh