非root用户无法在docker中启动w3svc服务

时间:2021-02-17 17:49:01

标签: asp.net .net docker iis

我正在使用 docker 映像“mcr.microsoft.com/dotnet/framework/runtime:4.8-windowsservercore-ltsc2019”。我注意到 windowsservercore 的默认用户是 ContainerAdministrator。如果我尝试使用用户 ContainerUser (docker run -u ContainerUser mcr.microsoft.com/dotnet/framework/runtime:4.8-windowsservercore-ltsc2019) 运行图像,我会收到以下错误:错误:无法停止或查询状态服务“w3svc”错误 [80070005]。 我认为该错误与用户运行 ServiceMonitor 所需的权限有关。那么,首先,假设 windowsservercore 镜像必须与 ContainerAdministrator 一起运行而不能与 ContainerUser 一起运行是否正确?

如果上述假设是正确的,我想确认使用 ContainerAdministrator 运行容器是否会使容器面临安全问题。据我了解,即使 ServiceMonitor.exe 是使用 ContainerAdministrator 启动的,面向外部的进程也是 IIS Windows 服务,它在 IIS_IUSRS 组中的本地帐户下运行。因此,即使攻击者可以破坏应用程序,它也不会具有对容器的管理员访问权限。谁能确认这是否正确?

2 个答案:

答案 0 :(得分:0)

ContainerAdministrator 是一个特殊的虚拟帐户。ContainerAdministrator 是您运行容器时的默认帐户 - 因此,如果您的 CMD 指令启动控制台应用程序,该应用程序将作为 ContainerAdministrator 运行。如果您的应用作为 Windows 服务在后台运行,则该帐户将是服务帐户,因此 ASP.NET 应用在应用程序池帐户下运行。

您可以参考以下链接:

Accessing the Docker host pipe inside windows container with non-admin user

答案 1 :(得分:0)

我和你的情况一样。我无法证实您的假设(尽管我假设相同)。但我可以提供我们的 dockerfile,它使我们能够以非 root 身份运行(以遵守 AKS 政策)。

dockerfile

FROM mcr.microsoft.com/dotnet/framework/wcf:4.8-windowsservercore-ltsc2019

SHELL ["cmd", "/S", "/C"]
# username = '1000' so the k8s policy can verify it's a non-root user
RUN net user /add 1000
# We copy some config files in the actual startup.ps1 so we need write access here
RUN icacls C:\inetpub\wwwroot\ /grant 1000:(OI)(CI)F /t
# ServiceMonitor.exe puts some environment variables in the applicationHost.config
RUN icacls C:\Windows\System32\inetsrv\Config\ /grant 1000:(OI)(CI)F /t /c

# S-1-5-32-545 is group Builtin\Users which contains user 1000. Allows user to restart the w3svc service
RUN sc.exe sdset w3svc D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)    (A;;CCLCSWLOCRRC;;;IU)(A;;CCLCSWLOCRRC;;;SU)(A;;RPWPDTLO;;;S-1-5-32-545)

COPY startup.ps1 /

WORKDIR /inetpub/wwwroot
ARG source=obj/Docker/publish
COPY ${source} .
USER 1000

ENTRYPOINT ["powershell", "/startup.ps1"]

startup.ps1

# ContainerAdministrators doesn't have these variables, but the
# custom account does have them. If this gets put into the applicationHost.config
# iis will try to write to the user specific temp directory, and fail (with an unrelated error)
Remove-Item env:TMP
Remove-Item env:TEMP
C:/ServiceMonitor.exe w3svc
相关问题