Not
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    1
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # IIS web.config 連線字串帳號密碼加密 ###### tags: `資安` ## 問題 IIS 的網站會有一個檔案 web.config,通常裡面會放一些設定或是機密資料,例如資料庫連線字串,甚至包含帳號密碼。 為了避免這些資訊被竊取,理想的情況是使用另一個程式將字串加密,有許多教學或開發人員會分享自己開發的加解密程式,雖然安全性高,但管理上較不方便,比方說 1. 遺失加解密工具或遺失使用方法 2. 開發人員離職後無人維護(加解密演算法過時未更新) 3. 教導新人需要特別學習這套獨一無二的程式增加學習成本 ## 解決方法 使用 ASP .NET IIS 註冊工具 (Aspnet_regiis.exe) 把 web.config 加密 [ASP.NET IIS 註冊工具 (Aspnet_regiis.exe)](https://learn.microsoft.com/zh-tw/previous-versions/dotnet/netframework-2.0/k6h9cz8h(v=vs.80)?redirectedfrom=MSDN) 是微軟提供的小程式,他能針對 web.config 內的連線字串加密或解密,即使是加密狀態,同一台電腦的IIS網站可正常讀取不受影響,但直接把 web.config 拿到其它電腦時將無法使用,可以避免帳號密碼等敏感資訊外洩。 :::info **提醒** 加密後的檔案拿去其他電腦無法使用,是因為加密金鑰綁在作業系統底層,因此記得移轉時要解密再移轉,或是整台作業系統一起備份喔 ::: ## 如果電腦有安裝 IIS 網站伺服器請看這邊 aspnet_regiis 這個程式可以在 Windows資料夾內Microsoft.NET目錄的Framework64目錄中找到,例如以下路徑 ```C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727版號可能不同請依自己電腦資料夾更改\aspnet_regiis.exe``` ## (參考選項)如果電腦有 Visual Studio ,希望常常使用 aspnet_regiis 請看這邊(不建議使用) 而微軟的 Visual Studio本身也有內建 aspnet_regiis,以下採用 Visual Studio 2019預設安裝路徑位為範例,先用系統管理員的權限開cmd,然後執行下面這個bat,就能在任意位置使用 aspnet_regiis 了 ```C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat``` ## 加密方式 在管理員權限的 cmd 介面輸入以下指令 ```=cmd aspnet_regiis -pef connectionStrings D:\web -prov "RsaProtectedConfigurationProvider" ``` 執行後的結果如下 ```=cmd C:\Windows\Microsoft.NET\Framework64\v2.0.50727> aspnet_regiis -pef appSettings D:\web -prov "RsaProtectedConfigurationProvider" 正在加密組態區段... 成功! ``` 加密後 web.config 會像這樣 ![](https://hackmd.io/_uploads/S1TsROGJR.png) ### 指令說明 指令中的第一個參數 -pef connectionStrings ,代表將 connectionStrings 這個區塊加密,總共有4個區塊可以加密,請依照自己的敏感資訊躲在哪一塊自己更改 * appSettings * connectionStrings * identity * sessionState 這個指令預設會加密 D:\web\ 這個路徑的 web.config 這個檔案,如果想要特別指定其他的檔名,也可以將 ```D:\web\``` 這一段改寫 ```D:\web\another.config``` 後面的 -prov "RsaProtectedConfigurationProvider" 則是指使用 Rsa 的演算法進行加密保護 這個演算法是 .NET Framework 隨附的,有這兩個可以選擇 * RSAProtectedConfigurationProvider -使用非對稱 RSA 演算法 進行加密和解密。 * DPAPIProtectedConfigurationProvider -使用 Windows 資料保護 API (DPAPI) 進行加密和解密。 ### 設定金鑰容器權限 前面加密的時候是使用系統管理員權限,還要設定讓網站應用程式執行的時候也有讀取權限,這樣程式運作時才能自己解密,指令如下 ``` aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITY\NETWORK SERVICE" ``` 注意 "NT AUTHORITY\NETWORK SERVICE" 是IIS預設的帳號,如果網站應用程式有特別指定別的使用者,那這一段要換成其他帳號,這樣網站運作的時候才能正常運作 ### 解密方式 如果未來需要修改加密的內容,可以利用這個指令將檔案解密 ``` aspnet_regiis -pdf "connectionStrings" "D:\web" ``` ## 參考文件 * 保護連接字串與其他設定資訊 (C#) https://docs.microsoft.com/zh-tw/aspnet/web-forms/overview/data-access/advanced-data-access-scenarios/protecting-connection-strings-and-other-configuration-information-cs * [Day15] 資料使用安全(保護連接字串)下 https://ithelp.ithome.com.tw/articles/10188072 * ASP.NET 2.0 筆記:加密組態檔 https://dotblogs.com.tw/huanlin/2008/04/23/3226 * web.config 加密 https://blog.xuite.net/sugopili/computerblog/83661629-web.config+%E5%8A%A0%E5%AF%86 * Web.config connectionStrings 加密 發生錯誤 http://www.blueshop.com.tw/board/FUM20041006161839LRJ/BRD2007101311071909M.html * RSA 金鑰對Web.config資料庫連線字串加密 https://medium.com/selectprogram/使用rsa-金鑰對web-config資料庫連線字串加密-19f2b66d7a

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully