-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
205 lines (164 loc) · 4.49 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
Copyright 2019 Adevinta
*/
package main
import (
"bufio"
"context"
"crypto/tls"
"encoding/json"
"errors"
"net/http"
"net/url"
"time"
"github.com/sirupsen/logrus"
check "github.com/adevinta/vulcan-check-sdk"
"github.com/adevinta/vulcan-check-sdk/helpers"
"github.com/adevinta/vulcan-check-sdk/state"
checkstate "github.com/adevinta/vulcan-check-sdk/state"
report "github.com/adevinta/vulcan-report"
)
const (
checkName = "vulcan-http-headers"
observatoryBin = "httpobs-local-scan"
)
type options struct {
// Override default ports.
HTTPPort string `json:"http-port"`
HTTPSPort string `json:"https-port"`
}
var behindOktaVuln = report.Vulnerability{
Summary: "Okta Authentication",
Description: "The asset is not reachable because it is behind Okta.",
Score: report.SeverityThresholdNone,
Labels: []string{"issue", "http"},
}
func main() {
run := func(ctx context.Context, target, assetType, optJSON string, state checkstate.State) error {
logger := check.NewCheckLog(checkName)
e := logger.WithFields(logrus.Fields{"target": target, "options": optJSON})
if target == "" {
return errors.New("missing check target")
}
isReachable, err := helpers.IsReachable(target, assetType, nil)
if err != nil {
logger.Warnf("Can not check asset reachability: %v", err)
}
if !isReachable {
return checkstate.ErrAssetUnreachable
}
// TODO: This is maybe too concrete for the check as maybe there are some targets behind other kind of
// SSO.
u := hostnameToURL(target)
if u == nil {
e.Debug("No HTTP server on target")
return nil
}
behindSSO, redirectingTo, err := helpers.IsRedirectingTo(u.String(), helpers.OKTADomain)
if err != nil {
// From go doc: "Any returned error will be of type *url.Error. The
// url.Error value's Timeout method will report true if request
// timed out or was canceled."
// https://golang.org/pkg/net/http/#Client.Do
e, ok := err.(*url.Error)
if !ok || !e.Timeout() {
return err
}
return nil
}
if behindSSO {
v := buildBehindOktaVuln(target, redirectingTo)
state.AddVulnerabilities(v)
return nil
}
var opt options
if optJSON != "" {
if err := json.Unmarshal([]byte(optJSON), &opt); err != nil {
return err
}
}
var scanner observatoryScanner
checker := check.NewProcessChecker(
observatoryBin,
[]string{
target,
},
bufio.ScanLines,
&scanner,
)
if _, err := checker.Run(ctx); err != nil {
return err
}
e.WithFields(logrus.Fields{"raw": string(scanner.output)}).Debug("Raw output scanned")
var res observatoryResult
if err := json.Unmarshal(scanner.output, &res); err != nil {
return err
}
e.WithFields(logrus.Fields{"parsed": res}).Debug("Results parsed")
return processResults(target, res, state)
}
c := check.NewCheckFromHandler(checkName, run)
c.RunAndServe()
}
func buildBehindOktaVuln(target, redirectingTo string) report.Vulnerability {
behindOktaVuln.AffectedResource = target
res := report.ResourcesGroup{
Name: "",
Header: []string{
"Target",
"RedirectingTo",
},
}
res.Rows = []map[string]string{
map[string]string{
"Target": target,
"RedirectingTo": redirectingTo,
},
}
behindOktaVuln.Resources = append(behindOktaVuln.Resources, res)
behindOktaVuln.Fingerprint = helpers.ComputeFingerprint(behindOktaVuln.Resources)
return behindOktaVuln
}
func processResults(target string, r observatoryResult, s state.State) error {
if r.Error != "" {
return errors.New(r.Error)
}
if err := processCSP(cspVuln, target, r, s); err != nil {
return err
}
processCookies(cookiesVuln, target, r, s)
processCORS(corsVuln, target, r, s)
processRedirect(redirectVuln, target, r, s)
processReferrer(referrerVuln, target, r, s)
processHSTS(hstsVuln, target, r, s)
processSRI(sriVuln, target, r, s)
processXContent(xContentVuln, target, r, s)
processXFrame(xFrameVuln, target, r, s)
processGrading(observatoryGrading, target, r, s)
return nil
}
func hostnameToURL(hostname string) *url.URL {
u := url.URL{}
u.Path = "//"
u.Host = hostname
for _, scheme := range []string{"https", "http"} {
u.Scheme = scheme
timeout := 5 * time.Second
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
Transport: tr,
Timeout: timeout,
}
_, err := client.Get(u.String())
if err != nil {
continue
}
return &u
}
return nil
}