IDA-IDC腳本編寫語法


1、IDA腳本編寫基礎

  IDC是IDA內置的腳本語言,其語法與C非常相似,它是一種解釋性語言。

  • 執行方法
    • 在IDA中按SHIFT+F2鍵會彈出一個對話框,把語句直接寫在對話框中,點擊run就可被運行。
    • 在菜單欄中File | Script file載入idc文件。

2、IDC語法

  IDC語言可參考C語言,語句以分號結束,注釋為//或/**/,但也有很多不同。

  • 2.1 輸出(類似C語言中的printf函數)
  • void Message(string format, …);
  • Message(“Hello world!”);
  • Message(“%s\n”, “Hello world!”);

2.2 變量

  IDC中所有變量都被定義成auto類型,會自動進行類型轉換,一般類型有整數型、字符串類型、浮點型。

  • 局部變量:auto counter;
  • extern 引入全局變量的聲明,extern outsideGlobal;
  • 字符串支持加好連接:auto str = "hello" + "world";
  • 字符串支持分片操作:str1 = str[7:9];

2.3 操作符

  • 許多標准的C語言操作符(+、-、*、/、%、<<、>>、++、--)在IDC同樣適用,但復合賦值運算符+=不支持、逗號操作符也不被支持。

2.4 條件

  •  if、if else及三目運算符“?:”是支持的,但是switch語句是不支持的。
auto currAddr;
currAddr = ScreenEA();
if (currAddr % 2)
    Message(“%x is odd\n”, currAddr);
else
    Message(“%x is even\n”, currAddr);

 

2.5 循環

  • 循環可以用for、while、do while實現。
auto origEA, currEA, funStart, funEnd;
origEA = ScreenEA();
funStart = GetFunctionAttr(origEA, FUNCATTR_START);
funEnd = GetFunctionAttr(origEA, FUNCATTR_END);
if(funStart == -1)
    Message(“%x is not part of a functuion\n”), origEA);
for(currEA=funStart; currEA != BADADDR; currEA=NextHead(currEA, funEnd)){
    Message(“%8x\n”, currEA);
}

 

2.6 函數

  • IDC所有函數必須被定義為靜態。
  • 函數的聲明與C語言不同,不需要指定類型。
  • 函數參數傳遞中,加上&表示傳地址,不加表示傳值。
  • return 返回函數輸出。
  • 把自己的IDC函數庫加入到ida.idc文件中,我們就可以全局使用自己的IDC函數了。
// 例1:
static outputCurrentAddress(){
    auto currAddress;
    currAddress = ScreeenEA();
    Message(“%x\n”, currAddress);
    return currAddress;
}
// 例2:
my_func(q, r, s)

 

2.7 IDC對象

class ExampleClass{
    ExampleClass(x, y){
        this.x = x;
        this.y = y;
    }
    ~ExampleClass(){
    }
    foo(x){
        this.a = this.a + x;
    }
}

static main(){
    ExampleClass ex;
    auto ex = ExampleClass(1, 2);
    ex.foo(10);
    ex.z = "string" //特點,可隨時給對象添加成員
}

 

2.8 IDC程序

  •  將IDC腳本代碼放到一個文件中可永久性保存。
  •   #include <文件> 將指定文件包含在當前文件中
  •   #define <宏名稱> [可選值]
  •   #ifdef <名稱> 測試宏是否存在
  •   #else 與#ifelse共同使用
  •   #endif #ifdef的終止符
  •   #undef <名稱>刪除宏定義
#include <idc.idc>
static main(){
    // 代碼體
}

 

2.9 IDC錯誤處理

  •  try/catch

2.10 數組

 

3、有用的IDC函數

  

  

  

  

  

  

  

  

  

  

  

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM