Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบ >> Windows Server

การใช้ Managed Service Accounts (MSA และ gMSA) ใน Active Directory

Managed Service Account (MSA) เป็นประเภทบัญชี Active Directory พิเศษที่ใช้เพื่อรัน Services, Applications และ Scheduled Tasks อย่างปลอดภัย แนวคิดหลักคือ รหัสผ่านสำหรับบัญชีเหล่านี้ถูกจัดการโดย Active Directory อย่างสมบูรณ์ รหัสผ่านที่ซับซ้อนและยาว 240 ตัวอักษร จะถูกสร้างขึ้นอัตโนมัติสำหรับพวกเขา ซึ่งเปลี่ยนโดยอัตโนมัติ (ตามค่าเริ่มต้น ทุก 30 วัน) ใช้เฉพาะ Kerberos สำหรับการตรวจสอบตัวตน (ไม่มีปัญหา NTLM Security) ไม่อนุญาต Interactive Logon รหัสผ่านไม่รู้จักโดยใครและไม่เก็บบนระบบท้องถิ่น (ไม่สามารถดึงรหัสผ่านจากกระบวนการระบบ LSASS ด้วย mimikatz หรือเครื่องมือคล้ายๆ ได้) ดังนั้น เพื่อเริ่ม Service หรือ Unattended Jobs ไม่ต้องสร้าง Service Users แยกต่างหากใน AD และจัดการรหัสผ่านของพวกเขา

Managed Service Accounts ถูกนำเสนอใน Windows Server 2008 R2 (Object Type msDS-ManagedServiceAccount) ข้อจำกัดหลักคือ บัญชีดังกล่าวสามารถใช้ได้บนเซิร์ฟเวอร์เพียงเครื่องเดียว (ไม่สามารถใช้ใน Cluster และ NLB Services) ดังนั้น Windows Server 2012 จึงนำเสนอ Group Managed Service Accounts/gMSA (Type msDS-GroupManagedServiceAccount) บัญชี gMSA สามารถใช้พร้อมกันบนหลายโฮสต์ได้

มาพิจารณาคุณสมบัติการใช้ MSA และ gMSA เพื่อเริ่ม Services และ Tasks บนเซิร์ฟเวอร์และ Workstation ใน Active Directory

ข้อกำหนดสำหรับการใช้ MSA/gMSA Service Accounts

Managed Service AccountGroup Managed Service Account
AD Domain and Forest Functional LevelWindows Server 2008 R2 หรือใหม่กว่าWindows Server 2012 หรือใหม่กว่า
KDCDomain Controller ที่เปิดใช้งาน Microsoft Key Distribution Service (KdsSvc)
PowerShellเพื่อสร้างและจัดการ Service AD Accounts ต้องติดตั้ง Active Directory Module for Windows PowerShell
.Net Framework.NET Framework 3.5 หรือใหม่กว่าต้องติดตั้งบนเซิร์ฟเวอร์
Supported Windows VersionsWindows 7/Windows Server 2008 R2 หรือใหม่กว่าWindows Server 2012/Windows 8 หรือใหม่กว่า

สร้าง Key Distribution Service (KDS) Root Key

ก่อนเริ่มสร้างบัญชี MSA/gMSA ต้องดำเนินการครั้งเดียวและสร้าง KDS Root Key รันคำสั่ง PowerShell ต่อไปนี้บน Domain Controller (Microsoft Key Distribution Services ต้องติดตั้งและทำงานอยู่):

Add-KdsRootKey –EffectiveImmediately

ในกรณีนี้ Key จะถูกสร้างและพร้อมใช้งานใน 10 ชั่วโมงหลังจาก AD Replication เสร็จสิ้น

เคล็ดลับ. เพื่อใช้ Key ทันทีในสภาพแวดล้อมทดสอบ สามารถรันคำสั่งนี้: Add-KdsRootKey –EffectiveTime ((get-date).addhours(-10))

ตรวจสอบว่า KDS Root Key ถูกสร้างสำเร็จ:
Get-KdsRootKey

ใช้คำสั่งตรวจสอบ KDS Key:
Test-KdsRootKey -KeyId (Get-KdsRootKey).KeyId

