在 Windows XP 時代,系統服務與使用者應用程式共享同一個 Session 0,服務若勾選「允許服務與桌面互動」,即可直接呼叫 MessageBox.Show 顯示視窗。但自 Windows Vista 起,微軟為了安全性引入 Session 0 隔離:服務獨佔 Session 0,使用者登入後各自分配 Session 1、Session 2…,導致服務無法直接存取使用者桌面。
using System.ServiceProcess;
using System.Windows.Forms;
namespace AlertService
{
public partial class Service1 : ServiceBase
{
protected override void OnStart(string[] args)
{
MessageBox.Show("來自 AlertService 的訊息。");
}
}
}
InstallUtil.exe 註冊服務。



點選「檢視訊息」後出現的訊息方塊,其背景已非 Windows 7 預設桌面,而是經典灰色背景,證明該視窗運行於 Session 0,而非使用者桌面。這也是為何無法直接截圖,必須透過 Virtual PC 才能捕捉該畫面。

服務與應用程式同處 Session 0,權限界限模糊,服務以高權限運行,普通應用程式卻可能被惡意利用,安全風險極高。



AlertService.exe,右鍵 Properties → Security 分頁,查看 Session 欄位。Outlook.exe 等使用者程式,明顯位於不同 Session。


本文確立了 Session 0 隔離的成因與現象。下一篇將詳細介紹三大主流解決方案:
作者:李敬然(Gnie)
出處:GnieTech