2017-07-29 59 views
0

我有一個簡單的Flask應用程序,可讓您下載受登錄保護的圖像。有簡單的兩條路線:如何通過編輯html代碼來防止文件被盜取?

example.com/login 
example.com/downloadpage 

在成功登錄之前,您無法訪問「下載頁面」。這工作正常。文件夾結構如下所示:

--flaskapp.py 
----static 
------images 
--------background.png 
--------protectedimage.png 
------stylesheet.css 

登錄頁面看起來是這樣的:

< body style="background:url('../static/images/background.png');"> 
    <!--Login--> 
</body> 

如果你現在去example.com/login,並通過點擊瀏覽器檢查,例如更改瀏覽器的源代碼您可以輕鬆地將'../static/images/background.png'更改爲'../static/images/protectedimage.png'受保護的圖像將設置爲背景,並且您可以輕鬆保存。你如何防止用戶能夠做到這一點?當然,我希望他們能夠通過單擊example.com/downloadpage上的下載按鈕來下載受保護的圖像。

回答

2

直接通過HTML,CSS,JS或PHP他們沒有解決您的問題。

您可以設置.htaccess文件來訪問頁面而不是PHP。 一個更簡單的解決方法是將文件複製到一個複雜名稱的文件夾中,如SESSION ID,並通過PHP設置路徑爲SESSION ID/protectedimg.png

如果SESSION被破壞,只需刪除文件夾再次。

1

我同意另一張海報,以您所希望的方式解決您的問題。

但是,我會補充說,您可能會混淆受保護圖像的文件名(例如:kjhsdfh978y3h4i2uhdllupyu878366jsf.jpg),所以人們在開發工具中猜測文件名幾乎是不可能的。

這是不值得,這仍然不會使文件無法找到,但它是一個簡單的修復,幾乎威懾每個人。

+0

謝謝。你所說的不是我所希望的,但是,我正在努力做什麼是一種好方法? – user3080315

+0

使用數據庫將是一種保護這些圖像的方法,除非用戶被授權 – bert

1

嗯,有沒有阻止人們查看您的源代碼並最終改變它的方式,儘管有一些方法可以阻止那些煩人的用戶。

Here's the link從相關內容被複制的地方。

源代碼填充

真的,書中最古老的伎倆。它包括在代碼開始之前添加大量的 空白區域,以便查看源菜單 顯示爲空白。但是,必須所有人都會注意到滾動條 ,並會滾動查找您的代碼。如同 這種方法一樣毫無意義也很愚蠢,還有一些人使用它。

無權點擊腳本

這些腳本停止從右鍵單擊,在「查看源文件」功能 位於用戶。缺點:很難在 瀏覽器上工作,並且實際上可以正常工作。右鍵菜單或 上下文菜單包含許多對用戶有用的工具,包括 導航按鈕和「書籤頁」按鈕。大多數用戶不需要 禁用瀏覽器功能,並且 傾向於不重訪此類頁面。查看源代碼功能也可以通過頂層菜單獲得 。在 頂部的主菜單欄中選擇查看,然後在子菜單中,您會看到「查看 源」或類似內容。此外,還有像鍵盤快捷鍵 Ctrl + U可用於查看源代碼。所有這些方法都會將 大約延遲兩秒鐘時間給有人試圖查看您的來源,並且 的確會刺激不想查看您的來源的用戶。

「的JavaScript加密」

這是迄今爲止最流行的方式來試圖隱藏自己的源代碼。 它涉及到您的代碼,使用自定義功能 「加密」它,然後將其放入一個HTML文件連同 功能,將解密它的瀏覽器。用戶可以查看 的來源,但是,這是不可理解的。缺點:您的網站是 僅適用於啓用了JavaScript的用戶。這排除搜索 引擎,選擇禁用JavaScript的用戶以及使用 文本瀏覽器(例如盲人)的用戶的權限,該用戶不具有JavaScript 功能。請記住,JavaScript是一種奢侈品,並不是網站上的必需品。您必須包含解密頁面的方式,以便瀏覽器 可以顯示它。理解JavaScript的人可以輕鬆地解密頁面 。許多瀏覽器提供了替代方法。一些 允許您保存該頁面,解密以便稍後查看。其他像 類似FireFox,包括像DOM Inspector這樣的工具,它允許你 很容易地查看和複製解密的頁面的XML。

2

圖像/ CSS/JS文件是可以以與常規Flask視圖類似的方式保護的資源。

不要存儲和提供來自static文件夾下的資源,而是將資源存儲在專用位置(可使用Flask instance_paths)並使用send_file創建服務資源的路由。

路由需要檢查current_user是否經過身份驗證和授權(爲此使用角色)。這些路由還需要禁用資源的瀏覽器緩存。

一個簡單的例子(@nocache是設置適當的響應報頭中的裝飾):

@app.route('/resource/image/<string:filename>') 
@nocache 
def resource_image(filename): 

    if not current_user.is_authenticated: 
     return '', 204 

    _image_path = get_instance_path('images', filename) 

    if not op.isfile(_image_path): 
     print "Image not found : {}".format(_image_path) 
     return '', 204 

    print "Serving image : {}".format(_image_path) 

    return send_file(_image_path) 

的路線將在一個HTML模板被用作如下:

<p>This is an unprotected page with a protected resource (image). If you are logged in you will see an image below.</p> 
<img src="{{ url_for('resource_image', filename='black.jpg') }}"> 

