1.什么是HttpOnly?

如果您在cookie中设置了HttpOnly属性,那么通过js脚本将无法读取到cookie信息,这样能有效的防止XSS攻击,具体一点的介绍请google进行搜索

2.javaEE的API是否支持?

目前sun公司还没有公布相关的API,但PHP、C#均有实现。搞javaEE的兄弟们比较郁闷了,别急下文有变通实现

3.HttpOnly的设置样例

javaEE

1
2
response.setHeader("Set-Cookie", "cookiename=value;
Path=/;Domain=domainvalue;Max-Age=seconds;HTTPOnly");

具体参数的含义再次不做阐述,设置完毕后通过js脚本是读不到该cookie的,但使用如下方式可以读取

1
Cookie cookies[]=request.getCookies();

C#

1
2
3
HttpCookie myCookie = new HttpCookie("myCookie");  
myCookie.HttpOnly = true;  
Response.AppendCookie(myCookie);

VB.NET

1
2
3
Dim myCookie As HttpCookie = new HttpCookie("myCookie")  
myCookie.HttpOnly = True  
Response.AppendCookie(myCookie)

但是在 .NET 1.1 ,中您需要手动添加

1
Response.Cookies[cookie].Path += ";HTTPOnly";

PHP4

1
header("Set-Cookie: hidden=value; httpOnly");

PHP5

1
setcookie("abc", "test", NULL, NULL, NULL, NULL, TRUE);

最后一个参数为HttpOnly属性

----------------------------------------------------------------------------------

webBrowser

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices; namespace WindowsFormsApplication1
{
/// <summary>
/// WinInet.dll wrapper
/// </summary>
internal static class CookieReader
{ private const int INTERNET_COOKIE_HTTPONLY = 0x00002000; [DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetGetCookieEx(
string url,
string cookieName,
StringBuilder cookieData,
ref int size,
int flags,
IntPtr pReserved);
public static string GetCookie(string url)
{
int size = 512;
StringBuilder sb = new StringBuilder(size);
if (!InternetGetCookieEx(url, null, sb, ref size, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero))
{
if (size < 0)
{
return null;
}
sb = new StringBuilder(size);
if (!InternetGetCookieEx(url, null, sb, ref size, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero))
{
return null;
}
}
return sb.ToString();
}
}
}

  

********************************************************************************************************************************************************

using System;
using System.ComponentModel;
using System.Net;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Text; namespace CookieHandler
{
    internal sealed class INativeMethods
    {
        #region enums         public enum ErrorFlags
        {
            ERROR_INSUFFICIENT_BUFFER = 122,
            ERROR_INVALID_PARAMETER = 87,
            ERROR_NO_MORE_ITEMS = 259
        }         public enum InternetFlags
        {
            INTERNET_COOKIE_HTTPONLY = 8192, //Requires IE 8 or higher     
            INTERNET_COOKIE_THIRD_PARTY = 131072,
            INTERNET_FLAG_RESTRICTED_ZONE = 16
        }         #endregion         #region DLL Imports         [SuppressUnmanagedCodeSecurity, SecurityCritical, DllImport("wininet.dll", EntryPoint = "InternetGetCookieExW", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
        internal static extern bool InternetGetCookieEx([In] string Url, [In] string cookieName, [Out] StringBuilder cookieData, [In, Out] ref uint pchCookieData, uint flags, IntPtr reserved);         #endregion
    }
}

  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Text; namespace CookieHandler
{
    /// <SUMMARY></SUMMARY>
    /// 取得WebBrowser的完整Cookie。
    /// 因为默认的webBrowser1.Document.Cookie取不到HttpOnly的Cookie
    /// IE7不兼容,IE8可以,其它未知
    ///
    public class FullWebBrowserCookie
    {
        public static Dictionary<string, string> GetCookieList(Uri uri, bool throwIfNoCookie)
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            string cookie = GetCookieInternal(uri, throwIfNoCookie);
            Console.WriteLine("FullWebBrowserCookie - 所有cookie:" + cookie);
            string[] arrCookie = cookie.Split(';');
            foreach (var item in arrCookie)
            {
                string[] arr = item.Split('=');
                string key = arr[0].Trim();
                string val = "";
                if (arr.Length >= 2)
                {
                    val = arr[1].Trim();
                }                 if (!dict.ContainsKey(key))
                {
                    dict.Add(key, val);
                }
            }
            Console.WriteLine("FullWebBrowserCookie - cookie已载入dict,共" + dict.Count.ToString() + "项");             return dict;
        }         public static string GetCookieValue(string key, Uri uri, bool throwIfNoCookie)
        {
            Console.WriteLine("GetCookieValue");
            Dictionary<string, string> dict = GetCookieList(uri, throwIfNoCookie);             if (dict.ContainsKey(key))
            {
                return dict[key];
            }
            return "";
        }         [SecurityCritical]
        public static string GetCookieInternal(Uri uri, bool throwIfNoCookie)
        {
            Console.WriteLine("GetCookieInternal");             uint pchCookieData = 0;
            string url = UriToString(uri);
            uint flag = (uint)INativeMethods.InternetFlags.INTERNET_COOKIE_HTTPONLY;             //Gets the size of the string builder     
            if (INativeMethods.InternetGetCookieEx(url, null, null, ref pchCookieData, flag, IntPtr.Zero))
            {
                pchCookieData++;
                StringBuilder cookieData = new StringBuilder((int)pchCookieData);                 //Read the cookie     
                if (INativeMethods.InternetGetCookieEx(url, null, cookieData, ref pchCookieData, flag, IntPtr.Zero))
                {
                    DemandWebPermission(uri);
                    return cookieData.ToString();
                }
            }             int lastErrorCode = Marshal.GetLastWin32Error();             if (throwIfNoCookie || (lastErrorCode != (int)INativeMethods.ErrorFlags.ERROR_NO_MORE_ITEMS))
            {
                throw new Win32Exception(lastErrorCode);
            }             return null;
        }         private static void DemandWebPermission(Uri uri)
        {
            string uriString = UriToString(uri);             if (uri.IsFile)
            {
                string localPath = uri.LocalPath;
                new FileIOPermission(FileIOPermissionAccess.Read, localPath).Demand();
            }
            else
            {
                new WebPermission(NetworkAccess.Connect, uriString).Demand();
            }
        }         private static string UriToString(Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }             UriComponents components = (uri.IsAbsoluteUri ? UriComponents.AbsoluteUri : UriComponents.SerializationInfoString);
            return new StringBuilder(uri.GetComponents(components, UriFormat.SafeUnescaped), 2083).ToString();
        }
    }
}

  

