防止頁面被iframe惡意巢狀
新blog地址:http://hengyunabc.github.io/prevent-iframe-stealing/
緣起
在看資料時,看到這樣的防止iframe巢狀的程式碼:
try {
if (window.top != window.self) {
var ref = document.referer;
if (ref.substring(0, 2) === `//`) {
ref = `http:` + ref;
} else if (ref.split(`://`).length === 1) {
ref = `http://` + ref;
}
var url = ref.split(`/`);
var _l = {auth: ``};
var host = url[2].split(`@`);
if (host.length === 1) {
host = host[0].split(`:`);
} else {
_l.auth = host[0];
host = host[1].split(`:`);
}
var parentHostName = host[0];
if (parentHostName.indexOf("test.com") == -1 && parentHostName.indexOf("test2.com") == -1) {
top.location.href = "http://www.test.com";
}
}
} catch (e) {
}
假定test.com,test2.com是自己的域名,當其它網站惡意巢狀本站的頁面時,跳轉回本站的首頁。
上面的程式碼有兩個問題:
- referer拼寫錯誤,實際上應該是referrer
- 解析referrer的程式碼太複雜,還不一定正確
無論在任何語言裡,都不建議手工寫程式碼處理URL。因為url的複雜度超出一般人的想像。很多安全的問題就是因為解析URL不當引起的。比如防止CSRF時判斷referrer。
URI的語法:
http://en.wikipedia.org/wiki/URI_scheme#Generic_syntax
在javascript裡解析url最好的辦法
在javascript裡解析url的最好辦法是利用瀏覽器的js引擎,通過建立一個a標籤:
var getLocation = function(href) {
var l = document.createElement("a");
l.href = href;
return l;
};
var l = getLocation("http://example.com/path");
console.debug(l.hostname)
簡潔防iframe惡意巢狀的方法
下面給出一個簡潔的防止iframe惡意巢狀的判斷方法:
if(window.top != window && document.referrer){
var a = document.createElement("a");
a.href = document.referrer;
var host = a.hostname;
var endsWith = function (str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
if(!endsWith(host, `.test.com`) || !endsWith(host, `.test2.com`)){
top.location.href = "http://www.test.com";
}
}
java裡處理URL的方法
http://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html
用contain, indexOf, endWitch這些函式時都要小心。
public static void main(String[] args) throws Exception {
URL aURL = new URL("http://example.com:80/docs/books/tutorial"
+ "/index.html?name=networking#DOWNLOADING");
System.out.println("protocol = " + aURL.getProtocol());
System.out.println("authority = " + aURL.getAuthority());
System.out.println("host = " + aURL.getHost());
System.out.println("port = " + aURL.getPort());
System.out.println("path = " + aURL.getPath());
System.out.println("query = " + aURL.getQuery());
System.out.println("filename = " + aURL.getFile());
System.out.println("ref = " + aURL.getRef());
}
參考
http://stackoverflow.com/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript
http://stackoverflow.com/questions/5522097/prevent-iframe-stealing
相關文章
- 防止API被惡意呼叫API
- 網站被惡意篡改什麼原因?防止網頁惡意篡改的方法網站網頁
- 防止獨立IP被其它惡意域名惡意解析
- 頁面巢狀,介面套娃,除了用iframe,還有其他方式嗎?巢狀
- 如何防止伺服器被惡意網路攻擊?伺服器
- Spring Boot 防止介面被惡意重新整理、暴力請求Spring Boot
- 寶塔皮膚怎麼防止IP被惡意解析進來?
- 利用惡意頁面攻擊本地 Xdebug
- JQuery iframe頁面jQuery
- Windows中Google瀏覽器主頁被惡意篡改WindowsGo瀏覽器
- iframe巢狀(等寬高比自適應:aspectRatio)巢狀
- 遞迴元件組合拳,無懼頁面巢狀遞迴元件巢狀
- 前端 防止使用 target="_blank" 的惡意攻擊前端
- Iframe內部頁面高度賦值於其父頁面的Iframe標籤賦值
- 第三方系統透過iframe巢狀整合grafana巢狀Grafana
- 禁用iframe子頁面滑鼠右鍵
- iframe父子頁面通訊解決方案
- 【防止惡意使用者註冊】-- 手機在網狀態 API 的防欺詐應用解析API
- keycloak~對接login-status-iframe頁面判斷使用者狀態變更
- 防止惡意攻擊,伺服器DDoS防禦軟體科普伺服器
- 遮蔽iframe子頁面F12按鍵
- 防止頁面按鈕多次點選
- jquery獲取低程式碼平臺iframe巢狀的父級元素指定元素jQuery巢狀
- 最新 Mac 惡意軟體 OSX/CrescentCore 被發現Mac
- 分散式的節點能有效地防止惡意攻擊事件分散式事件
- 怎樣使用iframe重新整理父級頁面?
- JavaScript系列:動態建立iframe並載入頁面JavaScript
- MySQL——優化巢狀查詢和分頁查詢MySql優化巢狀
- 被各種巢狀判斷噁心的你,想到狀態模式了嗎?巢狀模式
- 惡意營銷損害消費者利益 “贈話費”類惡意營銷將被重點打擊
- 如何防止網站被侵入,如何防止網站被掛馬,如何防止網站被入侵?網站
- Nginx配置各種響應頭防止XSS,點選劫持,frame惡意攻擊Nginx
- 網站被植入惡意程式碼 該怎麼解決網站
- 列表巢狀操作巢狀
- vue路由巢狀Vue路由巢狀
- iframe頁面總是提示需要重新登入怎麼辦
- 網頁被劫持什麼?防止頁面劫持方法介紹網頁
- 網站資料被惡意篡改洩露如何查詢漏洞網站
- Websense稱3萬多合法網站被注入惡意程式碼Web網站