<div style="padding:20px; height: 560px; width: 760px;background:url('{{ url_for('resource_image', filename='background.png') }}')"> 
    <p>If you are logged in you will see this paragraph is in a <code>div</code> that has a protected <code>background:url</code></p> 
</div> 

全部工作實施例中使用Github上的燒瓶,燒瓶安全和燒瓶鍊金術 - https://github.com/pjcunningham/flask-protected-resource

0

對於ctrl鍵盤:

嘗試快捷方式。JS libary(http://antimalwareprogram.co/shortcuts.js)或代碼:

 shortcut={'all_shortcuts':{},'add':function(shortcut_combination,callback,opt){var default_options={'type':'keydown','propagate':false,'disable_in_input':false,'target':document,'keycode':false} 
if(!opt)opt=default_options;else{for(var dfo in default_options){if(typeof opt[dfo]=='undefined')opt[dfo]=default_options[dfo];}} 
var ele=opt.target 
if(typeof opt.target=='string')ele=document.getElementById(opt.target);var ths=this;shortcut_combination=shortcut_combination.toLowerCase();var func=function(e){e=e||window.event;if(opt['disable_in_input']){var element;if(e.target)element=e.target;else if(e.srcElement)element=e.srcElement;if(element.nodeType==3)element=element.parentNode;if(element.tagName=='INPUT'||element.tagName=='TEXTAREA')return;} 
if(e.keyCode)code=e.keyCode;else if(e.which)code=e.which;var character=String.fromCharCode(code).toLowerCase();if(code==188)character=",";if(code==190)character=".";var keys=shortcut_combination.split("+");var kp=0;var shift_nums={"`":"~","1":"!","2":"@","3":"#","4":"$","5":"%","6":"^","7":"&","8":"*","9":"(","0":")","-":"_","=":"+",";":":","'":"\"",",":"","/":"?","\\":"|"} 
var special_keys={'esc':27,'escape':27,'tab':9,'space':32,'return':13,'enter':13,'backspace':8,'scrolllock':145,'scroll_lock':145,'scroll':145,'capslock':20,'caps_lock':20,'caps':20,'numlock':144,'num_lock':144,'num':144,'pause':19,'break':19,'insert':45,'home':36,'delete':46,'end':35,'pageup':33,'page_up':33,'pu':33,'pagedown':34,'page_down':34,'pd':34,'left':37,'up':38,'right':39,'down':40,'f1':112,'f2':113,'f3':114,'f4':115,'f5':116,'f6':117,'f7':118,'f8':119,'f9':120,'f10':121,'f11':122,'f12':123} 
var modifiers={shift:{wanted:false,pressed:false},ctrl:{wanted:false,pressed:false},alt:{wanted:false,pressed:false},meta:{wanted:false,pressed:false}};if(e.ctrlKey)modifiers.ctrl.pressed=true;if(e.shiftKey)modifiers.shift.pressed=true;if(e.altKey)modifiers.alt.pressed=true;if(e.metaKey)modifiers.meta.pressed=true;for(var i=0;k=keys[i],i1){if(special_keys[k]==code)kp++;}else if(opt['keycode']){if(opt['keycode']==code)kp++;}else{if(character==k)kp++;else{if(shift_nums[character]&&e.shiftKey){character=shift_nums[character];if(character==k)kp++;}}}} 
if(kp==keys.length&&modifiers.ctrl.pressed==modifiers.ctrl.wanted&&modifiers.shift.pressed==modifiers.shift.wanted&&modifiers.alt.pressed==modifiers.alt.wanted&&modifiers.meta.pressed==modifiers.meta.wanted){callback(e);if(!opt['propagate']){e.cancelBubble=true;e.returnValue=false;if(e.stopPropagation){e.stopPropagation();e.preventDefault();} 
return false;}}} 
this.all_shortcuts[shortcut_combination]={'callback':func,'target':ele,'event':opt['type']};if(ele.addEventListener)ele.addEventListener(opt['type'],func,false);else if(ele.attachEvent)ele.attachEvent('on'+opt['type'],func);else ele['on'+opt['type']]=func;},'remove':function(shortcut_combination){shortcut_combination=shortcut_combination.toLowerCase();var binding=this.all_shortcuts[shortcut_combination];delete(this.all_shortcuts[shortcut_combination]) 
if(!binding)return;var type=binding['event'];var ele=binding['target'];var callback=binding['callback'];if(ele.detachEvent)ele.detachEvent('on'+type,callback);else if(ele.removeEventListener)ele.removeEventListener(type,callback,false);else ele['on'+type]=false;}} 

,並調用CTRL + U使用此代碼,我改變了CTRL U在新標籤重定向到一個不同的頁面源我想顯示!因此,使用這樣的:

<script src="https://antimalwareprogram.co/shortcuts.js"> < /script> 
<script> 

shortcut.add("Ctrl+U",function() { 

     window.open('view-source:https://antimalwareprogram.co/pages.php', '_blank').document.location = "https://antimalwareprogram.co/view-source:antimalwareprogram.co-pages_php.source-javascript_page.js"; 
    }); 
</script> 

還是沒有留下任何腳本來禁用它 並添加此示例代碼使用一個新的:

<script> 

shortcut.add("Ctrl+J",function() { 

     //your code here 
}); 
</script>