VC++資訊保安程式設計(1)分析實現程式自我複製
阿新 • • 發佈:2019-02-20
程式自我複製,是軟體程式備份的一種功能,防止程式被修改,被除錯,被破解。
詳細程式碼分析如下
#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CCopyMeDlg dialog CCopyMeDlg::CCopyMeDlg(CWnd* pParent /*=NULL*/) : CDialog(CCopyMeDlg::IDD, pParent) { //{{AFX_DATA_INIT(CCopyMeDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CCopyMeDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CCopyMeDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CCopyMeDlg, CDialog) //{{AFX_MSG_MAP(CCopyMeDlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_CopyMe, OnCopyMe) ON_WM_TIMER() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCopyMeDlg message handlers BOOL CCopyMeDlg::OnInitDialog() { CDialog::OnInitDialog(); // 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 // TODO: Add extra initialization here char *fPtr=::GetCommandLine(); strncpy(name,fPtr,255); for(int i=0;i<256;i++){ if(i>0&&name[i]=='\"'){ name[i-1]=0; break; } name[i]=fPtr[i+1];//去掉 fPtr中引號 } i=strlen(name)-1; int len=strlen(name)-1; for(i=len;i>=0;i--){ if(name[i]=='\\')break; } memset(SelfName,0,256); strcpy(SelfName,name+i+1); name[i+1]=0; return TRUE; // return TRUE unless you set the focus to a control } // 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 CCopyMeDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (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 to obtain the cursor to display while the user drags // the minimized window. HCURSOR CCopyMeDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CCopyMeDlg::OnCopyMe() { CString FilePtr=name; //fptr 很長,必須使到空格處中斷 //MessageBox(fPtr,FilePtr,MB_OK); //CString FilePtr1=fPtr; //CString inf; //可試驗出fptr很長 //inf.Format("%s,,%d,%d",name,FilePtr.GetLength(),strlen(name)); //AfxMessageBox(inf);// //return; HANDLE hFile = {NULL}; DWORD dwSize, bytes_read; BYTE *ptr; CString filename=name; filename+=SelfName; hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL); if(hFile == INVALID_HANDLE_VALUE) { MessageBox("不能開啟檔案...\n\n"); return; } dwSize = GetFileSize(hFile, NULL); ptr=(BYTE*)calloc(1,dwSize); ReadFile(hFile, ptr, dwSize, &bytes_read, NULL); CloseHandle(hFile); CString newFile="c:\\"; newFile+=SelfName; hFile = CreateFile(newFile, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, NULL); if(hFile == INVALID_HANDLE_VALUE) { MessageBox("不能建立檔案.\n\n"); return; } WriteFile(hFile, ptr, dwSize, &bytes_read, NULL); CloseHandle(hFile); free(ptr); AfxMessageBox("自我複製成功!"); } void CCopyMeDlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default CDialog::OnTimer(nIDEvent); }