forked from rverton/webanalyze
-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker.go
199 lines (165 loc) · 4.37 KB
/
worker.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
package webanalyze
import (
"bytes"
"crypto/tls"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"
"sync"
"time"
"github.com/PuerkitoBio/goquery"
)
var Timeout = 8 * time.Second
// start n worker and let them listen on c for hosts to scan
func initWorker(count int, c chan *Job, results chan Result, wg *sync.WaitGroup) {
// start workers based on flag
for i := 0; i < count; i++ {
wg.Add(1)
go worker(c, results, wg)
}
}
// worker loops until channel is closed. processes a single host at once
func worker(c chan *Job, results chan Result, wg *sync.WaitGroup) {
for job := range c {
if !strings.HasPrefix(job.URL, "http://") && !strings.HasPrefix(job.URL, "https://") {
job.URL = fmt.Sprintf("http://%s", job.URL)
}
t0 := time.Now()
result, err := process(job)
t1 := time.Now()
res := Result{
Host: job.URL,
Matches: result,
Duration: t1.Sub(t0),
Error: err,
}
results <- res
}
wg.Done()
}
func fetchHost(host string) ([]byte, *http.Header, error) {
client := &http.Client{
Timeout: Timeout,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}}
req, err := http.NewRequest("GET", host, nil)
if err != nil {
return nil, nil, err
}
req.Header.Add("Accept", "*/*")
resp, err := client.Do(req)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil && err != io.EOF {
// ignore error, body/document not always needed
return nil, &resp.Header, nil
}
return body, &resp.Header, nil
}
// do http request and analyze response
func process(job *Job) ([]Match, error) {
var apps = make([]Match, 0)
if (job.Body == nil || len(job.Body) == 0) && !job.forceNotDownload {
_body, headers, err := fetchHost(job.URL)
if err != nil {
return nil, err
}
job.Body = _body
job.Headers = *headers
}
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(job.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(job.Body), app.HTMLRegex); len(m) > 0 {
findings.Matches = append(findings.Matches, m...)
findings.updateVersion(v)
}
// check response header
headerFindings, version := app.FindInHeaders(job.Headers)
findings.Matches = append(findings.Matches, headerFindings...)
findings.updateVersion(version)
// check url
if m, v := findMatches(job.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)
}
})
}
if len(findings.Matches) > 0 {
apps = append(apps, findings)
}
}
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 ""
}