来自 下载:
暴力、强制删除文件源代码
http://www.bmpj.net/forum-viewthread-tid-783-fromuid-16.html
// MzfDeleteFileDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MzfDeleteFile.h"
#include "MzfDeleteFileDlg.h"
#include "common.h"
#include <winioctl.h>
#include <winsvc.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
static int i = 0;
#define SERVICE_NAME "MzfKillFile"
#define DRIVER_NAME "MzfKillFile.sys"
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMzfDeleteFileDlg dialog
CMzfDeleteFileDlg::CMzfDeleteFileDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMzfDeleteFileDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMzfDeleteFileDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMzfDeleteFileDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMzfDeleteFileDlg)
DDX_Control(pDX, IDC_STATIC52POJIE, m_52pojie);
DDX_Control(pDX, IDC_STATICURL, m_Link);
DDX_Control(pDX, IDC_LISTFILE, m_list);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMzfDeleteFileDlg, CDialog)
//{{AFX_MSG_MAP(CMzfDeleteFileDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SURE, OnSure)
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_WM_DROPFILES()
ON_BN_CLICKED(IDC_ALL, OnAll)
ON_BN_CLICKED(IDC_NOTALL, OnNotall)
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMzfDeleteFileDlg message handlers
BOOL CMzfDeleteFileDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
if (!ReleaseRes(DRIVER_NAME, (WORD)IDR_SYS, "SYS"))
{
MessageBox("释放驱动程序失败!");
ExitProcess(0);
}
BOOL bRet = LoadNTDriver(SERVICE_NAME, DRIVER_NAME);
if (!bRet)
{
UnloadNTDriver(SERVICE_NAME);
bRet = LoadNTDriver(SERVICE_NAME, DRIVER_NAME);
if (!bRet)
{
MessageBox("加载驱动失败, 程序退出运行!");
ExitProcess(0);
return FALSE;
}
}
bRet = Startservice(SERVICE_NAME, DRIVER_NAME);
if (!bRet)
{
MessageBox("启动服务失败, 程序退出运行!");
ExitProcess(0);
return FALSE;
}
DeleteFile(DRIVER_NAME);
m_list.SetExtendedStyle(LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES );
m_list.InsertColumn(0, "文件名");
m_list.InsertColumn(1, "文件路径");
m_list.SetColumnWidth(0, 130);
m_list.SetColumnWidth(1, 380);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMzfDeleteFileDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CMzfDeleteFileDlg::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 CMzfDeleteFileDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMzfDeleteFileDlg::OnSure()
{
// TODO: Add your control notification handler code here
for (int j=0; j<m_list.GetItemCount(); j++)
{
if (m_list.GetCheck(j))
{
WCHAR szwChar[512];
DWORD dwRet;
CString str = m_list.GetItemText(j, 1);
int nChar = MultiByteToWideChar (CP_ACP, 0, str, -1, szwChar, 256);
// MessageBoxW(NULL, szwChar, L"fuck", MB_OK);
HANDLE hFile = CreateFile(WIN32_NAME, GENERIC_READ|GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
MessageBox("打开驱动失败!", "提示:");
return;
}
DeviceIoControl(hFile, IOCTL_DELETEFILE, szwChar, sizeof(szwChar), NULL, 0, &dwRet, NULL);
m_list.DeleteItem(j);
j--;
i--;
}
}
}
void CMzfDeleteFileDlg::OnAdd()
{
// TODO: Add your control notification handler code here
CString strFilter;
strFilter = "All Files(*.*)|*.*||";
CFileDialog fileDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, strFilter);
fileDlg.m_ofn.lStructSize = sizeof(OPENFILENAME);
if (IDOK == fileDlg.DoModal())
{
CString strPath, strName;;
strPath = fileDlg.GetPathName();
strName = strPath.Right(strPath.GetLength() - strPath.ReverseFind('\\') - 1);
m_list.InsertItem(i, strName);
m_list.SetItemText(i++, 1, strPath);
}
}
void CMzfDeleteFileDlg::OnDelete()
{
// TODO: Add your control notification handler code here
for ( int j=0; j<m_list.GetItemCount(); j++)
{
if (m_list.GetCheck(j))
{
m_list.DeleteItem(j);
j--;
i--;
}
}
}
void CMzfDeleteFileDlg::OnDropFiles(HDROP hDropInfo)
{
CHAR filePath[ MAX_PATH ]={0};
DragQueryFile(hDropInfo, 0, filePath, MAX_PATH-1);
if (GetFileAttributes(filePath) == FILE_ATTRIBUTE_DIRECTORY)
{
MessageBox("暂不支持对文件夹的删除!", "提示:");
return;
}
CString strPath, strName;;
strPath = filePath;
strName = strPath.Right(strPath.GetLength() - strPath.ReverseFind('\\') - 1);
m_list.InsertItem(i, strName);
m_list.SetItemText(i++, 1, strPath);
DragFinish(hDropInfo);
}
void CMzfDeleteFileDlg::OnAll()
{
// TODO: Add your control notification handler code here
for ( int j=0; j<m_list.GetItemCount(); j++)
{
m_list.SetCheck(j);
}
}
void CMzfDeleteFileDlg::OnNotall()
{
// TODO: Add your control notification handler code here
for ( int j=0; j<m_list.GetItemCount(); j++)
{
m_list.SetCheck(j, FALSE);
}
}
void CMzfDeleteFileDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CRect rcLink, rc52pojie;
m_Link.GetClientRect( &rcLink );
m_Link.ClientToScreen( &rcLink );
ScreenToClient( &rcLink );
m_52pojie.GetClientRect( &rc52pojie );
m_52pojie.ClientToScreen( &rc52pojie );
ScreenToClient( &rc52pojie );
if( point.x > rcLink.left && point.x < rcLink.right && point.y > rcLink.top && point.y < rcLink.bottom )
{
HCURSOR hCursor;
hCursor = ::LoadCursor(NULL, IDC_HAND );
SetCursor( hCursor );
}
else if (point.x > rc52pojie.left && point.x < rc52pojie.right && point.y > rc52pojie.top && point.y < rc52pojie.bottom)
{
HCURSOR hCursor;
hCursor = ::LoadCursor(NULL, IDC_HAND );
SetCursor( hCursor );
}
CDialog::OnMouseMove(nFlags, point);
}
void CMzfDeleteFileDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CRect rcLink, rc52pojie;
m_Link.GetClientRect( &rcLink );
m_Link.ClientToScreen( &rcLink );
ScreenToClient( &rcLink );
m_52pojie.GetClientRect( &rc52pojie );
m_52pojie.ClientToScreen( &rc52pojie );
ScreenToClient( &rc52pojie );
if( point.x > rcLink.left && point.x < rcLink.right && point.y > rcLink.top && point.y < rcLink.bottom )
{
ShellExecute( NULL, NULL, _T("http://hi.baidu.com/hu3167343/"), NULL, NULL, SW_NORMAL );
}
else if (point.x > rc52pojie.left && point.x < rc52pojie.right && point.y > rc52pojie.top && point.y < rc52pojie.bottom)
{
ShellExecute( NULL, NULL, _T("http://www.52pojie.cn"), NULL, NULL, SW_NORMAL );
}
CDialog::OnLButtonDown(nFlags, point);
}
BOOL CMzfDeleteFileDlg::ReleaseRes(CString strFileName, WORD wResID, CString strFileType)
{
DWORD dwWrite;
HANDLE hFile = CreateFile(strFileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
return FALSE;
}
HRSRC hrsc = FindResource(NULL, MAKEINTRESOURCE(wResID), strFileType);
HGLOBAL hg = LoadResource(NULL, hrsc);
DWORD dwSize = SizeofResource(NULL, hrsc);
WriteFile(hFile, hg, dwSize, &dwWrite, NULL);
CloseHandle(hFile);
return TRUE;
}
BOOL CMzfDeleteFileDlg::LoadNTDriver(CHAR* lpszDriverName, CHAR* lpszDriverPath)
{
CHAR szDriverImagePath[256];
BOOL bRet = TRUE;
SC_HANDLE hServiceMgr=NULL;
SC_HANDLE hServiceDDK=NULL;
GetFullPathName(lpszDriverPath, 256, szDriverImagePath, NULL);
hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if( hServiceMgr == NULL )
{
bRet = FALSE;
goto BeforeLeave;
}
hServiceDDK = CreateService(hServiceMgr,
lpszDriverName,
lpszDriverName,
SERVICE_ALL_ACCESS,
SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START,
SERVICE_ERROR_IGNORE,
szDriverImagePath,
NULL, NULL, NULL, NULL, NULL
);
DWORD dwRtn;
if( hServiceDDK == NULL )
{
dwRtn = GetLastError();
if( dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_EXISTS )
{
bRet = FALSE;
goto BeforeLeave;
}
hServiceDDK = OpenService( hServiceMgr, lpszDriverName, SERVICE_ALL_ACCESS );
if( hServiceDDK == NULL )
{
bRet = FALSE;
goto BeforeLeave;
}
}
bRet = TRUE;
BeforeLeave:
if(hServiceDDK)
{
CloseServiceHandle(hServiceDDK);
}
if(hServiceMgr)
{
CloseServiceHandle(hServiceMgr);
}
return bRet;
}
BOOL CMzfDeleteFileDlg::Startservice(CHAR* lpszDriverName, CHAR* lpszDriverPath)
{
CHAR szDriverImagePath[256];
BOOL bRet = TRUE;
SC_HANDLE hServiceMgr=NULL;
SC_HANDLE hServiceDDK=NULL;
GetFullPathName(lpszDriverPath, 256, szDriverImagePath, NULL);
hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if( hServiceMgr == NULL )
{
bRet = FALSE;
goto BeforeLeave;
}
hServiceDDK = OpenService( hServiceMgr, lpszDriverName, SERVICE_ALL_ACCESS );
if( hServiceDDK == NULL )
{
bRet = FALSE;
goto BeforeLeave;
}
bRet= StartService( hServiceDDK, NULL, NULL );
if( !bRet )
{
DWORD dwRtn = GetLastError();
if( dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_ALREADY_RUNNING )
{
bRet = FALSE;
goto BeforeLeave;
}
else
{
if( dwRtn == ERROR_IO_PENDING )
{
MessageBox("StartService() Faild ERROR_IO_PENDING ! \n");
bRet = FALSE;
goto BeforeLeave;
}
else
{
bRet = TRUE;
goto BeforeLeave;
}
}
}
BeforeLeave:
if(hServiceDDK)
{
CloseServiceHandle(hServiceDDK);
}
if(hServiceMgr)
{
CloseServiceHandle(hServiceMgr);
}
return bRet;
}
BOOL CMzfDeleteFileDlg::UnloadNTDriver(CHAR* szSvrName)
{
bool bRet = TRUE;
SC_HANDLE hServiceMgr=NULL;
SC_HANDLE hServiceDDK=NULL;
SERVICE_STATUS SvrSta;
hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if( hServiceMgr == NULL )
{
bRet = FALSE;
goto BeforeLeave;
}
hServiceDDK = OpenService( hServiceMgr, szSvrName, SERVICE_ALL_ACCESS );
if( hServiceDDK == NULL )
{
bRet = FALSE;
goto BeforeLeave;
}
if( !ControlService( hServiceDDK, SERVICE_CONTROL_STOP , &SvrSta ) )
{
bRet = FALSE;
goto BeforeLeave;
}
if( !DeleteService( hServiceDDK ) )
{
bRet = FALSE;
goto BeforeLeave;
}
BeforeLeave:
if(hServiceDDK)
{
CloseServiceHandle(hServiceDDK);
}
if(hServiceMgr)
{
CloseServiceHandle(hServiceMgr);
}
return bRet;
}
void CMzfDeleteFileDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
UnloadNTDriver(SERVICE_NAME);
// DeleteFile(DRIVER_NAME);
CDialog::OnClose();
}