Запретить изменение "ASP .NET_SessionId" пользователем c# - PullRequest
1 голос
/ 08 января 2020

Я пытался запретить пользователю изменять "ASP.NET_SessionId"

Я пытался этот код:

Response.Cookies["ASP.NET_SessionId"].Value = GenerateHashKey();

Но мой session (Session["userId"]) был удален при попытке установить повара ie

Вот некоторый код, который я пробовал без успеха:

protected void Application_BeginRequest(object sender, EventArgs e)
{

    //Check If it is a new session or not , if not then do the further checks
    if (Request.Cookies["ASP.NET_SessionId"] != null && Request.Cookies["ASP.NET_SessionId"].Value != null)
    {
        string newSessionID = Request.Cookies["ASP.NET_SessionID"].Value;
        //Check the valid length of your Generated Session ID
        if (newSessionID.Length <= 24)
        {
            //Log the attack details here
            Response.StatusCode = 401;
        }

        //Genrate Hash key for this User,Browser and machine and match with the Entered NewSessionID
        if (GenerateHashKey() != newSessionID.Substring(24))
        {
            //Log the attack details here
            Response.StatusCode = 401;
            //throw new HttpException("401");
        }
        //Use the default one so application will work as usual//ASP.NET_SessionId
        Request.Cookies["ASP.NET_SessionId"].Value = Request.Cookies["ASP.NET_SessionId"].Value.Substring(0, 24);
    }
}

private string GenerateHashKey()
{
    StringBuilder myStr = new StringBuilder();
    myStr.Append(Request.Browser.Browser);
    myStr.Append(Request.Browser.Platform);
    myStr.Append(Request.Browser.MajorVersion);
    myStr.Append(Request.Browser.MinorVersion);
    SHA1 sha = new SHA1CryptoServiceProvider();
    byte[] hashdata = sha.ComputeHash(Encoding.UTF8.GetBytes(myStr.ToString()));
    return Convert.ToBase64String(hashdata);
}


protected void Application_EndRequest(object sender, EventArgs e)
{
    //Pass the custom Session ID to the browser.
    if (Response.Cookies["ASP.NET_SessionId"] != null)
    {
        Response.Cookies["ASP.NET_SessionId"].Value = Request.Cookies["ASP.NET_SessionId"].Value + GenerateHashKey();
    }

}

Как запретить пользователю установить сеанс enter image description here

1 Ответ

1 голос
/ 08 января 2020

Похоже, вы пытаетесь защитить значение сеанса от подделки? Если вы сами установите значение, вы переопределите идентификатор сеанса и уничтожите цель asp сеанса повара ie.

ASP .Net_SessionId - это повар ie, который используется для идентификации сеанса пользователя на сервере. Сеанс является областью на сервере, которая может использоваться для хранения данных между запросами http.

Если вы пытаетесь решить проблему с фиксацией сеанса, которая является единственной уязвимостью сеанса asp cook ie, затем вам нужно ввести нового cook ie, например: https://medium.com/@grep_security / session-fixation-broken-authentication-and-session-management-c37ce0111bf5

...