[原]VC++信息安全编程(7)分析实现程序自我复制
程序自我复制,是软件程序备份的一种功能,防止程序被修改,被调试,被破解。详细代码分析如下
#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=='\"'){ name=0; break; }
name=fPtr;//去掉 fPtr中引号
}
i=strlen(name)-1;
int len=strlen(name)-1;
for(i=len;i>=0;i--){
if(name=='\\')break;
}
memset(SelfName,0,256);
strcpy(SelfName,name+i+1);
name=0;
return TRUE;// return TRUEunless 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);
}
作者:yincheng01 发表于2011-12-14 23:40:28 原文链接
页:
[1]