|
本帖最后由 diddom 于 2012-5-31 08:16 编辑
平常我们 Install Driver 後, Unload Driver,
假如想再 Install Driver却发现会失败, 为什麽....
因为网路上大部分的 Install Driver 工具,它其实没有 DeleteService 刚刚用 CreateService 建立的 Service
先来看点 sniper code,下面是网路上普遍 service 的 Driver Load 的 source code
- int _cdecl main(void)
- {
- HANDLE hSCManager;
- HANDLE hService;
- SERVICE_STATUS ss;
- hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
-
- printf("Load Driver\n");
- if(hSCManager)
- {
- printf("Create Service\n");
- hService = CreateService(hSCManager, "Example", "Example Driver", SERVICE_START | DELETE | SERVICE_STOP, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, "C:\\example.sys", NULL, NULL, NULL, NULL, NULL);
- if(!hService)
- {
- hService = OpenService(hSCManager, "Example", SERVICE_START | DELETE | SERVICE_STOP);
- }
- if(hService)
- {
- printf("Start Service\n");
- StartService(hService, 0, NULL);
- printf("Press Enter to close service\r\n");
- getchar();
- ControlService(hService, SERVICE_CONTROL_STOP, &ss);
- CloseServiceHandle(hService);
- DeleteService(hService);
- }
- CloseServiceHandle(hSCManager);
- }
-
- return 0;
- }
复制代码 千篇一律是这样,真是伤脑筋...><"
正常我们会先 m_schSCManager = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ALL_ACCESS)
然後 m_schService = CreateService(m_schSCManager, _
szDrvSvcName, _
szDrvDisplayName, _
SERVICE_ALL_ACCESS, _
SERVICE_KERNEL_DRIVER, _
SERVICE_DEMAND_START, _
SERVICE_ERROR_NORMAL, _
szDrvFilePath, _
0, 0, 0, 0, 0)
接下来 m_hSvcHandle = OpenService(m_schSCManager, szDrvSvcName, SERVICE_ALL_ACCESS)
有发现吗?..............
在 CreateService 和 OpenService 这两函数的返回值,我用不同的变数接住
为什麽?...因为我们等等要真正的删除刚刚用 CreateService 建立的 Service
接下来我们进入讨论一般 InstDrv 工具的 Start Driver 和 Stop Drtiver 的 code
也就是一般的 net Start ServiceName 和 net Stop ServiceName
正常 net Start ServiceName 的 code 是 Ret = StartService(m_hSvcHandle, 0, 0)
再而 net Stop ServiceName 的 code 是 StopDrv = ControlService(m_hSvcHandle, SERVICE_CONTROL_STOP, SS)
好像偏离主题了.........><"
不过我想说的是,如果我们 Start Driver 後, 没有 Stop Drtiver,
你没办法删除 CreateService 建立的 Service, 因为它还在Running状态
讲重点好了~~~~~~~~~~
在上述过程中, 我们建立了 Service, 但也建立两三个 Handle
m_schSCManager
m_schService
m_hSvcHandle
按照 MSDN ,应该是这样
Call CloseServiceHandle(m_hSvcHandle)
Call DeleteService(m_hSvcHandle)
Call CloseServiceHandle(m_schService)
Call DeleteService(m_schService)
Call CloseServiceHandle(m_schSCManager)
假如这样, 就头大了
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我们的目的是真正删除 CreateService 建立的 Service
应该是这样才对
Call CloseServiceHandle(m_hSvcHandle)
Call DeleteService(m_hSvcHandle)
Call DeleteService(m_schService)
Call CloseServiceHandle(m_schService)
Call CloseServiceHandle(m_schSCManager)
你也可以实验看看这过程颠倒後结果会是怎样
再来, 希望大家帮我的问题
Device Driver 编译连结分 free 和 checked
我玩了一个晚上, 就是没办法将 checked 版本的 drvier 把它 StopDriver
但 free版本从 Load , Start, Stop, Unload却很正常
不知道为什麽, 请知道的大侠帮我一下看问题出在哪,
付上 source
InstDrv_vb6_20120521_04.rar
(22.13 KB, 下载次数: 3)
里面的Delete SC那个Button的函数没用处,那是实验用的
Driver 的 sample file
HookNtProcess_20120518_01.rar
(22.74 KB, 下载次数: 13)
Drv example by Codeproject.rar
(23.33 KB, 下载次数: 13)
InlineHook_ObReferenceObjectByHandlele_20120427_02.rar
(39.66 KB, 下载次数: 14)
那上面3个Drv当实验品,实验一下
////////////////////////////////////////////////////////////
你假如要使用HookNtProcess_20120518_01.rar 这个 sample Drv
注意下面这函数
__declspec(naked) NTSTATUS __stdcall MyNtOpenProcess(
IN PHANDLE ProcessHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes,
IN PCLIENT_ID ClientId)
{
DbgPrint("[HookSSDT] MyNtOpenProcess() called.\n");
__asm{
push 0C4h
push 804F5308h // <--- 这个地址请自己用WinDBG看看
jmp [JmpAddress]
}
}
启动 WinDBG 後, [File] -> [Kernel Debug] -> Local 页签,确定 ,开始Debug
然後输入指令 u nt!NtOpenProcess
你会看到
lkd> u nt!NtOpenProcess
nt!NtOpenProcess:
8058093a 68c4000000 push 0C4h
8058093f 6808534f80 push offset nt!ObWatchHandles+0x25c (804f5308) <-- 括号内的地址
80580944 e85a35f6ff call nt!_SEH_prolog (804e3ea3)
80580949 33f6 xor esi,esi
8058094b 8975d4 mov dword ptr [ebp-2Ch],esi
8058094e 33c0 xor eax,eax
80580950 8d7dd8 lea edi,[ebp-28h]
80580953 ab stos dword ptr es:[edi]
__asm{
push 0C4h
push 804F5308h // <--- 这个地址请改成括号内的地址
jmp [JmpAddress]
}
|
|