找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 7770|回复: 6

ida python函数

[复制链接]

307

主题

228

回帖

7319

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5569 个
贡献
253 次
宅之契约
0 份
在线时间
945 小时
注册时间
2014-1-25
发表于 2015-10-18 15:34:46 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
本帖最后由 元始天尊 于 2015-10-18 16:07 编辑

总结一下:
idautils.py

CodeRefsFrom(ea,flow)   查找给定代码段地址指令引用到的代码
ea=地址
for ref in CodeRefsFrom(ScreenEA(), 1):
        print ref

CodeRefsTo(ea,flow)   查找引用给定代码段地址指令到的代码
DataRefsTo(ea,flow)
DataRefsTo(ea,flow)

XrefsFrom(ea, flags=0) 查找所有交叉引用
XrefsTo(ea, flags=0)

Functions(start=None, end=None)  获取地址范围中的每个函数
Chunks(start) 获取指定地址所在函数范围
Names() 遍历符号表
Segments() 遍历各个段基址
Entries() 入口点信息
FuncItems(start)  每行汇编代码的地址
Structs()  遍历结构体
StructMembers(sid)  遍历结构体成员

DecodePrecedingInstruction(ea)  获取指令结构
DecodePreviousInstruction(ea)
DecodeInstruction(ea)

GetInputFileMD5() 获取输入文件md5
Strings(object) 获取字符串
GetIdbDir() 获取idb目录
GetRegisterList() 获取寄存器名表
GetInstructionList 获取汇编指令表

Assemble(ea, line)  汇编一条指令到机器码
print Assemble(0x13480,{"nop",GetDisasm(0x13480)})
(True, ['\xe8O\xc6\x01\x00', '\x90'])

atoa(ea)  获取所在段
Jump(ea) 移动光标
Eval(expr) 计算表达式
Exec(command) 执行命令行
MakeCode(ea)  分析代码区
MakeNameEx(ea, name, flags)  重命名地址
MakeArray(ea, nitems) 创建数组
MakeStr(ea, endea) 创建字符串
MakeData(ea, flags, size, tid) 创建数据
MakeByte(ea)
MakeWord(ea)
MakeDWord(ea)
MakeQWord(ea)
MakeOWord(ea)
MakeYWord(ea)
MakeFlot(ea)
MakeDouble(ea)
MakePackReal(ea)
MakeTbyte(ea)
MakeStructEx(ea)
MakeCustomDataEx(ea)

PatchByte(ea, value) 修改程序字节
PatchWord(ea, value)
PatchDword(ea, value)
PatchByte(ea, value)
PatchByte(ea, value)

Byte(ea) 将地址解释为Byte
Word(ea)
DWord(ea)
QWord(ea)
GetFloat(ea)
GetDouble(ea)
GetDisasm(ea) 获取反汇编
GetString(ea, length = -1, strtype = ASCSTR_C)  获取字符串
LocByName(name)  获取符号地址

ScreenEA()  获取光标所在地址
GetCurrentLine()  获取光标所在行反汇编

ItemSize(ea)  获取指令或数据长度

FindText(ea, flag, y, x, searchstr)查找文本
FindBinary(ea, flag, searchstr, radix=16) 查找16进制

FirstSeg()  遍历pe段
NextSeg(ea)
SegStart(ea)  获取段首位置
SegEnd(ea)  获取段尾位置
SegName(ea)  获取段名

GetEntryPointQty() 获取入口点个数
GetEntryOrdinal(index)  获取入口点地址
GetEntryName(ordinal) 入口名





回复

使用道具 举报

307

主题

228

回帖

7319

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5569 个
贡献
253 次
宅之契约
0 份
在线时间
945 小时
注册时间
2014-1-25
 楼主| 发表于 2015-10-30 23:56:26 | 显示全部楼层
本帖最后由 元始天尊 于 2015-11-4 13:35 编辑

查找mov [ebp+?],?  形式存储的字符串,用python脚本自动查找:

  1. Base=0
  2. while Base < 0xFFFFFFFF:
  3.         print SegName(Base)
  4.         if SegName(Base) == ".text" or SegName(Base) == "INIT":
  5.                 codebegin=Base
  6.                 codeend=SegEnd(Base)
  7.                 while codebegin <= codeend:
  8.                         if Word(codebegin) == 0x45C6 and Word(codebegin+4) == 0x45C6 and Byte(codebegin+3) < 0x80:
  9.                                 str=""
  10.                                 print "string at %x"%(codebegin)
  11.                                 while (Word(codebegin) == 0x45C6 ):
  12.                                         str=str+chr(Byte(codebegin+3))
  13.                                         codebegin=codebegin+4                                         
  14.                                 print str  
  15.                         elif Word(codebegin) == 0x85C6 and Word(codebegin+7) == 0x85C6 and Byte(codebegin+6) < 0x80:
  16.                                 str=""
  17.                                 print "string at %x"%(codebegin)
  18.                                 while (Word(codebegin) == 0x85C6 ):
  19.                                                 str=str+chr(Byte(codebegin+6))
  20.                                                 codebegin=codebegin+7                                               
  21.                                 print str         
  22.                         elif Byte(codebegin) == 0x6A and Byte(codebegin+7) == 0x6A and Byte(codebegin+1) < 0x80:
  23.                                 str=""
  24.                                 print "string at %x"%(codebegin)
  25.                                 while (Byte(codebegin) == 0x6A ):
  26.                                                 str=str+chr(Byte(codebegin+1))
  27.                                                 codebegin=codebegin+7                                               
  28.                                 print str                         
  29.                         codebegin=codebegin+1
  30.         Base=NextSeg(Base)
