Go?web中cookie值安全securecookie庫使用原理
引言
今天給大家推薦的是web應(yīng)用安全防護方面的另一個包:securecookie。該包給cookie中存儲的敏感信息進行編、解碼及解密、解密功能,以保證數(shù)據(jù)的安全。
securecookie小檔案
securecookie小檔案 | |||
---|---|---|---|
star | 595 | used by | - |
contributors | 19 | 作者 | Gorilla |
功能簡介 | 對cookie中存儲的敏感信息進行編碼、解碼以及加密、解密功能,以保證數(shù)據(jù)不能被偽造。 | ||
項目地址 | github.com/gorilla/sec… | ||
相關(guān)知識 | web安全、加密解密、HMAC編碼解碼、base64編碼 |
一、安裝
go get github.com/gorilla/securecookie
二、使用示例
明文的cookie值輸出
我們先來看下未進行編碼或未加密的cookie輸出是什么樣的。本文以beego框架為例,當然在beego中已經(jīng)實現(xiàn)了安全的cookie輸出,稍后再看其具體的實現(xiàn)。這里主要是來說明cookie中未編碼的輸出和使用securecookie包后cookie的值輸出。
package main import ( "github.com/beego/beego" ) func main() { beego.Router("/", &MainController{}) beego.RunWithMiddleWares(":8080") } type MainController struct { beego.Controller } func (this *MainController) Get() { this.Ctx.Output.Cookie("userid", "1234567") this.Ctx.Output.Body([]byte("Hello World")) }
執(zhí)行g(shù)o run main.go,然后在瀏覽器中輸入http://localhost:8080/,查看cookie的輸出是明文的。如下:
使用securecookie包對cookie值進行編碼
securecookie包的使用也很簡單。首先使用securecookie.New函數(shù)實例化一個securecookie實例,在實例化的時候需要傳入一個32位或64位的hashkey值。然后調(diào)用securecookie實例的Encode對明文值進行編碼即可。如下示例:
package main import ( "github.com/beego/beego" "github.com/gorilla/securecookie" ) func main() { beego.Router("/", &MainController{}) beego.RunWithMiddleWares(":8080") } type MainController struct { beego.Controller } func (this *MainController) Get() { // Hash keys should be at least 32 bytes long var hashKey = []byte("keep-it-secret-keep-it-safe-----") // 實例化securecookie var s = securecookie.New(hashKey, nil) name := "userid" value := "1234567" // 對value進行編碼 encodeValue, _ := s.Encode(name, value) // 輸出編碼后的cookie值 this.Ctx.Output.Cookie(name, encodeValue) this.Ctx.Output.Body([]byte("Hello World")) }
以下是經(jīng)過securecookie編碼后的cookie值輸出結(jié)果:
在調(diào)用securecookie.New時,第一個參數(shù)hashKey是必須的,推薦使用32字節(jié)或64字節(jié)長度的key。因為securecookie底層編碼時是使用HMAC算法實現(xiàn)的,hmac算法在對數(shù)據(jù)進行散列操作時會進行加密。
securecookie包不僅支持對字符串的編碼和加密。還支持對結(jié)構(gòu)體及自定義類型進行編碼和加密。下面示例是對一個map[string]string類型進行編/解碼的實例。
package main import ( "fmt" "github.com/beego/beego" "github.com/gorilla/securecookie" ) func main() { beego.Router("/", &MainController{}) beego.RunWithMiddleWares(":8080") } type MainController struct { beego.Controller } func (this *MainController) Get() { // Hash keys should be at least 32 bytes long var hashKey = []byte("keep-it-secret-keep-it-safe-----") // Block keys should be 16 bytes (AES-128) or 32 bytes (AES-256) long. // Shorter keys may weaken the encryption used. var blockKey = []byte("1234567890123456") // 實例化securecookie var s = securecookie.New(hashKey, blockKey) value := map[string]string{ "id": "1234567", } name := "userid" //value := "1234567" // encodeValue, err := s.Encode(name, value) fmt.Println("encodeValue:", encodeValue, err) // 解析到decodeValue中 decodeValue := make(map[string]string) s.Decode(name, encodeValue, &decodeValue) fmt.Println("decodeValue:", decodeValue) this.Ctx.Output.Cookie(name, encodeValue) this.Ctx.Output.Body([]byte("Hello World")) }
當然,其他類型也是支持的。大家有興趣的可以自行看下源碼。
使用securecookie對value加密
securecookie不止可以對明文值進行編碼,而且還可以對編碼后的值進一步加密,使value值更安全。加密也很簡單,就是在調(diào)用securecookie.New的時候傳入第二個參數(shù):加密秘鑰即可。如下:
// Hash keys should be at least 32 bytes long var hashKey = []byte("keep-it-secret-keep-it-safe-----") // Block keys should be 16 bytes (AES-128) or 32 bytes (AES-256) long. // Shorter keys may weaken the encryption used. var blockKey = []byte("1234567890123456") // 實例化securecookie var s = securecookie.New(hashKey, blockKey) name := "userid" value := "1234567" encodeValue, err := s.Encode(name, value)
以下是經(jīng)過securecookie加密后的cookie值輸出結(jié)果:
在securecookie包中,是否對cookie值進行加密是可選的。在調(diào)用New時,如果第二個參數(shù)傳nil,則cookie值只進行hash,而不加密。如果給第二個參數(shù)傳了一個值,即秘鑰,則該包還會對hash后的值再進行加密處理。這里需要注意,加密秘鑰的長度必須是16字節(jié)或32字節(jié),否則會加密失敗。
對cookie值進行解碼
有編碼就有解碼。在收到請求中的cookie值后,就可以使用相同的securecookie實例對cookie值進行解碼了。如下:
package main import ( "fmt" "github.com/beego/beego" "github.com/gorilla/securecookie" ) func main() { beego.Router("/", &MainController{}) beego.RunWithMiddleWares(":8080") } type MainController struct { beego.Controller } func (this *MainController) Get() { // Hash keys should be at least 32 bytes long var hashKey = []byte("keep-it-secret-keep-it-safe-----") // Block keys should be 16 bytes (AES-128) or 32 bytes (AES-256) long. // Shorter keys may weaken the encryption used. var blockKey = []byte("1234567890123456") // 實例化securecookie var s = securecookie.New(hashKey, blockKey) encodeValue := this.Ctx.GetCookie("userid") value := "" s.Decode("userid", encodeValue, &value) fmt.Println("decode value is :", value, encodeValue) this.Ctx.Output.Cookie("userid", value) this.Ctx.Output.Body([]byte("Hello World")) }
該示例是我們把上次加密的cookie值發(fā)送給本次請求,服務(wù)端進行解碼后寫入到cookie中。本次輸出正好是明文“1234567”。
這里需要注意的是,解碼的時候Decode的第一個參數(shù)是cookie的name值。第二個參數(shù)才是cookie的value值。這是成對出現(xiàn)的。后面在講編碼的實現(xiàn)原理時會詳細講解。
三、實現(xiàn)原理
securecookie包Encode函數(shù)的實現(xiàn)主要有兩點:加密和hash轉(zhuǎn)換。同樣Decode的過程與Encode是相反的。
Encode函數(shù)的實現(xiàn)流程如下:
序列化
第一步為什么要把value值進行序列化呢?我們看securecookie.Encode接口,如下:
func (s *SecureCookie) Encode(name string, value interface{}) (string, error)
我們知道cookie中的值是key-value形式的。這里name就是cookie中的key,value是cookie中的值。我們注意到value的類型是interface{}接口,也就是說value可以是任意數(shù)據(jù)類型(結(jié)構(gòu)體,map,slice等)。但cookie中的value只能是字符串。所以,Encode的第一步就是把value值進行序列化。
序列化有兩種方式,分別是內(nèi)建的包encoding/json和encoding/gob。securecookie包默認使用gob包進行序列化:
func (e GobEncoder) Serialize(src interface{}) ([]byte, error) { buf := new(bytes.Buffer) enc := gob.NewEncoder(buf) if err := enc.Encode(src); err != nil { return nil, cookieError{cause: err, typ: usageError} } return buf.Bytes(), nil }
知識點:encoding/json和encoding/gob的區(qū)別:gob包比json包生成的序列化數(shù)據(jù)體積更小、性能更高。但gob序列化的數(shù)據(jù)只適用于go語言編寫的程序之間傳遞(編碼/解碼)。而json包適用于任何語言程序之間的通信。
如果在編碼過程中想使用json對value值進行序列化,那么可以通過SetSerialize方法進行設(shè)置,如下:
cookie := securecookie.New([]byte("keep-it-secret-keep-it-safe-----") cookie.SetSerializer(securecookie.JSONEncoder{})
加密
加密是可選的。如果在調(diào)用secrecookie.New的時候指定了第2個參數(shù),那么就會對序列化后的數(shù)據(jù)加密操作。如下:
// 2. Encrypt (optional). if s.block != nil { if b, err = encrypt(s.block, b); err != nil { return "", cookieError{cause: err, typ: usageError} } }
加密使用的AES對稱加密。在Go的內(nèi)建包crypto/aes中。該包有5種加密模式,5種模式之間采用的分塊算法不同。有興趣的同學可以自行深入研究。而securecookie包采用的是CTR模式。如下是加密相關(guān)代碼:
func encrypt(block cipher.Block, value []byte) ([]byte, error) { iv := GenerateRandomKey(block.BlockSize()) if iv == nil { return nil, errGeneratingIV } // Encrypt it. stream := cipher.NewCTR(block, iv) stream.XORKeyStream(value, value) // Return iv + ciphertext. return append(iv, value...), nil }
該對稱加密算法其實還可以應(yīng)用其他具有敏感信息的傳輸中,比如價格信息、密碼等。
base64編碼
經(jīng)過上述編碼(或加密)后的數(shù)據(jù)實際上是一串字節(jié)序列。如果轉(zhuǎn)換成字符串大家可以看到會有亂碼的出現(xiàn)。這里的亂碼實際上是不可見字符。如果想讓不可見字符變成可見字符,最常用的就是使用base64編碼。 base64編碼是將二進制字節(jié)轉(zhuǎn)換成文本的一種編碼方式。該編碼方式是將二進制字節(jié)轉(zhuǎn)換成可打印的asc碼。就是先預定義一個可見字符的編碼表,參考RFC4648文檔。然后將原字符串的二進制字節(jié)序列以每6位為一組進行分組,然后再將每組轉(zhuǎn)換成十進制對應(yīng)的數(shù)字,在根據(jù)該數(shù)字從預定義的編碼表中找到對應(yīng)的字符,最終組成的字符串就是經(jīng)過base64編碼的字符串。在base64編碼中有4種模式:
- base64.StdEncoding:標準模式是依據(jù)RFC 4648文檔實現(xiàn)的,最終轉(zhuǎn)換成的字符由A到Z、a-z、0-9以及+和 / 符號組成的。
- base64.URLEncoding: URLEncoding模式最終轉(zhuǎn)成的字符是由A到Z、a-z、0-9以及 - 和 _ 組成的。就是把標準模式中的+和/字符替換成了-和/。因為該模式主要應(yīng)用于URL地址傳輸中,而在URL中+和/是保留字符,不能出現(xiàn),所以講其做了替換。
- base64.RawEncoding: 該模式使用的字符集和StdEncoding一樣。但該模式是按照位數(shù)來的,每6bits換為一個base64字符,就沒有在尾部補齊到4的倍數(shù)字節(jié)了。
- base64.RawURLEncoding: 該模式使用的字符集和URLEncoding模式一樣。同樣該模式也是按照位數(shù)來的,每6bits換為一個base64字符,就沒有在尾部補齊到4的倍數(shù)字節(jié)了。
base64編碼的具體應(yīng)用和實現(xiàn)原理大家可參考我的另外一篇文章:
使用hmac做hash
簡單來講就是對字符串做了加密的hash轉(zhuǎn)換。在上文中我們提到,加密是可選的,hmac才是必需的。如果沒有使用加密,那么經(jīng)過上述序列化、base64編碼后的字符串依然是明文的。所以無論有沒有加密,都要做一次hash。這里使用的是內(nèi)建包crypto/hmac。
做hmac操作時,不是只對value值進行hash,而是經(jīng)過了字符串的拼接。實際上是對cookie名、日期、value值三部分進行拼接,并用 "|"隔開進行的:
代碼如下:
// 3. Create MAC for "name|date|value". Extra pipe to be used later. b = []byte(fmt.Sprintf("%s|%d|%s|", name, s.timestamp(), b)) mac := createMac(hmac.New(s.hashFunc, s.hashKey), b[:len(b)-1]) // Append mac, remove name. b = append(b, mac...)[len(name)+1:] // 4. Encode to base64. b = encode(b)
這里將name值拼接進字符串是因為在加碼驗證的時候可以對key-value對進行驗證,說明該value是屬于該name值的。 將時間戳拼接進去,主要是為了對cookie的有效期做驗證。在解密后,用當前時間和字符串中的時間做比較,就能知道該cookie值是否已經(jīng)過期了。
最后,將經(jīng)過hmac的hash值除去name值后再和b進行拼接。拼接完,為了在url中傳輸,所以再做一次base64的編碼。
相關(guān)知識:HMAC是密鑰相關(guān)的哈希運算消息認證碼(Hash-based Message Authentication Code)的縮寫,由H.Krawezyk,M.Bellare,R.Canetti于1996年提出的一種基于Hash函數(shù)和密鑰進行消息認證的方法。其能提供兩方面的內(nèi)容: ① 消息完整性認證:能夠證明消息內(nèi)容在傳送過程沒有被修改。 ② 信源身份認證:因為通信雙方共享了認證的密鑰,接收方能夠認證發(fā)送該數(shù)據(jù)的信源與所宣稱的一致,即能夠可靠地確認接收的消息與發(fā)送的一致。
四、beego框架中的cookie安全
筆者查看了常用的web框架echo、gin、beego,發(fā)現(xiàn)只有在beego框架中集成了安全的cookie設(shè)置。但也只實現(xiàn)了用hmac算法對value值和時間戳做加密hash。該實現(xiàn)在Controller的SetSecureCookie函數(shù)中,如下:
// SetSecureCookie puts value into cookie after encoded the value. func (c *Controller) SetSecureCookie(Secret, name, value string, others ...interface{}) { c.Ctx.SetSecureCookie(Secret, name, value, others...) } // SetSecureCookie Set Secure cookie for response. func (ctx *Context) SetSecureCookie(Secret, name, value string, others ...interface{}) { vs := base64.URLEncoding.EncodeToString([]byte(value)) timestamp := strconv.FormatInt(time.Now().UnixNano(), 10) h := hmac.New(sha256.New, []byte(Secret)) fmt.Fprintf(h, "%s%s", vs, timestamp) sig := fmt.Sprintf("%02x", h.Sum(nil)) cookie := strings.Join([]string{vs, timestamp, sig}, "|") ctx.Output.Cookie(name, cookie, others...) }
五、總結(jié)
經(jīng)過securecookie編碼過的cookie值是不會被偽造的,因為該值是經(jīng)過hmac進行編碼的。而且還可以對編碼過的值再進行一次對稱加密。如果是敏感信息的話,建議不要存儲在cookie中。同時,敏感的信息也一定使用https進行傳輸,以降低泄露的風險。
以上就是Go web中cookie值安全securecookie庫使用原理的詳細內(nèi)容,更多關(guān)于Go web securecookie庫的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
如何使用Go語言實現(xiàn)基于泛型的Jaccard相似度算法
這篇文章主要介紹了如何使用Go語言實現(xiàn)基于泛型的Jaccard相似度算法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-08-08