解密大猩猩会话 Cookie 数据
Decrypting Gorilla Sessions Cookie Data
首先,让我先声明一下,我正在参加夺旗比赛,但我在回答与 Go Gorilla Sessions 相关的问题时遇到了一些困难。我从来没有用 Go 编写过代码,所以这很有趣,也很令人沮丧:)
我有一个密钥。我有一个编码的 Cookie。我需要解码 cookie,使用我拥有的密钥,编辑其中的任何数据,并使用我更改的数据重新加密以在挑战中取得进展。
我已经阅读了 Gorilla Sessions Package 文档,但并未真正获得任何帮助。
任何人都可以提供帮助,我从哪里开始?
查看文档 - 大猩猩提供了 secure cookie package。
根据您的应用程序架构 - 基本实现可以按如下方式工作:
创建一个会话管理包以供您的应用程序使用。为了举例 - 让我们称之为 sessionmngr
在 sessionmngr
内部,导入 "github.com/gorilla/securecookie"
。
在sessionmngr
包中,使用小写的init()
函数来设置securecookie
的私有实例。导入包后,将按照声明的顺序调用小写的 init() 函数。 (查看语言 spec 了解更多信息)。您将使用此实例对来自标准库 http.Request
的 cookie 进行编码和解码。
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)
}
然后您将在整个程序包中需要对 cookie 的值进行编码和解码的函数中使用 s
。 securecookie package documentation 提供了一个样板示例。
为了满足读取和修改已加密 cookie 的要求 - 在上面示例中设置的 securecookie
实例上使用 Decode
和 Encode
方法。
有点像 ---
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)
}
}
}
}
首先,让我先声明一下,我正在参加夺旗比赛,但我在回答与 Go Gorilla Sessions 相关的问题时遇到了一些困难。我从来没有用 Go 编写过代码,所以这很有趣,也很令人沮丧:)
我有一个密钥。我有一个编码的 Cookie。我需要解码 cookie,使用我拥有的密钥,编辑其中的任何数据,并使用我更改的数据重新加密以在挑战中取得进展。
我已经阅读了 Gorilla Sessions Package 文档,但并未真正获得任何帮助。
任何人都可以提供帮助,我从哪里开始?
查看文档 - 大猩猩提供了 secure cookie package。 根据您的应用程序架构 - 基本实现可以按如下方式工作:
创建一个会话管理包以供您的应用程序使用。为了举例 - 让我们称之为 sessionmngr
在 sessionmngr
内部,导入 "github.com/gorilla/securecookie"
。
在sessionmngr
包中,使用小写的init()
函数来设置securecookie
的私有实例。导入包后,将按照声明的顺序调用小写的 init() 函数。 (查看语言 spec 了解更多信息)。您将使用此实例对来自标准库 http.Request
的 cookie 进行编码和解码。
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)
}
然后您将在整个程序包中需要对 cookie 的值进行编码和解码的函数中使用 s
。 securecookie package documentation 提供了一个样板示例。
为了满足读取和修改已加密 cookie 的要求 - 在上面示例中设置的 securecookie
实例上使用 Decode
和 Encode
方法。
有点像 ---
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)
}
}
}
}