- UID
- 2
- 精华
- 积分
- 7738
- 威望
- 点
- 宅币
- 个
- 贡献
- 次
- 宅之契约
- 份
- 最后登录
- 1970-1-1
- 在线时间
- 小时
|
楼主 |
发表于 2015-8-2 16:53:54
|
显示全部楼层
此代码不稳定,可以测得写入数据,读取数据应该是采用了异步方式因此数据不正确
- // WinDbgSpyDlg.cpp : 实现文件
- //
- #include "stdafx.h"
- #include "WinDbgSpy.h"
- #include "WinDbgSpyDlg.h"
- #include "afxdialogex.h"
- #include <tlhelp32.h>
- #include <shlwapi.h>
- #include <windows.h>
- #pragma comment(lib,"shlwapi.lib")
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
- class CAboutDlg : public CDialogEx
- {
- public:
- CAboutDlg();
- // 对话框数据
- enum { IDD = IDD_ABOUTBOX };
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
- // 实现
- protected:
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
- {
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialogEx::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
- END_MESSAGE_MAP()
- // CWinDbgSpyDlg 对话框
- CWinDbgSpyDlg::CWinDbgSpyDlg(CWnd* pParent /*=NULL*/)
- : CDialogEx(CWinDbgSpyDlg::IDD, pParent)
- , m_content(_T(""))
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CWinDbgSpyDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialogEx::DoDataExchange(pDX);
- DDX_Text(pDX, IDC_CONTENT, m_content);
- DDX_Control(pDX, IDC_MSGLIST, m_msglist);
- }
- BEGIN_MESSAGE_MAP(CWinDbgSpyDlg, CDialogEx)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_WM_TIMER()
- ON_LBN_SELCHANGE(IDC_MSGLIST, &CWinDbgSpyDlg::OnLbnSelchangeMsglist)
- END_MESSAGE_MAP()
- // CWinDbgSpyDlg 消息处理程序
- void CWinDbgSpyDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialogEx::OnSysCommand(nID, lParam);
- }
- }
- // 如果向对话框添加最小化按钮,则需要下面的代码
- // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
- // 这将由框架自动完成。
- void CWinDbgSpyDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // 用于绘制的设备上下文
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
- // 使图标在工作区矩形中居中
- 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;
- // 绘制图标
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialogEx::OnPaint();
- }
- }
- //当用户拖动最小化窗口时系统调用此函数取得光标
- //显示。
- HCURSOR CWinDbgSpyDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- char apiname[][256]=
- {
- "DbgKdReadVirtualMemoryApi ",
- "DbgKdWriteVirtualMemoryApi ",
- "DbgKdGetContextApi ",
- "DbgKdSetContextApi ",
- "DbgKdWriteBreakPointApi ",
- "DbgKdRestoreBreakPointApi ",
- "DbgKdContinueApi ",
- "DbgKdReadControlSpaceApi ",
- "DbgKdWriteControlSpaceApi ",
- "DbgKdReadIoSpaceApi ",
- "DbgKdWriteIoSpaceApi ",
- "DbgKdRebootApi ",
- "DbgKdContinueApi2 ",
- "DbgKdReadPhysicalMemoryApi ",
- "DbgKdWritePhysicalMemoryApi ",
- "DbgKdQuerySpecialCallsApi ",
- "DbgKdSetSpecialCallApi ",
- "DbgKdClearSpecialCallsApi ",
- "DbgKdSetInternalBreakPointApi ",
- "DbgKdGetInternalBreakPointApi ",
- "DbgKdReadIoSpaceExtendedApi ",
- "DbgKdWriteIoSpaceExtendedApi ",
- "DbgKdGetVersionApi ",
- "DbgKdWriteBreakPointExApi ",
- "DbgKdRestoreBreakPointExApi ",
- "DbgKdCauseBugCheckApi ",
- "DbgKdSwitchProcessor ",
- "DbgKdPageInApi ",
- "DbgKdReadMachineSpecificRegister ",
- "DbgKdWriteMachineSpecificRegister ",
- "OldVlm1 ",
- "OldVlm2 ",
- "DbgKdSearchMemoryApi ",
- "DbgKdGetBusDataApi ",
- "DbgKdSetBusDataApi ",
- "DbgKdCheckLowMemoryApi ",
- "DbgKdClearAllInternalBreakpointsApi",
- "DbgKdFillMemoryApi ",
- "DbgKdQueryMemoryApi ",
- "DbgKdSwitchPartition ",
- };
- BOOL CWinDbgSpyDlg::OnInitDialog()
- {
- CDialogEx::OnInitDialog();
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- BOOL bNameValid;
- CString strAboutMenu;
- bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
- ASSERT(bNameValid);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- SetIcon(m_hIcon, TRUE); // 设置大图标
- SetIcon(m_hIcon, FALSE); // 设置小图标
- InitOk = FALSE;
- SetTimer(0,100,NULL);
- int MapSize=0x1000000;
- do
- {
- hMapRead = CreateFileMappingA(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,MapSize,"WDBGMAPREAD");
- if(hMapRead == NULL)
- break;
- lpBufRead = MapViewOfFile(hMapRead,FILE_MAP_READ|FILE_MAP_WRITE,0,0,MapSize);//预留16M容量满则清理
- if(lpBufRead == NULL)
- break;
- hMapWrite = CreateFileMappingA(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,MapSize,"WDBGMAPWRITE");
- if(hMapWrite == NULL)
- break;
- lpBufWrite = MapViewOfFile(hMapWrite,FILE_MAP_READ|FILE_MAP_WRITE,0,0,MapSize);//预留16M容量满则清理
- if(lpBufWrite == NULL)
- break;
- hEvent = CreateEventA(NULL,FALSE,TRUE,"WDBGEVENT");
- memset(lpBufRead,0,MapSize);memset(lpBufWrite,0,MapSize);
- MAPOBJ* Ptr = (MAPOBJ*)lpBufRead;
- Ptr->ReadOffset = sizeof(MAPOBJ);
- Ptr->WriteOffset = sizeof(MAPOBJ);
- Ptr->MaxOffset = MapSize;
- Ptr = (MAPOBJ*)lpBufWrite;
- Ptr->ReadOffset = sizeof(MAPOBJ);
- Ptr->WriteOffset = sizeof(MAPOBJ);
- Ptr->MaxOffset = MapSize;
- InitOk = TRUE;
- //注入
- PROCESSENTRY32 pe32;
- HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if(hProcessSnap != INVALID_HANDLE_VALUE)
- {
- pe32.dwSize = sizeof(pe32);
- if(Process32First(hProcessSnap,&pe32))
- {
- do
- {
- if(StrStrIA(pe32.szExeFile,"windbg.exe"))
- {
- char str[]="e:\\Projects\\WinDbgSpy\\Debug\\WinDbgMon.dll";
- HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID);
- LPVOID dllname = VirtualAllocEx(hProcess, NULL, 256, MEM_COMMIT, PAGE_READWRITE);
- WriteProcessMemory(hProcess, dllname, str, sizeof(str), NULL);
- HANDLE hthread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)LoadLibraryA, dllname, 0, NULL);
- WaitForSingleObject(hthread, INFINITE);
- CloseHandle(hthread);
- CloseHandle(hProcess);
- }
- } while (Process32Next(hProcessSnap,&pe32));
- }
- CloseHandle(hProcessSnap);
- }
- } while (0);
- return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
- }
- BOOL CWinDbgSpyDlg::DestroyWindow()
- {
- if(lpBufRead)
- UnmapViewOfFile(lpBufRead);
- if(lpBufWrite)
- UnmapViewOfFile(lpBufWrite);
- if(hMapRead)
- CloseHandle(hMapRead);
- if(hMapWrite)
- CloseHandle(hMapWrite);
- KillTimer(0);
- return CDialogEx::DestroyWindow();
- }
- void CWinDbgSpyDlg::OnTimer(UINT_PTR nIDEvent)
- {
- if(!InitOk)
- return;
- if (WAIT_OBJECT_0 == WaitForSingleObject(hEvent,100))
- {
- MAPOBJ* mapobj1 = (MAPOBJ*)lpBufRead;
- MAPOBJ* mapobj2 = (MAPOBJ*)lpBufWrite;
- volatile int end1 = mapobj1->WriteOffset;
- volatile int end2 = mapobj2->WriteOffset;
- if(mapobj1->ReadOffset + sizeof(KD_PACKET) < end1)
- {
- FlushViewOfFile((BYTE*)mapobj1+mapobj1->ReadOffset,mapobj1->WriteOffset - mapobj1->ReadOffset);
- int origin = mapobj1->ReadOffset;
- if(!ProcessPacket(mapobj1, true, end1))
- mapobj1->ReadOffset = origin;
- }
- if(mapobj2->ReadOffset + sizeof(KD_PACKET) < end2)
- {
- int origin = mapobj2->ReadOffset;
- if(!ProcessPacket(mapobj2, false, end2))
- mapobj2->ReadOffset = origin;
- }
- SetEvent(hEvent);
- }
- CDialogEx::OnTimer(nIDEvent);
- }
- bool CWinDbgSpyDlg::ProcessPacket(MAPOBJ* mapobj, bool isreceive, int end)
- {
- int curbyte=GetByte(mapobj);//尝试读取一个字节,同步导引包
- switch(curbyte)
- {
- case BREAKIN_PACKET_BYTE:
- return TryReceiveBreakInPacket(mapobj, isreceive);
- break;
- case PACKET_LEADER_BYTE:
- return TryReceiveInfoPacket(mapobj, isreceive);//尝试读取信息包
- break;
- case CONTROL_PACKET_LEADER_BYTE:
- return TryReceiveControlPacket(mapobj, isreceive);//尝试读取控制包
- break;
- default:
- {
- while(mapobj->ReadOffset + sizeof(KD_PACKET) < end)
- {
- mapobj->ReadOffset++;
- int curbyte = *((BYTE*)mapobj+mapobj->ReadOffset);
- if(curbyte == BREAKIN_PACKET_BYTE || curbyte == PACKET_LEADER_BYTE || curbyte == CONTROL_PACKET_LEADER_BYTE)
- {
- BYTE* begin = (BYTE*)mapobj+mapobj->ReadOffset;
- if(begin[0] == begin[1] && begin[1] == begin[2])
- return true;
- }
- }
- break;
- }
- }
- return false;
- }
- int CWinDbgSpyDlg::GetByte(MAPOBJ* mapobj)
- {
- int curbyte = *((BYTE*)mapobj+mapobj->ReadOffset);
- mapobj->ReadOffset++;
- return curbyte;
- }
- bool CWinDbgSpyDlg::TryReceiveBreakInPacket(MAPOBJ* mapobj, bool isreceive)
- {
- int count=0;
- while(count < 3)//之前GetByte已经经过一个leader byte
- {
- if(*((BYTE*)mapobj+mapobj->ReadOffset) != PACKET_LEADER_BYTE)
- break;
- mapobj->ReadOffset++;
- count++;
- }
- if(count < 2)//无法同步,匹配下一个握手信号
- return true;
- if(isreceive)
- m_msglist.AddString("<=中断包");
- else
- m_msglist.AddString(">=中断包");
- }
- bool CWinDbgSpyDlg::TryReceiveInfoPacket(MAPOBJ* mapobj, bool isreceive)
- {
- char buf[256];
- int count=0;
- while(true)//之前GetByte已经经过一个leader byte
- {
- if(*((BYTE*)mapobj+mapobj->ReadOffset) != PACKET_LEADER_BYTE)
- break;
- mapobj->ReadOffset++;
- count++;
- }
- if(count < 2)//无法同步,匹配下一个握手信号
- return true;
- KD_PACKET* curpack = (KD_PACKET*)((BYTE*)mapobj+mapobj->ReadOffset - (int)&((KD_PACKET*)0)->PacketType);
- curpack->PacketLeader=PACKET_LEADER;
- KD_PACKET curpackr;
- memcpy(&curpackr,curpack,sizeof(curpackr));
- curpackr.PacketLeader = CONTROL_PACKET_LEADER;//重建头部
- mapobj->ReadOffset += sizeof(KD_PACKET) - (int)&((KD_PACKET*)0)->PacketType;
- if(mapobj->ReadOffset > mapobj->WriteOffset)
- return false;
- if(isreceive)
- strcpy(buf,"<=I-");
- else
- strcpy(buf,">=I-");
- switch(curpackr.PacketType)
- {
- case PACKET_TYPE_KD_STATE_CHANGE32://KD_PACKET后跟DBGKD_WAIT_STATE_CHANGE32
- case PACKET_TYPE_KD_STATE_CHANGE64:
- {//本该由第一次接收版本号决定所使用的结构体
- DBGKD_WAIT_STATE_CHANGE32* info = (DBGKD_WAIT_STATE_CHANGE32*)((BYTE*)mapobj+mapobj->ReadOffset);
- strcat(buf,"statechange32 ");
- switch(info->NewState)
- {
- case DbgKdExceptionStateChange:
- strcat(buf,"exc ");
- break;
- case DbgKdLoadSymbolsStateChange:
- strcat(buf,"sym ");
- break;
- case DbgKdCommandStringStateChange:
- strcat(buf,"str ");
- break;
- default:
- sprintf(buf,"%s%d ",buf,info->NewState);
- break;
- }
- }
- break;
- case PACKET_TYPE_KD_STATE_MANIPULATE://KD_PACKET后跟DBGKD_MANIPULATE_STATE
- {
- DBGKD_MANIPULATE_STATE32* info = (DBGKD_MANIPULATE_STATE32*)((BYTE*)mapobj+mapobj->ReadOffset);
- strcat(buf,"api ");
- int index=info->ApiNumber-DbgKdMin;
- if(index>=0 && index<=45)
- strcat(buf,apiname[info->ApiNumber-DbgKdMin]);
- else
- sprintf(buf,"%s%d",buf,index);
- }
- break;
- case PACKET_TYPE_KD_DEBUG_IO:
- {
- strcat(buf,"debugio ");
- }
- break;
- case PACKET_TYPE_KD_POLL_BREAKIN:
- {
- strcat(buf,"breakin ");
- }
- break;
- case PACKET_TYPE_KD_TRACE_IO:
- {
- strcat(buf,"traceio ");
- }
- break;
- case PACKET_TYPE_KD_CONTROL_REQUEST:
- {
- strcat(buf,"ctrlreq");
- }
- break;
- case PACKET_TYPE_KD_FILE_IO:
- {
- strcat(buf,"fileio");
- }
- break;
- default:
- sprintf(buf,"%s%d",buf,curpackr.PacketType);
- }
- vector<BYTE> newdata;
- newdata.insert(newdata.end(),(BYTE*)&curpackr,(BYTE*)(&curpackr+1));
- newdata.insert(newdata.end(),(BYTE*)mapobj+mapobj->ReadOffset,(BYTE*)mapobj+mapobj->ReadOffset+curpackr.ByteCount);
- mapobj->ReadOffset += curpackr.ByteCount;
- if(mapobj->ReadOffset > mapobj->WriteOffset)
- return false;
- //等待0xAA
- bool waitsuccess = false;
- while(true)//终止条件待处理
- {
- if(*((BYTE*)mapobj+mapobj->ReadOffset) == 0xAA)
- {
- waitsuccess = true;
- break;
- }
- mapobj->ReadOffset++;
- if(mapobj->ReadOffset > mapobj->WriteOffset-1)
- break;
- }
- mapobj->ReadOffset++;
- if(waitsuccess)
- {
- int index = dataarray.size();
- dataarray.push_back(newdata);
- int index2 = m_msglist.AddString(buf);
- m_msglist.SetItemData(index2,index);
- packetread.push_back(curpackr);
- }
- return waitsuccess;
- }
- bool CWinDbgSpyDlg::TryReceiveControlPacket(MAPOBJ* mapobj, bool isreceive)
- {
- char buf[256];
- int count=0;
- while(true)//之前GetByte已经经过一个leader byte
- {
- if(*((BYTE*)mapobj+mapobj->ReadOffset) != CONTROL_PACKET_LEADER_BYTE)
- break;
- mapobj->ReadOffset++;
- count++;
- }
- if(count < 2)//无法同步,匹配下一个握手信号
- return true;
- KD_PACKET* curpack = (KD_PACKET*)((BYTE*)mapobj+mapobj->ReadOffset - (int)&((KD_PACKET*)0)->PacketType);
- KD_PACKET curpackr;
- memcpy(&curpackr,curpack,sizeof(curpackr));
- curpackr.PacketLeader = CONTROL_PACKET_LEADER;//重建头部
- mapobj->ReadOffset += sizeof(KD_PACKET) - (int)&((KD_PACKET*)0)->PacketType;
- if(mapobj->ReadOffset > mapobj->WriteOffset)
- return false;
- if(isreceive)
- strcpy(buf,"<=C-");
- else
- strcpy(buf,">=C-");
- switch(curpackr.PacketType)
- {
- case PACKET_TYPE_KD_ACKNOWLEDGE:
- strcat(buf,"acknowledge");
- break;
- case PACKET_TYPE_KD_RESEND:
- strcat(buf,"resend");
- break;
- case PACKET_TYPE_KD_RESET:
- strcat(buf,"reset");
- break;
- default:
- sprintf(buf,"%s%d",buf,curpackr.PacketType);
- break;
- }
- vector<BYTE> newdata;
- newdata.insert(newdata.end(),(BYTE*)&curpackr,(BYTE*)(&curpackr+1));
- int index = dataarray.size();
- dataarray.push_back(newdata);
- int index2 = m_msglist.AddString(buf);
- m_msglist.SetItemData(index2,index);
- packetwrite.push_back(curpackr);
- return true;
- }
- void CWinDbgSpyDlg::OnLbnSelchangeMsglist()
- {
- int cursel=m_msglist.GetCurSel();
- if(cursel>=0)
- {
- int index = m_msglist.GetItemData(cursel);
- vector<BYTE>& data = dataarray[index];
- char buf[256];
- UpdateData(TRUE);
- m_content="";
- for(int i=0;i<data.size();i++)
- {
- sprintf(buf,"%02X ",data[i]);
- m_content += buf;
- if((i%16)==15)
- {
- m_content += "\r\n";
- }
- }
- UpdateData(FALSE);
- }
- }
复制代码
- // dllmain.cpp : 定义 DLL 应用程序的入口点。
- #include "stdafx.h"
- #include "detours.h"
- #include "common.h"
- #include <Shlwapi.h>
- #include <stdio.h>
- #pragma comment(lib,"detours.lib")
- #pragma comment(lib,"shlwapi.lib")
- HANDLE hMapRead;
- HANDLE hMapWrite;
- LPVOID lpBufRead;
- LPVOID lpBufWrite;
- CRITICAL_SECTION lock;
- BOOL InitOk;
- HANDLE hEvent;
- HANDLE currentpipe=INVALID_HANDLE_VALUE;
- HANDLE WINAPI NEW_CreateFileW(LPCWSTR lpFileName,DWORD dwDesiredAccess,DWORD dwShareMode,LPSECURITY_ATTRIBUTES lpSecurityAttributes,
- DWORD dwCreationDisposition,DWORD dwFlagsAndAttributes,HANDLE hTemplateFile);
- BOOL WINAPI NEW_ReadFile(HANDLE hFile,LPVOID lpBuffer,DWORD nNumberOfBytesToRead,LPDWORD lpNumberOfBytesRead,LPOVERLAPPED lpOverlapped);
- BOOL WINAPI NEW_WriteFile(HANDLE hFile,LPCVOID lpBuffer,DWORD nNumberOfBytesToWrite,LPDWORD lpNumberOfBytesWritten,LPOVERLAPPED lpOverlapped);
- #define G(X) GetProcAddress(GetModuleHandleW(L"kernel32.dll"),X)
- #define HookMax 3
- FARPROC OLD_FUNCS[HookMax] =
- {
- G("CreateFileW"),
- G("ReadFile"),
- G("WriteFile"),
- };
- FARPROC NEW_FUNCS[HookMax] =
- {
- (FARPROC)NEW_CreateFileW,
- (FARPROC)NEW_ReadFile,
- (FARPROC)NEW_WriteFile,
- };
- enum
- {
- ECreateFileW,
- EReadFile,
- EWriteFile,
- };
- void __cdecl Hook()
- {
- DetourRestoreAfterWith();
- DetourTransactionBegin();
- DetourUpdateThread(GetCurrentThread());
- for (int i = 0; i < HookMax; i++)
- {
- DetourAttach(&(PVOID&)OLD_FUNCS[i], NEW_FUNCS[i]);
- }
- DetourTransactionCommit();
- }
- void __cdecl UnHook()
- {
- DetourTransactionBegin();
- DetourUpdateThread(GetCurrentThread());
- for (int i = 0; i < HookMax; i++)
- {
- DetourDetach(&(PVOID&)OLD_FUNCS[i], NEW_FUNCS[i]);
- }
- DetourTransactionCommit();
- }
- void Init()
- {
- InitializeCriticalSection(&lock);
- int MapSize=0x1000000;
- hMapRead = OpenFileMappingA(FILE_MAP_READ|FILE_MAP_WRITE,FALSE,"WDBGMAPREAD");
- if(hMapRead == NULL)
- return;
- hMapWrite = OpenFileMappingA(FILE_MAP_READ|FILE_MAP_WRITE,FALSE,"WDBGMAPWRITE");
- if(hMapWrite == NULL)
- return;
- lpBufRead = MapViewOfFile(hMapRead,FILE_MAP_READ|FILE_MAP_WRITE,0,0,MapSize);//预留16M容量满则清理
- lpBufWrite = MapViewOfFile(hMapWrite,FILE_MAP_READ|FILE_MAP_WRITE,0,0,MapSize);//预留16M容量满则清理
- InitOk = TRUE;
- hEvent = OpenEventA(SYNCHRONIZE|EVENT_MODIFY_STATE,FALSE,"WDBGEVENT");
- Hook();
- }
- BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
- {
- switch (ul_reason_for_call)
- {
- case DLL_PROCESS_ATTACH:
- Init();
- break;
- case DLL_THREAD_ATTACH:
- break;
- case DLL_THREAD_DETACH:
- break;
- case DLL_PROCESS_DETACH:
- UnHook();
- break;
- }
- return TRUE;
- }
- HANDLE WINAPI NEW_CreateFileW(LPCWSTR lpFileName,DWORD dwDesiredAccess,DWORD dwShareMode,LPSECURITY_ATTRIBUTES lpSecurityAttributes,
- DWORD dwCreationDisposition,DWORD dwFlagsAndAttributes,HANDLE hTemplateFile)
- {
- HANDLE hFile = ((HANDLE (WINAPI*)(LPCWSTR,DWORD,DWORD,LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))OLD_FUNCS[ECreateFileW])
- (lpFileName,dwDesiredAccess,dwShareMode,lpSecurityAttributes,dwCreationDisposition,dwFlagsAndAttributes,hTemplateFile);
- if(StrStrIW(lpFileName,L"pipe\\com_") && hFile != INVALID_HANDLE_VALUE)
- {
- currentpipe = hFile;
- }
- return hFile;
- }
- BOOL WINAPI NEW_ReadFile(HANDLE hFile,LPVOID lpBuffer,DWORD nNumberOfBytesToRead,LPDWORD lpNumberOfBytesRead,LPOVERLAPPED lpOverlapped)
- {
- BOOL ret = ((BOOL (WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD,LPOVERLAPPED))OLD_FUNCS[EReadFile])(hFile,lpBuffer,nNumberOfBytesToRead,
- lpNumberOfBytesRead,lpOverlapped);
- if(ret && currentpipe == hFile)
- {
- if(InitOk && WAIT_OBJECT_0 == WaitForSingleObject(hEvent,INFINITE))
- {
- MAPOBJ* mapobj = (MAPOBJ*)lpBufRead;
- //memset((BYTE*)mapobj+mapobj->WriteOffset,0,*lpNumberOfBytesRead);
- FlushViewOfFile((BYTE*)mapobj+mapobj->WriteOffset,*lpNumberOfBytesRead);
- memcpy((BYTE*)mapobj+mapobj->WriteOffset,lpBuffer,*lpNumberOfBytesRead);
- FlushViewOfFile((BYTE*)mapobj+mapobj->WriteOffset,*lpNumberOfBytesRead);
- mapobj->WriteOffset += *lpNumberOfBytesRead;
- SetEvent(hEvent);
- }
- }
- return ret;
- }
- BOOL WINAPI NEW_WriteFile(HANDLE hFile,LPCVOID lpBuffer,DWORD nNumberOfBytesToWrite,LPDWORD lpNumberOfBytesWritten,LPOVERLAPPED lpOverlapped)
- {
- BOOL ret = ((BOOL (WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD,LPOVERLAPPED))OLD_FUNCS[EWriteFile])(hFile,lpBuffer,nNumberOfBytesToWrite,
- lpNumberOfBytesWritten,lpOverlapped);
- if(ret && currentpipe == hFile)
- {
- if(InitOk && WAIT_OBJECT_0 == WaitForSingleObject(hEvent,INFINITE))
- {
- MAPOBJ* mapobj = (MAPOBJ*)lpBufWrite;
- memset((BYTE*)mapobj+mapobj->WriteOffset,0,*lpNumberOfBytesWritten);
- memcpy((BYTE*)mapobj+mapobj->WriteOffset,lpBuffer,*lpNumberOfBytesWritten);
- mapobj->WriteOffset += *lpNumberOfBytesWritten;
- SetEvent(hEvent);
- }
- }
- return ret;
- }
复制代码 |
|