技术实在是有限,讲解cookie越权的时候可能有点简单和粗糙。这里就简单记录学习下。

    首先自己写一段存在漏洞的代码code:

      sendCookie.java

          

  1. package cookie;
  2.  
  3. import java.io.IOException;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.Cookie;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9.  
  10. public class SendCookieServlet extends HttpServlet {
  11.  
  12. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. //服务器端生成set-cookie
  14. Cookie cookie = new Cookie("name", "admin");
  15. //设置cookie存活时间为十分钟
  16. cookie.setMaxAge(60*10);
  17. //设置会话cookie允许的路径
  18. //允许整个项目
  19. cookie.setPath("/");
  20. //将cookie中存储的信息发送到客户端---头
  21. response.addCookie(cookie);
  22. }
  23.  
  24. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  25. doGet(request, response);
  26. }
  27. }

      然后接收cookie中的键值,然后进行判断

  1.       GetCookieServlet代码如下:
  1. package cookie;
  2.  
  3. import java.io.IOException;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.Cookie;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9.  
  10. public class GetCookieServlet extends HttpServlet {
  11.  
  12. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. // 获取cookie的键值
  14. Cookie[] cookies = request.getCookies();
  15. request.setCharacterEncoding("UTF-8");
  16. response.setContentType("text/html;charset=UTF-8");
  17. String name=null;
  18. //判斷cookie不能为空
  19. if (cookies != null) {
  20. for (Cookie cookie : cookies) {
  21. // 获取键
  22. cookie.getName();
  23. if ("name".equals(cookie.getName())) {
  24. name=cookie.getValue();
  25. }
  26. }
  27. }
  28.  
  29. if(name.equals("admin")) {
  30. response.getWriter().write("欢迎admin登陆后台系统");
  31. }else {
  32. response.getWriter().write("欢迎xxx登陆后台系统");
  33. }
  34. }
  35.  
  36. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  37. doGet(request, response);
  38. }
  39. }

    先访问sendCookie然后访问getCookie:

      默认是进入admin系统

      

因为cookie中存储的name=admin,这里修复name=其他值

就越权进入了另一个系统

      

这里的问题就是没有使用session进行敏感信息的存储。 

  修复方案:验证session的有效性,session和用户是否匹配,以及用户当前权限

  这里我把cookie的存储方式改成seesion的存储方式:  

  代码如下:

      sendCookie:

        

  1. package cookie;
  2.  
  3. import java.io.IOException;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.Cookie;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import javax.servlet.http.HttpSession;
  10.  
  11. public class SendCookieServlet extends HttpServlet {
  12.  
  13. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. HttpSession session = request.getSession();
  15. session.setAttribute("name", "admin");
  16. String id = session.getId();
  17. //服务器端生成set-cookie
  18. Cookie cookie = new Cookie("JSESSIONID", id);
  19. //设置cookie存活时间为十分钟
  20. cookie.setMaxAge(60*10);
  21. //设置会话cookie允许的路径
  22. //允许整个项目
  23. cookie.setPath("/");
  24. //将cookie中存储的信息发送到客户端---头
  25. response.addCookie(cookie);
  26. }
  27.  
  28. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  29. doGet(request, response);
  30. }
  31. }

    getCookie代码如下:

      

  1. package cookie;
  2.  
  3. import java.io.IOException;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.Cookie;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import javax.servlet.http.HttpSession;
  10.  
  11. public class GetCookieServlet extends HttpServlet {
  12.  
  13. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. request.setCharacterEncoding("UTF-8");
  15. response.setContentType("text/html;charset=UTF-8");
  16. HttpSession session = request.getSession();
  17. String name = (String) session.getAttribute("name");
  18. if(name.equals("admin")) {
  19. response.getWriter().write("欢迎admin登陆后台系统");
  20. }else {
  21. response.getWriter().write("欢迎xxx登陆后台系统");
  22. }
  23. }
  24.  
  25. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  26. doGet(request, response);
  27. }
  28. }

  然后访问sendCookie然后再getCookie:

    先正常访问:

    

修改name=其他内容:

    

已经无法造成cookie的越权。事实证明使用session存储需要进行操作的数据更安全!

    进行判断的时候不要直接用cookie存储。使用Session验证。

      不忘初心,方得始终。

      

  

    

