C++友元函数获取成员变量(作为调试后门程序)

一.源码例子:

[user:Backdoor] ls
main.cpp
[user:Backdoor] cat main.cpp
/// @file main.cpp
/// @brief
/// @author EastonWoo
/// 0.01
/// @date 2012-12-24

#include

#define DEBUG_BACKDOOR //后门宏

typedef struct s_hello
{
int aa;
char bb;
int cc;
}s_hello;

class CTest
{
public:
CTest()
{
m_a = 1;
m_s.aa = 10;
m_s.bb = 20;
m_s.cc = 30;
}
#ifdef DEBUG_BACKDOOR
friend bool getValue(CTest &disk, int index, void** output, int &size); //友元函数
typedef enum e_value
{
E_M_A,
E_M_S,
}e_value;
#endif

private:
int m_a;
s_hello m_s;
};

#ifdef DEBUG_BACKDOOR
bool getValue(CTest &disk, int index, void** output, int &size) //获取私有成员变量
{
switch(index)
{
case CTest::E_M_A:
*output = (void*)&disk.m_a;
size = sizeof(disk.m_a);
break;
case CTest::E_M_S:
*output = (void*)&disk.m_s;
size = sizeof(disk.m_s);
break;
default:
return false;
break;
}
return true;
}
#endif

int main()
{
CTest hd;
int *pA = NULL;
int size = 0;
getValue(hd,CTest::E_M_A,(void**)&pA,size);
printf(“*pA = %d, size = %d\n”,*pA,size);

s_hello* my_hello;
int my_size = 0;
getValue(hd,CTest::E_M_S,(void**)&my_hello,my_size);
printf(“*pA = %d, size = %d\n”,my_hello->aa,my_size);
return 0;
}
[user:Backdoor]

 

二.编译运行:

[user:Backdoor] g++ -g main.cpp
[user:Backdoor] ls
a.out* main

Add a Comment

电子邮件地址不会被公开。 必填项已用*标注