SlideShare a Scribd company logo
1 of 22
.NET 安全應用程式開發 – 設定與調校
.NET Security Application Development –
Part III
Blackie Tsai
blackie1019@gmail.com
August 2018
課程大綱
• IIS 與安全網站開發部屬設定
• MSSQL 安全設定
IIS 與安全網站開發部屬設
定
預設的 ASP.NET 執行識別
• w3wp.exe 使用的帳號與IIS版本有關
• 在 Microsoft Windows 2000 和 Windows XP Professional 上
• 是本機 ASPNET 帳戶。
• 在 IIS6 或 IIS7 的傳統模式下
• 預設是 NETWORK SERVICE 帳戶。
• 在 IIS7 整合管線模式下
• 取得的身份識別預設是所屬之 IIS 應用程式集區的識別,所以不同的應用程式
集區會有不同的識別,如 IIS AppPoolDefaultAppPool .
• IIS 7.5之後
• w3wp.exe(IIS的Process)預設都是使用IIS AppPools<App Pool Name>這個
虛擬使用者帳號來執行
• 若不喜歡預設的執行識別,也可以變更應用程式集區的執行識別
Windows Process 與 IIS AppPool 權限設定
ASP.NET 的執行識別
• AppPool 的識別設定為 ApplicationPoolIdentity
• 執行識別是 IIS APPPOOL[AppPoolName
• 管線模式下的預設值
• AppPool 的識別設定為 NetworkService
NetworkService
• 可以指定特定的帳戶。
• 【IIS APPPOOL[AppPoolName】這是一個虛擬帳戶,若要對特定資料夾存取,
要注意其存取權限,
AppPool 的識別設定
匿名存取、表單驗證與Windows驗證
• 匿名存取
• 網站資料夾的最少授權對象為NT AUTHORITYIUSR及IIS
APPPOOLYourPoolName;
• 預設使用匿名存取
• 表單驗證
• 同上,網站資料夾的最少授權對象為NT AUTHORITYIUSR及IIS
APPPOOLYourPoolName;
• 利用ASP.NET當中自訂的登入表單,搭配MemberShip資料庫驗證使用者身分。
• Windows 驗證
• 利用IIS驗證使用者的Windows帳戶,然後存取ASP.NET應用程式。
• 除IIS APPPOOLYourPoolName 外要再指定為Authenticated Users群組、
Domain Users群組,或是可登入使用者所屬的特定群組三者之一。
• Windows Server 2016 開始要自行安裝該模組才可啟用
• 網站需要寫入的情境
• 如果有特別指定應用程式集區,則網站資料夾的安全性要指定 IIS AppPool站台
的應用程式集區 可以寫入
• 如果用預設的 DefaultAppPool 則要指定 IIS_IUSRS 可以寫入
從使用需求了解應用程式所需要權限
• httpCookies
• httpOnlyCookies = "true"
• requireSSL = "true"
• sessionState
• cookieless="true"
• regenerateExpiredSessionId="true"
• compilation
• debug = "false"
• httpRuntime
• enableHeaderChecking = "true"
• enableVersionHeader= "false"
• customErrors
• mode = "On" 或是 "RemoteOnly“
• trace
• enable = "false"
一般 Web Config 建議設定
• 一定要移除!!
• Server - 表示當前的 web server 版本
• IIS 10 之後需要改用 URL Rewrite Module 移除
• X-Powered-By - 標示當前用的開發技術 "powered by ASP.NET.“
<customHeaders>
<remove name="X-Powered-By" />
</customHeaders>
• X-AspNet-Version - 標示當前的 ASP.NET 版本
<httpRuntime targetFramework="4.6.2" enableVersionHeader="false" />
• X-AspNetMvc-Version
• 在 Global.asax 內移除
MvcHandler.DisableMvcResponseHeader = true;
ASP.NET 需要特別移除的預設標頭
• 防禦 XSS (Cross Site Scripting):
• Content-Security-Policy
• Set-Cookie: HttpOnly
• X-XSS-Protection
• X-Download-Options
• 防禦 Clickjacking:
• X-Frame-Options
• 強化 HTTPS 機制:
• Set-Cookie: Secure
• Strict-Transport-Security
• 避免瀏覽器誤判文件形態:
• X-Content-Type-Options
• 保護網站資源別被任意存取:
• Access-Control-Allow-Origin(此 header 若設定錯誤會適得其反!)
• X-Permitted-Cross-Domain-Policies
其他常見標頭防護
https://securityheaders.com/
1. 取得憑證。
2. 產生私密金鑰(安裝到 IIS 的憑證必須是 *.pfx 的檔案)
• OpenSSL 來產生 *.pfx
openssl pkcs12 -export -in certificate.crt -inkey private.key -certfile ca_bundle.crt -out
sample.pfx
3. 憑證放入IIS並繫結網域
• 打開 IIS,選取 IIS 站台首頁。
• 找到伺服器憑證。
• 在右變的動作列點選匯入。
• 載入預先做好的 PFX 憑證檔。
• 輸入PFX 憑證檔的密碼後即可完成
• 綁定 HTTPS 的站台。
• 輸入網域名稱。
• 選擇 SSL 憑證。
幫 IIS 設定 SSL 連線
預防DoS方法
• 設定 MaxRequestLength
• Request filter 過濾特定請求
• URL Rewrite 過濾請求
• 動態 IP 限制
• MaxRequestLength 限制
• 選用偏小的 4MB 預設值。僅有上傳檔案需求時才手動調大
<httpRuntime maxRequestLength="4096"
executionTimeout="300"/>
設定 MaxRequestLength 過濾惡意請求
• 可以在核心層次對所有流入IIS的HTTP資料流進行條件過濾篩選,支援下
列格式的篩選:
• 副檔名:可針對特定的副檔名進行封鎖,不允許用戶端瀏覽。
• 規則:可設定針對特定副檔名進行要求的標頭過濾,若發現有符合拒絕的內容時,
即將要求擋下。
• l隱藏區段:可設定網站內的特定資料夾 (或特定的路徑字串),在發現要求中包含
了這些字串時,將要求擋下。
• URL:針對特定的URL序列進行封鎖,或是允許特定的URL進入。
• 指令動詞:針對要求所使用的動詞 (GET/POST或其他) 進行過濾。
• 查詢字串:針對查詢字串的內容進行過濾。
Request filter 過濾特定請求
• 要在 IIS 安裝 URL Rewrite 模組,可以從 IIS 官方下載安裝檔。
• 支援五種 URL Rewriting 的設定:
• 空白規則:由管理人員自行設定 URL Rewriting 的規則,這個功能的彈性是最大
的。
• 易記 URL:由管理人員利用內建的 URL Rewriting 範本來建立親民的URL。
• 具重寫對應的規則:Rewrite Map 是一種規則定義的集合,這個功能可以直接利
用現有的 Rewrite Map 來設定重寫的規則。
• 要求封鎖:由管理人員設定 URL Rewriting 的規則,只要符合就擲回 HTTP 的錯
誤狀態碼。
• 強制小寫URL:在輸出時強制將URL轉換成小寫字元,以強化網站的SEO。
URL Rewrite 過濾惡意請求
Blacklist 與 Whitelist
http://www.hanselman.com/blog/BlockingImageHotlinkingLeechingAndEvilSploggersWithIISUrlRewrite.aspx
動態 IP 限制
欄位 說明
Deny IP address based on the number of concurrent requests
Maximum number of concurrent requests
設定用戶端的最大並行要求,若超出時執
行Action Type所指定的動作。
Deny IP addresses based on number of requests over a period of time
Maximum number of requests 設定要求允許進入IIS的最大數量。
Time period (in seconds) 設定要求最大數量的監測週期時間。
Deny for time period (in seconds)
設定超出要求最大數量時,封鎖要求的時
間週期。
Deny Action
Action type 當發生必須封鎖的動作時,要執行的動作。
MSSQL 安全設定
PersistSecurityInfo
• 可在SQL連線建立後,迅速清除記憶體中連接字串的密碼資訊
• SqlConnection.open()
• PersistSecurityInfo預設是false
• 連線資訊是在 SqlConnection 類別的 ConnectionString 屬性或
DbConnection 類別的 ConnectionString 屬性中
<connectionStrings>
<add name="BooDB1" connectionString="Data Source=BooDB1; persist security info=True; initial
catalog= Boo; user id=chris;password=bbbb1" providerName="System.Data.SqlClient" />
</connectionStrings>
MSSQL 連線透過 SSL 加密
1. 取得憑證並匯入主機(必須使用 makecert.exe 去建立)
2. 設定完需要重新啟動 MSSQL 主機
3. 連線會改為TLS
4. 客戶端Web.config連線字串加上 Encrypt=yes
https://support.microsoft.com/en-us/help/316898/how-to-enable-ssl-encryption-for-an-instance-of-sql-server-by-using-mi

More Related Content

What's hot

运维安全 抵抗黑客攻击_云络安全沙龙4月上海站主题分享
运维安全 抵抗黑客攻击_云络安全沙龙4月上海站主题分享运维安全 抵抗黑客攻击_云络安全沙龙4月上海站主题分享
运维安全 抵抗黑客攻击_云络安全沙龙4月上海站主题分享ChinaNetCloud
 
1116 Windows server 2008 - 使用 IIS 7.0 建置安全站台
1116 Windows server 2008 - 使用 IIS 7.0 建置安全站台1116 Windows server 2008 - 使用 IIS 7.0 建置安全站台
1116 Windows server 2008 - 使用 IIS 7.0 建置安全站台Timothy Chen
 
雲端上的資訊安全-Global Azure Bootcamp 2015 臺北場
雲端上的資訊安全-Global Azure Bootcamp 2015 臺北場雲端上的資訊安全-Global Azure Bootcamp 2015 臺北場
雲端上的資訊安全-Global Azure Bootcamp 2015 臺北場twMVC
 
Android Taipei 2013 August - Android Apps Security
Android Taipei 2013 August - Android Apps SecurityAndroid Taipei 2013 August - Android Apps Security
Android Taipei 2013 August - Android Apps SecurityTaien Wang
 
OWST - Orange Web Security Toolkit Documentation
OWST - Orange Web Security Toolkit DocumentationOWST - Orange Web Security Toolkit Documentation
OWST - Orange Web Security Toolkit DocumentationOrange Tsai
 
雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場
雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場
雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場twMVC
 
用戶端攻擊與防禦
用戶端攻擊與防禦用戶端攻擊與防禦
用戶端攻擊與防禦Taien Wang
 
一個微信專案從0到000的效能調教
一個微信專案從0到000的效能調教一個微信專案從0到000的效能調教
一個微信專案從0到000的效能調教Bruce Chen
 
微軟實戰課程日:玩轉雲端 技術與架構
微軟實戰課程日:玩轉雲端 技術與架構微軟實戰課程日:玩轉雲端 技術與架構
微軟實戰課程日:玩轉雲端 技術與架構Jeff Chu
 
伺服器端攻擊與防禦II
伺服器端攻擊與防禦II伺服器端攻擊與防禦II
伺服器端攻擊與防禦IITaien Wang
 
Azure mobileservice
Azure mobileservice Azure mobileservice
Azure mobileservice twMVC
 
6.web 安全架构浅谈
6.web 安全架构浅谈6.web 安全架构浅谈
6.web 安全架构浅谈Hsiao Tim
 
淡江大學 - 產品測試+安全性測試+壓力測試
淡江大學 - 產品測試+安全性測試+壓力測試淡江大學 - 產品測試+安全性測試+壓力測試
淡江大學 - 產品測試+安全性測試+壓力測試Taien Wang
 
ASP.NET 開發人員不可不知的 IIS (IIS for ASP.NET Developers)
ASP.NET 開發人員不可不知的 IIS (IIS for ASP.NET Developers)ASP.NET 開發人員不可不知的 IIS (IIS for ASP.NET Developers)
ASP.NET 開發人員不可不知的 IIS (IIS for ASP.NET Developers)Jeff Chu
 
基礎網頁程式攻擊檢驗
基礎網頁程式攻擊檢驗基礎網頁程式攻擊檢驗
基礎網頁程式攻擊檢驗Taien Wang
 
Windows 與 Azure 的容器旅程 @ Ignite Mini 2016
Windows 與 Azure 的容器旅程 @ Ignite Mini 2016Windows 與 Azure 的容器旅程 @ Ignite Mini 2016
Windows 與 Azure 的容器旅程 @ Ignite Mini 2016Jeff Chu
 
Microsoft Azure的20大常见安全漏洞与配置错误
Microsoft Azure的20大常见安全漏洞与配置错误Microsoft Azure的20大常见安全漏洞与配置错误
Microsoft Azure的20大常见安全漏洞与配置错误Cheah Eng Soon
 
使安全成為軟體開發必要部分
使安全成為軟體開發必要部分使安全成為軟體開發必要部分
使安全成為軟體開發必要部分Taien Wang
 
伺服器端攻擊與防禦III
伺服器端攻擊與防禦III伺服器端攻擊與防禦III
伺服器端攻擊與防禦IIITaien Wang
 
WebConf 2013「Best Practices - The Upload」
WebConf 2013「Best Practices - The Upload」WebConf 2013「Best Practices - The Upload」
WebConf 2013「Best Practices - The Upload」Orange Tsai
 

What's hot (20)

运维安全 抵抗黑客攻击_云络安全沙龙4月上海站主题分享
运维安全 抵抗黑客攻击_云络安全沙龙4月上海站主题分享运维安全 抵抗黑客攻击_云络安全沙龙4月上海站主题分享
运维安全 抵抗黑客攻击_云络安全沙龙4月上海站主题分享
 
1116 Windows server 2008 - 使用 IIS 7.0 建置安全站台
1116 Windows server 2008 - 使用 IIS 7.0 建置安全站台1116 Windows server 2008 - 使用 IIS 7.0 建置安全站台
1116 Windows server 2008 - 使用 IIS 7.0 建置安全站台
 
雲端上的資訊安全-Global Azure Bootcamp 2015 臺北場
雲端上的資訊安全-Global Azure Bootcamp 2015 臺北場雲端上的資訊安全-Global Azure Bootcamp 2015 臺北場
雲端上的資訊安全-Global Azure Bootcamp 2015 臺北場
 
Android Taipei 2013 August - Android Apps Security
Android Taipei 2013 August - Android Apps SecurityAndroid Taipei 2013 August - Android Apps Security
Android Taipei 2013 August - Android Apps Security
 
OWST - Orange Web Security Toolkit Documentation
OWST - Orange Web Security Toolkit DocumentationOWST - Orange Web Security Toolkit Documentation
OWST - Orange Web Security Toolkit Documentation
 
雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場
雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場
雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場
 
用戶端攻擊與防禦
用戶端攻擊與防禦用戶端攻擊與防禦
用戶端攻擊與防禦
 
一個微信專案從0到000的效能調教
一個微信專案從0到000的效能調教一個微信專案從0到000的效能調教
一個微信專案從0到000的效能調教
 
微軟實戰課程日:玩轉雲端 技術與架構
微軟實戰課程日:玩轉雲端 技術與架構微軟實戰課程日:玩轉雲端 技術與架構
微軟實戰課程日:玩轉雲端 技術與架構
 
伺服器端攻擊與防禦II
伺服器端攻擊與防禦II伺服器端攻擊與防禦II
伺服器端攻擊與防禦II
 
Azure mobileservice
Azure mobileservice Azure mobileservice
Azure mobileservice
 
6.web 安全架构浅谈
6.web 安全架构浅谈6.web 安全架构浅谈
6.web 安全架构浅谈
 
淡江大學 - 產品測試+安全性測試+壓力測試
淡江大學 - 產品測試+安全性測試+壓力測試淡江大學 - 產品測試+安全性測試+壓力測試
淡江大學 - 產品測試+安全性測試+壓力測試
 
ASP.NET 開發人員不可不知的 IIS (IIS for ASP.NET Developers)
ASP.NET 開發人員不可不知的 IIS (IIS for ASP.NET Developers)ASP.NET 開發人員不可不知的 IIS (IIS for ASP.NET Developers)
ASP.NET 開發人員不可不知的 IIS (IIS for ASP.NET Developers)
 
基礎網頁程式攻擊檢驗
基礎網頁程式攻擊檢驗基礎網頁程式攻擊檢驗
基礎網頁程式攻擊檢驗
 
Windows 與 Azure 的容器旅程 @ Ignite Mini 2016
Windows 與 Azure 的容器旅程 @ Ignite Mini 2016Windows 與 Azure 的容器旅程 @ Ignite Mini 2016
Windows 與 Azure 的容器旅程 @ Ignite Mini 2016
 
Microsoft Azure的20大常见安全漏洞与配置错误
Microsoft Azure的20大常见安全漏洞与配置错误Microsoft Azure的20大常见安全漏洞与配置错误
Microsoft Azure的20大常见安全漏洞与配置错误
 
使安全成為軟體開發必要部分
使安全成為軟體開發必要部分使安全成為軟體開發必要部分
使安全成為軟體開發必要部分
 
伺服器端攻擊與防禦III
伺服器端攻擊與防禦III伺服器端攻擊與防禦III
伺服器端攻擊與防禦III
 
WebConf 2013「Best Practices - The Upload」
WebConf 2013「Best Practices - The Upload」WebConf 2013「Best Practices - The Upload」
WebConf 2013「Best Practices - The Upload」
 

Similar to .NET Security Application/Web Development - Part III

发布系统安装说明
发布系统安装说明发布系统安装说明
发布系统安装说明Yiwei Ma
 
應用程式部署
應用程式部署應用程式部署
應用程式部署Shengyou Fan
 
DEV305 - ASP.NET 5 開發攻略
DEV305 - ASP.NET 5 開發攻略DEV305 - ASP.NET 5 開發攻略
DEV305 - ASP.NET 5 開發攻略Will Huang
 
V mware v shield - 部署最安全云环境的基础
V mware v shield - 部署最安全云环境的基础V mware v shield - 部署最安全云环境的基础
V mware v shield - 部署最安全云环境的基础ITband
 
Helix安装说明
Helix安装说明Helix安装说明
Helix安装说明Yiwei Ma
 
第14章 创建与管理www服务
第14章 创建与管理www服务第14章 创建与管理www服务
第14章 创建与管理www服务ben1024
 
第14章 创建与管理www服务
第14章 创建与管理www服务第14章 创建与管理www服务
第14章 创建与管理www服务ben1024
 
Google雲端備份與虛擬伺服器建置工作坊
Google雲端備份與虛擬伺服器建置工作坊Google雲端備份與虛擬伺服器建置工作坊
Google雲端備份與虛擬伺服器建置工作坊Chung-Yi Huang
 
Windows Azure Developer Day - WAMS
Windows Azure Developer Day - WAMSWindows Azure Developer Day - WAMS
Windows Azure Developer Day - WAMSJeffray Huang
 
.NET Conf Taiwan 2022 - Tauri - 前端人員也能打造小巧快速的 Windows 應用程式
.NET Conf Taiwan 2022 - Tauri -前端人員也能打造小巧快速的 Windows 應用程式.NET Conf Taiwan 2022 - Tauri -前端人員也能打造小巧快速的 Windows 應用程式
.NET Conf Taiwan 2022 - Tauri - 前端人員也能打造小巧快速的 Windows 應用程式升煌 黃
 
BAS010_虛擬化基礎_v190330
BAS010_虛擬化基礎_v190330BAS010_虛擬化基礎_v190330
BAS010_虛擬化基礎_v190330rwp99346
 
BAS010_虛擬化基礎_v190330 (View online)
BAS010_虛擬化基礎_v190330 (View online)BAS010_虛擬化基礎_v190330 (View online)
BAS010_虛擬化基礎_v190330 (View online)rwp99346
 
0328 Windows Server 2008 應用程式相容性
0328 Windows Server 2008 應用程式相容性0328 Windows Server 2008 應用程式相容性
0328 Windows Server 2008 應用程式相容性Timothy Chen
 
Getting Started with Serverless Architecture - 深入淺出無伺服器架構應用程式
Getting Started with Serverless Architecture - 深入淺出無伺服器架構應用程式Getting Started with Serverless Architecture - 深入淺出無伺服器架構應用程式
Getting Started with Serverless Architecture - 深入淺出無伺服器架構應用程式Amazon Web Services
 
使用Nginx轻松实现开源负载均衡
使用Nginx轻松实现开源负载均衡使用Nginx轻松实现开源负载均衡
使用Nginx轻松实现开源负载均衡cachowu
 
使用Nginx轻松实现开源负载均衡——对外版
使用Nginx轻松实现开源负载均衡——对外版使用Nginx轻松实现开源负载均衡——对外版
使用Nginx轻松实现开源负载均衡——对外版pigso
 
PHP.on.Windows.Overview.CHS
PHP.on.Windows.Overview.CHSPHP.on.Windows.Overview.CHS
PHP.on.Windows.Overview.CHScqwang
 
Windows Azure Virtual Machine Services for Developers
Windows Azure Virtual Machine Services for DevelopersWindows Azure Virtual Machine Services for Developers
Windows Azure Virtual Machine Services for DevelopersJeff Chu
 
1107 Windows server 2008 - IIS 7.0 架構與建置
1107 Windows server 2008 - IIS 7.0 架構與建置1107 Windows server 2008 - IIS 7.0 架構與建置
1107 Windows server 2008 - IIS 7.0 架構與建置Timothy Chen
 

Similar to .NET Security Application/Web Development - Part III (20)

发布系统安装说明
发布系统安装说明发布系统安装说明
发布系统安装说明
 
應用程式部署
應用程式部署應用程式部署
應用程式部署
 
DEV305 - ASP.NET 5 開發攻略
DEV305 - ASP.NET 5 開發攻略DEV305 - ASP.NET 5 開發攻略
DEV305 - ASP.NET 5 開發攻略
 
V mware v shield - 部署最安全云环境的基础
V mware v shield - 部署最安全云环境的基础V mware v shield - 部署最安全云环境的基础
V mware v shield - 部署最安全云环境的基础
 
Helix安装说明
Helix安装说明Helix安装说明
Helix安装说明
 
第14章 创建与管理www服务
第14章 创建与管理www服务第14章 创建与管理www服务
第14章 创建与管理www服务
 
第14章 创建与管理www服务
第14章 创建与管理www服务第14章 创建与管理www服务
第14章 创建与管理www服务
 
Google雲端備份與虛擬伺服器建置工作坊
Google雲端備份與虛擬伺服器建置工作坊Google雲端備份與虛擬伺服器建置工作坊
Google雲端備份與虛擬伺服器建置工作坊
 
Windows Azure Developer Day - WAMS
Windows Azure Developer Day - WAMSWindows Azure Developer Day - WAMS
Windows Azure Developer Day - WAMS
 
1~60
1~601~60
1~60
 
.NET Conf Taiwan 2022 - Tauri - 前端人員也能打造小巧快速的 Windows 應用程式
.NET Conf Taiwan 2022 - Tauri -前端人員也能打造小巧快速的 Windows 應用程式.NET Conf Taiwan 2022 - Tauri -前端人員也能打造小巧快速的 Windows 應用程式
.NET Conf Taiwan 2022 - Tauri - 前端人員也能打造小巧快速的 Windows 應用程式
 
BAS010_虛擬化基礎_v190330
BAS010_虛擬化基礎_v190330BAS010_虛擬化基礎_v190330
BAS010_虛擬化基礎_v190330
 
BAS010_虛擬化基礎_v190330 (View online)
BAS010_虛擬化基礎_v190330 (View online)BAS010_虛擬化基礎_v190330 (View online)
BAS010_虛擬化基礎_v190330 (View online)
 
0328 Windows Server 2008 應用程式相容性
0328 Windows Server 2008 應用程式相容性0328 Windows Server 2008 應用程式相容性
0328 Windows Server 2008 應用程式相容性
 
Getting Started with Serverless Architecture - 深入淺出無伺服器架構應用程式
Getting Started with Serverless Architecture - 深入淺出無伺服器架構應用程式Getting Started with Serverless Architecture - 深入淺出無伺服器架構應用程式
Getting Started with Serverless Architecture - 深入淺出無伺服器架構應用程式
 
使用Nginx轻松实现开源负载均衡
使用Nginx轻松实现开源负载均衡使用Nginx轻松实现开源负载均衡
使用Nginx轻松实现开源负载均衡
 
使用Nginx轻松实现开源负载均衡——对外版
使用Nginx轻松实现开源负载均衡——对外版使用Nginx轻松实现开源负载均衡——对外版
使用Nginx轻松实现开源负载均衡——对外版
 
PHP.on.Windows.Overview.CHS
PHP.on.Windows.Overview.CHSPHP.on.Windows.Overview.CHS
PHP.on.Windows.Overview.CHS
 
Windows Azure Virtual Machine Services for Developers
Windows Azure Virtual Machine Services for DevelopersWindows Azure Virtual Machine Services for Developers
Windows Azure Virtual Machine Services for Developers
 
1107 Windows server 2008 - IIS 7.0 架構與建置
1107 Windows server 2008 - IIS 7.0 架構與建置1107 Windows server 2008 - IIS 7.0 架構與建置
1107 Windows server 2008 - IIS 7.0 架構與建置
 

More from Chen-Tien Tsai

關於軟體工程師職涯的那些事
關於軟體工程師職涯的那些事關於軟體工程師職涯的那些事
關於軟體工程師職涯的那些事Chen-Tien Tsai
 
Designing distributedsystems cht6
Designing distributedsystems cht6Designing distributedsystems cht6
Designing distributedsystems cht6Chen-Tien Tsai
 
Reactive application with akka.NET & .NET Core
Reactive application with akka.NET & .NET CoreReactive application with akka.NET & .NET Core
Reactive application with akka.NET & .NET CoreChen-Tien Tsai
 
The Cloud - What's different
The Cloud - What's differentThe Cloud - What's different
The Cloud - What's differentChen-Tien Tsai
 
How to be a professional speaker
How to be a professional speakerHow to be a professional speaker
How to be a professional speakerChen-Tien Tsai
 
響應式程式開發之 .NET Core 應用 
響應式程式開發之 .NET Core 應用 響應式程式開發之 .NET Core 應用 
響應式程式開發之 .NET Core 應用 Chen-Tien Tsai
 
Artifacts management with DevOps
Artifacts management with DevOpsArtifacts management with DevOps
Artifacts management with DevOpsChen-Tien Tsai
 
Web optimization with service woker
Web optimization with service wokerWeb optimization with service woker
Web optimization with service wokerChen-Tien Tsai
 
GCPUG.TW Meetup #25 - ASP.NET Core with GCP
GCPUG.TW Meetup #25 - ASP.NET Core with GCPGCPUG.TW Meetup #25 - ASP.NET Core with GCP
GCPUG.TW Meetup #25 - ASP.NET Core with GCPChen-Tien Tsai
 
.NET Study Group - ASP.NET Core with GCP
.NET Study Group - ASP.NET Core with GCP.NET Study Group - ASP.NET Core with GCP
.NET Study Group - ASP.NET Core with GCPChen-Tien Tsai
 
Webpack and Web Performance Optimization
Webpack and Web Performance OptimizationWebpack and Web Performance Optimization
Webpack and Web Performance OptimizationChen-Tien Tsai
 
DotNet MVC and webpack + Babel + react
DotNet MVC and webpack + Babel + reactDotNet MVC and webpack + Babel + react
DotNet MVC and webpack + Babel + reactChen-Tien Tsai
 
Website Auto scraping with Autoit and .Net HttpRequest
Website Auto scraping with Autoit and .Net HttpRequestWebsite Auto scraping with Autoit and .Net HttpRequest
Website Auto scraping with Autoit and .Net HttpRequestChen-Tien Tsai
 
C# 2 to 5 short Introduction
C# 2 to 5 short IntroductionC# 2 to 5 short Introduction
C# 2 to 5 short IntroductionChen-Tien Tsai
 
DevOps for dummies study sharing - part II
DevOps for dummies study sharing - part IIDevOps for dummies study sharing - part II
DevOps for dummies study sharing - part IIChen-Tien Tsai
 
Git essential training & sharing self
Git essential training & sharing selfGit essential training & sharing self
Git essential training & sharing selfChen-Tien Tsai
 
How to make a Load Testing with Visual Studio 2012
How to make a Load Testing with Visual Studio 2012How to make a Load Testing with Visual Studio 2012
How to make a Load Testing with Visual Studio 2012Chen-Tien Tsai
 

More from Chen-Tien Tsai (20)

關於軟體工程師職涯的那些事
關於軟體工程師職涯的那些事關於軟體工程師職涯的那些事
關於軟體工程師職涯的那些事
 
Designing distributedsystems cht6
Designing distributedsystems cht6Designing distributedsystems cht6
Designing distributedsystems cht6
 
Reactive application with akka.NET & .NET Core
Reactive application with akka.NET & .NET CoreReactive application with akka.NET & .NET Core
Reactive application with akka.NET & .NET Core
 
The Cloud - What's different
The Cloud - What's differentThe Cloud - What's different
The Cloud - What's different
 
How to be a professional speaker
How to be a professional speakerHow to be a professional speaker
How to be a professional speaker
 
Agile tutorial
Agile tutorialAgile tutorial
Agile tutorial
 
響應式程式開發之 .NET Core 應用 
響應式程式開發之 .NET Core 應用 響應式程式開發之 .NET Core 應用 
響應式程式開發之 .NET Core 應用 
 
Artifacts management with DevOps
Artifacts management with DevOpsArtifacts management with DevOps
Artifacts management with DevOps
 
Web optimization with service woker
Web optimization with service wokerWeb optimization with service woker
Web optimization with service woker
 
GCPUG.TW Meetup #25 - ASP.NET Core with GCP
GCPUG.TW Meetup #25 - ASP.NET Core with GCPGCPUG.TW Meetup #25 - ASP.NET Core with GCP
GCPUG.TW Meetup #25 - ASP.NET Core with GCP
 
.NET Study Group - ASP.NET Core with GCP
.NET Study Group - ASP.NET Core with GCP.NET Study Group - ASP.NET Core with GCP
.NET Study Group - ASP.NET Core with GCP
 
Webpack and Web Performance Optimization
Webpack and Web Performance OptimizationWebpack and Web Performance Optimization
Webpack and Web Performance Optimization
 
DotNet MVC and webpack + Babel + react
DotNet MVC and webpack + Babel + reactDotNet MVC and webpack + Babel + react
DotNet MVC and webpack + Babel + react
 
Website Auto scraping with Autoit and .Net HttpRequest
Website Auto scraping with Autoit and .Net HttpRequestWebsite Auto scraping with Autoit and .Net HttpRequest
Website Auto scraping with Autoit and .Net HttpRequest
 
C# 2 to 5 short Introduction
C# 2 to 5 short IntroductionC# 2 to 5 short Introduction
C# 2 to 5 short Introduction
 
Docker - fundamental
Docker  - fundamentalDocker  - fundamental
Docker - fundamental
 
DevOps for dummies study sharing - part II
DevOps for dummies study sharing - part IIDevOps for dummies study sharing - part II
DevOps for dummies study sharing - part II
 
Redis tutoring
Redis tutoringRedis tutoring
Redis tutoring
 
Git essential training & sharing self
Git essential training & sharing selfGit essential training & sharing self
Git essential training & sharing self
 
How to make a Load Testing with Visual Studio 2012
How to make a Load Testing with Visual Studio 2012How to make a Load Testing with Visual Studio 2012
How to make a Load Testing with Visual Studio 2012
 

.NET Security Application/Web Development - Part III

  • 1. .NET 安全應用程式開發 – 設定與調校 .NET Security Application Development – Part III Blackie Tsai blackie1019@gmail.com August 2018
  • 5. • w3wp.exe 使用的帳號與IIS版本有關 • 在 Microsoft Windows 2000 和 Windows XP Professional 上 • 是本機 ASPNET 帳戶。 • 在 IIS6 或 IIS7 的傳統模式下 • 預設是 NETWORK SERVICE 帳戶。 • 在 IIS7 整合管線模式下 • 取得的身份識別預設是所屬之 IIS 應用程式集區的識別,所以不同的應用程式 集區會有不同的識別,如 IIS AppPoolDefaultAppPool . • IIS 7.5之後 • w3wp.exe(IIS的Process)預設都是使用IIS AppPools<App Pool Name>這個 虛擬使用者帳號來執行 • 若不喜歡預設的執行識別,也可以變更應用程式集區的執行識別 Windows Process 與 IIS AppPool 權限設定
  • 7. • AppPool 的識別設定為 ApplicationPoolIdentity • 執行識別是 IIS APPPOOL[AppPoolName • 管線模式下的預設值 • AppPool 的識別設定為 NetworkService NetworkService • 可以指定特定的帳戶。 • 【IIS APPPOOL[AppPoolName】這是一個虛擬帳戶,若要對特定資料夾存取, 要注意其存取權限, AppPool 的識別設定
  • 8. 匿名存取、表單驗證與Windows驗證 • 匿名存取 • 網站資料夾的最少授權對象為NT AUTHORITYIUSR及IIS APPPOOLYourPoolName; • 預設使用匿名存取 • 表單驗證 • 同上,網站資料夾的最少授權對象為NT AUTHORITYIUSR及IIS APPPOOLYourPoolName; • 利用ASP.NET當中自訂的登入表單,搭配MemberShip資料庫驗證使用者身分。 • Windows 驗證 • 利用IIS驗證使用者的Windows帳戶,然後存取ASP.NET應用程式。 • 除IIS APPPOOLYourPoolName 外要再指定為Authenticated Users群組、 Domain Users群組,或是可登入使用者所屬的特定群組三者之一。 • Windows Server 2016 開始要自行安裝該模組才可啟用
  • 9. • 網站需要寫入的情境 • 如果有特別指定應用程式集區,則網站資料夾的安全性要指定 IIS AppPool站台 的應用程式集區 可以寫入 • 如果用預設的 DefaultAppPool 則要指定 IIS_IUSRS 可以寫入 從使用需求了解應用程式所需要權限
  • 10. • httpCookies • httpOnlyCookies = "true" • requireSSL = "true" • sessionState • cookieless="true" • regenerateExpiredSessionId="true" • compilation • debug = "false" • httpRuntime • enableHeaderChecking = "true" • enableVersionHeader= "false" • customErrors • mode = "On" 或是 "RemoteOnly“ • trace • enable = "false" 一般 Web Config 建議設定
  • 11. • 一定要移除!! • Server - 表示當前的 web server 版本 • IIS 10 之後需要改用 URL Rewrite Module 移除 • X-Powered-By - 標示當前用的開發技術 "powered by ASP.NET.“ <customHeaders> <remove name="X-Powered-By" /> </customHeaders> • X-AspNet-Version - 標示當前的 ASP.NET 版本 <httpRuntime targetFramework="4.6.2" enableVersionHeader="false" /> • X-AspNetMvc-Version • 在 Global.asax 內移除 MvcHandler.DisableMvcResponseHeader = true; ASP.NET 需要特別移除的預設標頭
  • 12. • 防禦 XSS (Cross Site Scripting): • Content-Security-Policy • Set-Cookie: HttpOnly • X-XSS-Protection • X-Download-Options • 防禦 Clickjacking: • X-Frame-Options • 強化 HTTPS 機制: • Set-Cookie: Secure • Strict-Transport-Security • 避免瀏覽器誤判文件形態: • X-Content-Type-Options • 保護網站資源別被任意存取: • Access-Control-Allow-Origin(此 header 若設定錯誤會適得其反!) • X-Permitted-Cross-Domain-Policies 其他常見標頭防護 https://securityheaders.com/
  • 13. 1. 取得憑證。 2. 產生私密金鑰(安裝到 IIS 的憑證必須是 *.pfx 的檔案) • OpenSSL 來產生 *.pfx openssl pkcs12 -export -in certificate.crt -inkey private.key -certfile ca_bundle.crt -out sample.pfx 3. 憑證放入IIS並繫結網域 • 打開 IIS,選取 IIS 站台首頁。 • 找到伺服器憑證。 • 在右變的動作列點選匯入。 • 載入預先做好的 PFX 憑證檔。 • 輸入PFX 憑證檔的密碼後即可完成 • 綁定 HTTPS 的站台。 • 輸入網域名稱。 • 選擇 SSL 憑證。 幫 IIS 設定 SSL 連線
  • 14. 預防DoS方法 • 設定 MaxRequestLength • Request filter 過濾特定請求 • URL Rewrite 過濾請求 • 動態 IP 限制
  • 15. • MaxRequestLength 限制 • 選用偏小的 4MB 預設值。僅有上傳檔案需求時才手動調大 <httpRuntime maxRequestLength="4096" executionTimeout="300"/> 設定 MaxRequestLength 過濾惡意請求
  • 16. • 可以在核心層次對所有流入IIS的HTTP資料流進行條件過濾篩選,支援下 列格式的篩選: • 副檔名:可針對特定的副檔名進行封鎖,不允許用戶端瀏覽。 • 規則:可設定針對特定副檔名進行要求的標頭過濾,若發現有符合拒絕的內容時, 即將要求擋下。 • l隱藏區段:可設定網站內的特定資料夾 (或特定的路徑字串),在發現要求中包含 了這些字串時,將要求擋下。 • URL:針對特定的URL序列進行封鎖,或是允許特定的URL進入。 • 指令動詞:針對要求所使用的動詞 (GET/POST或其他) 進行過濾。 • 查詢字串:針對查詢字串的內容進行過濾。 Request filter 過濾特定請求
  • 17. • 要在 IIS 安裝 URL Rewrite 模組,可以從 IIS 官方下載安裝檔。 • 支援五種 URL Rewriting 的設定: • 空白規則:由管理人員自行設定 URL Rewriting 的規則,這個功能的彈性是最大 的。 • 易記 URL:由管理人員利用內建的 URL Rewriting 範本來建立親民的URL。 • 具重寫對應的規則:Rewrite Map 是一種規則定義的集合,這個功能可以直接利 用現有的 Rewrite Map 來設定重寫的規則。 • 要求封鎖:由管理人員設定 URL Rewriting 的規則,只要符合就擲回 HTTP 的錯 誤狀態碼。 • 強制小寫URL:在輸出時強制將URL轉換成小寫字元,以強化網站的SEO。 URL Rewrite 過濾惡意請求
  • 19. 動態 IP 限制 欄位 說明 Deny IP address based on the number of concurrent requests Maximum number of concurrent requests 設定用戶端的最大並行要求,若超出時執 行Action Type所指定的動作。 Deny IP addresses based on number of requests over a period of time Maximum number of requests 設定要求允許進入IIS的最大數量。 Time period (in seconds) 設定要求最大數量的監測週期時間。 Deny for time period (in seconds) 設定超出要求最大數量時,封鎖要求的時 間週期。 Deny Action Action type 當發生必須封鎖的動作時,要執行的動作。
  • 21. PersistSecurityInfo • 可在SQL連線建立後,迅速清除記憶體中連接字串的密碼資訊 • SqlConnection.open() • PersistSecurityInfo預設是false • 連線資訊是在 SqlConnection 類別的 ConnectionString 屬性或 DbConnection 類別的 ConnectionString 屬性中 <connectionStrings> <add name="BooDB1" connectionString="Data Source=BooDB1; persist security info=True; initial catalog= Boo; user id=chris;password=bbbb1" providerName="System.Data.SqlClient" /> </connectionStrings>
  • 22. MSSQL 連線透過 SSL 加密 1. 取得憑證並匯入主機(必須使用 makecert.exe 去建立) 2. 設定完需要重新啟動 MSSQL 主機 3. 連線會改為TLS 4. 客戶端Web.config連線字串加上 Encrypt=yes https://support.microsoft.com/en-us/help/316898/how-to-enable-ssl-encryption-for-an-instance-of-sql-server-by-using-mi

Editor's Notes

  1. Part III (1 hr, 60 pages) IIS & Security Web Development(30 mins) SQL Server Configuration(15 mins)
  2.  IIS 7.5 是為了避免 NETWORK SERVICE 的服務被木馬病毒或駭客影響而淪陷的話,會連帶影響 IIS的運作。因此 IIS 7.5 新增了「虛擬帳戶」的概念.,不同的站台服務可以不必再共用 NETWORK SERVICE的身份,可以大大將安全性提高
  3. http://vito-note.blogspot.com/2013/04/windows.html
  4. IIS7設定站台資料夾的”寫入”權限: 如果站台有”特別指定應用程式集區”, 則網站資料夾的安全性要指定 “IIS AppPool\站台的應用程式集區” 可以寫入 如果站台沒有指定,是用預設的DefaultAppPool 則網站資料夾的安全性要指定 “IIS_IUSRS” 可以寫入 (因為IIS_IUSRS群組已經包括DefaultAppPool帳戶)
  5. httpCookies httpOnlyCookies = true 這個值表示 Cookie 只會在透過網路 http 時被傳輸,換句話說,程式 JavaScript 的讀取,該 cookie 不會被讀取 requireSSL = true 這個值表示只有在 SSL 也就是 https 的情況下才會傳輸 cookie 給 Web Server 端。 SSL 會強制將雙方通訊的內容加密,因此可以避免 man in the Middle 的攻擊 sessionState 設定如果多久沒有用就必須 timeout。“regenerateExpiredSessionId” 主要是用來重新產生 Session ID 避免駭客容易猜到,或是使用之前用過的 SessionID compilation 這個值 debug = true 時,就會把相關的 debug symbols 輸出到網頁錯誤頁面上。 造成太多的內部資訊讓有心的駭客知道。 httpRuntime enableHeaderChecking = true 這個值可以幫助 web Server 檢查基本的 Injection Attacks 或是 XSS (cross-site scripts) enableVersionHeader= false 如果設定為 enableVersionHeader= true. 則該網站會回傳過多的版本資訊給使用者。 間接讓駭客有機會查詢該版本已知的弱點進行攻擊。 customErrors On:啟用自訂錯誤,但若未指定defaultRedirect時,不管用戶端或本機都將顯示 ASP.NET 錯誤訊息畫面。 Off:停用自訂錯誤,不管用戶端或本機都將顯示 ASP.NET 錯誤訊息畫面。 RemoteOnly:只對遠端用戶端啟用自訂錯誤,本機端顯示 ASP.NET 錯誤訊息。 (預設值) trace Enable = false => 關閉 ASP.NET 應用程式的追蹤,注意是否會影響正常log
  6. https://stackoverflow.com/questions/3418557/how-to-remove-asp-net-mvc-default-http-headers <system.webServer> <httpProtocol> <customHeaders> <remove name="X-Powered-By" /> <add name="X-Frame-Options" value="SAMEORIGIN" /> <add name="X-XSS-Protection" value="1;mode=block" /> <add name="Referrer-Policy" value="strict-origin" /> <add name="Content-Security-Policy" value="default-src 'self';"/> <add name="X-Content-Type-Options" value="nosniff" /> <add name="Strict-Transport-Security" value="max-age=31536000"/> <add name="Feature-Policy" value="geolocation '*';notifications '*'" /> </customHeaders> </httpProtocol> <rewrite> <outboundRules> <rule name="Remove RESPONSE_Server" > <match serverVariable="RESPONSE_Server" pattern=".+" /> <action type="Rewrite" value="Blackie Demo" /> </rule> </outboundRules> </rewrite> <system.webServer>
  7. 在安裝 SSL 憑證之前,當然要先取得憑證囉~ SSL 憑證通常是花錢購買,但也有免費的可以用。 如果是網站要用的 SSL 憑證,免費跟付費的差異大概就是信任度、有效期、認證標章等。 至於加密技術跟安全性,大致上都依樣。 需要免費的 SSL 憑證,可以參考這篇:免費 SSL 申請
  8. https://msdn.microsoft.com/zh-tw/library/system.web.configuration.httpruntimesection.maxrequestlength(v=vs.110).aspx
  9. 若發現要求內有被封鎖或不符合規則的要求時,即會直接拒絕,不會讓它流進Web應用程式內,確保應用程式處於安全的環境內。
  10. http://www.hanselman.com/blog/BlockingImageHotlinkingLeechingAndEvilSploggersWithIISUrlRewrite.aspx
  11. http://blog.darkthread.net/post-2017-02-07-iis-dynamic-ip-restriction.aspx https://blogs.msdn.microsoft.com/friis/2014/12/30/security-guidelines-to-detect-and-prevent-dos-attacks-targeting-iisazure-web-role-paas/
  12. SQL Server 使用的 SSL 憑證,必須使用 makecert.exe 去建立,而且有一定的規範,用 IIS 去建立的自我簽署憑證是不能用的。