|
| 1 | +package output |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/sha256" |
| 5 | + "encoding/hex" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "sort" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +// SARIF (2.1.0) output for takeover candidates (#129), so findings can be |
| 13 | +// uploaded to GitHub code scanning (or any SARIF consumer) and tracked, |
| 14 | +// triaged and dismissed there. Only names with a takeover hint are results; |
| 15 | +// a run with none writes a valid document with no results. |
| 16 | + |
| 17 | +const sarifHelp = "https://tmhsdigital.github.io/subenum/" |
| 18 | + |
| 19 | +type sarifLog struct { |
| 20 | + Schema string `json:"$schema"` |
| 21 | + Version string `json:"version"` |
| 22 | + Runs []sarifRun `json:"runs"` |
| 23 | +} |
| 24 | + |
| 25 | +type sarifRun struct { |
| 26 | + Tool sarifTool `json:"tool"` |
| 27 | + Results []sarifResult `json:"results"` |
| 28 | +} |
| 29 | + |
| 30 | +type sarifTool struct { |
| 31 | + Driver sarifDriver `json:"driver"` |
| 32 | +} |
| 33 | + |
| 34 | +type sarifDriver struct { |
| 35 | + Name string `json:"name"` |
| 36 | + Version string `json:"version,omitempty"` |
| 37 | + InformationURI string `json:"informationUri"` |
| 38 | + Rules []sarifRule `json:"rules"` |
| 39 | +} |
| 40 | + |
| 41 | +type sarifRule struct { |
| 42 | + ID string `json:"id"` |
| 43 | + Name string `json:"name"` |
| 44 | + ShortDescription sarifText `json:"shortDescription"` |
| 45 | + FullDescription sarifText `json:"fullDescription"` |
| 46 | + HelpURI string `json:"helpUri"` |
| 47 | + DefaultConfiguration sarifRuleLevel `json:"defaultConfiguration"` |
| 48 | + Properties sarifRuleProps `json:"properties"` |
| 49 | +} |
| 50 | + |
| 51 | +type sarifRuleLevel struct { |
| 52 | + Level string `json:"level"` |
| 53 | +} |
| 54 | + |
| 55 | +type sarifRuleProps struct { |
| 56 | + Tags []string `json:"tags"` |
| 57 | + SecuritySeverity string `json:"security-severity"` |
| 58 | +} |
| 59 | + |
| 60 | +type sarifText struct { |
| 61 | + Text string `json:"text"` |
| 62 | +} |
| 63 | + |
| 64 | +type sarifResult struct { |
| 65 | + RuleID string `json:"ruleId"` |
| 66 | + Level string `json:"level"` |
| 67 | + Message sarifText `json:"message"` |
| 68 | + Locations []sarifLocation `json:"locations"` |
| 69 | + PartialFingerprints map[string]string `json:"partialFingerprints"` |
| 70 | +} |
| 71 | + |
| 72 | +type sarifLocation struct { |
| 73 | + PhysicalLocation sarifPhysical `json:"physicalLocation"` |
| 74 | +} |
| 75 | + |
| 76 | +type sarifPhysical struct { |
| 77 | + ArtifactLocation sarifArtifact `json:"artifactLocation"` |
| 78 | +} |
| 79 | + |
| 80 | +type sarifArtifact struct { |
| 81 | + URI string `json:"uri"` |
| 82 | +} |
| 83 | + |
| 84 | +// takeoverRule maps a takeover marker ("dangling", "provider:heroku", |
| 85 | +// "dangling:heroku") to its SARIF rule. |
| 86 | +func takeoverRule(marker string) sarifRule { |
| 87 | + kind, provider, _ := strings.Cut(marker, ":") |
| 88 | + r := sarifRule{HelpURI: sarifHelp + "cli.html", Properties: sarifRuleProps{Tags: []string{"security", "subdomain-takeover", "dns"}}} |
| 89 | + switch { |
| 90 | + case kind == "dangling" && provider == "": |
| 91 | + r.ID, r.Name = "subenum/dangling-cname", "DanglingCNAME" |
| 92 | + r.ShortDescription.Text = "CNAME points at a name that does not exist" |
| 93 | + r.FullDescription.Text = "The subdomain is an alias for a name that returns NXDOMAIN. Whoever registers or claims that name may control the subdomain. Remove the record or reclaim the target." |
| 94 | + r.DefaultConfiguration.Level, r.Properties.SecuritySeverity = "error", "7.5" |
| 95 | + case kind == "dangling": |
| 96 | + r.ID, r.Name = "subenum/dangling-cname/"+provider, "DanglingCNAMEAt"+provider |
| 97 | + r.ShortDescription.Text = "CNAME points at an unclaimed " + provider + " name" |
| 98 | + r.FullDescription.Text = "The subdomain is an alias for a " + provider + " name that returns NXDOMAIN. " + provider + " lets anyone claim such names, so this is a likely subdomain takeover. Remove the record or reclaim the resource." |
| 99 | + r.DefaultConfiguration.Level, r.Properties.SecuritySeverity = "error", "8.6" |
| 100 | + default: |
| 101 | + r.ID, r.Name = "subenum/takeover-prone-provider/"+provider, "TakeoverProneProvider"+provider |
| 102 | + r.ShortDescription.Text = "CNAME points at " + provider + ", a takeover-prone service" |
| 103 | + r.FullDescription.Text = "The subdomain is an alias for a " + provider + " resource. It resolves today, but if the resource is deleted while the record stays, anyone can claim it. Keep the record and the resource in the same lifecycle." |
| 104 | + r.DefaultConfiguration.Level, r.Properties.SecuritySeverity = "note", "3.0" |
| 105 | + } |
| 106 | + return r |
| 107 | +} |
| 108 | + |
| 109 | +// WriteSARIF writes the takeover candidates among results to path as a |
| 110 | +// SARIF 2.1.0 log, atomically. version is the tool version. |
| 111 | +func WriteSARIF(path, version string, results []Result) error { |
| 112 | + rules := map[string]sarifRule{} |
| 113 | + var out []sarifResult |
| 114 | + for _, r := range results { |
| 115 | + if r.Takeover == "" { |
| 116 | + continue |
| 117 | + } |
| 118 | + rule := takeoverRule(r.Takeover) |
| 119 | + rules[rule.ID] = rule |
| 120 | + target := "" |
| 121 | + for _, rec := range r.Records { |
| 122 | + if rec.Type == "CNAME" { |
| 123 | + target = strings.TrimSuffix(rec.Value, ".") |
| 124 | + } |
| 125 | + } |
| 126 | + msg := fmt.Sprintf("%s: %s", r.Subdomain, rule.ShortDescription.Text) |
| 127 | + if target != "" { |
| 128 | + msg = fmt.Sprintf("%s is a CNAME for %s. %s.", r.Subdomain, target, rule.ShortDescription.Text) |
| 129 | + } |
| 130 | + sum := sha256.Sum256([]byte(r.Subdomain + "\x00" + r.Takeover)) |
| 131 | + out = append(out, sarifResult{ |
| 132 | + RuleID: rule.ID, |
| 133 | + Level: rule.DefaultConfiguration.Level, |
| 134 | + Message: sarifText{Text: msg}, |
| 135 | + Locations: []sarifLocation{{PhysicalLocation: sarifPhysical{ArtifactLocation: sarifArtifact{URI: r.Subdomain}}}}, |
| 136 | + PartialFingerprints: map[string]string{"subenumTakeover/v1": hex.EncodeToString(sum[:16])}, |
| 137 | + }) |
| 138 | + } |
| 139 | + ids := make([]string, 0, len(rules)) |
| 140 | + for id := range rules { |
| 141 | + ids = append(ids, id) |
| 142 | + } |
| 143 | + sort.Strings(ids) |
| 144 | + driver := sarifDriver{Name: "subenum", Version: version, InformationURI: sarifHelp, Rules: []sarifRule{}} |
| 145 | + for _, id := range ids { |
| 146 | + driver.Rules = append(driver.Rules, rules[id]) |
| 147 | + } |
| 148 | + if out == nil { |
| 149 | + out = []sarifResult{} |
| 150 | + } |
| 151 | + doc := sarifLog{ |
| 152 | + Schema: "https://json.schemastore.org/sarif-2.1.0.json", |
| 153 | + Version: "2.1.0", |
| 154 | + Runs: []sarifRun{{Tool: sarifTool{Driver: driver}, Results: out}}, |
| 155 | + } |
| 156 | + data, err := json.MarshalIndent(doc, "", " ") |
| 157 | + if err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + f, err := CreateFile(path) |
| 161 | + if err != nil { |
| 162 | + return err |
| 163 | + } |
| 164 | + _, werr := f.Write(append(data, '\n')) |
| 165 | + if cerr := f.Close(werr == nil); werr == nil { |
| 166 | + werr = cerr |
| 167 | + } |
| 168 | + return werr |
| 169 | +} |
0 commit comments