使用IDA的通用解压外挂程式理
- 格式:docx
- 大小:550.57 KB
- 文档页数:11
使用IDA的通用解壓外掛程式從4.9版開始,IDA就集成了Universal PE Unpacker通用解壓外掛程式,其原始程式碼可以在IDA Pro SDK中找到。
這個小教程將會演示如何使用這個外掛程式,並簡單介紹其內部工作原理。
一個壓縮的應用程式下面是當我們執行這個可執行程式的運行結果:非常簡單的程式,但是,如果我們使用IDA來打開它,會出現下面的警告提示:IDA檢測到異常的輸入段,並提示我們檔可能被壓縮了…如果我們看一下它的輸入表視窗,我們就會發現:我們的程式只導入了kernel32.dll中的3個函數。
我們可以看到在加壓縮殼的程式種常用到的2個動態連結程式庫函數LoadLibraryA和GetProcAddress,它們通常用於恢復程式的輸入表。
使用通用PE Universal Unpacker在外掛程式的子功能表種選擇Universal PE unpacker,開始解壓:你會看到外掛程式的選項對話方塊:在這個對話方塊中,我們可以配置一個的位址範圍,當程式運行到這個區域(原始的程式入口點區域),它會掛起程式的執行。
你也可以指定一個檔用於保存解壓的資源。
按過確定按鈕後,外掛程式開始運行,它啟動了我們的程式,並自己解壓,直到它運行到我們設定的地址範圍內。
這時解壓已經結束,依照對話方塊中的提示,我們保存了當前的記憶體快照。
你會注意到,我們遇到了兩個中斷點,這個我們最後再去討論它。
為了重新構造原始的程式輸入表,外掛程式創建了一個新段:解壓完成後,我們在start()函數處可以看到更多我們熟悉的代碼結構。
我們還可以進一步改進,讓這個反彙編結果盡可能的更完美一些!使用簽名如果我們查看解壓後的程式的字串視窗,我們可以看出我們的程式是使用Virsual C++編譯的,我們可以應用相應的FLIRT(快速庫識別恢復技術)的庫簽名。
應用簽名庫後,最終的反彙編結果如下:是不是更好了!外掛程式分析下面我們仔細研究一下這個外掛程式,看它是如何使用SDK的調試API來完成這些工作的。
主要的操作就是啟動這個進程,然後根據調試器捕捉到的一些事件,進行相應的處理,直到我們確認程式已經完全被解壓。
我們先設定一個控制碼用於接收調試器的事件,並啟動這個程式直到它到達入口點。
if ( !hook_to_notification_point(HT_DBG, callback, NULL) ){warning("Could not hook to notification point\n");return;}// Let's start the debuggerif ( !run_to(inf.beginEA) ){warning("Sorry, could not start the process");unhook_from_notification_point(HT_DBG, callback, NULL);}事件將會被送到我們聲明的控制碼,定義如下:static int idaapi callback(void * /*user_data*/,int notification_code,va_list va){switch ( notification_code ){case dbg_process_start:...case dbg_library_load:...case dbg_run_to:...case dbg_bpt:...case dbg_trace:...case dbg_process_exit:......}return 0;}當我們通過一個函數run_to()來開始我們的進程時,我們將會收到一個相應的事件dbg_run_to,它表示run_to()命令被正確的執行。
現在我們來到壓縮檔的入口點,在GetProcAddress()這個函數上設定一個中斷點,(假定這個解壓代碼在重構原始輸入表之前結束):case dbg_run_to: // Parameters: thread_id_t tiddbg->stopped_at_debug_event(true);gpa = get_name_ea(BADADDR, "kernel32_GetProcAddress");...else if( !add_bpt(gpa) ){bring_debugger_to_front();warning("Sorry, can not set bpt to kernel32.GetProcAddress");goto FORCE_STOP;}else{++stage;set_wait_box("Waiting for a call to GetProcAddress()");}continue_process();break;當程式運行到GetProcAddress()中斷點,我們收到一個dbg_bpt事件,我們可以從堆疊中提取出這個位址,然後刪除這個中斷點,並在返回地址設定第二個中斷點,以便能在GetProcAddress()函數返回時立刻中止。
case dbg_bpt: // A user defined breakpoint was reached.// Parameters: thread_id_t tid// ea_t breakpoint_ea{/*tid_t tid =*/ va_arg(va, tid_t);ea_t ea = va_arg(va, ea_t);...if ( ea == gpa ){regval_t rv;if ( get_reg_val("esp", &rv) ){ea_t esp = rv.ival;invalidate_dbgmem_contents(esp, 1024);ea_t ret = get_long(esp);...if ( !del_bpt(gpa) || !add_bpt(ret) )error("Can not modify breakpoint");還記得我們之前在運行解壓外掛程式時碰到的兩個中斷點嗎?中斷在0x7C80AC28和0x00040C68D,第一個是我們的GetProcAddress()中斷點,而第而個是在返回地址上的中斷點。
在下面的反彙編視窗中,你可以看到進入GetProcAddress()中斷點的那個調用指令,現在我們要繼續運行直到解壓程式恢復了程式寄存器的原始內容,然後跳到解壓後的程式的真實入口點:我們在第二個中斷點之後開始單步跟蹤,直到指令執行到我們之前設定的位址範圍。
del_bpt(ea);if ( !is_library_entry(ea) ){deb(IDA_DEBUG_PLUGIN, "%a: reached unpacker code, switching to trace mode\n", ea);enable_step_trace(true);...set_wait_box("Waiting for the unpacker to finish");}else{warning("%a: bpt in library code", ea); // how can it be?add_bpt(gpa);}在每一步指令執行時,我們判斷其位址是否在我們之前設定的範圍之內。
如果我們運行到了那個區域,重新分析這些解壓後的代碼,調整入口點,重建輸入表,保存資源然後……最後來一張記憶體快照!case dbg_trace: // A step occured (one instruction was executed). This event// notification is only generated if step tracing is enabled.// Parameter: none…/*tid_t tid =*/ va_arg(va, tid_t);ea_t ip = va_arg(va, ea_t);if ( oep_area.contains(ip) ){// stop the trace modeenable_step_trace(false);// reanalyze the unpacked codeset_wait_box("Reanalyzing the unpacked code");do_unknown_range(oep_area.startEA, oep_area.endEA, false);auto_make_code(ip);noUsed(oep_area.startEA, oep_area.endEA);auto_mark_range(oep_area.startEA, oep_area.endEA, AU_FINAL);// mark the program's entry pointmove_entry(ip);set_wait_box();...set_wait_box("Recreating the import table");invalidate_dbgmem_config();...create_impdir();set_wait_box("Storing resources to 'resource.res'");if ( resfile[0] != '\0' )extract_resource(resfile);set_wait_box();if ( take_memory_snapshot(true) )goto FORCE_STOP;這樣,就在IDA資料庫中得到了進程的記憶體映射,我們可以象通常那樣來分析這些解壓後的代碼了。
現在就去看看那些在SDK中的原始程式碼吧!去瞭解一下這個外掛程式的所有實現細節。