首页
社区
课程
招聘
6
[原创]Immunity Debugger内存搜素python脚本
发表于: 2013-3-2 15:39 10278

[原创]Immunity Debugger内存搜素python脚本

2013-3-2 15:39
10278
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import struct
import math
import libanalyze
 
RD_CHAR = "*"
FD_CHAR ="-"
 
#get max end function
def func_max_end(imm, addr):
    fun = libanalyze.Function(imm, addr)
    end1 = fun.getFunctionEnd()
    if len(end1) == 0:
        return 0
    end_max = end1[0]
    for e in end1:
        if e>end_max:
            end_max = e
    return end_max           
                 
#search asmcode in function
#["mov byte ptr ss:[R32+100], R8", 3,"mov byte ptr ss:[R32+101], R8"] R32 R_32 R16  R_16 R8 R_8
#ret []
def search_asmcode_infun(imm, fun_begin_addr, c_list):
    end_max = func_max_end(imm, fun_begin_addr)
    if end_max==0 :
        return []
    ret_addr = []
    space = -1
    begin_addr = fun_begin_addr
    imm.log("")
    imm.log("start to search asm !", highlight = 1)
    imm.log("-----------------------------------------------------")
    for c in c_list:
        if type(c)== type(1):
            space = c
        else:
            s_ret = search_asmcode(imm, begin_addr, end_max, c, space)
            if len(s_ret) !=0:
                begin_addr = s_ret[0]
                imm.log("get address at 0x%x" % begin_addr)
                ret_addr.append(begin_addr)
                space = -1
    imm.log("-----------------------------------------------------")
    return ret_addr
 
#"lea eax, dword ptr ds:[ecx+edx-FA]" -->  "lea eax, dword ptr ds:[ecx+edx-0xFA]"
def change_special(asm_code):
    def change_list_chrsp(st, chr_sp):
        st_tmp = st
        for chr_elt in chr_sp:
            l_flt = change_hex(st_tmp, chr_elt)
            st_tmp= l_flt
        return st_tmp
             
    def change_hex(st, chr_sp):
        l = st.split(chr_sp)
        xx = []
        for ii in l:
            try:
                xx.append("0x%x" %int(ii, 16))
            except (ValueError, TypeError):
                xx.append("%s" % ii)
        return chr_sp.join(xx)
    fit = asm_code.find("[")
    sec = asm_code.find("]")
    aft = ""
    if (fit!=-1) and (sec !=-1) and (sec>fit):
        ll = asm_code[fit+1: sec]
        aft += change_list_chrsp(ll, "+-*")
        asm_code = asm_code.replace(asm_code[fit+1: sec], aft)
    asm_code = change_list_chrsp(asm_code, ", ")
    if asm_code.startswith("0x"):
        asm_code = asm_code[2:]
    return asm_code
     
def search_asmcode(imm, start_addr, end_addr, asmcode, space = -1):
    imm.log("asmreg to search: %s  space: %d  addr: 0x%x" % (asmcode, space, start_addr), highlight = 1)
    register_32 = ["EAX", "EBX", "ECX", "EDX", "EBP", "ESI", "EDI", "ESP"]
    register_16 = ["AX", "BX", "CX", "DX", "BP", "SI", "DI", "SP"]
    register_8 = ["AH", "AL", "BH", "BL", "CL", "CH", "DL", "DH"]
    ret_list = []
    ret_list.append(asmcode)
    ret_list.extend([xx.replace("R32", r) for r in register_32 for xx in ret_list if (xx.find("R32")!= -1)])
    ret_list.extend([xx.replace("R_32", r) for r in register_32  for xx in ret_list if (xx.find("R_32")!= -1)])
    ret_list.extend([xx.replace("R16", r) for r in register_16  for xx in ret_list if (xx.find("R16")!= -1)])
    ret_list.extend([xx.replace("R_16", r) for r in register_16  for xx in ret_list if (xx.find("R_16")!= -1)])
    ret_list.extend([xx.replace("R_8", r) for r in register_8  for xx in ret_list if (xx.find("R_8")!= -1)])
    ret_list.extend([xx.replace("R8", r) for r in register_8  for xx in ret_list if (xx.find("R8")!= -1)])
    ret_list = [r for r in ret_list if ((r.find("R32")==-1) and (r.find("R_32")==-1) and (r.find("R16")==-1) \
                                        and (r.find("R_16")==-1) and (r.find("R8")==-1) and (r.find("R_8")==-1))]
    code = ""
    for r in ret_list :
        code += imm.assemble(change_special(r)) # get code list
         
    cnt = 0
    while True:
        op = imm.disasm(start_addr)
        start_addr = start_addr + op.getSize()
        new_asm = change_special(op.getDisasm())
        this_code = imm.assemble(new_asm)
        if (this_code in code) and (cnt<=space if space>0 else True):
            imm.log("hit addr 0x%x   asm:%s"%(op.getAddress(), op.getDisasm()), highlight = 1)
            return [op.getAddress(), op.getDisasm()]           
        if start_addr > end_addr:
            break
        cnt = cnt +1
    return []
 
     
