找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 3535|回复: 0

[原]VC++信息安全编程(7)分析实现程序自我复制

[复制链接]
发表于 2012-3-8 14:53:48 | 显示全部楼层 |阅读模式
程序自我复制,是软件程序备份的一种功能,防止程序被修改,被调试,被破解。
详细代码分析如下
  1. #ifdef _DEBUG
  2. #define new DEBUG_NEW
  3. #undef THIS_FILE
  4. static char THIS_FILE[] = __FILE__;
  5. #endif
  6. /////////////////////////////////////////////////////////////////////////////
  7. // CCopyMeDlg dialog
  8. CCopyMeDlg::CCopyMeDlg(CWnd* pParent /*=NULL*/)
  9.         : CDialog(CCopyMeDlg::IDD, pParent)
  10. {
  11.         //{{AFX_DATA_INIT(CCopyMeDlg)
  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 CCopyMeDlg::DoDataExchange(CDataExchange* pDX)
  18. {
  19.         CDialog::DoDataExchange(pDX);
  20.         //{{AFX_DATA_MAP(CCopyMeDlg)
  21.                 // NOTE: the ClassWizard will add DDX and DDV calls here
  22.         //}}AFX_DATA_MAP
  23. }
  24. BEGIN_MESSAGE_MAP(CCopyMeDlg, CDialog)
  25.         //{{AFX_MSG_MAP(CCopyMeDlg)
  26.         ON_WM_PAINT()
  27.         ON_WM_QUERYDRAGICON()
  28.         ON_BN_CLICKED(IDC_CopyMe, OnCopyMe)
  29.         ON_WM_TIMER()
  30.         //}}AFX_MSG_MAP
  31. END_MESSAGE_MAP()
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CCopyMeDlg message handlers
  34. BOOL CCopyMeDlg::OnInitDialog()
  35. {
  36.         CDialog::OnInitDialog();
  37.         // Set the icon for this dialog.  The framework does this automatically
  38.         //  when the application's main window is not a dialog
  39.         SetIcon(m_hIcon, TRUE);                        // Set big icon
  40.         SetIcon(m_hIcon, FALSE);                // Set small icon
  41.        
  42.         // TODO: Add extra initialization here
  43. char *fPtr=::GetCommandLine();
  44. strncpy(name,fPtr,255);
  45. for(int i=0;i<256;i++){
  46.         if(i>0&&name[i]=='"'){ name[i-1]=0; break; }
  47.      name[i]=fPtr[i+1];//去掉 fPtr中引号
  48. }
  49. i=strlen(name)-1;
  50. int len=strlen(name)-1;
  51. for(i=len;i>=0;i--){
  52.         if(name[i]=='\\')break;
  53. }
  54. memset(SelfName,0,256);
  55. strcpy(SelfName,name+i+1);
  56. name[i+1]=0;
  57.         return TRUE;  // return TRUE  unless you set the focus to a control
  58. }
  59. // If you add a minimize button to your dialog, you will need the code below
  60. //  to draw the icon.  For MFC applications using the document/view model,
  61. //  this is automatically done for you by the framework.
  62. void CCopyMeDlg::OnPaint()
  63. {
  64.         if (IsIconic())
  65.         {
  66.                 CPaintDC dc(this); // device context for painting
  67.                 SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  68.                 // Center icon in client rectangle
  69.                 int cxIcon = GetSystemMetrics(SM_CXICON);
  70.                 int cyIcon = GetSystemMetrics(SM_CYICON);
  71.                 CRect rect;
  72.                 GetClientRect(&rect);
  73.                 int x = (rect.Width() - cxIcon + 1) / 2;
  74.                 int y = (rect.Height() - cyIcon + 1) / 2;
  75.                 // Draw the icon
  76.                 dc.DrawIcon(x, y, m_hIcon);
  77.         }
  78.         else
  79.         {
  80.                 CDialog::OnPaint();
  81.         }
  82. }
  83. // The system calls this to obtain the cursor to display while the user drags
  84. //  the minimized window.
  85. HCURSOR CCopyMeDlg::OnQueryDragIcon()
  86. {
  87.         return (HCURSOR) m_hIcon;
  88. }
  89. void CCopyMeDlg::OnCopyMe()
  90. {
  91. CString FilePtr=name;
  92. //fptr 很长,必须使到空格处中断
  93. //MessageBox(fPtr,FilePtr,MB_OK);
  94. //CString FilePtr1=fPtr;
  95. //CString inf; //可试验出fptr很长
  96. //inf.Format("%s,,%d,%d",name,FilePtr.GetLength(),strlen(name));
  97. //AfxMessageBox(inf);//
  98. //return;
  99. HANDLE hFile = {NULL};
  100. DWORD dwSize, bytes_read;
  101. BYTE *ptr;
  102. CString filename=name;
  103. filename+=SelfName;
  104.         hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
  105.                 NULL, OPEN_EXISTING,
  106.                 FILE_FLAG_SEQUENTIAL_SCAN, NULL);
  107.         if(hFile == INVALID_HANDLE_VALUE)
  108.         {
  109.                 MessageBox("不能打开文件...\n\n");
  110.                 return;
  111.         }
  112.         dwSize = GetFileSize(hFile, NULL);
  113.     ptr=(BYTE*)calloc(1,dwSize);
  114.         ReadFile(hFile, ptr, dwSize, &bytes_read, NULL);
  115.         CloseHandle(hFile);
  116.         CString newFile="c:\";
  117.         newFile+=SelfName;
  118.         hFile = CreateFile(newFile, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
  119.                 NULL, CREATE_ALWAYS,
  120.                 FILE_FLAG_SEQUENTIAL_SCAN, NULL);
  121.         if(hFile == INVALID_HANDLE_VALUE)
  122.         {
  123.                 MessageBox("不能建立文件.\n\n");
  124.                 return;
  125.         }
  126.     WriteFile(hFile, ptr, dwSize, &bytes_read, NULL);
  127.         CloseHandle(hFile);
  128.     free(ptr);
  129.     AfxMessageBox("自我复制成功!");
  130. }
  131. void CCopyMeDlg::OnTimer(UINT nIDEvent)
  132. {
  133.         // TODO: Add your message handler code here and/or call default
  134.         CDialog::OnTimer(nIDEvent);
  135. }
复制代码


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

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

本版积分规则

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

GMT+8, 2024-4-29 11:33 , Processed in 0.011901 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

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