doucuodan0897 2016-09-25 19:18
浏览 57
已采纳

解密大猩猩会话Cookie数据

Firstly, let me preface by saying I'm taking part in a Capture the Flag contest and I'm having some difficulty with a question related to Go Gorilla Sessions. I've never coded in Go, so this is fun, and frustrating :)

I have a secret key. I have an encoded Cookie. I need to decode the cookie, using the secret key I have, edit whatever data is in there, and re-encrypt with my altered data to progress in the challenge.

I've read the Gorilla Sessions Package documentation and not really getting any help.

Can anyone assist, where do I start?

  • 写回答

1条回答 默认 最新

  • duanjianhe1388 2016-09-25 21:11
    关注

    Looking at the docs - gorilla provides a secure cookie package. Depending on your apps architecture - a basic implementation could work as follows:

    Create a session management package to be used by your app. For the sake of example - lets call it sessionmngr

    Inside of sessionmngr, import "github.com/gorilla/securecookie".

    In the sessionmngr package, use a lower case init() function to set up a private instance of securecookie. Once a package is imported, lowercase init() functions are called in the order they are declared. (Check out the language spec for more info). You will use this instance to encode and decode cookies from the standard library's http.Request.

    import (
        "github.com/gorilla/securecookie"      
    
        //you will need this later
        "http" 
    )
    
    //declare private secure cookie 
    var s *securecookie.SecureCookie
    
    //initialize it here (taken from the gorilla docs example)
    func init() {
        var hashKey = []byte("very-secret")
        var blockKey = []byte("a-lot-secret")
        s = securecookie.New(hashKey, blockKey)
    }
    

    You will then use s throughout the package in functions that need to encode and decode the a cookie's value. The securecookie package documentation provides a boilerplate example.

    To meet the requirements of reading and modifying an already encrypted cookie - use the Decode and Encode methods on the instance of securecookie that was setup in the example above.

    Something Like ---

    func DecodeAndModify(w http.ResponseWriter, r *http.Request) {
        //get reference to cookie if set
        if cookie, err := r.Cookie("cookie-name"); err == nil {
    
            value := make(map[string]string)
            //use Decode to get the value from the cookie
            if err = s.Decode("cookie-name", cookie.Value, &value); err == nil {
                //modify the value in some way
                value["newKey"] = "newValue"
                //re-encode it
                if encoded, err := s.Encode("cookie-name", value); err == nil {
                    cookie := &http.Cookie{
                        Name:  "cookie-name",
                        Value: encoded,
                        Path:  "/",
                    }
                    http.SetCookie(w, cookie)
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作