วิธีสร้าง Managed MSA Account ใน Active Directory

เพื่อสร้าง MSA Managed Account ใหม่ใน AD ใช้คำสั่ง:

New-ADServiceAccount -Name msaMunSrv1 –RestrictToSingleComputer

โดยค่าเริ่มต้น MSA และ gMSA ถูกสร้างใน Container CN=Managed Service Accounts แต่สามารถเปลี่ยน OU ได้โดยใช้พารามิเตอร์ Path

ลิงก์ MSA Service Account กับคอมพิวเตอร์เป้าหมาย:

$Identity = Get-ADComputer -identity mun-srv01
Add-ADComputerServiceAccount -Identity $identity -ServiceAccount msaMunSrv1
เตือนอีกครั้ง สามารถใช้ MSA Account ได้บน AD Host เพียงเครื่องเดียวเท่านั้น

เปิดคอนโซล ADUC (Active Directory Users and Computers) และตรวจสอบว่ามีบัญชีใหม่ประเภท msDS-ManagedServiceAccount ปรากฏใน Container (OU) Managed Service Accounts

AD Container นี้ซ่อนอยู่ตามค่าเริ่มต้น เพื่อเห็น ต้องเปิดใช้งาน Advanced Features ในเมนู View ของ Snap-in

สามารถดูข้อมูล MSA Account ได้ด้วยคำสั่ง:
Get-ADServiceAccount msaMunSrv1

สร้าง Group Managed Service Account (gMSA) ใน Active Directory

ก่อนสร้างบัญชี gMSA ให้สร้าง Domain Security Group และเพิ่มเซิร์ฟเวอร์ที่จะได้รับอนุญาตให้ใช้รหัสผ่านสำหรับ Group Service Account นี้ วิธีง่ายที่สุดสร้างและเติมกลุ่มคือใช้ PowerShell:
New-ADGroup grMunSQL1 -path 'OU=Groups,OU=Munich,OU=DE,dc=woshub,DC=com' -GroupScope Global -PassThru –Verbose
Add-AdGroupMember -Identity grMunSQL1 -Members mun-sql01$, mun-sql02$, mun-sql03$

เพื่อสร้าง Group Managed Service Account (gMSA) ใช้คำสั่ง:

New-ADServiceAccount -name gmsaMunSQL1 -DNSHostName gmsaMunSQL1.woshub.com -PrincipalsAllowedToRetrieveManagedPassword grMunSQL1 –verbose

บัญชี gMSA ก็ถูกสร้างใน Managed Service Accounts OU ตามค่าเริ่มต้น

ติดตั้ง Group Managed Service Account บน Windows

เพื่อใช้ MSA/gMSA Service Accounts บนเซิร์ฟเวอร์เป้าหมายหรือ Workstation ต้องติดตั้ง Active Directory PowerShell Module ก่อน:

Add-WindowsFeature RSAT-AD-PowerShell

ติดตั้ง MSA (gMSA) Service Account บนเซิร์ฟเวอร์:

Install-ADServiceAccount -Identity gmsaMunSQL1

ตรวจสอบว่าบัญชี Service Account ติดตั้งถูกต้อง:

Test-ADServiceAccount gmsaMunSQL1

หากคำสั่งคืนค่า True แสดงว่ากำหนดค่าถูกต้อง

หากคำสั่งคืนค่า False น่าจะเป็นเพราะ MSA Account ไม่ได้ติดตั้งบนเซิร์ฟเวอร์ หรือคอมพิวเตอร์นี้ไม่มีสิทธิ์ใช้งาน:

