IDA模板

MyIdc类对IDA中的接口进行封装
目前实现功能:

  • 按指定类型获取数据,请在初始化时初始化类型'byte': 1字节, 'word': 2字节, 'dword': 4字节, 'qword': 8字节
  • get_word方法的输出格式可以通过show_type参数指定'bin':2进制,'oct':8进制,'dec':10进制,'hex':16进制,默认为16进制
  • get_word方法的类型通过type参数指定'byte': 1字节, 'word': 2字节, 'dword': 4字节, 'qword': 8字节,可以与类的type不相同

更新计划:

  • 添加patch函数
  • ........
class MyIdc:
    """
    对pythonidc进行封装
    """
    types = {'byte': 1, 'word': 2, 'dword': 4, 'qword': 8}
    get_data_funs = {'byte': idc.get_wide_byte, 'word': idc.get_wide_word, 'dword': idc.get_wide_dword,
                     'qword': idc.get_qword}

    def __init__(self, type='byte'):
        self.type = ''
        self.width = None
        self.get_data = None
        if not type in MyIdc.types.keys():
            raise Exception("类型初始化错误")
        else:
            self.type = type
            self.width = MyIdc.types[type]
            self.get_data = MyIdc.get_data_funs[type]

    def show_type(self):
        print(self.type)

    def __show_list(self, local_list, show_type):
        if show_type not in ['bin', 'oct', 'dec', 'hex']:
            raise Exception("show_type参数问题:请在['bin','oct','dec','hex']中选择参数")
        print('长度为%d' % len(local_list))
        if show_type == 'bin':
            print('[{}]'.format(', '.join(bin(x) for x in local_list)))
        elif show_type == 'oct':
            print('[{}]'.format(', '.join(oct(x) for x in local_list)))
        elif show_type == 'dec':
            print(local_list)
        elif show_type == 'hex':
            print('[{}]'.format(', '.join(hex(x) for x in local_list)))

    def get_word(self, start_addr=None, length=None, end_addr=None, type=None, show_type='hex', max_length=1024):
        """
	    按指定类型获取数据
        :param start_addr: 数据的开始地址
        :param length: 【可选】数据的长度,以自定义类型为单位,如word为2,dword为4
        :param end_addr: 【可选】结尾地址
        :param type: 【可选】指定数据类型
        :param show_type: 【可选】 显示的类型 默认为16进制 其他进制请参考 'bin':2进制,'oct':8进制,'dec':10进制,'hex':16进制
        :param max_length: 【可选】最大长度,默认为1024
        :return: 读取数据的数组
        """
        anslist = []
        cout = 0
        width = self.width
        local_get_word = self.get_data

        print(
            '\n----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------')
        if start_addr is None:
            print('请输入start_addr')
            return
        if end_addr is None and length is None:
            print('请输入end_addr或length')
            return
        if type is not None:
            print('类型:', type)
            width = MyIdc.types[type]
            local_get_word = MyIdc.get_data_funs[type]
        if length:
            max_length = length

        if end_addr is None:
            for i in range(max_length):
                x = local_get_word(start_addr + i * width)
                anslist.append(x)
                cout += 1
        else:
            for i in range(max_length):
                if start_addr + i * width < end_addr:
                    x = local_get_word(start_addr + i * width)
                    anslist.append(x)
                    cout += 1
                else:
                    break

        self.__show_list(anslist, show_type)

        return anslist

使用实例

mi = MyIdc('dword')
mi.get_word(start_addr=0x0001940, length=10, type='byte',show_type='dec')

运行效果如下

欢迎留言,提出意见

posted @ 2023-05-17 17:36  noahze  阅读(56)  评论(0编辑  收藏  举报