-
2020-04-29 13:55:29
去年做一个项目,
保护我们云电脑中的计费程序等后台程序、脚本,不被用户结束。
综合各种方案,最终选择了双服务互相守护,服务1监管我们的计费程序不被用户结束。为什么选择用服务来守护进程
- 大多数Windows服务是以SYSTEM用户启动的;
- 拥有最高的权限,例如:无需UAC就能以管理员权限启动一个进程;
- 可以操作注册表中Local Machine、系统目录;
- 服务可以先于 用户登录 启动,用户注销时服务也不会停止;
- 服务的隐蔽性更高,服务的自启动大部分人不了解(大部分用户想要关闭程序的自启动,首先会从开始菜单、启动项、计划任务、注册表等位置进行检查,很少有人会想到服务自启动);
注册服务
程序启动,构建一个迷惑性强的路径和文件名,例如:"C:\Windows\System32\SystemConfig.exe
拷贝自身到指定路径,覆盖旧版本文件执行命令行操作 创建服务 设置自启动
(替换代码中的ServiceName为你的服更多相关内容 -
python编写的WINDOWS进程守护小工具
2021-08-03 13:22:50python编写的WINDOWS应用守护进程(定时检查进程是否存在),检查时间可设置,进程名称可更改,直接与进程放在同一目录即可 -
windows守护进程小工具
2019-08-27 14:06:15检测指定的程序是否有在运行,没有运行则,运行程序。 通过config.ini配置文件配置,需要守护进程的路径,以及程序名称 -
原版win10-进程保护驱动源码_c_保护驱动_进程保护_win10驱动保护_win10进程保护
2021-09-11 06:52:39WI10-进程保护驱动程序源代码,驱动程序级保护为您的应用程序。 -
【进程保护】让程序禁止结束进程并提示拒绝访问,可调用API
2018-05-10 18:08:13类似于360,腾讯管家等在任务管理器中无法结束提示拒绝访问 ...可以用程序调用并且传入要保护的进程名即可,保护完成后程序自动退出(但是无法取消保护,不过程序自己可以正常退出) 压缩包里有GIF教程图片 -
windows进程守护
2018-05-22 11:23:44用于程序的保护,当进程因各种原因死掉时,,此工具会自动启动该进程。 -
windows 进程看门狗批处理脚本 用于进程守护
2017-12-07 15:10:24一个监控进程的脚本程序,可以监控进程并自动启动进程。 -
C++编写的守护进程服务程序
2017-10-19 15:52:47C++编写的守护进程,守护进程以windows服务的方式运行,可通过配置文件配置需要守护的exe。 -
C# 进程守护程序
2018-08-29 15:32:50这是一个windows服务版本的线程跟服务守护程序,里面有代码跟.bat服务安装文件,vs2017开发的 -
ProcessGuard:保护Windows系统中的程序,可以不被关闭
2021-03-13 23:55:02过程卫士 -
隐藏:具有用户模式界面的Windows驱动程序,可以隐藏文件系统和注册表的对象,保护进程等
2021-02-05 23:44:12隐藏:具有用户模式界面的Windows驱动程序,可以隐藏文件系统和注册表的对象,保护进程等 -
Python 编写windows守护进程程序
2017-09-10 17:49:33为了谨防程序崩溃,特别编写一个守护进程程序,时刻监控程序是否崩溃并重新启动。 下面介绍两种实现方式。 二、第一种 监控进程PID方式 Demo.py 10秒关闭程序,模拟程序崩溃 # -*- coding: utf-8 -*- import log简介
项目中使用python写一个监控程序,每隔5秒监控目录,发现文件就立刻FTP同步到另一台服务器。业务要求高实时性和高可用性。为了谨防程序崩溃,特别编写一个守护进程程序,时刻监控程序是否崩溃并重新启动。
下面介绍两种实现方式。
第一种 监控进程PID方式
- Demo.py 10秒关闭程序,模拟程序崩溃
# -*- coding: utf-8 -*- import logging import time import os logging.basicConfig(level=logging.INFO, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='e:/monitor/demo.log', filemode='a') # Demo.py 10秒关闭程序,模拟程序崩溃 # author 胖胖的alex 2017/09/10 class Demo: pidlogpath = "e:/monitor/pid.run" def __init__(self): pass def setpid(self): # 记录进程号PID fo = open(self.pidlogpath, "w") pid = str(os.getpid()) fo.write(pid) fo.flush() fo.close() return pid def execute(self): pid = self.setpid() logging.info("启动程序,进程号:" + str(pid)) i = 1 while True: logging.info("pid = " + pid + " ---------- run " + str(i) + " s ") time.sleep(1) i += 1 if i > 10: break logging.info("程序关闭...") if __name__ == '__main__': Demo().execute()
- e:/monitor/pid.run 储存Demo.py进程号
- MonitorPid.py 监控windows系统所有进程任务
定时任务执行,如果run.pid内进程号在系统中不存在,执行重启命令
# -*- coding: utf-8 -*- import psutil import os import logging logging.basicConfig(level=logging.INFO, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='e:/monitor/monitor.log', filemode='a') # PROCESS_RE = re.compile("pid=\d{1,4},\sname='\S{1,20}'") # 采用正则,获取数据 pid=x/xx/xxx/xxxx, name=[1~20个字符, # 监控windows系统所有进程服务任务。定时任务执行,发现run.pid进程号系统中不存在,执行命令python Demo.py启动程序 # author 胖胖的alex 2017/09/1 class Monitor: pidNotHandle = [] pidlogpath = "e:/monitor/pid.run" def __init__(self): self.pidNotHandle = list(psutil.process_iter()) # 获取当前计算机的pid def getpid(self): # 获取进程号PID fo = open(self.pidlogpath, "r") result = fo.read() fo.flush() fo.close() return result def execute(self): pid = [] for each in self.pidNotHandle: a = str(each) # each 是 class类型,可用type(each)查看类型 # a 数据样式为:psutil.Process(pid=0, name='System Idle Process') pid.append(a[15:-1]) # 只取括号内部分;pid=0, name='System Idle Process' status = 0 # 被监控程序进程存在状态,0不存在,1存在 for each in pid: nameposition = each.find("name") # 获取name的位置;name='System Idle Process' namevalue = each[nameposition + 6:-1] # 获取name值;System Idle Process pidposition = each.find("pid") pidvalue = each[pidposition + 4:nameposition-2] print("name="+namevalue + ", pid="+pidvalue+"\n") if pidvalue == self.getpid(): status = 1 print("发现进程==============name=" + namevalue + ", pid=" + pidvalue + "\n") break if status == 0: # 进程不存在,重新启动程序 cmd = "python Demo.py" os.popen(cmd) print("重启程序.............") print("ending.............") return 0 if __name__ == '__main__': Monitor().execute()
- pyInstaller 将MonitorPid.py程序生成monitor.exe文件
pyInstaller百度经验:http://jingyan.baidu.com/article/a378c960b47034b3282830bb.html
pyInstaller官方网站:http://www.pyinstaller.org/
执行命令
E:\mywork\python\workspace\monitor>pyinstaller -w MonitorPid.py 85 INFO: PyInstaller: 3.2.1 85 INFO: Python: 3.5.4 85 INFO: Platform: Windows-10-10.0.15063-SP0 86 INFO: wrote E:\mywork\python\workspace\monitor\MonitorPid.spec 87 INFO: UPX is not available. 88 INFO: Extending PYTHONPATH with paths ['E:\\mywork\\python\\workspace\\monitor', 'E:\\mywork\\python\\workspace\\monitor'] 88 INFO: checking Analysis 102 INFO: checking PYZ 113 INFO: checking PKG 115 INFO: Building because toc changed 115 INFO: Building PKG (CArchive) out00-PKG.pkg 133 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully. 137 INFO: Bootloader e:\mywork\python\soft\python35\lib\site-packages\PyInstaller\bootloader\Windows-64bit\runw.exe 137 INFO: checking EXE 137 INFO: Building because name changed 137 INFO: Building EXE from out00-EXE.toc 138 INFO: Appending archive to EXE E:\mywork\python\workspace\monitor\build\MonitorPid\MonitorPid.exe 141 INFO: Building EXE from out00-EXE.toc completed successfully. 144 INFO: checking COLLECT 145 INFO: Building COLLECT out00-COLLECT.toc 388 INFO: Building COLLECT out00-COLLECT.toc completed successfully.
打包成功,杀毒软件会报警,选择找回文件或添加信任,下图是打包后程序
- 设置MonitorPid.exe到windows计划任务
windows设置计划任务百度经验教程:http://jingyan.baidu.com/article/ca00d56c767cfae99febcf73.html
贴出核心配置截图:
第二种监控进程名方式
- 同第一种方式的第一步
- pyInstaller 将Demo.py打包成DemoServer.exe
Python程序获取进程名称,最直接办法是打包成exe文件E:\mywork\python\workspace\monitor>pyinstaller -w DemoServer.py 87 INFO: PyInstaller: 3.2.1 90 INFO: Python: 3.5.4 90 INFO: Platform: Windows-10-10.0.15063-SP0 93 INFO: wrote E:\mywork\python\workspace\monitor\DemoServer.spec 99 INFO: UPX is not available. 101 INFO: Extending PYTHONPATH with paths ['E:\\mywork\\python\\workspace\\monitor', 'E:\\mywork\\python\\workspace\\monitor'] 101 INFO: checking Analysis 101 INFO: Building Analysis because out00-Analysis.toc is non existent 102 INFO: Initializing module dependency graph... 103 INFO: Initializing module graph hooks... 104 INFO: Analyzing base_library.zip ... 2195 INFO: running Analysis out00-Analysis.toc 2196 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable required by e:\mywork\python\soft\python35\python.exe 2595 INFO: Caching module hooks... 2598 INFO: Analyzing E:\mywork\python\workspace\monitor\DemoServer.py 2825 INFO: Loading module hooks... 2826 INFO: Loading module hook "hook-encodings.py"... 2912 INFO: Loading module hook "hook-xml.py"... 3057 INFO: Loading module hook "hook-pywintypes.py"... 3288 INFO: Loading module hook "hook-pydoc.py"... 3304 INFO: Looking for ctypes DLLs 3304 INFO: Analyzing run-time hooks ... 3310 INFO: Looking for dynamic libraries 3556 INFO: Looking for eggs 3556 INFO: Using Python library e:\mywork\python\soft\python35\python35.dll 3556 INFO: Found binding redirects: [] 3560 INFO: Warnings written to E:\mywork\python\workspace\monitor\build\DemoServer\warnDemoServer.txt 3568 INFO: checking PYZ 3568 INFO: Building PYZ because out00-PYZ.toc is non existent 3568 INFO: Building PYZ (ZlibArchive) E:\mywork\python\workspace\monitor\build\DemoServer\out00-PYZ.pyz 3993 INFO: Building PYZ (ZlibArchive) E:\mywork\python\workspace\monitor\build\DemoServer\out00-PYZ.pyz completed successfully. 4009 INFO: checking PKG 4011 INFO: Building PKG because out00-PKG.toc is non existent 4012 INFO: Building PKG (CArchive) out00-PKG.pkg 4025 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully. 4028 INFO: Bootloader e:\mywork\python\soft\python35\lib\site-packages\PyInstaller\bootloader\Windows-64bit\runw.exe 4028 INFO: checking EXE 4028 INFO: Building EXE because out00-EXE.toc is non existent 4028 INFO: Building EXE from out00-EXE.toc 4029 INFO: Appending archive to EXE E:\mywork\python\workspace\monitor\build\DemoServer\DemoServer.exe 4031 INFO: Building EXE from out00-EXE.toc completed successfully. 4036 INFO: checking COLLECT 4036 INFO: Building COLLECT because out00-COLLECT.toc is non existent 4039 INFO: Building COLLECT out00-COLLECT.toc 4378 INFO: Building COLLECT out00-COLLECT.toc completed successfully.
- MonitorPidServer.py 程序
监控系统所有进程名称,如果没有DemoServer.exe进程名称,执行命令DemoServer.exe
# -*- coding: utf-8 -*- import psutil import os import logging logging.basicConfig(level=logging.INFO, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='e:/monitor/monitor.log', filemode='a') # PROCESS_RE = re.compile("pid=\d{1,4},\sname='\S{1,20}'") # 采用正则,获取数据 pid=x/xx/xxx/xxxx, name=[1~20个字符, # 监控windows系统所有进程服务任务。定时任务执行,发现run.pid进程号系统中不存在,执行命令python Demo.py启动程序 # author 胖胖的alex 2017/09/1 class MonitorServer: pidNotHandle = [] servername = "DemoServer.exe" def __init__(self): self.pidNotHandle = list(psutil.process_iter()) # 获取当前计算机的pid def execute(self): pid = [] for each in self.pidNotHandle: a = str(each) # each 是 class类型,可用type(each)查看类型 # a 数据样式为:psutil.Process(pid=0, name='System Idle Process') pid.append(a[15:-1]) # 只取括号内部分;pid=0, name='System Idle Process' status = 0 # 被监控程序进程存在状态,0不存在,1存在 for each in pid: nameposition = each.find("name") # 获取name的位置;name='System Idle Process' namevalue = each[nameposition + 6:-1] # 获取name值;System Idle Process pidposition = each.find("pid") pidvalue = each[pidposition + 4:nameposition-2] print("name="+namevalue + ", pid="+pidvalue+"\n") if namevalue == self.servername: status = 1 print("发现进程==============name=" + namevalue + ", pid=" + pidvalue + "\n") break if status == 0: # 进程不存在,重新启动程序 cmd = "E:/mywork/python/workspace/monitor/dist/DemoServer/" + self.servername os.popen(cmd) print("重启程序.............") print("ending.............") return 0 if __name__ == '__main__': MonitorServer().execute()
- 同第一种方式的第四步
- 同第一种方式的第五步
参考文章
python怎么获取windows服务的PID http://blog.csdn.net/ck3207/article/details/51955937
- Demo.py 10秒关闭程序,模拟程序崩溃
-
Windows使用bat批处理实现守护进程脚本.bat
2019-06-23 15:03:15Windows使用bat批处理实现守护进程脚本 -
C#C++保护自身进程无法被任务管理器结束,WIN7 3264测试可用源码
2018-10-15 11:25:08C#C++保护自身进程无法被任务管理器结束,WIN7 3264测试可用源码 C#C++保护自身进程无法被任务管理器结束,WIN7 3264测试可用源码 C#C++保护自身进程无法被任务管理器结束,WIN7 3264测试可用源码 -
c#守护程序来实现进程保护源码
2011-08-17 17:01:36在编辑一个程序不被不小心关闭的时候,想到了一个进程防护,即做一个守护程序~ -
驱动型文件保护和进程保护
2020-02-11 08:58:53经过N多辗转和持续验证,终于搞定了在驱动程序中同时保护进程和文件(经持久测试后,避免了导致系统蓝屏的情况),重点...进程保护回调函数: NTSTATUS ProtectProcess(BOOLEAN Enable) { OB_CALLBACK_REGISTRATI...经过N多辗转和持续验证,终于搞定了在驱动程序中同时保护进程和文件(经持久测试后,避免了导致系统蓝屏的情况),重点
1)拒绝通过进程管理器关闭进程,同时又允许某些进程可以管理
2)通过比较文件名,截获被保护的文件操作完整代码,可从https://download.csdn.net/download/pony12/12149153下载
进程保护回调函数:
NTSTATUS ProtectProcess(BOOLEAN Enable) { OB_CALLBACK_REGISTRATION obReg; OB_OPERATION_REGISTRATION opReg; memset(&obReg, 0, sizeof(obReg)); obReg.Version = ObGetFilterVersion(); obReg.OperationRegistrationCount = 1; obReg.RegistrationContext = NULL; RtlInitUnicodeString(&obReg.Altitude, L"321000"); memset(&opReg, 0, sizeof(opReg)); //初始化结构体变量 //下面请注意这个结构体的成员字段的设置 opReg.ObjectType = PsProcessType; opReg.Operations = OB_OPERATION_HANDLE_CREATE|OB_OPERATION_HANDLE_DUPLICATE; opReg.PreOperation = (POB_PRE_OPERATION_CALLBACK)&processPreCall; //在这里注册一个回调函数指针 obReg.OperationRegistration = &opReg; //注意这一条语句 return ObRegisterCallbacks(&obReg, &processCallBackHandle); //在这里注册回调函数 } OB_PREOP_CALLBACK_STATUS processPreCall(PVOID RegistrationContext, POB_PRE_OPERATION_INFORMATION pOperationInformation) { if(pOperationInformation->KernelHandle == TRUE) { //如果是KernelHandle则直接返回不做操作 return(OB_PREOP_SUCCESS); } if (pOperationInformation->ObjectType != *PsProcessType) { return(OB_PREOP_SUCCESS); } HANDLE pid = PsGetProcessId((PEPROCESS)pOperationInformation->Object); char szProcName[512+1]={0}; UNREFERENCED_PARAMETER(RegistrationContext); _snprintf(szProcName, sizeof(szProcName)-1, GetProcessImageNameByProcessID((ULONG)pid)); parentsProtectedProcess = PsGetCurrentProcess(); char szSvcProcName[512+1]={0}; HANDLE svc_pid = PsGetProcessId(parentsProtectedProcess); _snprintf(szSvcProcName, sizeof(szSvcProcName)-1, GetProcessImageNameByProcessID((ULONG)svc_pid)); //如果是auditflow_svc.exe进程的操作,则放行 if( !_strnicmp(szSvcProcName,"auditflow_svc.", 14)) { DbgPrint("PID : %ld CmdExe: %s\r\n", (ULONG64)svc_pid, szSvcProcName); return(OB_PREOP_SUCCESS); } //保护auditflow_svc.exe,auditflow_svc_child.exe,auditflow_child.exe,auditflowAgent.exe等进程 //GetProcessImageNameByProcessID只能获得进程名字的前14位+\0,所以只能比较前几位 if( !_strnicmp(szProcName,"auditflow", 9)) { if (pOperationInformation->Operation == OB_OPERATION_HANDLE_CREATE) { if(pOperationInformation->Parameters->CreateHandleInformation.OriginalDesiredAccess == PROCESS_TERMINATE) { //拒绝PROCESS_TERMINATE处理 if ((pOperationInformation->Parameters->CreateHandleInformation.OriginalDesiredAccess & PROCESS_TERMINATE) == PROCESS_TERMINATE) { //Terminate the process, such as by calling the user-mode TerminateProcess routine.. pOperationInformation->Parameters->CreateHandleInformation.DesiredAccess &= ~PROCESS_TERMINATE; } /* if ((pOperationInformation->Parameters->CreateHandleInformation.OriginalDesiredAccess & PROCESS_VM_OPERATION) == PROCESS_VM_OPERATION) { //Modify the address space of the process, such as by calling the user-mode WriteProcessMemory and VirtualProtectEx routines. pOperationInformation->Parameters->CreateHandleInformation.DesiredAccess &= ~PROCESS_VM_OPERATION; } if ((pOperationInformation->Parameters->CreateHandleInformation.OriginalDesiredAccess & PROCESS_VM_READ) == PROCESS_VM_READ) { //Read to the address space of the process, such as by calling the user-mode ReadProcessMemory routine. pOperationInformation->Parameters->CreateHandleInformation.DesiredAccess &= ~PROCESS_VM_READ; } if ((pOperationInformation->Parameters->CreateHandleInformation.OriginalDesiredAccess & PROCESS_VM_WRITE) == PROCESS_VM_WRITE) { //Write to the address space of the process, such as by calling the user-mode WriteProcessMemory routine. pOperationInformation->Parameters->CreateHandleInformation.DesiredAccess &= ~PROCESS_VM_WRITE; } */ } } } return OB_PREOP_SUCCESS; }
文件保护函数:
OB_PREOP_CALLBACK_STATUS FilePreCallBack(PVOID RegistrationContext, POB_PRE_OPERATION_INFORMATION OperationInformation) { UNICODE_STRING uniDosName; uniDosName.Length = 0; ACCESS_MASK oldCreateDesiredAccess = 0; ACCESS_MASK oldDuplicateDesiredAccess = 0; //参数检查 if(NULL == OperationInformation) return OB_PREOP_SUCCESS; PFILE_OBJECT FileObject = (PFILE_OBJECT)OperationInformation->Object; HANDLE CurrentProcessId = PsGetCurrentProcessId(); UNREFERENCED_PARAMETER(RegistrationContext); //有效性检查 if(NULL == FileObject) return OB_PREOP_SUCCESS; //(1)屏蔽非IoFileObjectType类型的处理 if( OperationInformation->ObjectType!=*IoFileObjectType) { return OB_PREOP_SUCCESS; } //(2)过滤无效指针 if( FileObject->FileName.Buffer==NULL || !MmIsAddressValid(FileObject->FileName.Buffer) || FileObject->DeviceObject==NULL || !MmIsAddressValid(FileObject->DeviceObject) ) { return OB_PREOP_SUCCESS; } //(3)过滤无效路径, 否则使用RtlVolumeDeviceToDosName获取盘符会蓝屏 /**/ if( !_wcsicmp(FileObject->FileName.Buffer,L"\\Endpoint") || !_wcsicmp(FileObject->FileName.Buffer,L"?") || !_wcsicmp(FileObject->FileName.Buffer,L"\\.\\.") || !_wcsicmp(FileObject->FileName.Buffer,L"\\")) return OB_PREOP_SUCCESS; //被保护的4个文件 UNICODE_STRING UnicodeString1; RtlInitUnicodeString(&UnicodeString1, L"audit_auth.conf"); UNICODE_STRING UnicodeString2; RtlInitUnicodeString(&UnicodeString2, L"ignsql.cfg"); UNICODE_STRING UnicodeString3; RtlInitUnicodeString(&UnicodeString3, L"auditflow.ini"); UNICODE_STRING UnicodeString4; RtlInitUnicodeString(&UnicodeString4, L"DBLocalInfo.ini"); //获取操作文件char*名称 char cbFileName[1024] = {0}; UnicodeStringToChar(cbFileName, &FileObject->FileName); MyUpper(cbFileName); //(4)禁止删除下列文件, 经验证到这一步是有效的(非长时间,但区分了大小写) /* wcsstr容易造成蓝屏,应该使用RtlEqualUnicodeString if(wcsstr(_wcslwr(FileObject->FileName.Buffer),L"audit_auth.conf") || wcsstr(_wcslwr(FileObject->FileName.Buffer),L"ignsql.cfg") || wcsstr(_wcslwr(FileObject->FileName.Buffer),L"auditflow.ini") || wcsstr(_wcslwr(FileObject->FileName.Buffer),L"DBLocalInfo.ini")) */ /* StrStrIW就是款字符的、StrStrIA是非宽字符的忽略大小写查找 不能用相等 if(RtlEqualUnicodeString(&FileObject->FileName, &UnicodeString1,1) || RtlEqualUnicodeString(&FileObject->FileName, &UnicodeString2, 1) || RtlEqualUnicodeString(&FileObject->FileName, &UnicodeString3,1) || RtlEqualUnicodeString(&FileObject->FileName, &UnicodeString4, 1)) */ if(strstr(cbFileName, "AUDIT_AUTH.CONF") || strstr(cbFileName, "IGNSQL.CFG") || strstr(cbFileName, "AUDITFLOW.INI") || strstr(cbFileName, "DBLOCALINFO.INI")) { oldCreateDesiredAccess = OperationInformation->Parameters->CreateHandleInformation.DesiredAccess; oldDuplicateDesiredAccess = OperationInformation->Parameters->DuplicateHandleInformation.DesiredAccess; if (FileObject->DeleteAccess==TRUE/*||FileObject->WriteAccess==TRUE*/) { if (OperationInformation->Operation == OB_OPERATION_HANDLE_CREATE) { OperationInformation->Parameters->CreateHandleInformation.DesiredAccess=0; } if(OperationInformation->Operation == OB_OPERATION_HANDLE_DUPLICATE) { //OperationInformation->Parameters->DuplicateHandleInformation.DesiredAccess=0; } /* RtlVolumeDeviceToDosName(FileObject->DeviceObject, &uniDosName); DbgPrint("PID : %ld Drive: %wZ, Path: %wZ, fullPath: %wZ\r\n", (ULONG64)CurrentProcessId, &uniDosName, FileObject->FileName, &uniFilePath); //PID : 996 Drive: C:, Path: \Windows\System32\net1.exe, fullPath: C:\Windows\System32\net1.exe DbgPrint("Operation=%d, DeleteAccess=%d, WriteAccess=%d, oldCreateDesiredAccess=%d, newCreateDesiredAcces=%d, oldDuplicateDesiredAccess=%d, newDuplicateDesiredAcces=%d\r\n", OperationInformation->Operation, FileObject->DeleteAccess, FileObject->WriteAccess, oldCreateDesiredAccess, OperationInformation->Parameters->CreateHandleInformation.DesiredAccess, oldDuplicateDesiredAccess, OperationInformation->Parameters->DuplicateHandleInformation.DesiredAccess); */ } } return OB_PREOP_SUCCESS; }
-
Window进程守护(exe)
2017-12-05 17:09:23window下检测一个进程是否关闭,如果关闭了,那么将会重新启动这个exe -
查看windows进程完整路径并强行终止受保护进程
2011-04-27 18:32:13初衷是用windows自带进程管理器找木马等可疑进程,但是windows自带的只显示可执行程序名,不显示文件路径,并且大部分"系统进程"不能被终止,所以自己写了一个。 功能:可查看绝大部分进程的完整路径,可执行文件... -
18.保护进程.mp4
2021-09-10 20:02:37windows x64驱动程序开发 18.保护进程 -
win10下驱动进程保护
2020-01-31 00:48:52网上可以查到很多WIN7下的驱动后台进程保护代码,而那些代码在WIN10下并不适用,故写此篇来文章来总结我在编写WIN10下后台进程保护驱动程序的过程与经验 说起进程关闭,通常会用三种方法: 1.利用进程管理器关闭 ...网上可以查到很多WIN7下的驱动后台进程保护代码,而那些代码在WIN10下并不适用,故写此篇来文章来总结我在编写WIN10下后台进程保护驱动程序的过程与经验,因为源码文件结构比较杂乱,在此文章里我粘部分代码,在最后再给出完整项目的github地址。
首先说进程关闭,通常会用三种方法:
1.利用进程管理器关闭
2.打开CMD,调用taskkill指令关闭
3.调用taskkill加上-t参数,进行强行进程关闭在代码方面与网上其他大神在win7下的进程保护驱动的代码与思路完全一致,都是调用 ObRegisterCallbacks()回调函数
OB_CALLBACK_REGISTRATION obReg; OB_OPERATION_REGISTRATION opReg; memset(&obReg, 0, sizeof(obReg)); obReg.Version = ObGetFilterVersion(); obReg.OperationRegistrationCount = 1; obReg.RegistrationContext = NULL; RtlInitUnicodeString(&obReg.Altitude, L"321000"); memset(&opReg, 0, sizeof(opReg)); //初始化结构体变量 //下面请注意这个结构体的成员字段的设置 opReg.ObjectType = PsProcessType; opReg.Operations = OB_OPERATION_HANDLE_CREATE | OB_OPERATION_HANDLE_DUPLICATE; opReg.PreOperation = (POB_PRE_OPERATION_CALLBACK)&preCall; //在这里注册一个回调函数指针 obReg.OperationRegistration = &opReg; //注意这一条语句 return ObRegisterCallbacks(&obReg, &obHandle); //在这里注册回调函数
preCall回调函数定义为
OB_PREOP_CALLBACK_STATUS preCall(PVOID RegistrationContext, POB_PRE_OPERATION_INFORMATION pOperationInformation) { //获取pid,这里的HANDLE保存的其实是一个进程pid HANDLE pid = PsGetProcessId((PEPROCESS)pOperationInformation->Object); char szProcName[16] = { 0 }; UNREFERENCED_PARAMETER(RegistrationContext); strcpy(szProcName, GetProcessImageNameByProcessID((ULONG)pid)); //比较字符串,返回0,则字符串相同 if (!_stricmp(szProcName, "Project1.exe")) { //如果创建句柄 if (pOperationInformation->Operation == OB_OPERATION_HANDLE_CREATE) { //OriginalDesiredAccess为原本权限,DesiredAccess为即将要更改的新权限 //如果要结束进程,进程管理器结束进程发送0x1001,taskkill指令结束进程发送0x0001,taskkil加/f参数结束进程发送0x1401 int code = pOperationInformation->Parameters->CreateHandleInformation.OriginalDesiredAccess; if ((code == PROCESS_TERMINATE_0) || (code == PROCESS_TERMINATE_1) || (code == PROCESS_KILL_F)) //给进程赋予新权限 pOperationInformation->Parameters->CreateHandleInformation.DesiredAccess = 0; //DbgPrint("hello %x", pOperationInformation->Parameters->CreateHandleInformation.OriginalDesiredAccess); } } return OB_PREOP_SUCCESS; }
要注意的是在这里负责获取进程名称以用于比较的函数GetProcessImageNameByProcessID为自定义的·,我会在结尾贴上代码的github地址。
看到这就会有人发现我这里的代码与网上win7下的代码别无二致,其实两者区别的关键就是三个值:
PROCESS_TERMINATE_0,
PROCESS_TERMINATE_1,
PROCESS_KILL_F
这三个值在我的自定义头文件myhead.h里是这样定义的//进程管理器结束代码 #define PROCESS_TERMINATE_0 0x1001 //taskkill指令结束代码 #define PROCESS_TERMINATE_1 0x0001 //taskkill指令加/f参数强杀进程结束码 #define PROCESS_KILL_F 0x1401
这就是我与网上win7下的代码不一样的地方,如果照搬网上win7下的代码编译后在win10下运行可能会出现被保护进程连打都打不开的现象
最后,程序虽然有了一定成果,但是还有不足,有待后续的改正,首先说说成果
1.可以成功防止cmd指令taskkill -pid xxx以及
taskkill -f -pid xxx来关闭进程
(pid为9912)
效果图:
从此可以看出虽然指令行反馈为成功,实际上后台进程依然在运行,再看看taskkill -f -pid
从此可以看出加上-f参数的强制关闭直接反馈拒绝访问,进程管理器的后台进程也是依然在运行的,说明没有问题
再看看进程管理器结束进程的效果,在这里我要分成两种情况,一种是直接打开win10下的进程管理器显示给我们的界面,也就是进程管理器菜单栏那一栏显示“进程”的那个页面,还有一种情况则是菜单栏显示“详细信息“的那一栏,为什么我要分为两种情况,因为我刚刚提到的不足就在这里,再“详细信息”一栏关闭程序,驱动可以正常拦截并阻止进程的关闭,而在“进程”一栏的界面直接结束进程,则会出现被保护的用户层程序突然出现CPU占用爆炸的情况CPU占用甚至可能会出现90%以上的现象,到底是为什么我也还没有搞清楚,如果有大神可以指点一二小弟感激不尽,我在后续也会继续进行调试研究,然后看效果图:
在”详细信息“一栏:
可以看到弹出的拒绝访问的错误弹窗,说明是没问题的。
在“进程“一栏:
CPU占用会高达90%,我用这个驱动程序去保护Qt编写的运行在后台的窗口程序,被保护的窗口程序也无法继续在前台正常运行最后崩溃停止运行,事实上这种情况感觉就好像是随机发生的,有时会出现这种情况,有时又不会,让人摸不着头脑。补充:
在Windows进程管理器下会出现CPU调用过高导致程序崩溃的原因是,内存资源已被强制释放,而进程又没被关闭,进程又没有任何读写权限导致程序崩溃,解决此问题的办法是,先捕获结束进程返回码0x1001 捕获到此值后先将进程权限变为0阻止进程被关闭,再进行捕获返回码0x1041如果捕获到说明就是在是在“进程”页面结束进程,所以在捕获到0x1041后,再恢复进程的本来所有权限就能完美解决问题了。代码地址:
github(AGPL v3.0协议) -
Windows编程之双进程守护
2017-12-26 20:25:39需求分析设计实现双进程守护程序,当两个进程同时运行时,关闭其中任意一个,另一个进程会识别出来并马上打开被关闭的进程。设计原理相关函数CreateEvent( ) CreateEvent是一个Windows API函数。它用来创建或打开一... -
C#进程守护程序
2018-10-20 15:30:06最近写了好多次进程守护程序,今天在这里总结一下。 用到的知识点: 1、在程序中启动进程, 2、写Windows服务, 3、以及在Windows服务中启动带界面的程序 关于第三点的问题,我在我的上一篇博客单独介绍... -
Windows守护进程的一种简单实现
2016-05-13 14:51:54一讲到守护进程,很多人都想到了Linux系统,确实在Windows上这个说的比较少。今天上午群里有个朋友问我了下Windows下守护进程的实现问题,我想了想,简单用C++写了个小例子,用来实现系统开机自启动(注册表启动),... -
windows程序设计(王艳平)电子书及源程序源代码
2013-07-18 18:13:429.3.5 HOOK实例——进程保护器 340 9.4 其他常用的侦测方法 344 9.4.1 使用注册表注入DLL 344 9.4.2 使用远程线程注入DLL 345 9.4.3 通过覆盖代码挂钩API 351 9.5 【实例】用户模式下侦测Win32API的例子... -
C#写进程守护程序
2017-03-03 22:01:07最近写了好多次进程守护程序,今天在这里总结一下。 用到的知识点: 1、在程序中启动进程, 2、写Windows服务, 3、以及在Windows服务中启动带界面的程序 关于第三点的问题,我在我的上一篇博客单独介绍了解决... -
进程保护软件ProcessGuard
2012-11-07 19:09:09除此之外还可以设定程序的权力,比如 保护程序免被/授权程序可以 终止、修改、读取其他进程。 注册版本有更强大的功能,主要有: 一、保护物理内存; 二、拦截全局钩子; 三、拦截 Rootkit/驱动/服务 安装;... -
深入理解操作系统原理之Windows进程管理
2017-06-29 22:31:44对象、进程、线程是Windows2000三个基元成份,它们之间有互相交叉的关系。 1、对象 对象是一个抽象的数据结构,在Windows2000中用以表示广义的资源。它是构成OS的三个基元成份中非活动的成份,对象是数据和有关... -
c++ 进程锁:在进程间使用Windows的互斥锁Mutex
2021-11-10 10:44:34说明:当多个进程对计算机上的同一份文件进行操作时候,需要对文件进行保护,防止多个进程同时对文件进行修改造成错误,因此可采用Windows的互斥锁Mutex来进行加锁。 // CreatMutex.cpp : 定义控制台应用程序的入口... -
什么是Windows登录应用程序(winlogon.exe),为什么它在我的PC上运行?
2020-09-13 06:35:01The winlogon.exe process is a critical part of the Windows operating system. This process is always running in the background on Windows, and it’s responsible for some important system functions. ...