WARNING: Test failed for Managed Service Account gmsaMunSQL1. If standalone Managed Service Account, the account is linked to another computer object in the Active Directory. If group Managed Service Account, either this computer does not have permission to use the group MSA or this computer does not support all the Kerberos encryption types required for the gMSA.
หมายเหตุ. ไม่สามารถใช้ RunAs มาตรฐานเพื่อตรวจสอบว่า Services และ Scripts ของคุณสามารถรันภายใต้ MSA Service Account ได้ ให้ใช้เครื่องมือ PsExec แทน (เราแสดงวิธีใช้ psexec รัน Command Prompt ภายใต้ NT Authority\System ไว้ก่อนหน้านี้)
  1. เปิด Command Prompt เป็น Administrator
  2. รันคำสั่ง: PsExec64.exe -i -u woshub\gmsaMunSQL1$ -p ~ cmd.exe แทนที่รหัสผ่านด้วย ~ หมายถึงรหัสผ่านต้องดึงมาจาก AD
  3. ในหน้าต่าง CMD ที่เปิด ให้รันคำสั่ง whoami เพื่อให้แน่ใจว่าคอนโซลรันภายใต้ gMSA Account
  4. ตรวจสอบว่า Scripts, Programs หรือ Services ทำงานถูกต้องภายใต้ Managed Service Account

ตอนนี้เหลือแค่กำหนดค่า Windows Services, Task Scheduler Tasks, IIS Pools ฯลฯ ที่จำเป็น ให้รันภายใต้ MSA/gMSA User

วิธีรัน Windows Service ภายใต้ Managed Service Account

ตอนนี้สามารถกำหนดค่า Windows Service ที่จำเป็นให้รันภายใต้ MSA/gMSA Account

  1. เปิดคอนโซลจัดการบริการ (services.msc)
  2. เปิด Properties ของบริการที่ต้องการ และไปที่แท็บ "Log On"
  3. เลือกตัวเลือก This account และป้อนชื่อบัญชี MSA อย่าลืมเพิ่มสัญลักษณ์ $ ที่ท้ายชื่อบัญชี (ไม่ต้องป้อนรหัสผ่านบัญชี)
  4. MSA Service Account จะได้รับสิทธิ์ Log On As a Service โดยอัตโนมัติ
  5. หลังบันทึกการเปลี่ยนแปลง บริการต้อง Restart
สำหรับบริการที่ซับซ้อนกับ gMSA ตรวจสอบเอกสารว่ารองรับหรือไม่ ปัจจุบัน gMSA รองรับใน SQL Server, IIS, AD LDS, Exchange Server

รัน Scheduled Task ด้วย Managed Service Account/gMSA

สามารถกำหนดค่า Windows Task Scheduler ให้รันงานภายใต้ gMSA Service Account สะดวกเพราะรหัสผ่านสำหรับ gMSA Account ไม่ได้เก็บในสคริปต์ ไม่ต้องเข้ารหัสหรือป้องกันพวกมัน เมื่อรหัสผ่านเปลี่ยน ไม่ต้องกำหนดค่า Task ใหม่

เพื่อให้สิทธิ์แก่บัญชี MSA/gMSA เพียงเพิ่มเข้าไปใน Security Group ที่จำเป็น ตัวอย่างเช่น Local Administrators Group, Domain Admins, DNS Admins ฯลฯ

สามารถกำหนด Task ให้รันเป็น gMSA Account โดยใช้ PowerShell ตัวอย่าง สคริปต์ต่อไปนี้จะสร้าง Scheduled Task ใหม่ที่รัน PowerShell Script ทุกวันเวลา 23:00 เพื่อ Backup Database:

$action = New-ScheduledTaskAction -Execute powershell.exe -Argument "-file C:\PS\Scripts\DBBackup.ps1 -executionpolicy bypass -NoProfile"
$trigger = New-ScheduledTaskTrigger -At 23:00 -Daily
$principal = New-ScheduledTaskPrincipal -UserID woshub\gmsaMunSQL1$ -LogonType Password
Register-ScheduledTask DBBackup –Action $action –Trigger $trigger –Principal $principal
เคล็ดลับ. เพื่อรัน Scheduled Task ต้องให้สิทธิ์ "Log on as a batch job" แก่บัญชี gMSA

อาร์กิวเมนต์ -LogonType Password ระบุว่ารหัสผ่านสำหรับ gMSA Account นี้จะถูกดึงมาจาก Domain Controller

สามารถสร้าง Scheduled Job ด้วยการตั้งค่าที่ต้องการโดยใช้ taskschd.msc GUI แล้วจึงกำหนดค่าใหม่โดยใช้ schtasks.exe Tool ให้รันภายใต้ Managed Service Account:

schtasks /Change /TN BackupDB /RU "woshub\gmsaMunSQL1$" /RP ""