首页
社区
课程
招聘
[旧帖] [讨论]钱林松深拷贝构造函数一章求解 0.00雪花
发表于: 2015-11-22 19:10 2663

[旧帖] [讨论]钱林松深拷贝构造函数一章求解 0.00雪花

2015-11-22 19:10
2663
源代码
#include <iostream>
using namespace std;

class CMyString{
public:
        CMyString(){
                m_pString=NULL;
        }
        CMyString(CMyString&obj){
                int nLen=strlen(obj.m_pString);
                this->m_pString=new char[nLen+sizeof(char)];
                strcpy(this->m_pString,obj.m_pString);
        }
        ~CMyString(){
                if(m_pString!=NULL)
                {
                        delete[] m_pString;
                        m_pString=NULL;
                }
        }
        void SetString(char* pString){
                int nLen=strlen(pString);
                if(m_pString!=NULL)
                {
                        delete [] m_pString;
                        m_pString =NULL;
                }
                m_pString=new char[nLen+sizeof(char)];
                strcpy(m_pString,pString);
        }
        char*m_pString;
};

void Show(CMyString MyString){
        printf(MyString.m_pString);
}

int main(int argc,char*argv[]){
        CMyString MyString;
        MyString.SetString("Hello");
        Show(MyString);
        return 0;
}

37:
38:   int main(int argc,char*argv[]){
00401120   push        ebp
00401121   mov         ebp,esp
00401123   push        0FFh
00401125   push        offset __ehhandler$_main (0041f289)
0040112A   mov         eax,fs:[00000000]
00401130   push        eax
00401131   mov         dword ptr fs:[0],esp
00401138   sub         esp,50h
0040113B   push        ebx
0040113C   push        esi
0040113D   push        edi
0040113E   lea         edi,[ebp-5Ch]
00401141   mov         ecx,14h
00401146   mov         eax,0CCCCCCCCh
0040114B   rep stos    dword ptr [edi]
39:       CMyString MyString;
0040114D   lea         ecx,[ebp-10h]
00401150   call        @ILT+5(CMyString::CMyString) (0040100a)
00401155   mov         dword ptr [ebp-4],0
40:       MyString.SetString("Hello");
0040115C   push        offset string "Hello" (0043101c)
00401161   lea         ecx,[ebp-10h]
00401164   call        @ILT+0(CMyString::SetString) (00401005)
41:       Show(MyString);
00401169   push        ecx
0040116A   mov         ecx,esp
0040116C   mov         dword ptr [ebp-14h],esp
0040116F   lea         eax,[ebp-10h]
00401172   push        eax
00401173   call        @ILT+10(CMyString::CMyString) (0040100f)
00401178   mov         dword ptr [ebp-1Ch],eax
0040117B   call        @ILT+35(Show) (00401028)
00401180   add         esp,4
42:       return 0;
00401183   mov         dword ptr [ebp-18h],0
0040118A   mov         dword ptr [ebp-4],0FFFFFFFFh
00401191   lea         ecx,[ebp-10h]
00401194   call        @ILT+30(CMyString::~CMyString) (00401023)
00401199   mov         eax,dword ptr [ebp-18h]
43:   }
0040119C   mov         ecx,dword ptr [ebp-0Ch]
0040119F   mov         dword ptr fs:[0],ecx
004011A6   pop         edi
004011A7   pop         esi
004011A8   pop         ebx
004011A9   add         esp,5Ch
004011AC   cmp         ebp,esp
004011AE   call        __chkesp (004084c0)
004011B3   mov         esp,ebp
004011B5   pop         ebp
004011B6   ret

反汇编中
0040116A   mov         ecx,esp
这一行的表示获取参数对象的地址,保存到ecx中。
那为什么用esp表示参数对象的地址,并把他放在[ebp-14h],第一个参数不是[ebp+8]吗

两个问题呀


[培训]《安卓高级研修班(网课)》月薪三万计划,掌握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法

