Windows服务dll搜索路径

发布于 2024-09-18 06:41:40 字数 386 浏览 4 评论 0原文

我使用 .net 开发了一个 Windows 服务。我的服务对非托管代码进行一些调用,如下所示 -

[DllImport("cmxConnect.dll")]
private unsafe static extern String cmxQuery([MarshalAs(UnmanagedType.LPStr)] String s, long* connPointer);

我已将 cmxConnect.dll 放置在与服务可执行文件相同的文件夹中。如果我将登录用户设置为我的域帐户,则服务可以正常启动。但是,如果我使用本地系统帐户启动服务,则会出现 DLL 未找到异常。我猜测我的环境设置中有一些东西可以让 Windows 找到 cmxConnect.dll。有人能指出这到底是什么吗?

I have developed a windows service using .net . My service makes some calls to unmanaged code like follows -

[DllImport("cmxConnect.dll")]
private unsafe static extern String cmxQuery([MarshalAs(UnmanagedType.LPStr)] String s, long* connPointer);

I have placed cmxConnect.dll within the same folder as the service executable. The service starts fine if I set the logon user to be my domain account. But if I start the service using the local system account I get DLL not found exceptions. I am guessing there is something in my environment settings thats enables windows to find cmxConnect.dll. Can someone point out what exactly this is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雨后彩虹 2024-09-25 06:41:52

据我了解,DllImport 属性只是包装了对 LoadLibrary 的调用,因此标准 动态链接库搜索顺序应适用。

服务将在一个比用户代码更受限制的环境中运行 - 我可以看到,从 exe 文件夹和 System32 之外的任何位置加载 dll 都是不可取的 - 其他任何地方都会导致预加载攻击,这会对服务相当认真。

事情可能就这么简单:服务只能搜索 System32 中的 dll?

查找 dll 的可信位置是:

  1. 当传递显式路径时,LoadLibrary 清楚地知道应用程序知道它想要哪个 dll。您可以将完全限定的路径传递给 DllImport 吗?
  2. 搜索 dll 最值得信赖的非完全限定位置是 WinSxS - 如果您自己构建 dll,也许可以选择将其部署为本机并排程序集。
  3. exe自己的文件夹。通常。我无法想象,因为该服务是一个 .net 应用程序,所以这不适用。但显然这里有一个问题。
  4. System32 - 您可能需要在此处安装它。

As I understand things the DllImport attribute simply wraps a call to LoadLibrary, so the standard Dynamic Link Library Search Order should apply.

Services will run in a far more restricted environment that user code - I can see that it would be undesirable to load dlls from any location other than the exe's folder and System32 - everywhere else opens one up to a pre-loading attack, and that would be pretty serious with a service.

It could be that simple: Services can only search for dlls from System32?

The trusted locations to find dlls are:

  1. When passed an explicit path its clear to LoadLibrary that the app knows which dll it wants. Can you pass a fully qualified path to DllImport?
  2. The most trusted non-fully qualified place to search for dlls is in WinSxS - if you build the dll yourself, perhaps deploying it as a native Side by side assembly is an option.
  3. The exe's own folder. Usually. I can't imagine that because the service is a .net app that this wouldn't apply. But clearly there is an issue here.
  4. System32 - you might need to install it here.
ペ泪落弦音 2024-09-25 06:41:50

我在这里猜测,但是你检查过环境变量吗?您的本地系统 a/c 是否具有相同的 Env 集。瓦尔斯?

I'm guessing here, but have you checked the Environment Variables. Does your local system a/c have the same set of Env. Vars?

ペ泪落弦音 2024-09-25 06:41:48

尝试使用 msft 的进程监视器。该工具将向您显示该服务正在哪里寻找您的 dll。它甚至可能正在寻找依赖的 dll。这也会显示在进程监视器中。

Try process monitor from msft. This tool will show you where the service is looking for your dll. It may even be looking for a dependant dll. This would also show up in process monitor.

私藏温柔 2024-09-25 06:41:47

本地系统帐户非常强大。为了安全起见,可能会为此帐户禁用 DLL 搜索顺序。 (如果它仅按名称进行搜索,并且有人设法将恶意 DLL 放入搜索顺序中的某个位置,那么您就遇到了权限升级漏洞。)如果它是 .NET 服务,您可能希望将 DLL 添加到您的清单并将 DLL 安装在 GAC 中。 (我不是 .NET 人。我之前刚刚听说过这些术语。)

The Local System account is pretty powerful. It's possible that the DLL search order is disabled for this account for security. (If it goes searching just by name, and somebody manages to put a malicious DLL somewhere in the search order, then you've got an escalation of privilege vulnerability.) If it's a .NET service, you probably want to add your DLL to your manifest and get your DLL installed in the GAC. (I'm not a .NET guy. I've just heard these terms before.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文