Application Pool 和 "Service Unavailable"

每个AppPool要指定一个身份(identity)来运行。IIS6中AppPool默认运行身份为NT AUTHORITY\NETWORK SERVICE。这是一个系统内建帐户,完全受系统控制。它支持所有必要的以服务登录(Log on as a service)的权限,密码也是内部来维护——换句话说,我们可以把以服务登录理解为它可以控制了哪些帐户能够使用、操作远程资源。我们可以通过很多方法控制远程资源的访问。比如修改远程资源ACL表,并允许远端机器访问本地资源;或者在远程访问时换作远端授信帐户(impersonating)进行操作;或者索性让我们的程序运行在某一个配置帐户下——log on as a service。不同的是它是宿主在IIS的AppPool中的,必然收到IIS控制,用户本身没有办法像普通服务程序那样启动它。我要试验的就是指定AppPool以配置帐户启动。

我们分几步来部署一个web程序以指定账户运行。
1. 打开IIS6,在AppPool节点找到我们要修改的web程序,右键属性。
默认IIS是选择Predefined帐户:Network Service。现在我们勾选Configurable,并指定帐户和密码。注意:这个帐号必须是一个域帐号!
2. 将我们之前指定的apppool帐号加入IIS_WPG用户组中。IIS_WPG用户组维护了所有可以以其中帐户启动worker Process的帐户。Worker Process是由IIS启动的。

做完这两步,本以为可以完美的开启自定义帐户运行的w3wp.exe。却不想得到"Service Unavailable”的异常。翻看event log,我们得到以下一些错误:
错误一:此错误基本就是重复一下Service Unavailable的意思。。。注意主谓关系:有某某后台要启动某个process来运行appPool。这个process肯定就是w3wp.exe。但那个后台是谁?这里还不知道。

Event Type: Error
Event Source: W3SVC
Event Category: None
Event ID: 
1059
Date:  1/18/2008
Time:  11:48:27 AM
User:  N
/A
Computer: HITOMI
Description:
A failure was encountered while launching the process serving application pool 'HITOMIWS'
. The application pool has been disabled. 

For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.

错误二:已经告诉我们所指定的appPool帐户非法。另外一个极重要的提示回答了error一的疑惑:是World Wide Web Publishing Service无法启动w3wp.exe。(这里也回答了文章最初的问题)至此,唯一的问题就是为什么帐户非法?往下看。。。

Event Type: Warning
Event Source: W3SVC
Event Category: None
Event ID: 
1057
Date:  1/18/2008
Time:  11:48:27 AM
User:  N
/A
Computer: HITOMI
Description:
The identity of application pool 'HITOMIWS' is invalid
, so the World Wide Web Publishing Service can not create a worker process to serve the application pool.  Therefore, the application pool has been disabled. 

For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.

错误三:谜底揭晓了。该账户没有被赋予logontype4,导致它非法。如果有经验的话,这种帐户-logonType控制是在local Security Settings\Local Policy\User Rights Assignments里定义的。具体来说就是log on as a service。

Event Type: Failure Audit
Event Source: Security
Event Category: Logon
/Logoff 
Event ID: 
534
Date:  1/18/2008
Time:  11:48:08 AM
User:  NT AUTHORITY
\SYSTEM
Computer: HITOMI
Description:
Logon Failure:
  Reason: The user has 
not been granted the requested
   logon 
type at this machine
  User Name: HITOMIWEBSERVICE
  Domain:  REDMOND
  Logon 
Type4
  Logon Process: Advapi  
  Authentication Package: Negotiate
  Workstation Name: HITOMI
  Caller User Name: HITOMI
$
  Caller Domain: REDMOND
  Caller Logon ID: 
(0x0,0x3E7)
  Caller Process ID: 
640
  Transited Services: -
  Source Network Address: -
  Source Port: - 

For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.

Start > Run, secpol.msc回车, 打开local Secuirty Settings,展开Local Policy\User Rights Assignments,选择Log on as a service这个policy,果然里面有NetworkService和sql server的n多帐户,我们再把appPool帐户加到这个list里面。做完之后,machine需要重新启动。等一切再次ready以后,我们再试一下web程序,欣喜看到w3wp.exe已经以指定帐号启动了!
回头再考虑一下我们什么时候要用指定帐户运行web程序呢?It depends。当我们的web 程序要经常访问域内其他节点资源的时候,为提高访问安全要用到指定帐户,加强ACL控制。道理看过下表就明白了。
!>此表为Network Service帐户的默认用户权限(包括权限如何派生)

Privilege

Source

Replace a process-level token (SeAssignPrimaryTokenPrivilege)

Explicit assignment

Adjust memory quotas for a process (SeIncreaseQuotaPrivilege)

Explicit assignment

Generate security audits (SeAuditPrivilege)

Explicit assignment

Bypass traverse checking (SeChangeNotifyPrivilege)

Through membership in the Everyone group

Access this computer from the network (SeNetworkLogonRight)

Through membership in the Everyone group

Log on as a batch job (SeBatchLogonRight)

Through membership in the IIS_WPG group

Log on as a service (SeInteractiveLogonRight)

Explicit assignment

Impersonate a client after authentication

Through membership in the IIS_WPG group

Bypass Traverse Checking都是通过Everyone group来验证的,其中Access this computer from the network一项如果用Everyone验证的话,对于严格安全管理来说太松散了,这也是为什么这个帐户被配置帐户取代的重要原因了吧。其实在大多数情况下NetworkService已经够用了。


posted @ 2008-01-21 15:16  格列佛游记  阅读(8761)  评论(6编辑  收藏  举报