winston 发表于 2012-3-8 14:55:46

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

很多时候,我们制作自动安装程序,安装完成以后自动删除,软件仅仅安装一次就自动删除,以保护软件知识产权。
软件自动删除的代码是如何实现的呢,我们来看下

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSelfDelDlg dialog

CSelfDelDlg::CSelfDelDlg(CWnd* pParent /*=NULL*/)
        : CDialog(CSelfDelDlg::IDD, pParent)
{
        //{{AFX_DATA_INIT(CSelfDelDlg)
                // 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 CSelfDelDlg::DoDataExchange(CDataExchange* pDX)
{
        CDialog::DoDataExchange(pDX);
        //{{AFX_DATA_MAP(CSelfDelDlg)
                // NOTE: the ClassWizard will add DDX and DDV calls here
        //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSelfDelDlg, CDialog)
        //{{AFX_MSG_MAP(CSelfDelDlg)
        ON_WM_PAINT()
        ON_WM_QUERYDRAGICON()
        ON_BN_CLICKED(IDC_Delete_1, OnDelete1)
        ON_BN_CLICKED(IDC_Delete_2, OnDelete2)
        ON_BN_CLICKED(IDC_Any, OnAny)
        //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSelfDelDlg message handlers

BOOL CSelfDelDlg::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
       
        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 CSelfDelDlg::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 CSelfDelDlg::OnQueryDragIcon()
{
        return (HCURSOR) m_hIcon;
}

void CSelfDelDlg::OnDelete1()
{
/*char name;
char *ptr=::GetCommandLine();       
strncpy(name,ptr,255);
for(int i=1;i<256;i++)if(name=='\"')break;
char path;
for(int j=0;j<i;
*/
HMODULE module = GetModuleHandle(0);
CHAR buf;
GetModuleFileName(module, buf, sizeof(buf));
if(DeleteFile(buf))AfxMessageBox("自我删除成功!");
else MessageBox("自我删除失败!","直接用 DeleteFile",MB_OK|MB_ICONEXCLAMATION);
}

void CSelfDelDlg::OnDelete2()
{
HMODULE module = GetModuleHandle(0);
CHAR buf;
GetModuleFileName(module, buf, sizeof buf);
CloseHandle(HANDLE(4));
__asm {
   lea eax, buf
   push 0
   push 0
push eax
push ExitProcess
push module
push DeleteFile
push UnmapViewOfFile
ret
}
}

void CSelfDelDlg::OnAny()
{
OSVERSIONINFO ver;
ver.dwOSVersionInfoSize=sizeof(ver);
GetVersionEx(&ver);       
//if(ver.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS){//Is win95 or 98
AfxMessageBox("98");
HMODULE h = GetModuleHandle(0);
CHAR buf;
GetModuleFileName(h, buf, sizeof buf);
FILE *fp;
if(!(fp=fopen("c:\\anyexe.bat","wb")))return;
CString in;
in="del ";
in+=buf;

in+="\r\n";
in+="del ";
in+="c:\\anyexe.bat";
fwrite(in.GetBuffer(0),in.GetLength(),1,fp);
fclose(fp);
AfxMessageBox(in);
::ShellExecute(0,NULL,"command.exe","c:\\anyexe.bat",NULL,0);
/*
}
else if(ver.dwPlatformId==VER_PLATFORM_WIN32_NT)//Is Nt or 2000
{

//AfxMessageBox("2000");
HMODULE module = GetModuleHandle(0);
CHAR buf;
GetModuleFileName(module, buf, sizeof buf);
CloseHandle(HANDLE(4));
__asm {
   lea eax, buf
   push 0
   push 0
push eax
push ExitProcess
push module
push DeleteFile
push UnmapViewOfFile
ret
}
*/
//}
//else{

//}

}


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

页: [1]
查看完整版本: [原]VC++信息安全编程(8)分析程序实现自我删除