Cookie中的HttpOnly的更多相关文章

  1. django-会话 cookie 中缺少HttpOnly 属性-安全加强

    如果django程序扫描到会话 cookie 中缺少 HttpOnly 属性问题,需要如何进行安全加强? https://docs.djangoproject.com/en/2.2/ref/setti ...

  2. 会话cookie中缺少HttpOnly属性 解决

    会话cookie中缺少HttpOnly属性 解决   只需要写一个过滤器即可 1 package com.neusoft.streamone.framework.security.filter; 2 ...

  3. Cookie中的HttpOnly详解

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt377 1.什么是HttpOnly? 如果您在cookie中设置了HttpOn ...

  4. Cookie中的httponly的属性和作用

    1.什么是HttpOnly? 如果cookie中设置了HttpOnly属性,那么通过js脚本将无法读取到cookie信息,这样能有效的防止XSS攻击,窃取cookie内容,这样就增加了cookie的安 ...

  5. Cookie中设置了 HttpOnly,Secure 属性,有效的防止XSS攻击,X-Frame-Options 响应头避免点击劫持

    属性介绍: 1) secure属性当设置为true时,表示创建的 Cookie 会被以安全的形式向服务器传输(ssl),即 只能在 HTTPS 连接中被浏览器传递到服务器端进行会话验证, 如果是 HT ...

  6. Cookie中存放数据l加密解密的算法

    public class CookieUtil { /** * * @param response HttpServletResponse类型的响应 * @param cookie 要设置httpOn ...

  7. Appscan漏洞 之 加密会话(SSL)Cookie 中缺少 Secure 属性

    近期 Appscan扫描出漏洞 加密会话(SSL)Cookie 中缺少 Secure 属性,已做修复,现进行总结如下: 1.1.攻击原理 任何以明文形式发送到服务器的 cookie.会话令牌或用户凭证 ...

  8. .NET跨平台之旅:ASP.NET Core从传统ASP.NET的Cookie中读取用户登录信息

    在解决了asp.net core中访问memcached缓存的问题后,我们开始大踏步地向.net core进军——将更多站点向asp.net core迁移,在迁移涉及获取用户登录信息的站点时,我们遇到 ...

  9. js获取cookie中存储的值

    最近看了试卷题目发现自己会的十分的匮乏, 第一题就把自己难住了,知道有这个东西,但是实际上没有操作过. ========================================= cookie ...

随机推荐

  1. Java中定义不了可变长数组怎么办---集合 泛型

    一.集合(Collections) Java使用集合来组织和管理对象. 1.Java的集合类 集合类主要负责保存.盛装和管理对象,因此集合类也被称为容器类. 集合类分为Set.List.Map和Que ...

  2. [内网渗透]HASH获取与HASH传递

    0x01 PTH简介 PTH,即Pass-The-Hash,首先我们来说下为什么要使用HASH传递,一是在目标机>=win server 2012时,lsass.exe进程中是抓不到明文密码的, ...

  3. js注册表单中实现地区选择效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. #C++初学记录(STL容器以及迭代器)

    STL初步 提交ACM会TLE /仅以学习STL与迭代器使用 C. Cards Sorting time limit per test1 second memory limit per test256 ...

  5. 将爬取的实习僧网站数据传入HDFS

     一.引言: 作为一名大三的学生,找实习对于我们而言是迫在眉睫的.实习作为迈入工作的第一步,它的重要性不言而喻,一份好的实习很大程度上决定了我们以后的职业规划. 那么,一份好的实习应该考量哪些因素呢? ...

  6. git : error occurred during unpacking on the remote end: unpack-objects abnormal exit

    error occurred during unpacking on the remote end: unpack-objects abnormal exit git服务器磁盘满了.

  7. 使用bert进行情感分类

    2018年google推出了bert模型,这个模型的性能要远超于以前所使用的模型,总的来说就是很牛.但是训练bert模型是异常昂贵的,对于一般人来说并不需要自己单独训练bert,只需要加载预训练模型, ...

  8. Angularjs Select获取数组下标

    一.定义资源 //资源类型 $scope.status=['项目测试','开发工具','安装包','工作计划','测试项目','我的游戏','我的音乐','博客首页图片']; 二.没错直接用  {{$ ...

  9. LeetCode_443. String Compression

    443. String Compression Easy Given an array of characters, compress it in-place. The length after co ...

  10. iptables的statistic模块配置rr

    网上搜的是这样的 : iptables -A PREROUTING -t nat -d 172.17.64.8 -m statistic --mode nth --every 2 --packet 0 ...