在0例子的基礎上植入代碼:
首先要植入的代碼存在password.txt文件中,通過文件讀取進入進程內存
其次利用verify_password( )函數中的strcpy把讀取到的文件內容(包含要植入的代碼)拷貝到buffer內存中
最後找到buffer(植入的代碼處於buffer的起始位置)的地址,用這個地址覆蓋掉調用verify_password( )函數之後的返回地址,就會執行我們的代碼了
1、我們要植入的彙編代碼,對應16進制如下圖:
注意上面連續0x90:
第一個連續4字節0x90:需要填入MessageBoxA( )函數地址,
第二個連續3字節0x90:補齊我們植入的代碼,使其是4字節的整數倍
第三個連續8字節0x90:覆蓋authenticated局部變量和用來保存main函數的ebp
第四個連續4字節0x90:需要填入verify_password( )函數中buffer的地址或者main函數中password的地址(兩者地址不同,值一致)
2、C代碼如下,buffer改爲32字節,main函數中加載user32.dll庫LoadLibraryA("user32.dll")
#include <stdio.h>
#include <Windows.h>
#define PASSWORD "123456"
int verify_password(char * password)
{
int authenticated;
char buffer[32];
authenticated=strcmp(password,PASSWORD);
strcpy(buffer,password);
return authenticated;
}
int main()
{
int valid_flag=0;
char password[1024];
LoadLibraryA("user32.dll");
FILE * fp=NULL;
if( !(fp=fopen("D:\\password.txt","r")) )
{
printf("open file fail!\n");
exit(0);
}
fscanf(fp,"%s",password);
fclose(fp);
fp=NULL;
valid_flag=verify_password(password);
if(valid_flag)
printf("incorrect password!\n");
else
printf("Congratulation! You have passed the verification!\n");
system("pause");
return 0;
}
3、編譯生成debug版本,記得編譯選項:
基本運行時檢查:默認值(就是兩者都不用)
緩衝區安全檢查:否
4、od調試找到buffer地址或者password地址和MessageBoxA地址
buffer地址:0x0012FAE4
password地址:0x0012FB64
(兩處的內存是一樣的,用哪個地址都可以)
MessageBoxA地址:0x77D507EA
5、把兩個地址填入password.txt文件對應處
6、調試
返回到0x12FB64地址代碼(我們植入的代碼)
轉到我們植入的代碼並彈出消息框