复制代码
回复 赞! 靠!

使用道具 举报

307

主题

228

回帖

7319

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5569 个
贡献
253 次
宅之契约
0 份
在线时间
945 小时
注册时间
2014-1-25
 楼主| 发表于 2015-10-31 15:29:56 | 显示全部楼层
如何处理 经过处理的字符串,除了上帖中的问题,还存在以下情况:
RegPath[0] = '\\';
  RegPath[1] = 'R';
  RegPath[2] = 'E';
  RegPath[3] = 'G';
  RegPath[4] = 'I';
  RegPath[5] = 'S';
  RegPath[6] = 'T';
  RegPath[7] = 'R';
  RegPath[8] = 'Y';
  RegPath[9] = '\\';
  RegPath[10] = 'M';
  RegPath[11] = 'A';
  RegPath[12] = 'C';
  RegPath[13] = 'H';
  RegPath[14] = 'I';
  RegPath[15] = 'N';
  RegPath[16] = 'E';
  RegPath[17] = '\\';
  RegPath[18] = 'S';
  RegPath[19] = 'Y';
  RegPath[20] = 'S';
  RegPath[21] = 'T';
  RegPath[22] = 'E';
  RegPath[23] = 'M';
  RegPath[24] = '\\';
  RegPath[25] = 'C';
  RegPath[26] = 'U';
  RegPath[27] = 'R';
  RegPath[28] = 'R';
  RegPath[29] = 'E';
  RegPath[30] = 'N';
  RegPath[31] = 'T';
  RegPath[32] = 'C';
  RegPath[33] = 'O';
  RegPath[34] = 'N';
  RegPath[35] = 'T';
  RegPath[36] = 'R';
  RegPath[45] = 'R';
  RegPath[37] = 'O';
  RegPath[46] = 'V';
  RegPath[47] = 'I';
  RegPath[48] = 'C';
  RegPath[49] = 'E';
  RegPath[50] = 'S';
  RegPath[52] = 'T';
  RegPath[38] = 'L';
  RegPath[53] = 'S';
  RegPath[39] = 'S';
  RegPath[54] = 'D';
  RegPath[40] = 'E';
  RegPath[55] = 'E';
  RegPath[41] = 'T';
  RegPath[56] = 'F';
  RegPath[42] = '\\';
  RegPath[57] = 'E';
  RegPath[43] = 'S';
  RegPath[60] = 'E';
  RegPath[61] = 'B';
  RegPath[44] = 'E';
  RegPath[51] = '\\';
  RegPath[58] = 'N';
  RegPath[59] = 'S';
  RegPath[62] = 'T';
  RegPath[63] = 0;

可见是乱序排列的,这时我们可以用idc脚本直接看结果:
auto RegPath="                                                                                                                                        " ;
  RegPath[0] = '\\';
  RegPath[1] = 'R';
  RegPath[2] = 'E';
  RegPath[3] = 'G';
  RegPath[4] = 'I';
  RegPath[5] = 'S';
  RegPath[6] = 'T';
  RegPath[7] = 'R';
  RegPath[8] = 'Y';
  RegPath[9] = '\\';
  RegPath[10] = 'M';
  RegPath[11] = 'A';
  RegPath[12] = 'C';
  RegPath[13] = 'H';
  RegPath[14] = 'I';
  RegPath[15] = 'N';
  RegPath[16] = 'E';
  RegPath[17] = '\\';
  RegPath[18] = 'S';
  RegPath[19] = 'Y';
  RegPath[20] = 'S';
  RegPath[21] = 'T';
  RegPath[22] = 'E';
  RegPath[23] = 'M';
  RegPath[24] = '\\';
  RegPath[25] = 'C';
  RegPath[26] = 'U';
  RegPath[27] = 'R';
  RegPath[28] = 'R';
  RegPath[29] = 'E';
  RegPath[30] = 'N';
  RegPath[31] = 'T';
  RegPath[32] = 'C';
  RegPath[33] = 'O';
  RegPath[34] = 'N';
  RegPath[35] = 'T';
  RegPath[36] = 'R';
  RegPath[45] = 'R';
  RegPath[37] = 'O';
  RegPath[46] = 'V';
  RegPath[47] = 'I';
  RegPath[48] = 'C';
  RegPath[49] = 'E';
  RegPath[50] = 'S';
  RegPath[52] = 'T';
  RegPath[38] = 'L';
  RegPath[53] = 'S';
  RegPath[39] = 'S';
  RegPath[54] = 'D';
  RegPath[40] = 'E';
  RegPath[55] = 'E';
  RegPath[41] = 'T';
  RegPath[56] = 'F';
  RegPath[42] = '\\';
  RegPath[57] = 'E';
  RegPath[43] = 'S';
  RegPath[60] = 'E';
  RegPath[61] = 'B';
  RegPath[44] = 'E';
  RegPath[51] = '\\';
  RegPath[58] = 'N';
  RegPath[59] = 'S';
  RegPath[62] = 'T';
  RegPath[63] = 0;
