1
#include <stdio.h>
2
#include <stdlib.h>
3
4
int foo_normal(char*);
5
int foo_abnormal(char*);
6
7
int main()
8

{
9
int i;
10
char szTmp[] =
{
11
0x31, 0x31, 0x31, 0x31,
12
0x32, 0x32, 0x32, 0x32,
13
0x33, 0x33, 0x33, 0x33,
14
0x00, 0x40, 0x13, 0x45,
15
16
0x31, 0x31, 0x31, 0x31,
17
0x32, 0x32, 0x32, 0x32,
18
0x33, 0x33, 0x33, 0x33,
19
0x4e, 0x13, 0x40, 0x00
20
};
21
22
i = 0;
23
24
if (i == 0)
25
foo_normal(szTmp);
26
else
27
foo_abnormal(szTmp);
28
29
return(0);
30
}
31
32
33
int foo_normal(char* _sz)
34

{
35
char buffer[5];
36
memcpy(buffer, _sz, sizeof(char)*32);
37
printf("Application terminates normally.\n");
38
return(0);
39
}
40
int foo_abnormal(char* _sz)
41

{
42
char buffer[5];
43
memcpy(buffer, _sz, sizeof(char)*3);
44
printf("You should have never seen this.\n");
45
return (0);
46
}
銆銆鎵撶畻閫氳繃foo_normal涓殑memcpy鍑芥暟瑕嗙洊鏍堜腑鐨凟IP錛屾敼涓鴻皟鐢╢oo_abnormal澶勭殑璇彞錛屾潵杈懼埌婧㈠嚭鏀誨嚮鐨勭洰鐨勩傛寜鐓ф甯告儏鍐電湅錛屽湪foo_normal涓紝鏍堥噷鏈?瀛楄妭鐨凜S鍜?瀛楄妭鐨凟IP錛岀劧鍚庢槸5瀛楄妭鐨勫瓧絎︿覆鏁扮粍鈥斺斿榻愬悗鏄?瀛楄妭錛岃繕鏈?瀛楄妭鐨凟BP銆傛墍浠ュ綋寰buffer涓鍒?2瀛楄妭鏁版嵁灝卞彲浠ヨ鐩栨帀EIP鑰岃揪鍒版孩鍑虹殑鐩殑銆備絾瀹為檯涓婁嬌鐢?2瀛楄妭鐨勬暟鎹鐩朾uffer鍙婂叾鍚庣殑鏁版嵁錛屾墠鎶奅IP緇欑収欏懼埌銆備嬌鐢∣llyDBG璺熶簡涓涓嬶細
1 004013F2 /$ 55 push ebp
2 004013F3 |. 89E5 mov ebp, esp
3 004013F5 |. 83EC 28 sub esp, 28
4 004013F8 |. C74424 08 050>mov dword ptr [esp+8], 5 ; ||
5 00401400 |. 8B45 08 mov eax, dword ptr [ebp+8] ; ||
6 00401403 |. 894424 04 mov dword ptr [esp+4], eax ; ||
7 00401407 |. 8D45 E8 lea eax, dword ptr [ebp-18] ; ||
8 0040140A |. 890424 mov dword ptr [esp], eax ; ||
9 0040140D |. E8 FE050000 call <jmp.&msvcrt.memcpy> ; |\memcpy
10 00401412 |. C70424 D01340>mov dword ptr [esp], 004013D0 ; |ASCII "Application terminates normally.",LF
11 00401419 |. E8 EA050000 call <jmp.&msvcrt.printf> ; \printf
12 0040141E |. B8 00000000 mov eax, 0
13 00401423 |. C9 leave
14 00401424 \. C3 retn
銆銆鍙戠幇鍦ㄨ繘鍏ュ嚱鏁扮殑鏃跺欑敵璇蜂簡28涓瓧鑺傜殑絀洪棿鈥斺旈櫎鍘?2瀛楄妭緇檓emcpy鐨勫弬鏁幫紝姣旈鎯崇殑澶氫簡6瀛楄妭銆傜湅鏉ヤ嬌鐢ㄧ殑3.3.1鐗堟湰鐨刧cc鏄?6瀛楄妭瀵歸綈鐨勩?br>
銆銆……璁╂垜鎶撶媯浜嗕竴涓皬鏃躲?

]]>