用IDA反汇编可执行文件时,经常遇到未命名代码片段。对于少量这样的代码片段可以用IDA的Search->not function来定位(快捷键通常为Alt-U),再用Edit->Functoins->Create function...(快捷键为P)创建新函数。如果出现大量这样的代码片段,反复用Alt-U,P操作就很繁琐。于是就想到利用用idc脚本来实现这个功能。查阅了IDA的Help(40dK9s2c8@1M7s2y4Q4x3@1q4Q4x3V1k6Q4x3V1k6%4N6%4N6Q4x3X3g2Z5k6i4S2Q4x3X3c8J5j5i4W2K6i4K6u0W2j5$3!0E0i4K6u0r3M7s2u0G2k6s2g2U0N6s2y4Q4x3V1k6A6k6r3q4Q4x3V1k6K6N6i4m8H3L8%4u0@1i4K6u0r3K9h3c8S2k6r3!0U0i4K6u0r3K9h3&6V1k6i4S2Q4x3X3g2K6K9s2c8E0L8q4!0q4c8W2!0n7b7#2)9^5z5g2!0q4c8W2!0n7b7#2)9^5b7#2y4W2j5i4u0U0K9q4)9J5k6q4)9J5y4X3N6@1i4K6y4n7L8X3!0@1 function这个功能的名称为JumpNotFunction,但在Help->Index of IDC functions中找不到相对应的函数(快捷键P有对应的MakeFunction)。
用Google搜索 "How to implement JumpNotFunction using idc script in IDA environment" (如何在IDA环境下用idc脚本实现JumpNotFunction),找不到答案。于是决定自己动手解决问题。经过一番学习,找到了主要的可以使用的相关函数:FindCode、GetFunctionName、FindFuncEnd、isCode、GetFlags,摘录如下。
1.
// ea - address to start from
// flag is combination of the following bits:
#define SEARCH_DOWN 0x01 // search forward
#define SEARCH_NEXT 0x02 // search next occurence
#define SEARCH_CASE 0x04 // search case-sensitive
// (only for bin&txt search)
#define SEARCH_REGEX 0x08 // enable regular expressions
#define SEARCH_NOBRK 0x10 // don't test ctrl-break
#define SEARCH_NOSHOW 0x20 // don't display the search progress
// return BADADDR - not found
long FindCode(long ea,long flag);
2.
// ea - any address belonging to the function
// returns: null string - function doesn't exist otherwise returns function name
string GetFunctionName(long ea);
3.
// ea - starting address of a new function
// returns: if a function already exists, then return its end address.
// if a function end cannot be determined, then return BADADDR
// otherwise return the end address of the new function
long FindFuncEnd(long ea);
5.
// ea - linear address
// returns: 32-bit value of internal flags. See start of IDC.IDC file for explanations.
long GetFlags(long ea); // get internal flags for ea