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

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

      sendCookie.java

          

package cookie;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class SendCookieServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//服务器端生成set-cookie
Cookie cookie = new Cookie("name", "admin");
//设置cookie存活时间为十分钟
cookie.setMaxAge(60*10);
//设置会话cookie允许的路径
//允许整个项目
cookie.setPath("/");
//将cookie中存储的信息发送到客户端---头
response.addCookie(cookie);
} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}

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

      GetCookieServlet代码如下:
package cookie;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class GetCookieServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取cookie的键值
Cookie[] cookies = request.getCookies();
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String name=null;
//判斷cookie不能为空
if (cookies != null) {
for (Cookie cookie : cookies) {
// 获取键
cookie.getName();
if ("name".equals(cookie.getName())) {
name=cookie.getValue();
}
}
} if(name.equals("admin")) {
response.getWriter().write("欢迎admin登陆后台系统");
}else {
response.getWriter().write("欢迎xxx登陆后台系统");
}
} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}

    先访问sendCookie然后访问getCookie:

      默认是进入admin系统

      

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

就越权进入了另一个系统

      

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

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

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

  代码如下:

      sendCookie:

        

package cookie;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; public class SendCookieServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.setAttribute("name", "admin");
String id = session.getId();
//服务器端生成set-cookie
Cookie cookie = new Cookie("JSESSIONID", id);
//设置cookie存活时间为十分钟
cookie.setMaxAge(60*10);
//设置会话cookie允许的路径
//允许整个项目
cookie.setPath("/");
//将cookie中存储的信息发送到客户端---头
response.addCookie(cookie);
} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}

    getCookie代码如下:

      

package cookie;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; public class GetCookieServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
HttpSession session = request.getSession();
String name = (String) session.getAttribute("name");
if(name.equals("admin")) {
response.getWriter().write("欢迎admin登陆后台系统");
}else {
response.getWriter().write("欢迎xxx登陆后台系统");
}
} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}

  然后访问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. Azure系列2.1.5 —— BlobOutputStream

    (小弟自学Azure,文中有不正确之处,请路过各位大神指正.) 网上azure的资料较少,尤其是API,全是英文的,中文资料更是少之又少.这次由于公司项目需要使用Azure,所以对Azure的一些学习 ...

  2. taro实战1

    npm install -g @tarojs/cli //或 yarn global add @tarojs/cli

  3. hive排序

    1.升序排序 hive > select  id,name,sal from emp order by sal; 2.降序  添加关键字desc hive > select  id,nam ...

  4. 21.PHP实现Word/Excel/PPT转换为PDF

    参考文档: https://www.cnblogs.com/woider/p/7003481.html http://blog.csdn.net/aoshilang2249/article/detai ...

  5. Java对象clone()的测试

    Object中自带native clone()方法. 研究了一下用法. public class DeepCopyTest { public static void main(String[] arg ...

  6. PLSQL过期:Your trial period for PL/SQL Developer is over .If you want to continue using this software ,you must purchase the retail version.

    PLSQL过期:Your trial period for PL/SQL Developer is over .If you want to continue using this software ...

  7. css的特性

    一.继承性: 继承是一种规则,它允许样式不仅应用于某个特定html标签元素,而且应用于其后代. /* 不具有继承性的css样式: */p{border:1px solid red;} 二.特殊性(优先 ...

  8. Wpf ViewModel中 ObservableCollection不支持从调度程序线程以外的线程对其 SourceCollection 进行的更改

    Wpf中ViewModel类里面经常会需要用到ObservableCollection来管理列表数据,在做异步通信的时候也会碰到“不支持从调度程序线程以外的线程对其 SourceCollection ...

  9. c++创建文件夹以及子文件夹

    #ifdef WIN32 #include <io.h> #include <direct.h> #else #include <unistd.h> #includ ...

  10. tomcat9 点击bin目录下的startup.bat一闪而过

    我装的是tomcat9免安装版,jdk版本是11,之后去tomcat bin目录下点击startup.bat闪退(好吧,只有想办法解决了) 博客中的解决办法五花八门,什么环境变量没配好....不过都不 ...