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

ติดตั้ง TLS/SSL Certificate ฟรี Let's Encrypt บน IIS Web Server / RDS

ในคู่มือนี้เราจะแสดงวิธีติดตั้งและ Bind TLS/SSL Certificate ฟรีจาก Let's Encrypt สำหรับเว็บไซต์บน IIS Web Server ที่รันบน Windows Server 2019/2016/2012 R2

สารบัญ

Let's Encrypt และ ACME Clients สำหรับ Windows

TLS/SSL Certificate ของเว็บไซต์ช่วยป้องกันข้อมูลผู้ใช้ที่ส่งผ่าน Public Network จากการโจมตี Man-in-the-Middle (MITM) และรับรอง Data Integrity ศูนย์รับรอง Nonprofit Let's Encrypt อนุญาตให้ออก X.509 Encryption TLS Certificates ฟรีสำหรับ HTTPS Encryption ผ่าน API ออกเฉพาะ Domain Validation Certificates ที่หมดอายุใน 90 วัน (จำกัด 50 Certificates ต่อ Domain ต่อสัปดาห์) แต่สามารถ Renew SSL Certificate อัตโนมัติได้ด้วย Scheduling เรียบร้อย

API Interface ของ Let's Encrypt สำหรับออก Certificate อัตโนมัติเรียกว่า Automated Certificate Management Environment (ACME) API มี ACME Client Implementations ยอดนิยม 3 ตัวสำหรับ Windows:

  1. Windows ACME Simple (WACS) — Command Prompt Tool สำหรับ Issue SSL Certificate แบบ Interactive และ Bind เข้ากับ IIS Site เฉพาะ
  2. PowerShell ACMESharp Module — PowerShell Library มี Cmdlets หลายตัวโต้ตอบกับ Let's Encrypt Servers ผ่าน ACME API
  3. Certify — Windows Graphics Tool จัดการ SSL Certificates แบบ Interactive ผ่าน ACME API

WACS Client ติดตั้ง Let's Encrypt TLS Certificate บน IIS Windows Server

วิธีง่ายที่สุดได้ SSL Certificate จาก Let's Encrypt คือใช้ Console Tool Windows ACME Simple (WACS) (ชื่อเก่า LetsEncrypt-Win-Simple) เป็น Wizard เรียบง่ายเลือก IIS Website หนึ่งในที่รัน ออก Certificate และ Bind อัตโนมัติ

สมมติคุณมี IIS Website รันบน Windows Server 2016 ต้องการ Switch Website ไป HTTPS Mode ด้วย Free SSL Certificate จาก Let's Encrypt

ดาวน์โหลด WACS Client ล่าสุดจาก GitHub https://github.com/PKISharp/win-acme/releases (ตัวอย่าง v2.0.10 ไฟล์ win-acme.v2.0.10.444.zip)

Extract Zip Archive ไปที่ Directory บน Server ที่ติดตั้ง IIS: c:\inetpub\letsencrypt

ต้องติดตั้ง .NET Framework 4.7.2 หรือสูงกว่า เพื่อใช้ Win-Acme (ตรวจสอบ .NET Framework Version ที่ติดตั้งอย่างไร?)

เปิด Elevated Command Prompt ไปที่ c:\inetpub\letsencrypt รัน wacs.exe จะเปิด Wizard สร้างและ Bind Let's Encrypt Certificate เข้า IIS Site แบบ Interactive เลือก N: — Create new certificates (simple for IIS) เพื่อสร้าง Certificate ใหม่เร็ว

ต่อไปเลือก Certificate Type ตัวอย่างไม่ต้องการ Certificate กับ Aliases (Multiple SAN — Subject Alternative Name) เลือก 1. Single binding of an IIS site หากต้องการ Wildcard Certificate เลือก Option 3

Utility จะแสดงรายการ Websites ที่รันบน IIS ให้เลือก Site ที่จะออก Certificate

