系列C++问题请教高手之九:buffer overflow

系列C++问题请教高手之九:buffer overflow
发表于:2007-10-24 13:41:51 楼主
近日走火入魔,看了一下buffer overflow,在以下代码中:在VC6中打印出来的信息是:Address of function: 0x0040100f,但在LINUX中打印出来的提示却是段错误,请问应该怎么改?
#include "string.h"
#include "stdio.h"

int copy(char* input)
{
return 0;
}

int hacked(void)
{
printf("Can you see me now?/n");
exit(0);
}

void main(int argc, char* argv[])
{
printf("Address of function: 0x%08x/n",hacked); 
    exit(1);
}

2、另外一个问题是:
在VC6中反汇编,结果如下:hacked()的地址貌似是:00401080,和打印出来的信息:Address of function: 0x0040100f不一样!!请高手指点!

19:   int hacked(void)
20:   {
00401080   push        ebp
00401081   mov         ebp,esp
00401083   sub         esp,40h
00401086   push        ebx
00401087   push        esi
00401088   push        edi
00401089   lea         edi,[ebp-40h]
0040108C   mov         ecx,10h
00401091   mov         eax,0CCCCCCCCh
00401096   rep stos    dword ptr [edi]
21:       printf("Can you see me now?/n");
00401098   push        offset string "AAAABBBBCCCCDDDDEEEE/x80/x10@" (0042001c)
0040109D   call        printf (00401410)
004010A2   add         esp,4
22:       exit(0);
004010A5   push        0
004010A7   call        exit (00401280)
23:   }
 
 
20 
id="Topic_Zone" style="HEIGHT: 4px" marginwidth="0" marginheight="0" src="/u/AD/Topic_Zone.aspx" frameborder="0" width="100%" scrolling="no" height="0">
发表于:2007-10-24 13:48:001楼 得分:0
00401080   push        ebp
00401081   mov         ebp,esp
00401083   sub         esp,40h
00401086   push        ebx 

前面这一排是EIP,不是函数地址。

打开你的调试器的寄存器,看EIP的变化~
 
发表于:2007-10-24 14:01:042楼 得分:0
00401080   push        ebp 
00401081   mov         ebp,esp 
00401083   sub         esp,40h 
00401086   push        ebx  
前面这一排是EIP,不是函数地址。 
打开你的调试器的寄存器,看EIP的变化~ 
=============================================
有点不明白,EIP不就是函数地址吗??
如果不是,那么函数的入口地址跟调用它执行代码时的EIP有什么关系?
请解答,谢谢!
 
发表于:2007-10-24 14:08:003楼 得分:0
1.大约是不是gcc把你的小函数给内联了?

2.你在debug模式下用vc跟踪汇编代码,
0x0040100f  jmp 00401080

而且release模式下就没有这个问题。
 
发表于:2007-10-24 14:44:134楼 得分:0
关于第二个问题,有位二星专家裸体跪求过,见贴:
http://topic.csdn.net/u/20070716/22/72161ea9-3635-4d17-830c-4334573d5408.html
 
发表于:2007-10-24 14:47:175楼 得分:0
1.大约是不是gcc把你的小函数给内联了?
2.你在debug模式下用vc跟踪汇编代码, 
0x0040100f  jmp 00401080 
=================================
厉害,看了一下,果然是:
0x0040100f  jmp 00401080 
对于1、这一点先不管它。。。
我现在:想做的一点是覆盖EIP,照着书上讲的意思,写了如下代码,
int copy(char* input)
{
  char var[20];
  char chtocopy[24] = "AAAABBBBCCCCDDDDEEEE/x80/x10/x40";
  strcpy(var, chtocopy);
// var[20] =  '/x80 ';
// var[21] =  '/x10 ';
// var[22] =  '/x40 ';
return 0;
}
请问运行结果为什么不正确?
 
发表于:2007-10-24 14:55:226楼 得分:0
函数的地址指向的实际上是一个跳转指令
 
发表于:2007-10-24 14:59:527楼 得分:0
Address of function: 0x0040100f是真正的函数地址。
比如说0x0040100f处的memory为e9   d9   0a, 它其实表示这样一条指令
jmp 代码段首地址+0x00000ad9 (也就是0x00401080)
 
发表于:2007-10-24 15:00:448楼 得分:0
关于第二个问题,有位二星专家裸体跪求过,见贴: 
http://topic.csdn.net/u/20070716/22/72161ea9-3635-4d17-830c-4334573d5408.html
=====================================================
谢谢!
第二个问题解决了,现在可以跳转到hacked()函数了。
但第一个问题gcc的问题,还不明白:
记得有个保留字可以禁止编译器优化代码--就是没用的变量也留着。。。那个关键字是什么写着?请问:)
 
发表于:2007-10-24 15:07:219楼 得分:0
gcc编译器用的比较少。
有时间访问一下www.devdiv.net吧
 
