三种方法打印 main函数的返回地址的值(old EIP)(用途,你懂得!)

这里可以简单的修改任意函数的返回地址,可以做到自定义EIP的指向,即可执行当前进程空间的任意指令,这里只是让大家更清楚栈帧结构,没有涉及跨进程的inline HOOK 等,后面会陆续讲下读取任意进程内存,修改任意进程函数执行流程等方法。

        废话不多说了,直接上菜:     

 

[cpp] view plain copy
 
  1. #include <stdio.h>  
  2. #include <windows.h>  
  3.   
  4. /*  
  5.  
  6. 打印 main函数的返回地址的值(用途,你懂得!)  
  7.  
  8. 所需知识:函数栈帧结构 
  9.  
  10.   C调用约定,自右向左压栈 
  11.  
  12.   push argv 
  13.   push argc 
  14.   call main ; jmp main 
  15.  
  16.  
  17. main: 
  18.   push ebp 
  19.   mov ebp, esp 
  20.   sub esp, 0x4 ; 0x4 == sum (sizeof(type) * count) 
  21.  
  22.  
  23. 栈中: 
  24.   argv 
  25.   argc ; new ebp + 8 
  26.   eip  ; main ret 地址 == new ebp + 4 
  27.   ebp  ; old ebp value, new ebp postation 
  28.   i    ; i 局部变量 == new ebp - 4 
  29.  
  30.  
  31.  */  
  32. int main(int argc, char* argv[])  
  33. {  
  34.     int i = 0; // ebp - 4  
  35.   
  36.     __asm {  
  37.   
  38.         mov eax, [ebp + 4]  
  39.         mov i, eax  
  40.   
  41.     }  
  42.   
  43.     printf("%08x\n",i);  
  44.   
  45.     printf("%p\n", *(DWORD*)(((DWORD)&argc) - 4));  
  46.   
  47.     printf("%p\n", *(DWORD*)(((DWORD)&i) + 8));  
  48.       
  49.     return 0;  
  50. }  

 

还有下面的调试图,一目了然:


https://blog.csdn.net/l_f0rm4t3d/article/details/39648643

posted @ 2018-03-22 20:03  findumars  Views(1064)  Comments(0Edit  收藏  举报