0


C++程序正向编译逆向反编译(一)

1.需求

  1. 逆向工程师必须先是一个正向开发工程师,如果没有C++/MFC的开发经验,就不会懂得如何逆向分析C++/MFC的程序,本文完成一个helloworldC++正逆向过程。

2.C++程序源码

  1. 编译环境:visual studio 2022
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6. int a;
  7. a = 100;
  8. for (int i = 0; i < a; i++)
  9. {
  10. cout << "Hello World! " << endl;
  11. }
  12. cin >> a;
  13. return 0;
  14. }

编译完毕后生成exe程序,导入Ghidra,分析完毕后,在symbol tree窗口,输入main查找到主函数位置:

3.反编译结果

3.1 Ghidra反汇编

  1. int __cdecl main(int _Argc,char **_Argv,char **_Env)
  2. {
  3. basic_ostream<char,std::char_traits<char>_> *this;
  4. int iVar1;
  5. _RTC_framedesc *extraout_EDX;
  6. int *piVar2;
  7. code *pcVar3;
  8. int local_20 [4];
  9. int local_10 [2];
  10. uint local_8;
  11. piVar2 = local_20;
  12. for (iVar1 = 7; iVar1 != 0; iVar1 = iVar1 + -1) {
  13. *piVar2 = -0x33333334;
  14. piVar2 = piVar2 + 1;
  15. }
  16. local_8 = __security_cookie ^ (uint)&stack0xfffffffc;
  17. local_10[0] = 100;
  18. /* static local (stored at 004124a0) <NoType>
  19. static local (stored at 00412494) <NoType>
  20. static local (stored at 0041248c) <NoType>
  21. static local (stored at 004124a0) <NoType>
  22. static local (stored at 00412494) <NoType>
  23. static local (stored at 0041248c) <NoType> */
  24. for (local_20[1] = 0; local_20[1] < local_10[0]; local_20[1] = local_20[1] + 1) {
  25. pcVar3 = std::endl<char,std::char_traits<char>_>;
  26. this = std::operator<<<std::char_traits<char>_>
  27. ((basic_ostream<char,std::char_traits<char>_> *)cout_exref,"Hello World! ");
  28. std::basic_ostream<char,struct_std::char_traits<char>_>::operator<<
  29. ((basic_ostream<char,struct_std::char_traits<char>_> *)this,pcVar3);
  30. _RTC_CheckEsp();
  31. }
  32. std::basic_istream<char,struct_std::char_traits<char>_>::operator>>
  33. ((basic_istream<char,struct_std::char_traits<char>_> *)cin_exref,local_10);
  34. _RTC_CheckEsp();
  35. iVar1 = 0;
  36. _RTC_CheckStackVars((void *)0x0,extraout_EDX);
  37. __security_check_cookie(local_8 ^ (uint)&stack0xfffffffc);
  38. local_8 = 0x412487;
  39. _RTC_CheckEsp();
  40. return iVar1;
  41. }

3.2 IDA 结果

  1. int __cdecl main()
  2. {
  3. std::ostream *v0; // eax
  4. int i; // [esp+D0h] [ebp-18h]
  5. int a; // [esp+DCh] [ebp-Ch] BYREF
  6. a = 100;
  7. for ( i = 0; i < a; ++i )
  8. {
  9. v0 = std::operator<<<std::char_traits<char>>(std::cout, "Hello World! ");
  10. std::ostream::operator<<(v0, std::endl<char,std::char_traits<char>>);
  11. }
  12. std::istream::operator>>(std::cin, &a);
  13. return 0;
  14. }

4.Visual studio 里查看汇编代码

在正向开发的过程中,可以在编译器Visual studio里,查看C++代码的汇编代码,在程序中设置断点,F5编译,等程序停住后,才有查看汇编选项:

