gpt4 book ai didi

python - 仅检查 Referer header 是否足以防止 CSRF?

转载 作者:太空狗 更新时间:2023-10-30 03:00:36 31 4
gpt4 key购买 nike

正在比较 Referer http header 足以防止 CSRF,我在下面有以下 html 代码。

<div id="Message"></div><br>
Username:<br>
<input type="text" name="Username" id="Username"><br>
Password:<br>
<input type="password" name="Password" id="Password"><br>
Keep me logged in:<br>
<input type="checkbox" id="KeepSessionAlive"><br>
<input type="submit" onClick="ProcessLogin();">
<script>
function ProcessLogin(){
Username=document.getElementById("Username").value;
Password=document.getElementById("Password").value;
KeepSessionAlive=document.getElementById("KeepSessionAlive").value;
var xmlhttp;
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("Message").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","/Login/Process",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("<A>Username</A><B>"+Username+"</B><A>Password</A><B>"+Password+"</B><A>KeepSessionAlive</A><B>"+KeepSessionAlive+"</B>");
}
</script>

这只是一个标准的 html 表单,但我想知道如果我使用下面的代码,我是否可以完全免受 CSRF 攻击。

class LoginProcess(webapp2.RequestHandler):
def post(self):
self.response.headers['Content-Type'] = 'text/plain'
HTTTP_REFERER=self.request.referer
if HTTP_REFER=="http://example.com":
self.response.write('Referer: '+cgi.escape(HTTTP_REFERER))

最佳答案

是的,这就足够了,但它被认为是一种较弱的保护形式:

Although it is trivial to spoof the referer header on your own browser, it is impossible to do so in a CSRF attack. Checking the referer is a commonly used method of preventing CSRF on embedded network devices because it does not require a per-user state. This makes a referer a useful method of CSRF prevention when memory is scarce. This method of CSRF mitigation is also commonly used with unauthenticated requests, such as requests made prior to establishing a session state which is required to keep track of a synchronization token.

However, checking the referer is considered to be a weaker from of CSRF protection. For example, open redirect vulnerabilities can be used to exploit GET-based requests that are protected with a referer check and some organizations or browser tools remove referrer headers as a form of data protection. There are also common implementation mistakes with referer checks. For example if the CSRF attack originates from an HTTPS domain then the referer will be omitted. In this case the lack of a referer should be considered to be an attack when the request is performing a state change. Also note that the attacker has limited influence over the referer. For example, if the victim's domain is "site.com" then an attacker have the CSRF exploit originate from "site.com.attacker.com" which may fool a broken referer check implementation. XSS can be used to bypass a referer check.

In short, referer checking is a reasonable form of CSRF intrusion detection and prevention even though it is not a complete protection. Referer checking can detect some attacks but not stop all attacks. For example, if you HTTP referrer is from a different domain and you are expecting requests from your domain only, you can safely block that request.

如果你想要一种防止 CSRF 来自 XHR 的“快速方法”,你可以设置并检查自定义 header ,例如 X-Requested-With .目前这是安全的,但推荐的方法是 Synchronizer Token Pattern .这对浏览器插件中的缺陷更有效,例如 old vulnerability in Flash这允许设置通常不应该设置的 header 。

关于python - 仅检查 Referer header 是否足以防止 CSRF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28651976/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com