找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 3475|回复: 0

[原]VC++信息安全编程(8)分析程序实现自我删除

[复制链接]
发表于 2012-3-8 14:55:46 | 显示全部楼层 |阅读模式
很多时候,我们制作自动安装程序,安装完成以后自动删除,软件仅仅安装一次就自动删除,以保护软件知识产权。
软件自动删除的代码是如何实现的呢,我们来看下
  1. #ifdef _DEBUG
  2. #define new DEBUG_NEW
  3. #undef THIS_FILE
  4. static char THIS_FILE[] = __FILE__;
  5. #endif
  6. /////////////////////////////////////////////////////////////////////////////
  7. // CSelfDelDlg dialog
  8. CSelfDelDlg::CSelfDelDlg(CWnd* pParent /*=NULL*/)
  9.         : CDialog(CSelfDelDlg::IDD, pParent)
  10. {
  11.         //{{AFX_DATA_INIT(CSelfDelDlg)
  12.                 // NOTE: the ClassWizard will add member initialization here
  13.         //}}AFX_DATA_INIT
  14.         // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  15.         m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  16. }
  17. void CSelfDelDlg::DoDataExchange(CDataExchange* pDX)
  18. {
  19.         CDialog::DoDataExchange(pDX);
  20.         //{{AFX_DATA_MAP(CSelfDelDlg)
  21.                 // NOTE: the ClassWizard will add DDX and DDV calls here
  22.         //}}AFX_DATA_MAP
  23. }
  24. BEGIN_MESSAGE_MAP(CSelfDelDlg, CDialog)
  25.         //{{AFX_MSG_MAP(CSelfDelDlg)
  26.         ON_WM_PAINT()
  27.         ON_WM_QUERYDRAGICON()
  28.         ON_BN_CLICKED(IDC_Delete_1, OnDelete1)
  29.         ON_BN_CLICKED(IDC_Delete_2, OnDelete2)
  30.         ON_BN_CLICKED(IDC_Any, OnAny)
  31.         //}}AFX_MSG_MAP
  32. END_MESSAGE_MAP()
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CSelfDelDlg message handlers
  35. BOOL CSelfDelDlg::OnInitDialog()
  36. {
  37.         CDialog::OnInitDialog();
  38.         // Set the icon for this dialog.  The framework does this automatically
  39.         //  when the application's main window is not a dialog
  40.         SetIcon(m_hIcon, TRUE);                        // Set big icon
  41.         SetIcon(m_hIcon, FALSE);                // Set small icon
  42.        
  43.         // TODO: Add extra initialization here
  44.        
  45.         return TRUE;  // return TRUE  unless you set the focus to a control
  46. }
  47. // If you add a minimize button to your dialog, you will need the code below
  48. //  to draw the icon.  For MFC applications using the document/view model,
  49. //  this is automatically done for you by the framework.
  50. void CSelfDelDlg::OnPaint()
  51. {
  52.         if (IsIconic())
  53.         {
  54.                 CPaintDC dc(this); // device context for painting
  55.                 SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  56.                 // Center icon in client rectangle
  57.                 int cxIcon = GetSystemMetrics(SM_CXICON);
  58.                 int cyIcon = GetSystemMetrics(SM_CYICON);
  59.                 CRect rect;
  60.                 GetClientRect(&rect);
  61.                 int x = (rect.Width() - cxIcon + 1) / 2;
  62.                 int y = (rect.Height() - cyIcon + 1) / 2;
  63.                 // Draw the icon
  64.                 dc.DrawIcon(x, y, m_hIcon);
  65.         }
  66.         else
  67.         {
  68.                 CDialog::OnPaint();
  69.         }
  70. }
  71. // The system calls this to obtain the cursor to display while the user drags
  72. //  the minimized window.
  73. HCURSOR CSelfDelDlg::OnQueryDragIcon()
  74. {
  75.         return (HCURSOR) m_hIcon;
  76. }
  77. void CSelfDelDlg::OnDelete1()
  78. {
  79. /*char name[256];
  80. char *ptr=::GetCommandLine();       
  81. strncpy(name,ptr,255);
  82. for(int i=1;i<256;i++)if(name[i]=='"')break;
  83. char path[256];
  84. for(int j=0;j<i;
  85. */
  86. HMODULE module = GetModuleHandle(0);
  87. CHAR buf[MAX_PATH];
  88. GetModuleFileName(module, buf, sizeof(buf));
  89. if(DeleteFile(buf))AfxMessageBox("自我删除成功!");
  90. else MessageBox("自我删除失败!","直接用 DeleteFile",MB_OK|MB_ICONEXCLAMATION);
  91. }
  92. void CSelfDelDlg::OnDelete2()
  93. {
  94.   HMODULE module = GetModuleHandle(0);
  95.   CHAR buf[MAX_PATH];
  96.   GetModuleFileName(module, buf, sizeof buf);
  97.   CloseHandle(HANDLE(4));
  98. __asm {
  99.    lea eax, buf
  100.    push 0
  101.    push 0
  102.   push eax
  103.   push ExitProcess
  104.   push module
  105.   push DeleteFile
  106.   push UnmapViewOfFile
  107.   ret
  108. }
  109. }
  110. void CSelfDelDlg::OnAny()
  111. {
  112. OSVERSIONINFO ver;
  113. ver.dwOSVersionInfoSize=sizeof(ver);
  114. GetVersionEx(&ver);       
  115. //if(ver.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS){//Is win95 or 98
  116.   AfxMessageBox("98");
  117.   HMODULE h = GetModuleHandle(0);
  118.   CHAR buf[MAX_PATH];
  119.   GetModuleFileName(h, buf, sizeof buf);
  120.   FILE *fp;
  121.   if(!(fp=fopen("c:\\anyexe.bat","wb")))return;
  122.   CString in;
  123.   in="del ";
  124.   in+=buf;
  125.   in+="\r\n";
  126.   in+="del ";
  127.   in+="c:\\anyexe.bat";
  128.   fwrite(in.GetBuffer(0),in.GetLength(),1,fp);
  129.   fclose(fp);
  130.   AfxMessageBox(in);
  131.   ::ShellExecute(0,NULL,"command.exe","c:\\anyexe.bat",NULL,0);
  132. /*
  133. }
  134. else if(ver.dwPlatformId==VER_PLATFORM_WIN32_NT)//Is Nt or 2000
  135. {
  136. //AfxMessageBox("2000");
  137.   HMODULE module = GetModuleHandle(0);
  138.   CHAR buf[MAX_PATH];
  139.   GetModuleFileName(module, buf, sizeof buf);
  140.   CloseHandle(HANDLE(4));
  141. __asm {
  142.    lea eax, buf
  143.    push 0
  144.    push 0
  145.   push eax
  146.   push ExitProcess
  147.   push module
  148.   push DeleteFile
  149.   push UnmapViewOfFile
  150.   ret
  151. }
  152. */
  153. //}
  154. //else{
  155. //}
  156. }
复制代码


作者:yincheng01 发表于2011-12-14 23:42:51 原文链接

您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-12-22 18:24 , Processed in 0.023575 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表