收藏
免费 0
支持
分享
最新回复 (12)
雪    币: 3586
活跃值: (759)
能力值: ( LV4,RANK:45 )
在线值:
发帖
回帖
粉丝
2
现在还在main函数里呀,ebp+8 是main函数的参数,进去 0040117B  call  @ILT+35(Show) (00401028) 里面才是 show的实现
2015-11-22 20:02
0
雪    币: 18
活跃值: (138)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
如果是main早就在main那段说明了,但是为什么用esp里存放着参数对象的地址,并把他放在[ebp-14h]中,ebp偏移14h呢
2015-11-22 20:22
0
雪    币: 3586
活跃值: (759)
能力值: ( LV4,RANK:45 )
在线值:
发帖
回帖
粉丝
4
这几行只是在为调用show做准备……还没有进入show函数里面……你看00401028
2015-11-22 23:37
0
雪    币: 1829
活跃值: (1357)
能力值: (RANK:50 )
在线值:
发帖
回帖
粉丝
5
参数对象使用栈空间,00401169处的push ecx相当于是sub esp, 4,也就给参数对象留下了空间(这个CMyString类型的参数对象只占4字节,关于对象长度的计算前面章节有讲),于是此时esp就是对象首地址了。
2015-11-23 00:29
0
雪    币: 18
活跃值: (138)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
00401169   push        ecx处的push ecx是把ecx中的数值压时栈,但是此时ecx的数值并不是为空,原先的ecx数值跑哪儿去了,原先的值为00431024
上传的附件:
2015-11-26 22:35
0
雪    币: 112
活跃值: (57)
能力值: ( LV12,RANK:200 )
在线值:
发帖
回帖
粉丝
7
你知道原来的ecx值, 那就单步好了,你看见ecx的值变了,再分析ecx到哪去了.
你可以将你编译好的PE发上来, 大家可以帮你单步下.

00401161 lea ecx, [ebp-10h] ; 这里ecx作为计数器, 被改掉了吧?
2015-11-27 09:29
0
雪    币: 18
活跃值: (138)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
源代码
#include <iostream>
using namespace std;

class CMyString{
public:
  CMyString(){
    m_pString=NULL;
  }
  CMyString(CMyString&obj){
    int nLen=strlen(obj.m_pString);
    this->m_pString=new char[nLen+sizeof(char)];
    strcpy(this->m_pString,obj.m_pString);
  }
  ~CMyString(){
    if(m_pString!=NULL)
    {
      delete[] m_pString;
      m_pString=NULL;
    }
  }
  void SetString(char* pString){
    int nLen=strlen(pString);
    if(m_pString!=NULL)
    {
      delete [] m_pString;
      m_pString =NULL;
    }
    m_pString=new char[nLen+sizeof(char)];
    strcpy(m_pString,pString);
  }
  char*m_pString;
};

void Show(CMyString MyString){
  printf(MyString.m_pString);
}

int main(int argc,char*argv[]){
  CMyString MyString;
  MyString.SetString("Hello");
  Show(MyString);
  return 0;
}

这个是源代码直接在里面看反汇编生成的,
断点在Show(MyString);处
2015-11-27 18:25
0
雪    币: 18
活跃值: (138)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
9
在vs里可以直接看反汇编,这个是vc6.0看到的,用vs又不一样
2015-11-27 18:29
0
雪    币: 18
活跃值: (138)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
10
[QUOTE=SilentGamb;1403715]你知道原来的ecx值, 那就单步好了,你看见ecx的值变了,再分析ecx到哪去了.
你可以将你编译好的PE发上来, 大家可以帮你单步下.

00401161 lea ecx, [ebp-10h] ; 这里ecx作为计数器, 被改掉了吧?[/QUOTE]

你可以看看上面我说的
2015-11-28 16:12
0
雪    币: 112
活跃值: (57)
能力值: ( LV12,RANK:200 )
在线值:
发帖
回帖
粉丝
11
你说的vc6和vs啥区别?
2015-11-28 17:15
0
雪    币: 18
活跃值: (138)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
12
一个是高级版本,一个是底级版本
2015-11-29 13:46
0
雪    币: 112
活跃值: (57)
能力值: ( LV12,RANK:200 )
在线值:
发帖
回帖
粉丝
13
/// 实验环境 :vs2010控制台, 贴的就是你上面贴出的代码, 一点没变
/// 实验目的 : 找出执行 SetString后, 执行Show(MyString)前, 谁将ecx改了
/// 实验结论 : SetString中的strlen(00B11747)将ecx改了

    CMyString MyString;
