Skip to content

Commit

Permalink
badkeys: update api a bit for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
hdm committed Jan 17, 2025
1 parent 3e75ec2 commit 3b3dcff
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion badkeys/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestCacheBasics(t *testing.T) {
t.Errorf("unexpected result: %s", diff)
}
expURL := "https://github.com/badkeys/debianopenssl/blob/main/rsa3072/ssh/be32/29491.key"
resURL := res.ToURL()
resURL := res.GetURL()
if resURL != expURL {
t.Errorf("unexpected url %s got %s", expURL, resURL)
}
Expand Down
21 changes: 18 additions & 3 deletions badkeys/result.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package badkeys

import "path"
import (
"path"
"strconv"
)

type Result struct {
Repo string
Expand All @@ -10,11 +13,23 @@ type Result struct {
RepoName string
KeyPath string
Private bool
Hash string
}

func (r *Result) ToURL() string {
func (r *Result) GetID() string {
if r.Private {
repStr := strconv.FormatUint(uint64(r.RepoID), 10)
return "badkeys-private-" + repStr + "-" + r.Hash
}
return "badkeys-" + r.RepoType + "-" + r.Repo + "-" + r.RepoPath + "-" + r.Hash
}

func (r *Result) GetURL() string {
if r.Private {
return "unpublished://" + r.GetID() + "-" + r.Hash
}
if r.RepoType != "github" {
return ""
return "https://" + r.RepoType + "/" + path.Join(r.Repo, "blob", r.RepoPath, r.KeyPath)
}
return "https://github.com/" + path.Join(r.Repo, "blob", r.RepoPath, r.KeyPath)
}
10 changes: 5 additions & 5 deletions cmd/check_hostkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func sshCheckBadKeysBlocklist(addr string, conf *ScanConfig, options *auth.Optio
return nil
}

for hkt, hkv := range root.HostKeys {
for _, hkv := range root.HostKeys {
raw, err := base64.StdEncoding.DecodeString(hkv)
if err != nil {
continue
Expand All @@ -44,16 +44,16 @@ func sshCheckBadKeysBlocklist(addr string, conf *ScanConfig, options *auth.Optio
hexPre := hex.EncodeToString(hpre)
conf.Logger.Warnf("%s %s found compromised unpublished hostkey with repo %s and hash %s", addr, tname, repStr, hexPre)
root.AddVuln(auth.VulnResult{
ID: "badkeys-private-" + repStr + "-" + hexPre,
ID: bkr.GetID(),
Ref: "https://badkeys.info/",
Proof: repStr + "-" + hexPre,
})
} else {
conf.Logger.Warnf("%s %s found compromised hostkey: %s", addr, tname, bkr.ToURL())
conf.Logger.Warnf("%s %s found compromised hostkey: %s", addr, tname, bkr.GetURL())
root.AddVuln(auth.VulnResult{
ID: "badkeys-" + bkr.RepoType + "-" + bkr.Repo + "-" + bkr.RepoPath + "-" + hkt,
ID: bkr.GetID(),
Ref: "https://badkeys.info/",
Proof: bkr.ToURL(),
Proof: bkr.GetURL(),
})
}
}
Expand Down
13 changes: 5 additions & 8 deletions cmd/cmd_analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"encoding/base64"
"encoding/csv"
"encoding/hex"
"encoding/json"
"io"
"os"
Expand Down Expand Up @@ -285,7 +284,7 @@ func isHoneypot(conf *ScanConfig, res *auth.AuthResult) bool {

func isBadKey(conf *ScanConfig, res *auth.AuthResult) bool {
found := 0
for hkt, hkv := range res.HostKeys {
for _, hkv := range res.HostKeys {
raw, err := base64.StdEncoding.DecodeString(hkv)
if err != nil {
continue
Expand All @@ -303,18 +302,16 @@ func isBadKey(conf *ScanConfig, res *auth.AuthResult) bool {
continue
}
if bkr.Private {
repStr := strconv.FormatUint(uint64(bkr.RepoID), 10)
hexPre := hex.EncodeToString(hpre)
res.AddVuln(auth.VulnResult{
ID: "badkeys-private-" + repStr + "-" + hexPre,
ID: bkr.GetID(),
Ref: "https://badkeys.info/",
Proof: repStr + "-" + hexPre,
Proof: bkr.GetURL(),
})
} else {
res.AddVuln(auth.VulnResult{
ID: "badkeys-" + bkr.RepoType + "-" + bkr.Repo + "-" + bkr.RepoPath + "-" + hkt,
ID: bkr.GetID(),
Ref: "https://badkeys.info/",
Proof: bkr.ToURL(),
Proof: bkr.GetURL(),
})
}
found++
Expand Down

0 comments on commit 3b3dcff

Please sign in to comment.