This repository has been archived by the owner on Mar 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwappalyze.go
216 lines (172 loc) · 4.81 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
package webanalyze
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strings"
)
// WappalyzerURL is the link to the latest apps.json file in the Wappalyzer repo
const WappalyzerURL = "https://raw.githubusercontent.com/AliasIO/Wappalyzer/master/src/technologies.json"
// StringArray type is a wrapper for []string for use in unmarshalling the apps.json
type StringArray []string
// App type encapsulates all the data about an App from apps.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]string `json:"meta"`
HTML StringArray `json:"html"`
Script StringArray `json:"script"`
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 apps.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 apps.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
}
// DownloadFile pulls the latest apps.json file from the Wappalyzer github
func DownloadFile(from, to string) error {
resp, err := http.Get(from)
if err != nil {
return err
}
defer resp.Body.Close()
f, err := os.Create(to)
if err != nil {
return err
}
_, err = io.Copy(f, resp.Body)
return err
}
// 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.MetaRegex = compileNamedRegexes(app.Meta)
app.CookieRegex = compileNamedRegexes(app.Cookies)
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(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 {
// Split version detection
splitted := strings.Split(regexString, "\\;")
regex, err := regexp.Compile(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
}