Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Security: Padding done right #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion gdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ func (R *Gdns) worker(server string) {
}
}

// To prevent misinterpretation of the URL, restrict the padding characters to the unreserved URL characters:
// upper- and lower-case letters, digits, hyphen, period, underscore and tilde. http://stackoverflow.com/a/695469/18829
const padChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~"

func getPaddedStr(n int) string {
s := make([]byte, n)
for i := range s {
s[i] = padChars[rand.Intn(len(padChars))]
}
return string(s)
}

func (R *Gdns) resolve(https *Https, server string, qname string, qtype int) *Reply {
r := Reply{ Status: -1 }
v := url.Values{}
Expand All @@ -80,7 +92,9 @@ func (R *Gdns) resolve(https *Https, server string, qname string, qtype int) *Re
v.Set("edns_client_subnet", *R.edns)
}
if !*R.nopad {
v.Set("random_padding", strings.Repeat(string(65+rand.Intn(26)), rand.Intn(500)))
// maximum dnslength+type.length (longest possible Type 5 digits)
// minus current to make always equal query lenght url
v.Set("random_padding", getPaddedStr(259-len(qname)-len(fmt.Sprintf("%d", qtype))))
}

/* query */
Expand All @@ -96,3 +110,7 @@ func (R *Gdns) resolve(https *Https, server string, qname string, qtype int) *Re

/* register module */
var _ = register("gdns", new(Gdns))

func initRand() {
rand.Seed(time.Now().UnixNano())
}