如何通过编辑HTML代码来防止文件被盗?

时间:2017-07-29 15:15:43

标签: html css security web flask

我有一个简单的Flask应用程序,可让您下载受登录保护的图像。只有两条路线:

example.com/login
example.com/downloadpage

在您成功登录之前无法访问“downloadpage”。这样可以正常工作。文件夹结构如下所示:

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

登录页面如下所示:

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

如果您现在转到example.com/login并通过点击Chrome中的检查来更改浏览器中的源代码,例如您可以轻松地将'../static/images/background.png'更改为'../static/images/protectedimage.png',将受保护的图片设置为背景,你可以轻松保存它。你怎么能阻止用户做到这一点?当然,我希望他们能够通过点击example.com/downloadpage上的下载按钮下载受保护的图像。

5 个答案:

答案 0 :(得分:2)

直接通过HTML,CSS,JS或PHP,他们无法解决您的问题。

您可以设置.htaccess文件来访问页面而不是PHP。 一个更简单的解决方案是将文件复制到名称复杂的文件夹,例如SESSION ID,并通过PHP将路径设置为SESSION ID / protectedimg.png

如果SESSION被销毁,只需再次删除该文件夹。

答案 1 :(得分:2)

Images / 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上使用Flask,Flask-Security和Flask-Alchemy的完整工作示例 - https://github.com/pjcunningham/flask-protected-resource

答案 2 :(得分:1)

我同意另一张海报,说明你的问题没有解决方案。

但是,我想补充一点,你可以对受保护图像的文件名(例如:kjhsdfh978y3h4i2uhdllupyu878366jsf.jpg)进行模糊处理,这样人们在开发工具中猜测文件名几乎是不可能的。 / p>

值得一提的是,这仍然不会使文件无法找到,但它是一个简单的解决方案,几乎可以阻止任何人。

答案 3 :(得分:1)

嗯,有没有方式阻止人们查看您的源代码并最终改变它,尽管有一些方法可以阻止那些讨厌的用户。

Here's the link,其中相关内容已被复制。

  

源代码填充

     

真的,这本书中最古老的伎俩。它涉及添加一吨   代码启动前的空白区域,以便查看源菜单   显得空白。但是,所有人都必须注意滚动条   并将滚动以查找您的代码。毫无意义和愚蠢   这个方法是,还有一些人还在使用它。

     

没有右键点击脚本

     

这些脚本阻止用户右键单击“查看源”   功能位于。缺点:众所周知,难以解决问题   浏览器和实际工作正常。右键单击菜单,或   上下文菜单,包括许多有用的用户工具,包括   导航按钮和“书签页面”按钮。大多数用户没有   请善用他们的浏览器功能禁用   倾向于不重访这些页面。 View Source功能也是   通过顶部菜单提供。在顶部的主菜单栏   您的浏览器,选择查看,然后在子菜单中,您将看到“查看”   来源“或类似的东西。此外,有键盘快捷键,如   Ctrl + U可用于查看源。所有这些方法都是添加   对于试图查看您的源和它的人大约两秒钟的延迟   确实会激怒那些不想查看你的来源的用户。

     

“JavaScript加密”

     

这是迄今为止尝试隐藏某个源代码的最常用方法。   它涉及使用自定义函数来获取代码   以某种方式“加密”它,然后将它与一个HTML文件放在一起   将为浏览器解密的功能。用户可以查看   然而,来源却是不可理解的。缺点:您的网站是   仅适用于启用了JavaScript的用户。这排除了搜索   引擎,选择禁用JavaScript的用户以及使用   没有JavaScript的文本浏览器(如盲人)   能力。请记住,JavaScript是一种奢侈品,而不是必需品   网页。您必须包含一种解密页面的方法,以便浏览器   可以显示它。了解JavaScript的人可以轻松解密   这页纸。许多浏览器都提供了替代方法。一些   允许您保存页面,解密以便日后查看。其他,   像FireFox一样,包括DOM Inspector之类的工具,它可以让你   轻松查看和复制页面的XML,解密。

答案 4 :(得分:0)

对于ctrl键盘:

尝试使用shortcuts.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更改为在New选项卡中重定向到我想要显示的源的不同页面!所以使用类似的东西:

adapterView.getAdapter().getItem(i)

或者不留下任何脚本来禁用它 并添加此示例代码以使用新代码:

<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>