Пишу плагин для IDA Pro

Тема в разделе "WASM.BEGINNERS", создана пользователем BitBoy, 23 апр 2019.

Метки:
  1. BitBoy

    BitBoy New Member

    Публикаций:
    0
    Регистрация:
    23 апр 2019
    Сообщения:
    1
    Пишу плагин, который добаляет ссылки к динамическим вызовам. Проблема в том, что Ида падает после запуска отладчика, но до вызова callback, не могу понять в чем проблема. Если закомментировать hook_to_notification_point, то отладчик работает нормально.
    Код (C++):
    1. #include <ida.hpp>
    2. #include <idp.hpp>
    3. #include <dbg.hpp>
    4. #include <loader.hpp>
    5. #include <kernwin.hpp>
    6. #include <bytes.hpp>
    7. #include <ua.hpp>
    8. #include <vector>
    9.  
    10.  
    11. using namespace std;
    12. void set_bpts();
    13. void del_bpts();
    14. void find_jumps();
    15. void idaapi term();
    16. bool contains(ea_t el);
    17. bool has_cref(ea_t from, ea_t to);
    18.  
    19. vector<ea_t> bpts;
    20. bool jumps_found = false;
    21.  
    22. bool contains(ea_t el)
    23. {
    24.     for (unsigned int i = 0; i < bpts.size(); i++)
    25.     {
    26.         if (bpts.at(i) == el) return true;
    27.     }
    28.     return false;
    29. }
    30.  
    31. bool has_cref(ea_t from, ea_t to)
    32. {
    33.     ea_t ref = get_first_cref_from(from);
    34.     while (ref != BADADDR)
    35.     {
    36.         if(from==to) return true;
    37.         ref = get_next_cref_from(from, ref);
    38.     }
    39.     return false;
    40. }
    41. //--------------------------------------------------------------------------
    42. static ssize_t idaapi callback(void * /*user_data*/, int notification_code, va_list va)
    43. {
    44.     ea_t addr;
    45.     switch (notification_code)
    46.     {
    47.     /*case dbg_process_start:
    48.         break;*/
    49.  
    50.     case dbg_bpt:
    51.         addr = va_arg(va, ea_t);
    52.         if (contains(addr))
    53.         {
    54.             insn_t ins;
    55.             decode_insn(&ins, addr);
    56.             if (!has_cref(addr, ins.ops[0].addr))
    57.             {
    58.                 if (ins.flags&CF_CALL) add_cref(addr, ins.ops[0].addr, fl_CN);
    59.                 else add_cref(addr, ins.ops[0].addr, fl_JN);
    60.             }
    61.            
    62.         }
    63.         request_continue_process();
    64.         run_requests();
    65.         break;
    66.  
    67.    
    68.  
    69.     case dbg_process_exit:
    70.         term();
    71.         break;
    72.     }
    73.     return 0;
    74. }
    75.  
    76. int idaapi init()
    77. {
    78.     if (ph.id != PLFM_386 || inf.filetype != f_PE)
    79.         return PLUGIN_SKIP;
    80.     return PLUGIN_OK;
    81. }
    82.  
    83. bool idaapi run(size_t)
    84. {
    85.     if (!jumps_found) find_jumps(); jumps_found = true;
    86.     set_bpts();
    87.     if (!hook_to_notification_point(HT_DBG, callback))
    88.     {
    89.         warning("Could not hook to notification point");
    90.         return true;
    91.     }
    92.  
    93.     if (dbg == NULL)
    94.         load_debugger("win32", false);
    95.     start_process();
    96.   return true;
    97. }
    98.  
    99. void find_jumps()
    100. {
    101.     for (ea_t i = inf.min_ea; i < inf.max_ea; i++)
    102.     {
    103.         flags_t f = get_flags(i);
    104.         if (is_code(f) && is_head(f))
    105.         {
    106.             insn_t ins;
    107.             int sz = decode_insn(&ins, i);
    108.             if (ins.flags & CF_JUMP && ins.ops[0].is_reg(0))
    109.             {
    110.                 bpts.push_back(i);
    111.             }
    112.             i += sz - 1;
    113.         }
    114.     }
    115. }
    116.  
    117. void set_bpts()
    118. {
    119.     for (unsigned int i = 0; i < bpts.size(); i++)
    120.     {
    121.         add_bpt(bpts.at(i));
    122.     }
    123. }
    124.  
    125. void del_bpts()
    126. {
    127.     for (unsigned int i = 0; i < bpts.size(); i++)
    128.     {
    129.         del_bpt(bpts.at(i));
    130.     }
    131. }
    132.  
    133. void idaapi term()
    134. {
    135.     // just to be safe
    136.     unhook_from_notification_point(HT_DBG, callback);
    137.     del_bpts();
    138. }
    139.  
    140. plugin_t PLUGIN =
    141. {
    142.   IDP_INTERFACE_VERSION,
    143.   PLUGIN_UNL,           // plugin flags
    144.   init,                 // initialize
    145.   NULL,                 // terminate. this pointer may be NULL.
    146.   run,                  // invoke plugin
    147.   NULL,                 // long comment about the plugin
    148.   NULL,                 // multiline help about the plugin
    149.   "ReffeR",                // the preferred short name of the plugin
    150.   NULL                  // the preferred hotkey to run the plugin
    151. };
    152.  
     
  2. f13nd

    f13nd Well-Known Member

    Публикаций:
    0
    Регистрация:
    22 июн 2009
    Сообщения:
    1.953
    Пишешь плагин для отладчика, а формулировка "ида где-то падает".
    Код (Text):
    1. hook_to_notification_point(HT_DBG, callback)
    Код (Text):
    1. idaman bool ida_export     hook_to_notification_point (hook_type_t hook_type, hook_cb_t *cb, void *user_data=NULL)