├── README.md
└── encode.go
/README.md:
--------------------------------------------------------------------------------
1 | # HPACK-Encode
2 | Encode strings with HPACK to produce a valid Server header for Nginx.
3 |
4 |
5 | The Nginx source contains the Server header "nginx" when using HTTP/2:
6 |
7 | static const u_char nginx[5] = "\x84\xaa\x63\x55\xe7";
8 |
9 | It can be seen here: https://trac.nginx.org/nginx/browser/nginx/src/http/v2/ngx_http_v2_filter_module.c#L148
10 |
11 |
12 | To replace this we need to HPACK encode the replacement string.
13 |
14 | Run the script with your desired Server header as the argument, and it will output the required line of code.
15 | If you get dependency errors, it may be necessary to run 'go get golang.org/x/net/http2/hpack' first.
16 |
17 | For example, running the script with `go run encode.go 'Encrypt All The Things!!!'` will output the following:
18 |
19 |
20 | static const u_char nginx[22] = "\x95\xc1\x51\x2c\xf5\x5a\x54\x86\x8a\x14\xdf\x39\x54\xdf\x39\xaa\x99\x1f\xc7\xf1\xfc\x7f";
21 |
22 | Replace this in the Nginx source and build.
23 |
24 |
25 | This script is based off the StackOverflow answer provided here.
26 |
--------------------------------------------------------------------------------
/encode.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "fmt"
5 | "golang.org/x/net/http2/hpack"
6 | "os"
7 | "strconv"
8 | )
9 |
10 | func main() {
11 | fmt.Println(Encode(os.Args[1]))
12 | }
13 |
14 | func Encode(s string) string {
15 | var result string
16 | var count int
17 |
18 | hd := hpack.AppendHuffmanString(nil, s)
19 | hl := hpack.HuffmanEncodeLength(s) | 0x80
20 |
21 | result += RenderByte(byte(hl))
22 |
23 | for _, b := range hd {
24 | result += RenderByte(b)
25 | count += 1
26 | }
27 |
28 | return "static const u_char nginx[" + strconv.Itoa(count + 1) + "] = \"" + string(result) + "\";"
29 | }
30 |
31 | func RenderByte(b byte) string {
32 | return fmt.Sprintf("\\x%x", b)
33 | }
34 |
--------------------------------------------------------------------------------