windows 如何防止任务管理器杀死我的程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1983564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:40:21  来源:igfitidea点击:

How can I keep Task Manager from killing my program?

windowsdelphiprocessmessagestaskmanager

提问by djiga4me

Is there any way to protect my Delphi application from being killed by the Windows task manager (or others like Process Explorer)?

有什么方法可以保护我的 Delphi 应用程序不被 Windows 任务管理器(或其他类似 Process Explorer)杀死?

I think Windows messages can do that (by doing a hook and intercepting the TerminateProcess message).

我认为 Windows 消息可以做到这一点(通过做一个钩子并拦截 TerminateProcess 消息)。

I want an example of this protection. The Kaspersky Anti-Virus suites are like this; we can't end their process in Task Manager.

我想要一个这种保护的例子。卡巴斯基反病毒套件是这样的;我们无法在任务管理器中结束他们的进程。

回答by Josh

As Kornel says, there are OS-level protection of processes isolated by users. But generally speaking, there's no way to stop your process from being terminated by a user with permission to do so. And a user has permission to terminate processes running as that user.

正如 Kornel 所说,对用户隔离的进程有操作系统级别的保护。但一般来说,没有办法阻止您的进程被有权限的用户终止。并且用户有权终止以该用户身份运行的进程。

Even if you wanted to run it as SYSTEM, you couldn't use this process to interact with the logged on user. You'd need to run it as a service and it would have no GUI. You could try other approaches such as getting a DLL loaded into a process like Explorer.exe that users won't terminate because they don't want to, but that's just abusive.

即使您想以 SYSTEM 身份运行它,也无法使用此进程与登录用户进行交互。您需要将其作为服务运行,并且没有 GUI。您可以尝试其他方法,例如将 DLL 加载到像 Explorer.exe 这样的进程中,用户不会因为他们不想终止而终止,但这只是滥用。

It would be a very bad situation for end users if developers could just write applications that could not be terminated. If this is an internal application you might check Server Fault to see if there's some way of achieving it with Group Policy.

如果开发人员只能编写无法终止的应用程序,这对最终用户来说将是一个非常糟糕的情况。如果这是一个内部应用程序,您可能会检查服务器故障以查看是否有某种方法可以通过组策略实现它。

回答by Remko

AV Programs like Kaspersky probably use a driver and use hook to prevent termination. In your situation I would advise to set an ACL on the process, this prevents termination with Task Manager or cmdline tools (if the user does not have the Debug privilege). Of course the user can always use a tool like Process Explorer, take ownership of the process, set new ACL and Terminate.

像卡巴斯基这样的 AV 程序可能使用驱动程序并使用钩子来防止终止。在您的情况下,我建议在进程上设置 ACL,这可以防止使用任务管理器或 cmdline 工具终止(如果用户没有调试权限)。当然,用户始终可以使用 Process Explorer 之类的工具,获得流程的所有权,设置新的 ACL 并终止。

If the user is not an administrator it would suffice to run the process in a different user context (eg launch it from a service).

如果用户不是管理员,则在不同的用户上下文中运行该进程就足够了(例如从服务启动它)。

Setting a process ACL is very easy with the Jedi Windows Security Libraryas this sampleshows.

如本示例所示,使用Jedi Windows 安全库设置进程 ACL 非常容易。

回答by Mason Wheeler

This is a very bad idea. If your program is running on someone else's computer, it's on their property, not yours, and needs to behave itself as a guest in their home. That means you don't act as if you own the place, and you certainly don't tell the computer's owner what he can't do with his own property, such as kill a task he doesn't want running. If you do that, then your program is no better than malware, and is likely to be treated like malware.

这是一个非常糟糕的主意。如果您的程序在其他人的计算机上运行,​​那么它就在他们的财产上,而不是您的财产上,并且需要在他们家中表现得像客人一样。这意味着你不会表现得好像你拥有这个地方,你当然不会告诉计算机所有者他不能用自己的财产做什么,例如杀死他不想运行的任务。如果您这样做,那么您的程序并不比恶意软件好,并且很可能被视为恶意软件。

回答by Alex

I think that you're asking a wrong question.

我认为你在问一个错误的问题。

You trying to solve "what if connection will be terminated in the middle of chat with server" problem in the wrong way. The answer is notto "deny process termination", but to "foresee connection problems and write error-fallback code"!

您试图以错误的方式解决“在与服务器聊天的过程中连接将被终止怎么办”的问题。答案不是拒绝进程终止”,而是“预见连接问题并编写错误回退代码”!

Why? Cause, obviosly, connection termination can be due to network problems (you can't deny your users to unplug machine from network) and not termination of the client!

为什么?原因很明显,连接终止可能是由于网络问题(你不能拒绝你的用户从网络上拔掉机器)而不是客户端的终止!

Why can't you trap TerminateProcess?

为什么不能捕获 TerminateProcess?

And if you're worried about application crashed - just use Application Restart & Recovery API.

如果您担心应用程序崩溃 - 只需使用Application Restart & Recovery API

回答by Jet

The most simple way: you could just start 2 processesand let them "look" on each other.
If one of them was closed, let the other proceess restart it. It will prevent newbies (as you said) from killing that process.

最简单的方法:你可以只启动2 个进程,让它们相互“看”。
如果其中一个被关闭,让另一个进程重新启动它。它会阻止新手(如您所说)杀死该进程。

Sorry, but I think that your "Server/Client application" has trojan/backdoorabilities. "Client app send some useful data" , "unkillable process" (in your comments) seem tooooo suspicous for me.

抱歉,我认为您的“服务器/客户端应用程序”具有木马/后门功能。“客户端应用程序发送一些有用的数据”、“不可杀死的进程”(在您的评论中)对我来说似乎太可疑了。

回答by Stijn Sanders

Create an NT-service project, service processes can't be killed with Process Manager, but are controlled by the internal service manager.

创建一个NT-service项目,服务进程不能被Process Manager杀死,而是由内部服务管理器控制。