ระบุ Email Address สำหรับรับ Notification เกี่ยว Certificate Renewal Problems และข้อความสำคัญอื่นๆ (ระบุหลาย Email คั่นด้วย Comma ได้) ยอมรับ Terms of Use WACS จะเชื่อมต่อ Let's Encrypt Servers พยายามสร้าง SSL Certificate ใหม่ให้ Website อัตโนมัติ

กระบวนการสร้างและติดตั้ง SSL Let's Encrypt Certificate สำหรับ IIS เป็น Fully Automated

หมายเหตุ: Domain Validation Default ทำในโหมด http-01 validation (SelfHosting) ต้องมี DNS Record ของ Domain ชี้ไปที่ Web Server เมื่อรัน WACS ใน Manual Mode (Full Options) สามารถเลือก Validation Type — 4 [http-01] Create temporary application in IIS (recommended) กรณีนี้จะสร้าง Application เล็กๆ บน IIS Web Server ให้ Let's Encrypt Servers ทำ Domain Validation ผ่านได้

ระหว่าง TLS/HTTP Validation เว็บไซต์ของคุณต้องเข้าถึงได้จากอินเทอร์เน็ตด้วย Full DNS Name ผ่าน HTTP (80/TCP) และ HTTPS (443/TCP)

WACS Tool บันทึก Private Key (*.pem), Certificate และไฟล์อื่นๆ ไว้ที่ C:\Users\%username%\AppData\Roaming\letsencrypt-win-simple จากนั้นติดตั้ง Let's Encrypt SSL Certificate ที่สร้างขึ้นใน Background และ Bind เข้ากับ IIS Site หากมี SSL Certificate ติดตั้งอยู่แล้ว (เช่น Self-signed Cert) จะถูกแทนที่

ใน IIS Manager เปิด Site Binding Settings สำหรับ Website ตรวจสอบว่าใช้ Certificate ที่ออกโดย Let's Encrypt Authority X3

Certificate นี้จะปรากฏว่า Trusted บนคอมพิวเตอร์ของคุณหาก Windows Trusted Root Certification Authorities อัปเดตแล้ว

พบ Let's Encrypt IIS Certificate ใน Computer Certificate Store ภายใต้ Web Hosting → Certificates

Windows ACME Simple สร้าง Job ใหม่ใน Windows Task Scheduler (win-acme-renew (acme-v02.api.letsencrypt.org)) เพื่อ Renew Certificate อัตโนมัติ Task รันทุกวัน Renewal ทำหลัง 60 วัน Task รันคำสั่ง:

C:\inetpub\letsencrypt\wacs.exe --renew --baseuri "https://acme-v02.api.letsencrypt.org"

คุณสามารถใช้คำสั่งเดียวกันเพื่อ Manual Update Let's Encrypt Certificate

Redirect HTTP ไป HTTPS ด้วย IIS URL Rewrite