#list[0x12345678, 0x12345678]
def search_code_Long(imm, begin, end, code_list, min, max):
    st = ""
    if (min<0) or (min>max):
        return []
    if type(code_list) != type([]):
        return []
    for i in range(len(code_list)-1):
        st += "%02X%02X%02X%02X"%(code_list[i]&0xff, (code_list[i]>>0x8)&0xff, (code_list[i]>>0x10)&0xff, (code_list[i]>>0x18)&0xff)
        st += "%s%02x%02x" % (RD_CHAR, min, max) if max>0 else ""
    last_num = code_list[len(code_list)-1]
    st += "%02X%02X%02X%02X"%(last_num&0xff, (last_num>>0x8)&0xff, (last_num>>0x10)&0xff, (last_num>>0x18)&0xff)
    return search_code_string(imm, begin, end, st)
     
#list[0x1234, 0x1234]   
def search_code_short(imm, begin, end, code_list, min, max):
    st = ""
    if (min<0) or (min>max):
        return []
    if type(code_list) != type([]):
        return []
    for i in range(len(code_list)-1):
        st += "%02X%02X"%(code_list[i]&0xff, (code_list[i]>>0x8)&0xff)
        st += "%s%02x%02x" % (RD_CHAR, min, max) if max>0 else ""
    last_num = code_list[len(code_list)-1]
    st += "%02X%02X"%(last_num&0xff, (last_num>>0x8)&0xff)
    return search_code_string(imm, begin, end, st)
     
#list[0x34, 0x34]
def search_code_byte(imm, begin, end, code_list, min, max):
    st = ""
    if (min<0) or (min>max):
        return []
    if type(code_list) != type([]):
        return []
    for i in range(len(code_list)-1):
        st += "%02X"%(code_list[i]&0xff)
        st += "%s%02x%02x" % (RD_CHAR, min, max) if max>0 else ""
    last_num = code_list[len(code_list)-1]
    st += "%02X"%(last_num&0xff)
    return search_code_string(imm, begin, end, st)
 
#str  "GUOJICHONGYUN" "\xff\xff\xff\xff"
def search_data_str(imm, begin, end, str_search, min, max):
    st = ""
    if (min<0) or (min>max):
        return []
    if type(str_search) != type(""):
        return []
    for i in range(len(str_search)-1):
        st += "%02X"%(ord(str_search[i]))
        st += "%s%02x%02x"%(RD_CHAR, min, max) if max>0 else ""
    st += "%02X"%(ord(str_search[len(str_search)-1]))
    return search_code_string(imm, begin, end, st)
 
