首页
社区
课程
招聘
[求助]求教各位大神如何获得C语言函数体的大小?
发表于: 2012-2-19 18:57 11532

[求助]求教各位大神如何获得C语言函数体的大小?

2012-2-19 18:57
11532
求教看雪各位大神,如何使用C或者汇编获得一个C函数体的大小?
没有思路,路过的大神麻烦指点一下啊哈,谢谢。

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (13)
雪    币: 435
活跃值: (1212)
能力值: ( LV13,RANK:388 )
在线值:
发帖
回帖
粉丝
2
递归下降反汇编 当遇到retn的时候记住该地址并停止反汇编 当所有分支都分析完成后 离函数头最远的那个retn可能就是结束地址
理论上
2012-2-19 19:43
0
雪    币: 154
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
不好意思,没看懂。。
bitt能给帮忙详细讲解下吗,谢谢啦。
2012-2-19 19:49
0
雪    币: 240
活跃值: (10)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
4
#pragma check_stack (off)
static DWORD func(LPVOID *pParam)
{
//Your funciton
}

// Dummy function used to get the address after your function
static void AtferFunc(void)
{
        return;
}
#pragma check_stack

//calculate your fuction size
DWORD cbSize = ((BYTE *)(DWORD)AtferFunc - (BYTE *)(DWORD)func);
2012-2-19 20:07
0
雪    币: 154
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
请问下哈,学学学学。
#pragma check_stack (off)
#pragma check_stack

这两个命令是告诉编译器,这中间的代码用栈做存储空间么?
2012-2-19 20:53
0
雪    币: 1708
活跃值: (586)
能力值: ( LV15,RANK:670 )
在线值:
发帖
回帖
粉丝
6
这个办法好。
2012-2-19 22:22
0
雪    币: 2242
活跃值: (488)
能力值: ( LV9,RANK:200 )
在线值:
发帖
回帖
粉丝
7
http://bbs.pediy.com/showthread.php?t=102977
2012-2-19 22:26
0
雪    币: 1708
活跃值: (586)
能力值: ( LV15,RANK:670 )
在线值:
发帖
回帖
粉丝
8
Instructs the compiler to turn off stack probes if off (or –) is specified, or to turn on stack probes if on (or +) is specified.

#pragma check_stack([ {on | off}] )
#pragma check_stack{+ | –}

Remarks
If no argument is given, stack probes are treated according to the default. This pragma takes effect at the first function defined after the pragma is seen. Stack probes are neither a part of macros nor of functions that are generated inline.

If you don't give an argument for the check_stack pragma, stack checking reverts to the behavior specified on the command line. For more information, see Compiler Reference. The interaction of the #pragma check_stack and the /Gs option is summarized in the following table.
2012-2-19 22:27
0
雪    币: 1708
活跃值: (586)
能力值: ( LV15,RANK:670 )
在线值:
发帖
回帖
粉丝
9
如果是自己的程序,也可以用这个帖子的回复里提到的使用 PDB 的办法,更可靠。
2012-2-19 22:31
0
雪    币: 154
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
10
谢谢各位,我现在只想取得自己进程的函数体大小。
所以雪雪雪雪的方法就可以满足我的需求了。
只是关于
#pragma check_stack([ {on | off}] )
#pragma check_stack{+ | –}
还是没能理解它们的作用是什么。
求教,它们的作用是什么呢?谢谢。
2012-2-20 11:15
0
雪    币: 278
活跃值: (709)
能力值: ( LV15,RANK:520 )
在线值:
发帖
回帖
粉丝
11
C语言的函数大小比较好获得,也就那么几个调用约定,根据调用约定扫描函数头和函数结尾,比如以下特征码
push ebp
mov esp,ebp
根据这样来扫描程序的开始以及结束,加个计数器,计数字节的大笑,顺便把调用约定一块给整理出来。
2012-2-21 09:52
0
雪    币: 154
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
12
以下是小弟自己用VC写的线程注入代码。
程序前边都没有问题。
问题在CreateRemoteThread函数调用后,目标进程会崩溃。
我想可能是我写入目标进程的代码不对造成的,可是鼓秋好长时间了
还是没能找出错误的根源。
所以,哪位大神能帮小弟看一眼程序吗。
顺便指出到底是哪里出了问题,谢谢了。

#include "stdafx.h"
#include <Windows.h>

BYTE bCodeData[1024] = {0};

static DWORD Myfunc(LPVOID *pParam)
{
        HWND TestHwnd = FindWindow(NULL,TEXT("1.0MFC"));
        MessageBox(TestHwnd, TEXT("MyThread"), TEXT("aaaaaaaa"), MB_OK);
        return *(DWORD*)pParam;
}

int _tmain(int argc, _TCHAR* argv[])
{
        BYTE* pMyFunAddr = (BYTE*)(DWORD)Myfunc;
        int i = 0;
        do
        {
                bCodeData[i++] = *pMyFunAddr++;
        }while(*pMyFunAddr != 0xc3);                //ret = c3
       

        int iError;
        HWND TestHwnd = FindWindow(NULL,TEXT("1.0MFC"));
       
        if(TestHwnd == NULL)
        {
                iError = GetLastError();
                printf("FindWindow Error = %d",iError);
                getchar();
                return 1;
        }

        DWORD PID, TID;
        TID = ::GetWindowThreadProcessId (TestHwnd, &PID);

        HANDLE hProcess;
        hProcess = OpenProcess(PROCESS_ALL_ACCESS,false,PID);

        char szBuffer[10] = {0};
        *(DWORD*)szBuffer=1000;
        void *pDataRemote =(char*) VirtualAllocEx( hProcess, 0, sizeof(szBuffer), MEM_COMMIT,
        PAGE_READWRITE );

        iError = ::WriteProcessMemory( hProcess, pDataRemote, szBuffer,sizeof(szBuffer),NULL);
        if(!iError)
        {
                printf("WriteProcessMemory Data Error = %d",iError);
                getchar();
                return 1;
        }

        PDWORD pCodeRemote = (PDWORD) VirtualAllocEx( hProcess, 0, i, MEM_COMMIT,
        PAGE_EXECUTE_READWRITE );

        WriteProcessMemory( hProcess, pCodeRemote, (BYTE*)(DWORD)Myfunc, i, NULL);
        if(!iError)
        {
                printf("WriteProcessMemory Code Error = %d",iError);
                getchar();
                return 1;
        }

        HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0,
        (LPTHREAD_START_ROUTINE) pCodeRemote,//
        pDataRemote, 0 , NULL);
        DWORD h;
        if (hThread)
        {
        ::WaitForSingleObject( hThread, INFINITE );
        ::GetExitCodeThread( hThread, &h );
        printf("run and return %d ",h);
        ::CloseHandle( hThread );
        }
        return 0;
}
2012-2-21 16:34
0
雪    币: 154
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
13
嘿嘿,知道错误原因了。
谢谢楼上各位,真心感谢。
2012-2-21 19:09
0
雪    币: 442
活跃值: (43)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
14
老问题了

获取自己写的函数大小用两条函数,函数名就是地址,在欲测长度的函数下面再添加一条函数,计算差值就基本是了

有点要注意的就是编译器优化,两条函数不能错位,或者被完全优化掉

算别人写的就要用分析的技术了,麻烦多了
2012-2-22 06:50
0
游客
登录 | 注册 方可回帖
返回
//