📄 keyloggerdlg.cpp
字号:
// KeyLoggerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "KeyLogger.h"
#include "KeyLoggerDlg.h"
#include <assert.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#pragma warning(disable:4996)
//#include "GlobalUtility.h"
#include "Debug.h"
using namespace NGlobalUtils;
// Load Driver Flag
#define LOADDRIVER
#define LAN LANG_CHINESE
#define SUB_LAN SUBLANG_CHINESE_SIMPLIFIED
#ifdef _DEBUG
#define OUTPUT_DEBUG CDebug::Print
#else
#define OUTPUT_DEBUG
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CKeyLoggerDlg dialog
CKeyLoggerDlg::CKeyLoggerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CKeyLoggerDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
TCHAR szDir[MAX_PATH];
GetModuleFileName(NULL, szDir, MAX_PATH);
*StrRChr(szDir, szDir + lstrlen(szDir), _T('\\')) = 0;
// Device Handle
m_hDevice = NULL;
// Driver Name
lstrcpy(m_DriverName, HIDEPORT_SYS_NODE);
// Driver Path
lstrcpy(m_szDriverPath, szDir);
#ifdef LOADDRIVER
lstrcat(m_szDriverPath, _T("\\SafeRun.sys"));
#else
lstrcat(m_szDriverPath, _T("\\SafeRun.sys"));
#endif
}
void CKeyLoggerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CKeyLoggerDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BTN_WSS_TODO, &CKeyLoggerDlg::OnBnClickedBtnWssTodo)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BTN_BROWSE, &CKeyLoggerDlg::OnBnClickedBtnBrowse)
ON_BN_CLICKED(IDC_BTN_APPLY, &CKeyLoggerDlg::OnBnClickedBtnApply)
END_MESSAGE_MAP()
// CKeyLoggerDlg message handlers
BOOL CKeyLoggerDlg::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
//
// Set TopMost
//
::SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
m_bRunning = FALSE;
SetDlgItemText(IDC_BTN_WSS_TODO, SZ_START);
SetDlgItemText(IDC_STATIC_WSS_STAUTS, SZ_STATUS_STOPPED);
SetDlgItemText(IDC_EDIT_WSS_PATH, m_szDriverPath);
// Run it
OnBnClickedBtnWssTodo();
return TRUE; // return TRUE unless you set the focus to a control
}
void CKeyLoggerDlg::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 CKeyLoggerDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CKeyLoggerDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
BOOL CKeyLoggerDlg::ShowErrorMessage( TCHAR* szError, DWORD dwError )
{
//
HLOCAL hlocal = NULL; // Buffer that gets the error message string
// Get the error code's textual description
BOOL fOk = FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, dwError,
MAKELANGID( LAN,SUB_LAN ), (PTSTR) &hlocal, 0, NULL);
if (!fOk)
{
// Is it a network-related error?
HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL,
DONT_RESOLVE_DLL_REFERENCES);
if (hDll != NULL)
{
FormatMessage(
FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM, hDll, dwError,
MAKELANGID(LAN, SUB_LAN), (PTSTR) &hlocal, 0, NULL);
FreeLibrary(hDll);
}
}
if (hlocal != NULL)
{
LPCTSTR szResult = (LPCTSTR) LocalLock(hlocal);
TCHAR szTemp[MAX_PATH];
wsprintf(szTemp, _T("\nFailed With %u:"), dwError);
TCHAR* szBuffer = new TCHAR[lstrlen(szResult) + lstrlen(szError) + 20];
lstrcpy(szBuffer, szError);
lstrcat(szBuffer, szTemp);
lstrcat(szBuffer, szResult);
MessageBox(szBuffer, _T("ERROR"), MB_ICONERROR);
//
LocalFree(hlocal);
delete [] szBuffer;
}
else
{
return FALSE;
}
return TRUE;
}
void CKeyLoggerDlg::OnBnClickedBtnWssTodo()
{
if(!m_bRunning)
{
BOOL bOK = FALSE;
DWORD dwError = 0;
if( m_SCManagerUtility.LoadDeviceDriver(m_DriverName, m_szDriverPath, &this->m_hDevice, &dwError))
{
//
bOK = TRUE;
this->m_bRunning = TRUE;
SetDlgItemText(IDC_BTN_WSS_TODO, SZ_STOP);
SetDlgItemText(IDC_STATIC_WSS_STAUTS, SZ_STATUS_RUNNING);
}
else
{
//
this->ShowErrorMessage(_T("Init Wss Driver Failed.."), dwError);
}
}
else
{
BOOL bOK = FALSE;
if(this->m_hDevice)
{
CloseHandle(this->m_hDevice);
this->m_hDevice = NULL;
}
if( m_SCManagerUtility.UnloadDeviceDriver(m_DriverName))
{
bOK = TRUE;
this->m_bRunning = FALSE;
SetDlgItemText(IDC_BTN_WSS_TODO, SZ_START);
SetDlgItemText(IDC_STATIC_WSS_STAUTS, SZ_STATUS_STOPPED);
}
else
{
m_SCManagerUtility.OpenDevice(m_DriverName, &this->m_hDevice);
}
SetDlgItemText(IDC_STATIC_INFO, _T(""));
}
}
void CKeyLoggerDlg::OnDestroy()
{
if(m_bRunning)
{
//
//
//
OnBnClickedBtnWssTodo();
}
CDialog::OnDestroy();
}
//////////////////////////////////////////////////////////////////////////
void CKeyLoggerDlg::OnBnClickedBtnBrowse()
{
//
CFileDialog dlg(TRUE, NULL, NULL, OFN_ALLOWMULTISELECT,
_T("Executable Files (*.EXE)|*.EXE||"));
if(dlg.DoModal() != IDOK)
{
return;
}
SetDlgItemText(IDC_EDIT_PATH, dlg.m_pOFN->lpstrFile);
}
BOOL CKeyLoggerDlg::CreateNormalProcess(LPCTSTR lpCommandLine, BOOL bHide /*= TRUE*/,
LPPROCESS_INFORMATION lpProInfor /*= NULL*/)
{
// Create Process
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
if(bHide)
{
si.wShowWindow = SW_HIDE;
si.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
}
else
{
si.wShowWindow = SW_NORMAL;
si.dwFlags = STARTF_USEPOSITION|STARTF_USESIZE;
}
/*
#ifndef _DEBUG
si.lpDesktop = _T("Winsta0\\Winlogon");
#endif
*/
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
// Start the child process.
TCHAR lpCommand[512] = {0};
wsprintf(lpCommand, _T("%s"), lpCommandLine);
BOOL bOK = ::CreateProcess(
NULL, lpCommand, NULL, NULL, FALSE, CREATE_NEW_CONSOLE,
NULL, NULL, &si, &pi);
if(lpProInfor)
{
memcpy(lpProInfor, &pi, sizeof(PROCESS_INFORMATION));
}
return bOK;
}
void CKeyLoggerDlg::OnBnClickedBtnApply()
{
TCHAR szProcess[MAX_PATH] = {0};
GetDlgItemText(IDC_EDIT_PATH, szProcess, MAX_PATH);
if(lstrlen(szProcess) == 0)
{
AfxMessageBox(_T("输入不完整!"));
return;
}
PROCESS_INFORMATION pi;
if(CreateNormalProcess(szProcess, FALSE, &pi) == FALSE)
{
AfxMessageBox(_T("创建进程失败!"));
return;
}
DWORD dwProcessId = pi.dwProcessId;
DWORD dwWritten = 0;
BOOL bOK = DeviceIoControl(
this->m_hDevice,
IOCTL_SAFERUN_SETID, &dwProcessId, sizeof(DWORD), NULL, NULL, &dwWritten, NULL);
if(bOK)
{
AfxMessageBox(_T("设置成功!"));
}
else
{
AfxMessageBox(_T("设置失败!"));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -