DebugProgramDlg.cpp
上传用户:yll456
上传日期:2022-03-08
资源大小:19064k
文件大小:15k
- // DebugProgramDlg.cpp : 实现文件
- //
- #include "stdafx.h"
- #include "DebugProgram.h"
- #include "DebugProgramDlg.h"
- #include "tchar.h"
- #include "PSAPI.h"
- #include "pe.h"
- #pragma comment(lib,"psapi.lib");
- CDebugProgramDlg *tdlg;
- #define MAX_MODULE 256
- typedef struct _ModuleInfo
- {
- DWORD dwFileSize;
- TCHAR strFilePath[MAX_PATH];
- TCHAR strFileTime[32];
- } MODULE_INFO;
- TCHAR * PrintProcessNameAndID( DWORD processID )
- {
- TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
- // Get a handle to the process.
- HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
- PROCESS_VM_READ,
- FALSE, processID );
- // Get the process name.
- if (NULL != hProcess )
- {
- HMODULE hMod;
- DWORD cbNeeded;
- if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
- &cbNeeded) )
- {
- GetModuleBaseName( hProcess, hMod, szProcessName,
- sizeof(szProcessName)/sizeof(TCHAR) );
- }
- }
- // Print the process name and identifier.
- // _tprintf( TEXT("%s (PID: %u)n"), szProcessName, processID );
- CloseHandle( hProcess );
- return szProcessName;
- }
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // 对话框数据
- enum { IDD = IDD_ABOUTBOX };
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
- // 实现
- 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()
- // CDebugProgramDlg 对话框
- CDebugProgramDlg::CDebugProgramDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CDebugProgramDlg::IDD, pParent)
- , m_ifChecked(FALSE)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CDebugProgramDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Control(pDX, IDC_COMBO_PROCESSLIST, m_ProcessList);
- DDX_Control(pDX, IDC_OPENFILENAME, m_OpenFileName);
- DDX_Control(pDX, IDC_LIST_BREAKPOINT, m_BreakPointList);
- DDX_Check(pDX, IDC_IFATTACHPROCESS, m_ifChecked);
- DDX_Control(pDX, IDC_IFATTACHPROCESS, m_ctlCheckAttach);
- }
- BEGIN_MESSAGE_MAP(CDebugProgramDlg, CDialog)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- //ON_BN_CLICKED(IDC_BUTTON1, &CDebugProgramDlg::OnBnClickedButton1)
- // ON_BN_CLICKED(IDC_BUTTON2, &CDebugProgramDlg::OnBnClickedButton2)
- ON_BN_CLICKED(IDC_BUTTON3, &CDebugProgramDlg::OnBnClickedButton3)
- ON_BN_CLICKED(IDC_SELECTBUTTON, &CDebugProgramDlg::OnBnClickedSelectbutton)
- ON_BN_CLICKED(IDC_REFRESHBUTTON, &CDebugProgramDlg::OnBnClickedRefreshbutton)
- ON_BN_CLICKED(IDC_IFATTACHPROCESS, &CDebugProgramDlg::OnBnClickedIfattachprocess)
- ON_BN_CLICKED(IDC_STARTBUTTON, &CDebugProgramDlg::OnBnClickedStartbutton)
- END_MESSAGE_MAP()
- // CDebugProgramDlg 消息处理程序
- BOOL CDebugProgramDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // 将“关于...”菜单项添加到系统菜单中。
- // IDM_ABOUTBOX 必须在系统命令范围内。
- 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);
- }
- }
- // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
- // 执行此操作
- SetIcon(m_hIcon, TRUE); // 设置大图标
- SetIcon(m_hIcon, FALSE); // 设置小图标
- DWORD aProcesses[1024], cbNeeded, cProcesses;
- TCHAR *ProcessName=new TCHAR[MAX_PATH];
- unsigned int i;
- unsigned int j=0;
- if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
- return -1;
- // Calculate how many process identifiers were returned.
- cProcesses = cbNeeded / sizeof(DWORD);
- // Print the name and process identifier for each process.
- for ( i = 0; i < cProcesses; i++ )
- {
- if( aProcesses[i] != 0 )
- {
- _stprintf(ProcessName,"%-32s",PrintProcessNameAndID( aProcesses[i] ) );
- if( _tcsstr(ProcessName,_T("<unknown>") ) == NULL )
- {
- m_ProcessList.AddString(ProcessName);
- }
- }
- }
- m_ProcessList.SetCurSel(0);
- // TODO: 在此添加额外的初始化代码
- ObjectExe="";
- /////////////////////////////////////////
- m_ifChecked=FALSE;
- CWnd *pWnd1=GetDlgItem(IDC_COMBO_PROCESSLIST);
- pWnd1->ShowWindow(SW_HIDE);
- CWnd *pWnd2=GetDlgItem(IDC_REFRESHBUTTON);
- pWnd2->ShowWindow(SW_HIDE);
-
- CWnd *pWnd3=GetDlgItem(IDC_OPENFILENAME);
- pWnd3->ShowWindow(SW_SHOW);
- CWnd *pWnd4=GetDlgItem(IDC_SELECTBUTTON);
- pWnd4->ShowWindow(SW_SHOW);
- m_ctlCheckAttach.SetCheck(0);
- /////////////////////////////////////////
- return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
- }
- void CDebugProgramDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // 如果向对话框添加最小化按钮,则需要下面的代码
- // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
- // 这将由框架自动完成。
- void CDebugProgramDlg::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
- {
- CDialog::OnPaint();
- }
- }
- //当用户拖动最小化窗口时系统调用此函数取得光标
- //显示。
- HCURSOR CDebugProgramDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- void CDebugProgramDlg::OnBnClickedButton1()//刷新进程
- {
- // TODO: 在此添加控件通知处理程序代码
- m_ProcessList.ResetContent();
- DWORD aProcesses[1024], cbNeeded, cProcesses;
- TCHAR *ProcessName=new TCHAR[MAX_PATH];
- unsigned int i;
- unsigned int j=0;
- if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
- return ;
- // Calculate how many process identifiers were returned.
- cProcesses = cbNeeded / sizeof(DWORD);
- // Print the name and process identifier for each process.
- for ( i = 0; i < cProcesses; i++ )
- {
- if( aProcesses[i] != 0 )
- {
- _stprintf(ProcessName,"%-32s",PrintProcessNameAndID( aProcesses[i] ) );
- if( _tcsstr(ProcessName,_T("<unknown>") ) == NULL )
- {
- m_ProcessList.AddString(ProcessName);
- }
- }
- }
- m_ProcessList.SetCurSel(0);
- }
- void CDebugProgramDlg::OnBnClickedButton2()//选择执行文件
- {
- // TODO: 在此添加控件通知处理程序代码
- char buffer[120480];
-
- CFileDialog OpenFile(TRUE,NULL,NULL,/*OFN_ALLOWMULTISELECT | */OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT);
-
- buffer[0]=0;
- OpenFile.m_ofn.lpstrFile=buffer;
- OpenFile.m_ofn.nMaxFile=120480;
- OpenFile.m_ofn.lpstrFilter="可执行文件(*.exe)";/*可执行文件(*.exe)|*/
- OpenFile.m_ofn.lpstrDefExt="exe";
-
- if( OpenFile.DoModal() == IDCANCEL) return;
- ObjectExe=OpenFile.GetPathName();
- //MessageBox(ObjectExe,0,0);
- }
- void CDebugProgramDlg::OnBnClickedButton3()//添加自定义断点
- {
- // TODO: 在此添加控件通知处理程序代码
- }
- void CDebugProgramDlg::OnBnClickedSelectbutton()
- {
- // TODO: 在此添加控件通知处理程序代码
- char buffer[120480];
-
- CFileDialog OpenFile(TRUE,NULL,NULL,/*OFN_ALLOWMULTISELECT | */OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT);
-
- buffer[0]=0;
- OpenFile.m_ofn.lpstrFile=buffer;
- OpenFile.m_ofn.nMaxFile=120480;
- OpenFile.m_ofn.lpstrFilter="可执行文件(*.exe)";/*可执行文件(*.exe)|*/
- OpenFile.m_ofn.lpstrDefExt="exe";
-
- if( OpenFile.DoModal() == IDCANCEL) return;
- ObjectExe=OpenFile.GetPathName();
- m_OpenFileName.SetWindowTextA(ObjectExe);
- }
- void CDebugProgramDlg::OnBnClickedRefreshbutton()
- {
- // TODO: 在此添加控件通知处理程序代码
- m_ProcessList.ResetContent();
- DWORD aProcesses[1024], cbNeeded, cProcesses;
- TCHAR *ProcessName=new TCHAR[MAX_PATH];
- unsigned int i;
- unsigned int j=0;
- if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
- return ;
- // Calculate how many process identifiers were returned.
- cProcesses = cbNeeded / sizeof(DWORD);
- // Print the name and process identifier for each process.
- for ( i = 0; i < cProcesses; i++ )
- {
- if( aProcesses[i] != 0 )
- {
- _stprintf(ProcessName,"%-32s",PrintProcessNameAndID( aProcesses[i] ) );
- if( _tcsstr(ProcessName,_T("<unknown>") ) == NULL )
- {
- m_ProcessList.AddString(ProcessName);
- }
- }
- }
- m_ProcessList.SetCurSel(0);
- }
- void CDebugProgramDlg::OnBnClickedIfattachprocess()
- {
- // TODO: 在此添加控件通知处理程序代码
- //m_ifChecked ^= TRUE;
- int CheckStatus=m_ctlCheckAttach.GetCheck();
- if(CheckStatus==1)
- {
- CWnd *pWnd1=GetDlgItem(IDC_COMBO_PROCESSLIST);
- pWnd1->ShowWindow(SW_SHOW);
- CWnd *pWnd2=GetDlgItem(IDC_REFRESHBUTTON);
- pWnd2->ShowWindow(SW_SHOW);
- CWnd *pWnd3=GetDlgItem(IDC_OPENFILENAME);
- pWnd3->ShowWindow(SW_HIDE);
- CWnd *pWnd4=GetDlgItem(IDC_SELECTBUTTON);
- pWnd4->ShowWindow(SW_HIDE);
- m_ifChecked=FALSE;
- }
- if(CheckStatus==0)
- {
- CWnd *pWnd1=GetDlgItem(IDC_COMBO_PROCESSLIST);
- pWnd1->ShowWindow(SW_HIDE);
- CWnd *pWnd2=GetDlgItem(IDC_REFRESHBUTTON);
- pWnd2->ShowWindow(SW_HIDE);
- CWnd *pWnd3=GetDlgItem(IDC_OPENFILENAME);
-
- pWnd3->ShowWindow(SW_SHOW);
- CWnd *pWnd4=GetDlgItem(IDC_SELECTBUTTON);
- pWnd4->ShowWindow(SW_SHOW);
- m_ifChecked=TRUE;
- }
- }
- UINT ThreadRun(LPVOID PARAM)
- {
-
- unsigned char Index=0;
- char strtemp[256];
- //char ObjectExe[256];
- CString ObjectExe;
- tdlg->m_OpenFileName.GetWindowTextA(ObjectExe);
- if(ObjectExe.IsEmpty())
- {
- tdlg->m_BreakPointList.InsertString(Index++,"请选择目标程序!!!");
- return 0;
- }
- sprintf(strtemp,"已经选择文件%s",ObjectExe);
- tdlg->m_BreakPointList.InsertString(Index++,strtemp);
- MAP_FILE_STRUCT stMapFile;
- char FileName[256];
- sprintf(FileName,"%s",ObjectExe);
- LoadFileR(FileName,&stMapFile);
- PIMAGE_OPTIONAL_HEADER pOH=GetOptionalHeader(stMapFile.ImageBase);
- if(!pOH)
- {
- tdlg->m_BreakPointList.InsertString(Index++,"Can't get Optional Header !");
- return 0;
- }
- tdlg->m_BreakPointList.InsertString(Index++,"成功获取执行文件Optional Header!!!");
- DWORD dwBreakAddr = pOH->ImageBase+pOH->AddressOfEntryPoint;
- sprintf(strtemp,"执行文件的入口点地址为%08x",dwBreakAddr);
- tdlg->m_BreakPointList.InsertString(Index++,strtemp);
- sprintf(strtemp,"设置的断点地址为%08x",dwBreakAddr);
- tdlg->m_BreakPointList.InsertString(Index++,strtemp);
- STARTUPINFO sif ;
- PROCESS_INFORMATION pi ;
- ::ZeroMemory(&sif, sizeof(STARTUPINFO)) ;
- ::ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)) ;
- sif.cb = sizeof(STARTUPINFO) ;
- bool hRes ;
- bool STOP ;
- hRes = ::CreateProcess (_T(FileName), NULL, NULL, NULL, NULL, DEBUG_PROCESS|DEBUG_ONLY_THIS_PROCESS,
- NULL, NULL, &sif, &pi) ;
- if (hRes != TRUE)
- {
- ::MessageBox(NULL, _T("建立进程出错"), _T("错误"), MB_OK) ;
- ::ExitProcess(-1) ;
- }
- DEBUG_EVENT DBEvent ;
- CONTEXT Regs ;
- DWORD dwState, dwBpCnt, dwSSCnt, dwAddrProc ;
- //static const DWORD dwBreakAddr = 0x401000 ;
- unsigned int iTotalCommandNum ;
- TCHAR tBuffer[256] ;
- dwBpCnt = dwSSCnt = 0 ;
- iTotalCommandNum = 0 ;
- STOP = false ;
- Regs.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS ;
- do
- {
- ::WaitForDebugEvent (&DBEvent, INFINITE) ;
- dwState = DBG_EXCEPTION_NOT_HANDLED ;
- switch (DBEvent.dwDebugEventCode)
- {
- case EXCEPTION_DEBUG_EVENT:
- {
- switch (DBEvent.u.Exception.ExceptionRecord.ExceptionCode)
- {
- case EXCEPTION_BREAKPOINT:
- {
- ++dwBpCnt ;
- if (dwBpCnt == 1)
- {
- ::GetThreadContext(pi.hThread, &Regs) ;
- Regs.Dr0 = (DWORD)(::GetProcAddress(::GetModuleHandle(_T("ntdll.dll")), _T("NtContinue")) );
- Regs.Dr7 = 0x101 ;
- ::SetThreadContext(pi.hThread, &Regs) ;
- dwState = DBG_CONTINUE ;
- }
- break ;
- }
- case EXCEPTION_SINGLE_STEP :
- {
- ++dwSSCnt ;
- if (dwSSCnt == 1)
- {
- ::GetThreadContext(pi.hThread, &Regs) ;
- Regs.Dr0 = Regs.Dr7 = 0 ;
- ::SetThreadContext(pi.hThread, &Regs) ;
- ::ReadProcessMemory(pi.hProcess, (LPCVOID)(Regs.Esp+4), &dwAddrProc, sizeof(DWORD), NULL) ;
- ::ReadProcessMemory(pi.hProcess, (LPCVOID)dwAddrProc, &Regs, sizeof(CONTEXT), NULL) ;
- Regs.Dr0 = dwBreakAddr ;
- Regs.Dr7 = 0x101 ;
- ::WriteProcessMemory(pi.hProcess, (LPVOID)dwAddrProc, &Regs, sizeof(CONTEXT), NULL) ;
- dwState = DBG_CONTINUE ;
- }
- else if (dwSSCnt == 2)
- {
- ::GetThreadContext(pi.hThread, &Regs) ;
- char buffer[2600];
- sprintf(buffer,"EAX=%08xnEBX=%08xnECX=%08xnEDX=%08xnESP=%08xnEIP=%08xn",Regs.Eax,Regs.Ebx,Regs.Ecx,Regs.Edx,Regs.Esp,Regs.Eip);
- MessageBox(NULL,buffer,"ok",MB_OK);
- Regs.Dr0 = Regs.Dr7 = 0 ;
- Regs.EFlags |= 0x100 ;//此处不屏蔽则会进入下一次单步运算
- ::SetThreadContext(pi.hThread, &Regs) ;
- ++iTotalCommandNum ;
- dwState = DBG_CONTINUE ;
- }
- else if (dwSSCnt == 3)
- {
- ::GetThreadContext(pi.hThread, &Regs) ;
- char buffer[2600];
- sprintf(buffer,"ESP=%08xn",Regs.Esp);
- MessageBox(NULL,buffer,"ok",MB_OK);
- unsigned char strbuf[256];
- unsigned char newstr[256]={"程序已经被成功修改,谢谢欣赏!!!"};
- for(int i=31;i<256;i++)
- newstr[i]=0;
- ::ReadProcessMemory(pi.hProcess, (LPCVOID)(Regs.Esp), &dwAddrProc, sizeof(DWORD), NULL) ;
- ::ReadProcessMemory(pi.hProcess, (LPCVOID)dwAddrProc, &strbuf, 256, NULL) ;
- ::WriteProcessMemory(pi.hProcess,(LPVOID)dwAddrProc, &newstr,256,NULL);
- Regs.Dr0 = Regs.Dr7 = 0 ;
- //Regs.EFlags |= 0x100 ;//此处不屏蔽则会进入下一次单步运算
- ::SetThreadContext(pi.hThread, &Regs) ;
- ++iTotalCommandNum ;
- dwState = DBG_CONTINUE ;
- }//*/
-
- break ;
- }
- }
- break ;
- }
- case EXIT_PROCESS_DEBUG_EVENT :
- {
- iTotalCommandNum ;
- STOP = TRUE ;
- ::sprintf(tBuffer, _T("程序总指令数: %08lX"), iTotalCommandNum) ;
- ::MessageBox(NULL, tBuffer, _T("结束"), MB_OK) ;
- //::ExitProcess(-1) ;
- break ;
- }
- }
- if (!STOP)
- {
- ::ContinueDebugEvent(pi.dwProcessId, pi.dwThreadId, dwState) ;
- }
- } while (!STOP) ;
- ::CloseHandle(pi.hProcess) ;
- ::CloseHandle(pi.hThread) ;
- //::ExitProcess(0) ;//*/
- return 0;
- }
- void CDebugProgramDlg::OnBnClickedStartbutton()
- {
- // TODO: 在此添加控件通知处理程序代码
- void *psa=NULL;
- unsigned cbStackSize=0;
- //_ptiddata ptd;
- unsigned dwCreateFlags=0;
- unsigned *pdwThreadID=new unsigned[5];
- tdlg=this;
- //HANDLE hThread=CreateThread((LPSECURITY_ATTRIBUTES)psa,cbStackSize,(LPTHREAD_START_ROUTINE)ThreadRun,NULL,dwCreateFlags,(LPDWORD)pdwThreadID);
- HANDLE hThread=AfxBeginThread(ThreadRun,NULL);
- WaitForSingleObject(hThread,INFINITE);
- }