-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathwappalyze.go
278 lines (220 loc) · 6.26 KB
/
wappalyze.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package webanalyze
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"regexp"
"strings"
)
const WappazlyerRoot = "https://raw.githubusercontent.com/enthec/webappanalyzer/main/src"
// StringArray type is a wrapper for []string for use in unmarshalling the technologies.json
type StringArray []string
// App type encapsulates all the data about an App from technologies.json
type App struct {
Cats StringArray `json:"cats"`
CatNames []string `json:"category_names"`
Cookies map[string]string `json:"cookies"`
Headers map[string]string `json:"headers"`
Meta map[string]StringArray `json:"meta"`
HTML StringArray `json:"html"`
Script StringArray `json:"scripts"`
URL StringArray `json:"url"`
Website string `json:"website"`
Implies StringArray `json:"implies"`
HTMLRegex []AppRegexp `json:"-"`
ScriptRegex []AppRegexp `json:"-"`
URLRegex []AppRegexp `json:"-"`
HeaderRegex []AppRegexp `json:"-"`
MetaRegex []AppRegexp `json:"-"`
CookieRegex []AppRegexp `json:"-"`
}
// Category names defined by wappalyzer
type Category struct {
Name string `json:"name"`
}
// AppsDefinition type encapsulates the json encoding of the whole technologies.json file
type AppsDefinition struct {
Apps map[string]App `json:"technologies"`
Cats map[string]Category `json:"categories"`
}
type AppRegexp struct {
Name string
Regexp *regexp.Regexp
Version string
}
func (app *App) FindInHeaders(headers http.Header) (matches [][]string, version string) {
var v string
for _, hre := range app.HeaderRegex {
if headers.Get(hre.Name) == "" {
continue
}
hk := http.CanonicalHeaderKey(hre.Name)
for _, headerValue := range headers[hk] {
if headerValue == "" {
continue
}
if m, version := findMatches(headerValue, []AppRegexp{hre}); len(m) > 0 {
matches = append(matches, m...)
v = version
}
}
}
return matches, v
}
// UnmarshalJSON is a custom unmarshaler for handling bogus technologies.json types from wappalyzer
func (t *StringArray) UnmarshalJSON(data []byte) error {
var s string
var sa []string
var na []int
if err := json.Unmarshal(data, &s); err != nil {
if err := json.Unmarshal(data, &na); err == nil {
// not a string, so maybe []int?
*t = make(StringArray, len(na))
for i, number := range na {
(*t)[i] = fmt.Sprintf("%d", number)
}
return nil
} else if err := json.Unmarshal(data, &sa); err == nil {
// not a string, so maybe []string?
*t = sa
return nil
}
fmt.Println(string(data))
return err
}
*t = StringArray{s}
return nil
}
func downloadTechnologies() (map[string]App, error) {
apps := make(map[string]App)
files := []string{"_", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
count := 0
for _, f := range files {
m := make(map[string]App)
url := fmt.Sprintf("%v/technologies/%v.json", WappazlyerRoot, f)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
if err := json.NewDecoder(resp.Body).Decode(&m); err != nil {
return nil, err
}
for key := range m {
apps[key] = m[key]
count = count + 1
}
resp.Body.Close()
}
return apps, nil
}
func downloadCategories() (map[string]Category, error) {
url := fmt.Sprintf("%v/categories.json", WappazlyerRoot)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
m := make(map[string]Category)
if err := json.NewDecoder(resp.Body).Decode(&m); err != nil {
return nil, err
}
return m, nil
}
// DownloadFile pulls the latest technologies.json file from the Wappalyzer github
func DownloadFile(to string) error {
// step1: download categories
categories, err := downloadCategories()
if err != nil {
return err
}
// step2: download technoligies from _, a-z
appDefs, err := downloadTechnologies()
if err != nil {
return err
}
technologiesFile := AppsDefinition{
Apps: appDefs,
Cats: categories,
}
data, _ := json.MarshalIndent(technologiesFile, "", " ")
return ioutil.WriteFile(to, data, 0644)
}
// load apps from io.Reader
func (wa *WebAnalyzer) loadApps(r io.Reader) error {
dec := json.NewDecoder(r)
if err := dec.Decode(&wa.appDefs); err != nil {
return err
}
for key, value := range wa.appDefs.Apps {
app := wa.appDefs.Apps[key]
app.HTMLRegex = compileRegexes(value.HTML)
app.ScriptRegex = compileRegexes(value.Script)
app.URLRegex = compileRegexes(value.URL)
app.HeaderRegex = compileNamedRegexes(app.Headers)
app.CookieRegex = compileNamedRegexes(app.Cookies)
// handle special meta field where value can be a list
// of strings. we join them as a simple regex here
metaRegex := make(map[string]string)
for k, v := range app.Meta {
metaRegex[k] = strings.Join(v, "|")
}
app.MetaRegex = compileNamedRegexes(metaRegex)
app.CatNames = make([]string, 0)
for _, cid := range app.Cats {
if category, ok := wa.appDefs.Cats[string(cid)]; ok && category.Name != "" {
app.CatNames = append(app.CatNames, category.Name)
}
}
wa.appDefs.Apps[key] = app
}
return nil
}
func compileNamedRegexes(from map[string]string) []AppRegexp {
var list []AppRegexp
for key, value := range from {
h := AppRegexp{
Name: key,
}
if value == "" {
value = ".*"
}
// Filter out webapplyzer attributes from regular expression
splitted := strings.Split(value, "\\;")
r, err := regexp.Compile("(?i)" + splitted[0])
if err != nil {
continue
}
if len(splitted) > 1 && strings.HasPrefix(splitted[1], "version:") {
h.Version = splitted[1][8:]
}
h.Regexp = r
list = append(list, h)
}
return list
}
func compileRegexes(s StringArray) []AppRegexp {
var list []AppRegexp
for _, regexString := range s {
if regexString == "" {
continue
}
// Split version detection
splitted := strings.Split(regexString, "\\;")
regex, err := regexp.Compile("(?i)" + splitted[0])
if err != nil {
// ignore failed compiling for now
// log.Printf("warning: compiling regexp for failed: %v", regexString, err)
} else {
rv := AppRegexp{
Regexp: regex,
}
if len(splitted) > 1 && strings.HasPrefix(splitted[0], "version") {
rv.Version = splitted[1][8:]
}
list = append(list, rv)
}
}
return list
}