#search code string
#ef00*0209EabA*0304eeaa  FF00-04AA 53f966*0000E000008ab70183C40899F7*00007C24
def search_code_string(imm, begin, end, code_str):
    st1 = code_str.upper()
    ret_error = 0
    ret = []
    imm.log("the code to search:%s"%st1, highlight = 1)
    start = 0
    x = 0
    for aa in st1:
        if (aa ==RD_CHAR) or (aa == FD_CHAR):
            break
        else:
            x += 1
    y = 0
    min_n = []
    max_n = []
    s1 = []
    s2 = []
    if x == len(st1):
        s1.append(st1)
    else:
        count_ = 0
        while True:
            count_ = 0
            if st1[x] == FD_CHAR:
                min_max_tmp = int(st1[x+1:x+3], 16)
                min_n.append(min_max_tmp*2)
                max_n.append(min_max_tmp*2)
                count_ = 3
            else:
                min_tmp = int(st1[x+1:x+3], 16)
                max_tmp = int(st1[x+3:x+5], 16)
                if (min_tmp>max_tmp) or (min_tmp<0):
                    return []
                min_n.append(min_tmp*2)
                max_n.append(max_tmp*2)
                count_ = 5
            if start != x:
                s1.append(st1[start:x])
            y = 0
            for aa in st1[x+count_:]:
                if (aa ==RD_CHAR) or (aa == FD_CHAR):
                    break
                else:
                    y += 1
            if y == len(st1[x+count_:]):
                s2.append(st1[x+count_:])
                break
            else:
                s2.append(st1[x+count_:y+x+count_])
                x = y + x + count_
                start = x
    #'''
    imm.log("min_n  %s " % min_n, highlight = 1)
    imm.log("max_n  %s " % max_n, highlight = 1)
    imm.log("s1     %s " % s1, highlight = 1)   
    imm.log("s2     %s " % s2, highlight = 1) #'''
    len1 = len(s1[0])
    mun1_str = ""
    for i in range(len1/2):
        x = ((int(s1[0][2*i])) if s1[0][2*i].isdigit() else (int(s1[0][2*i], 16)))*math.pow(16, 1)
        x += ((int(s1[0][2*i+1])) if s1[0][2*i+1].isdigit() else (int(s1[0][2*i+1], 16)))*math.pow(16, 0)
        mun1_str += struct.pack("B", x)
    first_ = imm.search(mun1_str)
    byte_num = 0
    for i in max_n:
        byte_num += i
    for si in s2:
        byte_num += len(si)
    for fx in first_:
        if (fx<begin) or (fx>end):
            continue
        buf = imm.readMemory(fx, byte_num)
        mem_read_str = ''.join(['%02X' % ord(x) for x in buf[0:]])
        imm.log("addr 0x%x  mem_read_str %s" %(fx, mem_read_str), highlight = 1)
        start = len(s1[0])
        i = 0
        flag = 0
        for s2_tmp in s2:
            y = mem_read_str[start:].find(s2_tmp)
            if y == -1:
                flag = 0
                break
            elif (y>=min_n[i]) and (y<=max_n[i]):
                flag += 1
                start = start + y + len(s2_tmp)
            else:
                break
            i = i +1
            #imm.log("flag %d len1 %d  i %d"%(flag, len1, i))
        #imm.log("flag %d len1%d"%(flag, len1))
        if flag == len(min_n):
            ret.append(fx)
    return ret
        

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

上传的附件:
收藏
免费 6
支持
分享
赞赏记录
参与人
雪币
留言
时间
伟叔叔
为你点赞~
2024-5-31 07:11
心游尘世外
为你点赞~
2024-5-31 04:07
QinBeast
为你点赞~
2024-5-31 03:58
飘零丶
为你点赞~
2024-4-3 00:03
shinratensei
为你点赞~
2024-2-4 04:11
PLEBFE
为你点赞~
2023-3-7 00:36
最新回复 (7)
雪    币: 143
活跃值: (263)
能力值: ( LV8,RANK:120 )
在线值:
发帖
回帖
粉丝
2
顶楼主!!!!
弱弱的问一个,为什么我的imm 在cmdbar里面Python 脚本不能用呢?提示import ...err 相同的命令在shell里面却可以
2013-3-2 16:17
0
雪    币: 143
活跃值: (263)
能力值: ( LV8,RANK:120 )
在线值:
发帖
回帖
粉丝
3
顶楼主!!!!
弱弱的问一个,为什么我的imm 在cmdbar里面Python 脚本不能用呢?提示import ...err 相同的命令在shell里面却可以
2013-3-2 16:18
0
雪    币: 209
活跃值: (15)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
没有用过immunity debugger,但python中一般出现import xxxx error的时候,你需要:
import sys
sys.path 看看你需要的那些库的路径是否被包含进来,如果没有就
sys.path.append(r'your lib path')导入进来,然后重新导入你的库就行了
2013-3-2 19:39
0
雪    币: 297
活跃值: (120)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
5
冒个泡支持,看标题就知道是好东西。
2013-3-2 21:47
0
雪    币: 307
活跃值: (65)
能力值: ( LV5,RANK:70 )
在线值:
发帖
回帖
粉丝
6
imm的shell里面,imm自动导入了一些模块,如果要用脚本的话,就得自己import。import模块的时候注意,模块名别写错了,而且这个模块文件在imm的Libs目录、PyCommands目录、python的安装目录下的lib子目录中存在。
2013-3-2 23:27
0
雪    币: 675
活跃值: (42)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
7
有个叫mona.py的脚本很强大,你可以去看看,好像有你说的这个功能
2013-3-3 20:33
0
雪    币: 307
活跃值: (65)
能力值: ( LV5,RANK:70 )
在线值:
发帖
回帖
粉丝
8
mona.py很强大,谢谢~~
2013-3-14 10:53
0
游客
登录 | 注册 方可回帖
返回

账号登录
验证码登录

忘记密码?
没有账号?立即免费注册