发表于:2007-10-24 15:13:4210楼 得分:0
Address of function: 0x0040100f是真正的函数地址。 
比如说0x0040100f处的memory为e9   d9   0a, 它其实表示这样一条指令 
jmp 代码段首地址+0x00000ad9 (也就是0x00401080)
============================================
明白,现在把 char chtocopy[28] = "AAAABBBBCCCCDDDDEEEE/x80/x10/x40";
改成char chtocopy[28] = "AAAABBBBCCCCDDDDEEEEFFFF/x80/x10/x40";已经可以跳转了。

就是Linux下GCC的那个问题没解决,不知道为什么。。。。
 
发表于:2007-10-24 15:14:3711楼 得分:0
gcc编译器用的比较少。 
有时间访问一下www.devdiv.net吧
==============================
好的,这个网站做什么的?
 
发表于:2007-10-24 15:16:5512楼 得分:0
在VC中,只要把debug information format设置成Program Database (/Zi)就不会有这么一个jmp了。
release模式下,已经默认设置为Program Database (/Zi),所以没有jmp问题
 
发表于:2007-10-24 15:26:1613楼 得分:0
gcc编译器用的比较少。 
有时间访问一下www.devdiv.net吧
=======================================
看了,你做的B


在VC中,只要把debug information format设置成Program Database (/Zi)就不会有这么一个jmp了。 
release模式下,已经默认设置为Program Database (/Zi),所以没有jmp问题BS?
========================================================================================
好的,谢谢。
 
发表于:2007-10-24 15:26:5014楼 得分:0
贴个GCC反汇编出来的看看。
 
发表于:2007-10-24 16:25:5015楼 得分:0
贴个GCC反汇编出来的看看。
==================================
.file "Cpp1.cpp"
.section .rodata
.LC0:
.string "AAAABBBBCCCCDDDDEEEEFFFF/200/020@"
.text
.align 2
.globl _Z4copyPc
.type _Z4copyPc,@function
_Z4copyPc:
.LFB2:
pushl %ebp
.LCFI0:
movl %esp, %ebp
.LCFI1:
pushl %edi
.LCFI2:
pushl %esi
.LCFI3:
subl $64, %esp
.LCFI4:
leal -72(%ebp), %edi
movl $.LC0, %esi
cld
movl $7, %eax
movl %eax, %ecx
rep
movsl
subl $8, %esp
leal -72(%ebp), %eax
pushl %eax
leal -40(%ebp), %eax
pushl %eax
.LCFI5:
call strcpy
addl $16, %esp
movl $0, %eax
leal -8(%ebp), %esp
popl %esi
popl %edi
leave
ret
.LFE2:
.Lfe1:
.size _Z4copyPc,.Lfe1-_Z4copyPc
.section .rodata
.LC1:
.string "Can you see me now?/n"
.text
.align 2
.globl _Z6hackedv
.type _Z6hackedv,@function
_Z6hackedv:
.LFB4:
pushl %ebp
.LCFI6:
movl %esp, %ebp
.LCFI7:
subl $8, %esp
.LCFI8:
subl $12, %esp
pushl $.LC1
.LCFI9:
call printf
addl $16, %esp
subl $12, %esp
pushl $0
call exit
.LFE4:
.Lfe2:
.size _Z6hackedv,.Lfe2-_Z6hackedv
.section .rodata
.LC2:
.string "Address of function: 0x%08x/n"
.text
.align 2
.globl main
.type main,@function
main:
.LFB6:
pushl %ebp
.LCFI10:
movl %esp, %ebp
.LCFI11:
subl $8, %esp
.LCFI12:
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
subl $12, %esp
movl 12(%ebp), %eax
addl $4, %eax
pushl (%eax)
.LCFI13:
call _Z4copyPc
addl $16, %esp
subl $8, %esp
pushl $_Z6hackedv
pushl $.LC2
call printf
addl $16, %esp
subl $12, %esp
pushl $1
call exit
.LFE6:
.Lfe3:
.size main,.Lfe3-main
.ident "GCC: (GNU) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)"
 
发表于:2007-10-24 16:26:5216楼 得分:0
贴个GCC反汇编出来的看看。 
================================== 
C源代码如下:
#include"stdlib.h"
#include "string.h"
#include "stdio.h"
int copy(char* input)
{
char var[20];
char chtocopy[28] = "AAAABBBBCCCCDDDDEEEEFFFF/x80/x10/x40";
strcpy(var, chtocopy);
return 0;
}
int hacked(void)
{
printf("Can you see me now?/n");
exit(0);
}

int main(int argc, char* argv[])
{
  copy(argv[1]);
  printf("Address of function: 0x%08x/n",hacked);
  exit(1);
  return 0;
}

 
发表于:2007-10-27 12:00:4317楼 得分:0
请大家说说以下这段代码:
int hacked(void) 

printf("Can you see me now?/n"); 
exit(0); 


int main(int argc, char* argv[]) 

  printf("Address of function: 0x%08x/n",hacked); 
  exit(1); 
  return 0; 

在Linux下运行出现段错误的问题啊。。。谢谢!
 
发表于:2007-10-27 14:02:4018楼 得分:0
我的在linux下面一切正常啊。
 
发表于:2007-10-29 14:50:0019楼 得分:0
我的在linux下面一切正常啊。
======================
其怪,今天重新编译又没有问题了。。。。
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值