汇编代码如下:

  1. --- C:\Users\paul\source\repos\helloc++\helloc++\helloc++.cpp ------------------
  2. 1: // helloc++.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
  3. 2: //
  4. 3:
  5. 4: #include <iostream>
  6. 5: #include <cstdlib>
  7. 6: using namespace std;
  8. 7:
  9. 8:
  10. 9: int main()
  11. 10: {
  12. 003F23D0 55 push ebp
  13. 003F23D1 8B EC mov ebp,esp
  14. 003F23D3 81 EC DC 00 00 00 sub esp,0DCh
  15. 003F23D9 53 push ebx
  16. 003F23DA 56 push esi
  17. 003F23DB 57 push edi
  18. 003F23DC 8D 7D E4 lea edi,[ebp-1Ch]
  19. 003F23DF B9 07 00 00 00 mov ecx,7
  20. 003F23E4 B8 CC CC CC CC mov eax,0CCCCCCCCh
  21. 003F23E9 F3 AB rep stos dword ptr es:[edi]
  22. 003F23EB A1 04 C0 3F 00 mov eax,dword ptr [__security_cookie (03FC004h)]
  23. 003F23F0 33 C5 xor eax,ebp
  24. 003F23F2 89 45 FC mov dword ptr [ebp-4],eax
  25. 11: int a;
  26. 12: a = 100;
  27. 003F23F5 C7 45 F4 64 00 00 00 mov dword ptr [a],64h
  28. 13: for (int i = 0; i < a; i++)
  29. 003F23FC C7 45 E8 00 00 00 00 mov dword ptr [ebp-18h],0
  30. 003F2403 EB 09 jmp __$EncStackInitStart+32h (03F240Eh)
  31. 003F2405 8B 45 E8 mov eax,dword ptr [ebp-18h]
  32. 003F2408 83 C0 01 add eax,1
  33. 003F240B 89 45 E8 mov dword ptr [ebp-18h],eax
  34. 003F240E 8B 45 E8 mov eax,dword ptr [ebp-18h]
  35. 003F2411 3B 45 F4 cmp eax,dword ptr [a]
  36. 003F2414 7D 2B jge __$EncStackInitStart+65h (03F2441h)
  37. 14: {
  38. 15: cout << "Hello World! " << endl;
  39. 003F2416 8B F4 mov esi,esp
  40. 003F2418 68 3C 10 3F 00 push offset std::endl<char,std::char_traits<char> > (03F103Ch)
  41. 003F241D 68 30 9B 3F 00 push offset string "Hello World! " (03F9B30h)
  42. 003F2422 A1 DC D0 3F 00 mov eax,dword ptr [__imp_std::cout (03FD0DCh)]
  43. 003F2427 50 push eax
  44. 003F2428 E8 7C ED FF FF call std::operator<<<std::char_traits<char> > (03F11A9h)
  45. 003F242D 83 C4 08 add esp,8
  46. 003F2430 8B C8 mov ecx,eax
  47. 003F2432 FF 15 A8 D0 3F 00 call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (03FD0A8h)]
  48. 003F2438 3B F4 cmp esi,esp
  49. 003F243A E8 50 EE FF FF call __RTC_CheckEsp (03F128Fh)
  50. 16: }
  51. 003F243F EB C4 jmp __$EncStackInitStart+29h (03F2405h)
  52. 17: cin >> a;
  53. 003F2441 8B F4 mov esi,esp
  54. 003F2443 8D 45 F4 lea eax,[a]
  55. 003F2446 50 push eax
  56. 003F2447 8B 0D 98 D0 3F 00 mov ecx,dword ptr [__imp_std::cin (03FD098h)]
  57. 003F244D FF 15 9C D0 3F 00 call dword ptr [__imp_std::basic_istream<char,std::char_traits<char> >::operator>> (03FD09Ch)]
  58. 003F2453 3B F4 cmp esi,esp
  59. 003F2455 E8 35 EE FF FF call __RTC_CheckEsp (03F128Fh)
  60. 18: return 0;
  61. 003F245A 33 C0 xor eax,eax
  62. 19:
  63. 20: }
  64. 003F245C 52 push edx
  65. 003F245D 8B CD mov ecx,ebp
  66. 003F245F 50 push eax
  67. 003F2460 8D 15 8C 24 3F 00 lea edx,ds:[3F248Ch]
  68. 003F2466 E8 C0 ED FF FF call @_RTC_CheckStackVars@8 (03F122Bh)
  69. 003F246B 58 pop eax
  70. 003F246C 5A pop edx
  71. 003F246D 5F pop edi
  72. 003F246E 5E pop esi
  73. 003F246F 5B pop ebx
  74. 003F2470 8B 4D FC mov ecx,dword ptr [ebp-4]
  75. 003F2473 33 CD xor ecx,ebp
  76. 003F2475 E8 02 ED FF FF call @__security_check_cookie@4 (03F117Ch)
  77. 003F247A 81 C4 DC 00 00 00 add esp,0DCh
  78. 003F2480 3B EC cmp ebp,esp
  79. 003F2482 E8 08 EE FF FF call __RTC_CheckEsp (03F128Fh)
  80. 003F2487 8B E5 mov esp,ebp
  81. 003F2489 5D pop ebp
  82. 003F248A C3 ret
  83. 003F248B 90 nop
  84. 003F248C 01 00 add dword ptr [eax],eax
  85. 003F248E 00 00 add byte ptr [eax],al
  86. 003F2490 94 xchg eax,esp
  87. 003F2491 24 3F and al,3Fh
  88. 003F2493 00 F4 add ah,dh
  89. 003F2495 FF ?? ??????
  90. 003F2496 FF ?? ??????
  91. 19:
  92. 20: }
  93. 003F2497 FF 04 00 inc dword ptr [eax+eax]
  94. 003F249A 00 00 add byte ptr [eax],al
  95. 003F249C A0 24 3F 00 61 mov al,byte ptr ds:[61003F24h]
  96. 003F24A1 00 CC add ah,cl

5.小结

IDA反编译的结果更接近源程序,因为1.读取了pdb文件,2.不显示参数检查的反编译代码( __security_cookie和_RTC_CheckStackVars等)。

5.1 什么是security cookie

并不是windows系统自带的保护机制,并不是说一个确实存在溢出漏洞的程序,放到带security cookie保护的环境中,就不能正常溢出了。其原理是在所有变量入栈前,多压入一个变量(随机数),然后函数执行完之后,再去检查这个数是不是一样的,

5.2 什么是_RTC_CheckStackVars。

这也是一个编译器行为,_RTC_CheckStackVars第一个参数是函数要检查的栈顶地址,第二个参数指向结构体_RTC_framedesc。该结构体第一个参数是压栈的局部变量个数,第二个参数保存局部变量相关信息。分别是变量的栈偏移地址,变量大小和变量的名字。因此我们可以通过地址跳转最终找到出错的变量

标签: 安全 网络安全

本文转载自: https://blog.csdn.net/qq_20031585/article/details/124395343
版权归原作者 向往生 所有, 如有侵权,请联系我们删除。

“C++程序正向编译逆向反编译(一)”的评论:

还没有评论