00B1153D  lea         ecx,[ebp-14h]  ///< ECX = 001FFCC4
00B11540  call        CMyString::CMyString (0B110CDh)  
00B11545  mov         dword ptr [ebp-4],0  ///< ECX = 001FFCC4 没变

ecx始终是由 ecx,[ebp-14h] 来的

00B11743  mov         eax,dword ptr [pString]  
00B11746  push        eax  
00B11747  call        @ILT+165(_strlen) (0B110AAh) ///< 将ecx改掉了
00B1174C  add         esp,4  

////////////////////////////////////////////////////////////////////////////////
    void SetString(char* pString){
00B11720  push        ebp  
00B11721  mov         ebp,esp  
00B11723  sub         esp,0F0h  
00B11729  push        ebx  
00B1172A  push        esi  
00B1172B  push        edi /// 保存现场结束
00B1172C  push        ecx /// 被 00B11747  call        @ILT+165(_strlen) (0B110AAh) ///< 将ecx改掉了
00B1172D  lea         edi,[ebp-0F0h]  
00B11733  mov         ecx,3Ch  
00B11738  mov         eax,0CCCCCCCCh  
00B1173D  rep stos    dword ptr es:[edi]  
00B1173F  pop         ecx  ///< 看, 这里就将ecx弹出来了, SetString执行完后, ecx已经变了
00B11740  mov         dword ptr [ebp-8],ecx  
        int nLen=strlen(pString);
00B11743  mov         eax,dword ptr [pString]  
00B11746  push        eax  
00B11747  call        @ILT+165(_strlen) (0B110AAh)  /// 执行完后, 将ecx改掉了
00B1174C  add         esp,4  
00B1174F  mov         dword ptr [nLen],eax  
        if(m_pString!=NULL)
00B11752  mov         eax,dword ptr [this]  
00B11755  cmp         dword ptr [eax],0  
00B11758  je          CMyString::SetString+5Dh (0B1177Dh)  
        {
            delete [] m_pString;
00B1175A  mov         eax,dword ptr [this]  
00B1175D  mov         ecx,dword ptr [eax]  
00B1175F  mov         dword ptr [ebp-0ECh],ecx  
00B11765  mov         edx,dword ptr [ebp-0ECh]  
00B1176B  push        edx  
00B1176C  call        operator delete[] (0B11023h)  
00B11771  add         esp,4  
            m_pString =NULL;
00B11774  mov         eax,dword ptr [this]  
00B11777  mov         dword ptr [eax],0  
        }
        m_pString=new char[nLen+sizeof(char)];
00B1177D  mov         eax,dword ptr [nLen]  
00B11780  add         eax,1  
00B11783  push        eax  
00B11784  call        operator new[] (0B110AFh)  
00B11789  add         esp,4  
00B1178C  mov         dword ptr [ebp-0E0h],eax  
00B11792  mov         ecx,dword ptr [this]  
00B11795  mov         edx,dword ptr [ebp-0E0h]  
00B1179B  mov         dword ptr [ecx],edx  
        strcpy(m_pString,pString);
00B1179D  mov         eax,dword ptr [pString]  
00B117A0  push        eax  
00B117A1  mov         ecx,dword ptr [this]  
00B117A4  mov         edx,dword ptr [ecx]  
00B117A6  push        edx  
00B117A7  call        @ILT+175(_strcpy) (0B110B4h)  
00B117AC  add         esp,8  
    }
00B117AF  pop         edi  ///< 开始恢复现场, ecx并不在恢复现场的范围内
00B117B0  pop         esi  
00B117B1  pop         ebx  
00B117B2  add         esp,0F0h  
00B117B8  cmp         ebp,esp  
00B117BA  call        @ILT+335(__RTC_CheckEsp) (0B11154h)  
00B117BF  mov         esp,ebp  
00B117C1  pop         ebp  
00B117C2  ret         4
2015-11-29 18:23
0
游客
登录 | 注册 方可回帖
返回
//