Перехватить функцию

Тема в разделе "WASM.WIN32", создана пользователем KatyaPanzerschreck, 18 фев 2017.

  1. KatyaPanzerschreck

    KatyaPanzerschreck New Member

    Публикаций:
    0
    Регистрация:
    24 янв 2017
    Сообщения:
    1
    Пытаюсь перехватить функцию. Судя по этой строчке:
    bool __userpurge sub_5AF810@<al>(int a1@<ecx>, int a2, signed int a3, int a4, int a5)
    функция эта thiscall. Во всяком случае мне нужно было перехватить еще несколько связанных функций и все они успешно перехватывались способом ниже:

    Код (C):
    1. std::shared_ptr<PLH::VEHHook> PLHVEH_5AF810;
    2. typedef bool(__thiscall* sub_5AF810_t)(void* Ecx, int a2, int a3, BYTE* a4, int a5);
    3. sub_5AF810_t real_sub_5AF810;
    4. bool __fastcall new_sub_5AF810(void* Ecx, void* Edx, int a2, int a3, BYTE* a4, int a5)
    5. {
    6.    Logger_5AF810.AddEntryTimeEvent(L"5AF810");
    7.    auto po = PLHVEH_5AF810->GetProtectionObject();
    8.    return real_sub_5AF810(Ecx, a2, a3, a4, a5);
    9. }
    Можно объявить функцию как naked и перейти туда простым джампом, но нужен способ, который не затрагивает код и желательно бы ограничиться этой самой библиотекой без самопальных наворотов, ибо хуков итак достаточно. Как найти правильный calling convention и параметры?

    Извиняюсь что весь код в куче, не вижу элементов форматирования.

    Код (Text):
    1. .text:005AF810 ; =============== S U B R O U T I N E =======================================
    2. .text:005AF810
    3. .text:005AF810
    4. .text:005AF810 sub_5AF810  proc near  ; CODE XREF: sub_5AF990+9Dp
    5. .text:005AF810  ; sub_5AFA60+31p ...
    6. .text:005AF810
    7. .text:005AF810 var_7810  = dword ptr -7810h
    8. .text:005AF810 var_780C  = dword ptr -780Ch
    9. .text:005AF810 var_7804  = dword ptr -7804h
    10. .text:005AF810 var_7800  = dword ptr -7800h
    11. .text:005AF810 var_77FC  = byte ptr -77FCh
    12. .text:005AF810 var_3BFC  = dword ptr -3BFCh
    13. .text:005AF810 var_3BF8  = byte ptr -3BF8h
    14. .text:005AF810 var_3BF7  = byte ptr -3BF7h
    15. .text:005AF810 var_4  = dword ptr -4
    16. .text:005AF810 arg_4  = dword ptr  8
    17. .text:005AF810 arg_8  = dword ptr  0Ch
    18. .text:005AF810 arg_C  = dword ptr  10h
    19. .text:005AF810
    20. .text:005AF810  mov  eax, 7810h
    21. .text:005AF815  call  __alloca_probe
    22. .text:005AF81A  mov  eax, dword_7445AC
    23. .text:005AF81F  xor  eax, esp
    24. .text:005AF821  mov  [esp+7810h+var_4], eax
    25. .text:005AF828  cmp  dword ptr [ecx+8], 0
    26. .text:005AF82C  push  ebx
    27. .text:005AF82D  mov  ebx, [esp+7814h+arg_8]
    28. .text:005AF834  mov  [esp+7814h+var_7810], ecx
    29. .text:005AF838  jnz  short loc_5AF854
    30. .text:005AF83A  xor  al, al
    31. .text:005AF83C  pop  ebx
    32. .text:005AF83D  mov  ecx, [esp+7810h+var_4]
    33. .text:005AF844  xor  ecx, esp
    34. .text:005AF846  call  sub_59E6F0
    35. .text:005AF84B  add  esp, 7810h
    36. .text:005AF851  retn  0Ch
    37. .text:005AF854 ; ---------------------------------------------------------------------------
    38. .text:005AF854
    39. .text:005AF854 loc_5AF854:  ; CODE XREF: sub_5AF810+28j
    40. .text:005AF854  push  edi
    41. .text:005AF855  mov  edi, [esp+7818h+arg_4]
    42. .text:005AF85C  cmp  edi, 3BFCh
    43. .text:005AF862  jb  short loc_5AF86B
    44. .text:005AF864  xor  al, al
    45. .text:005AF866  jmp  loc_5AF96F
    46. .text:005AF86B ; ---------------------------------------------------------------------------
    47. .text:005AF86B
    48. .text:005AF86B loc_5AF86B:  ; CODE XREF: sub_5AF810+52j
    49. .text:005AF86B  push  ebp
    50. .text:005AF86C  push  esi
    51. .text:005AF86D  push  3C00h
    52. .text:005AF872  lea  eax, [esp+7824h+var_780C+1]
    53. .text:005AF876  push  0
    54. .text:005AF878  push  eax
    55. .text:005AF879  mov  byte ptr [esp+782Ch+var_780C], 0
    56. .text:005AF87E  call  sub_59E292
    57. .text:005AF883  add  esp, 0Ch
    58. .text:005AF886  xor  ebp, ebp
    59. .text:005AF888  test  edi, edi
    60. .text:005AF88A  jle  short loc_5AF898
    61. .text:005AF88C  push  edi
    62. .text:005AF88D  push  ebx
    63. .text:005AF88E  call  sub_5B14D4
    64. .text:005AF893  add  esp, 8
    65. .text:005AF896  mov  ebp, eax
    66. .text:005AF898
    67. .text:005AF898 loc_5AF898:  ; CODE XREF: sub_5AF810+7Aj
    68. .text:005AF898  test  edi, edi
    69. .text:005AF89A  mov  eax, ebp
    70. .text:005AF89C  mov  [esp+7814h+var_7800], eax
    71. .text:005AF8A0  mov  esi, 4
    72. .text:005AF8A5  jle  short loc_5AF8BD
    73. .text:005AF8A7  push  edi
    74. .text:005AF8A8  lea  ecx, [esp+7818h+var_77FC]
    75. .text:005AF8AC  push  ebx
    76. .text:005AF8AD  push  ecx
    77. .text:005AF8AE  call  sub_59E152
    78. .text:005AF8B3  mov  eax, [esp+7820h+var_7800]
    79. .text:005AF8B7  add  esp, 0Ch
    80. .text:005AF8BA  lea  esi, [edi+4]
    81. .text:005AF8BD
    82. .text:005AF8BD loc_5AF8BD:  ; CODE XREF: sub_5AF810+95j
    83. .text:005AF8BD  xor  bl, bl
    84. .text:005AF8BF  cmp  eax, ebp
    85. .text:005AF8C1  jz  short loc_5AF939
    86. .text:005AF8C3  lea  edx, [eax+2000000h]
    87. .text:005AF8C9  cmp  edx, ebp
    88. .text:005AF8CB  jnz  short loc_5AF8D1
    89. .text:005AF8CD  mov  bl, 1
    90. .text:005AF8CF  jmp  short loc_5AF8DD
    91. .text:005AF8D1 ; ---------------------------------------------------------------------------
    92. .text:005AF8D1
    93. .text:005AF8D1 loc_5AF8D1:  ; CODE XREF: sub_5AF810+BBj
    94. .text:005AF8D1  add  ebp, 2000000h
    95. .text:005AF8D7  cmp  eax, ebp
    96. .text:005AF8D9  jnz  short loc_5AF8DD
    97. .text:005AF8DB  mov  bl, 2
    98. .text:005AF8DD
    99. .text:005AF8DD loc_5AF8DD:  ; CODE XREF: sub_5AF810+BFj
    100. .text:005AF8DD  ; sub_5AF810+C9j
    101. .text:005AF8DD  push  3C00h
    102. .text:005AF8E2  lea  eax, [esp+7818h+var_3BFC+1]
    103. .text:005AF8E9  push  0
    104. .text:005AF8EB  push  eax
    105. .text:005AF8EC  call  sub_59E292
    106. .text:005AF8F1  mov  ecx, [esp+7820h+arg_C]
    107. .text:005AF8F8  push  esi
    108. .text:005AF8F9  lea  edx, [esp+7824h+var_7800]
    109. .text:005AF8FD  push  edx
    110. .text:005AF8FE  lea  eax, [esp+7828h+var_3BF7]
    111. .text:005AF905  push  eax
    112. .text:005AF906  mov  [esp+3C30h], ecx
    113. .text:005AF90D  mov  [esp+782Ch+var_3BF8], bl
    114. .text:005AF914  call  sub_59E152
    115. .text:005AF919  add  esi, 5
    116. .text:005AF91C  push  esi
    117. .text:005AF91D  lea  ecx, [esp+7830h+var_3BFC]
    118. .text:005AF924  push  ecx
    119. .text:005AF925  lea  edx, [esp+7834h+var_7800]
    120. .text:005AF929  push  edx
    121. .text:005AF92A  call  sub_59E152
    122. .text:005AF92F  add  esp, 24h
    123. .text:005AF932  mov  edi, 10h
    124. .text:005AF937  jmp  short loc_5AF940
    125. .text:005AF939 ; ---------------------------------------------------------------------------
    126. .text:005AF939
    127. .text:005AF939 loc_5AF939:  ; CODE XREF: sub_5AF810+B1j
    128. .text:005AF939  mov  edi, [esp+7814h+arg_C]
    129. .text:005AF940
    130. .text:005AF940 loc_5AF940:  ; CODE XREF: sub_5AF810+127j
    131. .text:005AF940  mov  ecx, dword_B56FE0
    132. .text:005AF946  push  esi
    133. .text:005AF947  lea  eax, [esp+7818h+var_7800]
    134. .text:005AF94B  push  eax
    135. .text:005AF94C  call  sub_5B0D43
    136. .text:005AF951  mov  eax, [esp+7814h+var_7804]
    137. .text:005AF955  mov  edx, [eax+8]
    138. .text:005AF958  add  [eax+18h], esi
    139. .text:005AF95B  add  dword ptr [eax+1Ch], 1
    140. .text:005AF95F  lea  ecx, [esp+7814h+var_7800]
    141. .text:005AF963  push  ecx
    142. .text:005AF964  push  esi
    143. .text:005AF965  push  edi
    144. .text:005AF966  call  edx
    145. .text:005AF968  test  al, al
    146. .text:005AF96A  pop  esi
    147. .text:005AF96B  setnz  al
    148. .text:005AF96E  pop  ebp
    149. .text:005AF96F
    150. .text:005AF96F loc_5AF96F:  ; CODE XREF: sub_5AF810+56j
    151. .text:005AF96F  mov  ecx, [esp+7818h+var_4]
    152. .text:005AF976  pop  edi
    153. .text:005AF977  pop  ebx
    154. .text:005AF978  xor  ecx, esp
    155. .text:005AF97A  call  sub_59E6F0
    156. .text:005AF97F  add  esp, 7810h
    157. .text:005AF985  retn  0Ch
    158. .text:005AF985 sub_5AF810  endp
    HexRays:
    Код (C):
    1. bool __userpurge sub_5AF810@<al>(int a1@<ecx>, int a2, signed int a3, int a4, int a5)
    2. {
    3.   bool result; // al@2
    4.   int v6; // ebp@5
    5.   int v7; // eax@7
    6.   int v8; // esi@7
    7.   char v9; // bl@9
    8.   int v10; // edi@14
    9.   int v11; // eax@16
    10.   int (__cdecl *v12)(int, int, int *); // edx@16
    11.   int v13; // [sp+8h] [bp-780Ch]@5
    12.   int v14; // [sp+10h] [bp-7804h]@16
    13.   int v15; // [sp+14h] [bp-7800h]@7
    14.   char v16; // [sp+18h] [bp-77FCh]@8
    15.   int v17; // [sp+3C18h] [bp-3BFCh]@14
    16.   char v18; // [sp+3C1Ch] [bp-3BF8h]@14
    17.   char v19; // [sp+3C1Dh] [bp-3BF7h]@14
    18.  
    19.   if ( *(_DWORD *)(a1 + 8) )
    20.   {
    21.   if ( (unsigned int)a3 < 0x3BFC )
    22.   {
    23.   sub_59E292((char *)&v13 + 1, 0, 15360);
    24.   v6 = 0;
    25.   if ( a3 > 0 )
    26.   v6 = sub_5B14D4(a4, a3);
    27.   v7 = v6;
    28.   v15 = v6;
    29.   v8 = 4;
    30.   if ( a3 > 0 )
    31.   {
    32.   sub_59E152(&v16, a4, a3);
    33.   v7 = v15;
    34.   v8 = a3 + 4;
    35.   }
    36.   v9 = 0;
    37.   if ( v7 == v6 )
    38.   {
    39.   v10 = a5;
    40.   }
    41.   else
    42.   {
    43.   if ( v7 + 0x2000000 == v6 )
    44.   {
    45.   v9 = 1;
    46.   }
    47.   else if ( v7 == v6 + 0x2000000 )
    48.   {
    49.   v9 = 2;
    50.   }
    51.   sub_59E292((char *)&v17 + 1, 0, 15360);
    52.   v17 = a5;
    53.   v18 = v9;
    54.   sub_59E152(&v19, &v15, v8);
    55.   v8 += 5;
    56.   sub_59E152(&v15, &v17, v8);
    57.   v10 = 16;
    58.   }
    59.   sub_5B0D43(&v15, v8);
    60.   v11 = v14;
    61.   v12 = *(int (__cdecl **)(int, int, int *))(v14 + 8);
    62.   *(_DWORD *)(v11 + 24) += v8;
    63.   ++*(_DWORD *)(v11 + 28);
    64.   result = (unsigned __int8)v12(v10, v8, &v15) != 0;
    65.   }
    66.   else
    67.   {
    68.   result = 0;
    69.   }
    70.   }
    71.   else
    72.   {
    73.   result = 0;
    74.   }
    75.   return result;
    76. }
    Вызывается во многих местах, вот пример одной функции из которой идет вызов:
    Код (Text):
    1. .text:005AFB30 ; =============== S U B R O U T I N E =======================================
    2. .text:005AFB72  mov  dword_B56FBC, 1
    3. .text:005AFB7C  jnz  short loc_5AFB88
    4. .text:005AFB7E  mov  dword_B56FC0, 2
    5. .text:005AFB88
    6. .text:005AFB88 loc_5AFB88:  ; CODE XREF: sub_5AFB30+4Cj
    7. .text:005AFB88  push  offset dword_B56FEC
    8. .text:005AFB8D  push  39h
    9. .text:005AFB8F  push  9
    10. .text:005AFB91  mov  ecx, esi
    11. .text:005AFB93  call  sub_5AF810
    12. .text:005AFB98  test  al, al
    13. .text:005AFB9A  push  0
    14. .text:005AFB9C  mov  ecx, esi
     
  2. Indy_

    Indy_ Well-Known Member

    Публикаций:
    4
    Регистрация:
    29 апр 2011
    Сообщения:
    4.775
    Я ничего не понял, особенно первую порцию сурца :dash1:
     
  3. Коцит

    Коцит Active Member

    Публикаций:
    0
    Регистрация:
    31 янв 2017
    Сообщения:
    130
    вот прикол
     
  4. Rel

    Rel Well-Known Member

    Публикаций:
    2
    Регистрация:
    11 дек 2008
    Сообщения:
    5.255
    да-да, Инде не шипко в хуках шарит...