从Java的角度简单修复Cookie越权漏洞的更多相关文章

  1. 从Java的角度修复文件下载漏洞

    从Java的角度谈下文件下载漏洞的产生,然后到他的修复方案.这里我的修复方案是白名单,而没有采用黑名单的方式. 首先先看一段存在文件下载漏洞的代码code: HTML视图页面  download.ht ...

  2. 从Java的角度修复CSRF漏洞

    漏洞挖掘中,说实话挖过最多的漏洞就属CSRF漏洞了,提交CSRF漏洞很多次,绕过CSRF防御进行攻击也有很多次.CSRF漏洞是一个很容易引发的问题,今天我从Java的角度来说下这个安全漏洞的修复方案. ...

  3. 风炫安全WEB安全学习第三十八节课 越权漏洞演示与讲解

    风炫安全WEB安全学习第三十八节课 越权漏洞演示与讲解 越权漏洞 0x01 漏洞介绍 越权漏洞的危害与影响主要是与对应业务的重要性相关,比如说某一页面服务器端响应(不局限于页面返回的信息,有时信息在响 ...

  4. Pikachu漏洞练习平台实验——越权漏洞(八)

    1.概述 由于没有对用户权限进行严格的判断 导致低权限的账号(比如普通用户)可以去完成高权限账号(比如超管)范围内的操作 水行越权:A用户和B用户属于同一级别用户,但各自不能操作对方个人信息.A用户如 ...

  5. Java安全之Shiro 550反序列化漏洞分析

    Java安全之Shiro 550反序列化漏洞分析 首发自安全客:Java安全之Shiro 550反序列化漏洞分析 0x00 前言 在近些时间基本都能在一些渗透或者是攻防演练中看到Shiro的身影,也是 ...

  6. Java通过httpclient获取cookie模拟登录

    package Step1; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.Htt ...

  7. Java中如何读写cookie (二)

    Java中删除cookie Cookie[]   cookies=request.getCookies();       //cookies不为空,则清除       if(cookies!=null ...

  8. 我们一起分析一下这个刚刚修复的RDP漏洞CVE-2019-0708

    写在前面的话 在微软今年五月份的漏洞更新安全公告中,提到了一个跟远程桌面协议(RDP)有关的漏洞.我们之所以要在这里专门针对这个漏洞进行分析,是因为这个漏洞更新涉及到Windows XP以及其他多个W ...

  9. Java服务端对Cookie的简单操作

    Java服务端对Cookie的简单操作 时间 2016-04-07 10:39:44 极客头条 原文  http://www.cuiyongzhi.com/index.php/post/15.html ...

随机推荐

  1. Tomcat异常及解决办法——持续更新中

    公司项目,开发语言为java,中间件为Tomcat,运行过程中,从Tomcat出现了一些异常,现将异常及解决办法记录如下,仅供参考.(不断在补充中.......) 异常一: 1.日志内容 org.ap ...

  2. Latex常用软件

    Linux texMaker sudo apt-get install texlive-full sudo apt-get install texmaker

  3. css3 text-shadow字体阴影讲解

    text-shadow:为字体添加阴影, 可以通过对text-shadow属性设置相关的属性值,来实现现一些需要的字体阴影效果,减少了图片的使用. 基础说明:    text-shadow: X轴  ...

  4. lombok 使用 Idea

    Lombok 是一种 Java™ 实用工具,可用来帮助开发人员消除 Java 的冗长,尤其是对于简单的 Java 对象(POJO).它通过注解实现这一目的.import lombok.Getter;i ...

  5. MyBatis基础:MyBatis入门(1)

    1. MyBatis简介 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架. MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集. MyBatis ...

  6. 猜数字游戏 在控制台运行--java详解!了;来玩

    import java.util.Scanner;//导入包 import java.util.Scanner; 注意格式 符号的使用 public class Demo{ //猜数字游戏 练习 pu ...

  7. Vue生产环境部署

    前面的话 开发时,Vue 会提供很多警告来帮助解决常见的错误与陷阱.生产时,这些警告语句却没有用,反而会增加载荷量.再次,有些警告检查有小的运行时开销,生产环境模式下是可以避免的.本文将详细介绍Vue ...

  8. 【python练习题】程序13

    #题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个"水仙花数",因为153= ...

  9. iptables防火墙的原理及应用

    简介 (netfilter, 位于Linux内核中的包过滤功能体系  ,称为Linux防火墙的“内核态”) iptables防火墙工作在网络层,针对TCP/IP数据包实施过滤和限制,iptables防 ...

  10. linux缺失gcc的安装方法

    linux安装gcc操作 1.查看linux是否有gcc文件 这个是没有挂载的 2. 使用df,查看系统光盘的挂载位置 3.卸载分区 umount /dev/sr0 4.将redhat系统光盘重新载入 ...