forked from rverton/webanalyze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webanalyze.go
193 lines (155 loc) · 4.3 KB
/
webanalyze.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
package webanalyze
import (
"bytes"
"fmt"
"net/http"
"strings"
"github.com/PuerkitoBio/goquery"
)
// AppDefs provides access to the unmarshalled apps.json file
var AppDefs *AppsDefinition
// Match type encapsulates the App information from a match on a document
type Match struct {
App `json:"app"`
AppName string `json:"app_name"`
Matches [][]string `json:"matches"`
Version string `json:"version"`
}
func (m *Match) updateVersion(version string) {
if version != "" {
m.Version = version
}
}
// Analyze do http request (if necessary) and analyze response
func Analyze(url string, content *http.Response) ([]Match, error) {
buf := new(bytes.Buffer)
buf.ReadFrom(content.Body)
body := []byte(buf.String())
cookies := content.Cookies()
headers := content.Header
var apps = make([]Match, 0)
var err error
var cookiesMap = make(map[string]string)
for _, c := range cookies {
cookiesMap[c.Name] = c.Value
}
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(body))
if err != nil {
return nil, err
}
for appname, app := range AppDefs.Apps {
// TODO: Reduce complexity in this for-loop by functionalising out
// the sub-loops and checks.
findings := Match{
App: app,
AppName: appname,
Matches: make([][]string, 0),
}
// check raw html
if m, v := findMatches(string(body), app.HTMLRegex); len(m) > 0 {
findings.Matches = append(findings.Matches, m...)
findings.updateVersion(v)
}
// check response header
headerFindings, version := app.FindInHeaders(headers)
findings.Matches = append(findings.Matches, headerFindings...)
findings.updateVersion(version)
// check url
if m, v := findMatches(url, app.URLRegex); len(m) > 0 {
findings.Matches = append(findings.Matches, m...)
findings.updateVersion(v)
}
// check script tags
doc.Find("script").Each(func(i int, s *goquery.Selection) {
if script, exists := s.Attr("src"); exists {
if m, v := findMatches(script, app.ScriptRegex); len(m) > 0 {
findings.Matches = append(findings.Matches, m...)
findings.updateVersion(v)
}
}
})
// check meta tags
for _, h := range app.MetaRegex {
selector := fmt.Sprintf("meta[name='%s']", h.Name)
doc.Find(selector).Each(func(i int, s *goquery.Selection) {
content, _ := s.Attr("content")
if m, v := findMatches(content, []AppRegexp{h}); len(m) > 0 {
findings.Matches = append(findings.Matches, m...)
findings.updateVersion(v)
}
})
}
// check cookies
for _, c := range app.CookieRegex {
if _, ok := cookiesMap[c.Name]; ok {
// if there is a regexp set, ensure it matches.
// otherwise just add this as a match
if c.Regexp != nil {
// only match single AppRegexp on this specific cookie
if m, v := findMatches(cookiesMap[c.Name], []AppRegexp{c}); len(m) > 0 {
findings.Matches = append(findings.Matches, m...)
findings.updateVersion(v)
}
} else {
findings.Matches = append(findings.Matches, []string{c.Name})
}
}
}
if len(findings.Matches) > 0 {
apps = append(apps, findings)
// handle implies
for _, implies := range app.Implies {
for implyAppname, implyApp := range AppDefs.Apps {
if implies != implyAppname {
continue
}
f2 := Match{
App: implyApp,
AppName: implyAppname,
Matches: make([][]string, 0),
}
apps = append(apps, f2)
}
}
}
}
return apps, nil
}
// runs a list of regexes on content
func findMatches(content string, regexes []AppRegexp) ([][]string, string) {
var m [][]string
var version string
for _, r := range regexes {
matches := r.Regexp.FindAllStringSubmatch(content, -1)
if matches == nil {
continue
}
m = append(m, matches...)
if r.Version != "" {
version = findVersion(m, r.Version)
}
}
return m, version
}
// parses a version against matches
func findVersion(matches [][]string, version string) string {
/*
log.Printf("Matches: %v", matches)
log.Printf("Version: %v", version)
*/
var v string
for _, matchPair := range matches {
// replace backtraces (max: 3)
for i := 1; i <= 3; i++ {
bt := fmt.Sprintf("\\%v", i)
if strings.Contains(version, bt) && len(matchPair) >= i {
v = strings.Replace(version, bt, matchPair[i], 1)
}
}
// return first found version
if v != "" {
return v
}
}
return ""
}