print(RegPath);
回复 赞! 靠!

使用道具 举报

307

主题

228

回帖

7319

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5569 个
贡献
253 次
宅之契约
0 份
在线时间
945 小时
注册时间
2014-1-25
 楼主| 发表于 2015-10-31 21:20:55 | 显示全部楼层
本帖最后由 元始天尊 于 2015-11-2 00:01 编辑

标注常量UNICODE_STRING
#define RTL_CONSTANT_OBJECT_ATTRIBUTES(n, a) \
    { sizeof(OBJECT_ATTRIBUTES), NULL, RTL_CONST_CAST(PUNICODE_STRING)(n), a, NULL, NULL }
#define RTL_INIT_OBJECT_ATTRIBUTES(n, a) RTL_CONSTANT_OBJECT_ATTRIBUTES(n, a)
#define RTL_CONSTANT_STRING(s) { sizeof( s ) - sizeof( (s)[0] ), sizeof( s ), s }


  1. first=0
  2. next=FirstSeg()
  3. print SegName(next)
  4. while first!=next:
  5.     first=next
  6.     next=NextSeg(first)
  7.     print SegName(next)
  8.     if SegName(next) == ".rdata" or SegName(next) == ".data":
  9.                 databegin=next
  10.                 dataend=NextSeg(next)
  11.                 while databegin <= dataend:
  12.                         if (Word(databegin)+2 == Word(databegin+2)) and (GetStringType(Dword(databegin+4)) == ASCSTR_UNICODE):
  13.                                 MakeStruct(databegin,"UNICODE_STRING")
  14.                                 databegin=databegin+8
  15.                         elif (Word(databegin)+1 == Word(databegin+2)) and (GetStringType(Dword(databegin+4)) == ASCSTR_C):
  16.                                 MakeStruct(databegin,"ANSI_STRING")
  17.                                 databegin=databegin+8
  18.                         else:
  19.                                 databegin=databegin+2
复制代码
回复 赞! 靠!

使用道具 举报

307

主题

228

回帖

7319

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5569 个
贡献
253 次
宅之契约
0 份
在线时间
945 小时
注册时间
2014-1-25
 楼主| 发表于 2015-11-4 13:03:24 | 显示全部楼层
查找伪调用:
mov eax,ServiceIndex
lea edx,[esp+4]
int 2E


  1. first=0
  2. next=FirstSeg()
  3. print SegName(next)
  4. while first!=next:
  5.         first=next
  6.         next=NextSeg(first)
  7.         print SegName(next)
  8.         if SegName(next) == ".text" or SegName(next) == "INIT":
  9.                 codebegin=next
  10.                 codeend=NextSeg(next)
  11.                 while codebegin <= codeend:
  12.                         if Byte(codebegin) ==0xA1 and Dword(codebegin+5) == 0x424548D:
  13.                              print  "FakeNtCall at %x\n"%(codebegin)       
  14.                              codebegin=codebegin+11               
  15.                         codebegin=codebegin+1
复制代码
回复 赞! 靠!

使用道具 举报

0

主题

41

回帖

45

积分

用户组: 初·技术宅

UID
3351
精华
0
威望
2 点
宅币
0 个
贡献
0 次
宅之契约
0 份
在线时间
0 小时
注册时间
2018-1-14
发表于 2018-1-14 15:15:41 | 显示全部楼层
可以可以!!
回复

使用道具 举报

0

主题

41

回帖

45

积分

用户组: 初·技术宅

UID
3351
精华
0
威望
2 点
宅币
0 个
贡献
0 次
宅之契约
0 份
在线时间
0 小时
注册时间
2018-1-14
发表于 2018-1-14 15:17:22 | 显示全部楼层
可以可以!!
回复

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-3-29 21:58 , Processed in 0.043526 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表