เพื่อ Redirect HTTP Traffic ทั้งหมดไป HTTPS Website URL ติดตั้ง Microsoft URL Rewrite Module (https://www.iis.net/downloads/microsoft/url-rewrite) และตรวจสอบว่า Option Require SSL ปิดอยู่ใน Site Settings ตั้งค่า Redirect ใน web.config ด้วย Rewrite Rules:

<system.webServer>
  <rewrite>
    <rules>
      <rule name="HTTP to HTTPS Redirect" enabled="true" stopProcessing="true">
        <match url="(.*)" />
        <conditions>
          <add input="{HTTPS}" pattern="off" ignoreCase="true" />
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

สามารถตั้งค่า Redirect ผ่าน URL Rewrite Extension ใน IIS Manager GUI ได้: Select Sites → yoursitename → URL Rewrite → Create New Rule Add Rule → Blank rule

ตั้งค่า: Requested URL: Matches the Pattern, Using: Regular Expressions, Pattern: (.*)
Conditions: Logical Grouping: Match All → Add → Condition input: {HTTPS}, Check if input string: Matches the Pattern, Pattern: ^OFF$
Action: Action Type: Redirect, Redirect URL: https://{HTTP_HOST}/{R:1}, Redirect type: Permanent (301)

เปิด Browser ลองเข้าเว็บด้วย HTTP Address ควร Redirect ไป HTTPS URL อัตโนมัติ

ใช้ Let's Encrypt Certificates กับ RDS Gateway & Web Access

หากใช้ RDS Gateway หรือ RDS Web Access เชื่อมต่อ External Users เข้า Corporate Network สามารถใช้ Trusted SSL Certificate จาก Let's Encrypt แทน Self-signed Certificate มาดูวิธีติดตั้ง Let's Encrypt Certificate ให้ Remote Desktop Services บน Windows Server อย่างถูกต้อง

หาก RDSH Role ติดตั้งบน RD Gateway Server เดียวกัน ต้องป้องกัน Non-admin Users เข้าถึง Directory ที่เก็บ WACS Files (ตัวอย่าง c:\inetpub\letsencrypt) และ Let's Encrypt Certificate/Keys (C:\ProgramData\win-acme)

รัน wacs.exe บน RD Gateway Server เลือก IIS Site ที่ต้องการ (ปกติคือ Default Web Site) Let's Encrypt จะออก Certificate ใหม่ Bind เข้า IIS Website และ Task Scheduler สำหรับ Auto Renewal จะปรากฏ

สามารถ Export Certificate นี้ Manual Bind เข้า RDS Services ผ่าน SSL Binding แต่ต้องทำ Manual ทุก 60 วันเมื่อ Certificate Renew

ใช้ PowerShell Script Auto Bind SSL Certificate เข้า RDS Gateway หลัง Update Let's Encrypt Certificate โปรเจกต์ win-acme มี Script เตรียมไว้ — ImportRDGateway.ps1 (https://github.com/PKISharp/win-acme/tree/master/dist/Scripts) อนุญาตให้ติดตั้ง SSL Certificate ที่เลือกให้ Remote Desktop Services ข้อจำกัดคือต้องระบุ Thumbprint ของ Certificate ใหม่ Manual:

ImportRDGateway.ps1 <certThumbprint>

เพื่อ Auto Get Certificate Thumbprint จาก IIS Site ที่ระบุ ใช้ Modified Script ImportRDGateway_Cert_From_IIS.ps1 (based on ImportRDGateway.ps1)

รัน Script Manual: powershell -File ImportRDGateway_Cert_From_IIS.ps1

หาก RDS Gateway รันบน IIS "Default Web Site" Index 0 ใช้ Script ได้เลยโดยไม่แก้ไข

หา Site ID บน IIS: PowerShell → Import-Module WebAdministration
Get-ChildItem IIS:Sites|ft -AutoSize
คอลัมน์ ID แสดง Index ลบ 1 ออก นำ Index ที่ได้ไปแทน 0 ในบรรทัด 27 ของ Script: $NewCertThumbprint = (Get-ChildItem IIS:SSLBindings)[0].Thumbprint

เปิด Task Scheduler win-acme-renew แท็บ Actions เพิ่ม Task ใหม่รัน ImportRDGateway_Cert_From_IIS.ps1 หลัง Update SSL Certificate

เพื่อไม่ต้องเปลี่ยน PowerShell Execution Policy รัน Script ด้วย: PowerShell.exe -ExecutionPolicy Bypass -File c:\inetpub\letsencrypt\ImportRDGateway_Cert_From_IIS.ps1

ตอนนี้ Script Bind SSL Certificate เข้า RDS จะรันทันทีหลัง Let's Encrypt Certificate Renew RD Gateway Service จะ Restart อัตโนมัติด้วยคำสั่ง: Restart-Service TSGateway

[Alert] เมื่อ TSGateway Service Restart User Sessions ทั้งหมดจะถูก Disconnect แนะนำเปลี่ยนความถี่การรัน Certificate Renewal Task เป็นครั้งละ 60 วัน