-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwappalyzer.go
244 lines (230 loc) · 5.63 KB
/
wappalyzer.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
package wappalyzer
import (
"embed"
"encoding/json"
"fmt"
"io/fs"
"log"
"strings"
"sync"
)
var schemas Schema
var groups Groups
var categories Categories
var wappalyzer_fs fs.FS
var icon_url string
// 只需运行一次 - 第一个指纹wr不包含
func InitWappalyzerDB(wr embed.FS, file_ string) error {
wr_sub, err := fs.Sub(wr, "wappalyzer")
if err != nil {
return err
}
wappalyzer_fs = wr_sub
schemas = make(Schema)
technologies := "src/technologies/"
for i := 0; i < 27; i++ {
var chr = string(rune(96 + i))
file_content := make([]byte, 0)
if chr == "`" {
file_content = []byte(file_)
} else {
file_content, err = fs.ReadFile(wr_sub, technologies+chr+".json")
if err != nil {
return err
}
}
var schema Schema
err = json.Unmarshal(file_content, &schema)
if err != nil {
return err
}
for k, v := range schema {
schemas[k] = v
}
}
groups = make(Groups)
groups_file, err := fs.ReadFile(wr_sub, "src/groups.json")
if err != nil {
return err
}
err = json.Unmarshal(groups_file, &groups)
if err != nil {
return err
}
categories = make(Categories)
categories_file, err := fs.ReadFile(wr_sub, "src/categories.json")
if err != nil {
return err
}
err = json.Unmarshal(categories_file, &categories)
if err != nil {
return err
}
icon_null_count := 0
for name, val := range schemas {
// 测试是否有未知类型
_, err = TypeTest(val.Implies)
if err != nil {
return fmt.Errorf("%s %s", name, err)
}
_, err = TypeTest(val.Requires)
if err != nil {
return fmt.Errorf("%s %s", name, err)
}
_, err = TypeTest(val.RequiresCategory)
if err != nil {
return fmt.Errorf("%s %s", name, err)
}
_, err = TypeTest(val.Excludes)
if err != nil {
return fmt.Errorf("%s %s", name, err)
}
_, err = TypeTest(val.DOM)
if err != nil {
return fmt.Errorf("%s %s", name, err)
}
_, err = TypeTest(val.DNS)
if err != nil {
return fmt.Errorf("%s %s", name, err)
}
_, err = TypeTest(val.HTML)
if err != nil {
return fmt.Errorf("%s %s", name, err)
}
_, err = TypeTest(val.TEXT)
if err != nil {
return fmt.Errorf("%s %s", name, err)
}
_, err = TypeTest(val.CSS)
if err != nil {
return fmt.Errorf("%s %s", name, err)
}
_, err = TypeTest(val.Robots)
if err != nil {
return fmt.Errorf("%s %s", name, err)
}
_, err = TypeTest(val.URL)
if err != nil {
return fmt.Errorf("%s %s", name, err)
}
_, err = TypeTest(val.XHR)
if err != nil {
return fmt.Errorf("%s %s", name, err)
}
_, err = TypeTest(val.Meta)
if err != nil {
return fmt.Errorf("%s %s", name, err)
}
_, err = TypeTest(val.ScriptSrc)
if err != nil {
return fmt.Errorf("%s %s", name, err)
}
// 测试ICON是否读取正常
if val.ICON != "" && !strings.Contains(val.ICON, "<") {
// new version
_, err = fs.ReadFile(wr_sub, fmt.Sprintf("src/images/icons/%s", val.ICON))
if err != nil {
// old version
_, err = fs.ReadFile(wr_sub, fmt.Sprintf("src/drivers/webextension/images/icons/%s", val.ICON))
if err != nil {
log.Println(err)
}
}
continue
}
icon_null_count++
}
log.Println(fmt.Sprintf("wappalyzer fingers count %d, groups count %d, categories count %d, no icon count %d", len(schemas), len(groups), len(categories), icon_null_count))
return nil
}
/*
// 如果需要读取ICON信息
// 指纹识别: 读取ICON
func (a *api) GetICON(c *gin.Context) {
icon := c.Query("icon")
readICON := wappalyzer.ReadICON(icon)
if strings.HasSuffix(icon, "svg") {
c.Header("Content-Type", "image/svg+xml")
}
c.String(200, readICON)
}
*/
func SetReadICONURL(url string) {
icon_url = url
}
type Wappalyzer struct {
Technologies map[string]Technologie
lock sync.Mutex
displayError bool
}
type Technologie struct {
Name string `json:"name"` // 名称
Confidence int `json:"confidence"` // 价值
Version string `json:"version"` // 版本
Icon string `json:"icon"` // 产品标识
Website string `json:"website"` // 产品网站
Cpe string `json:"cpe"` // CPE
Categories []Categorie `json:"categories"` // 产品分类
}
type Categorie struct {
ID int `json:"id"`
Name string `json:"name"`
}
func NewWappalyzer(displayError bool) *Wappalyzer {
ts := make(map[string]Technologie)
return &Wappalyzer{Technologies: ts, displayError: displayError}
}
func (w *Wappalyzer) GetFingers() map[string]Technologie {
for name := range w.Technologies {
if schemas[name].Excludes == nil {
continue
}
switch exc := TypeDetect(schemas[name].Excludes).(type) {
case string:
delete(w.Technologies, exc)
break
case []string:
for i := 0; i < len(exc); i++ {
delete(w.Technologies, exc[i])
}
break
default:
fmt.Println("found no excludes type", schemas[name].Excludes)
}
}
for name := range w.Technologies {
if schemas[name].Implies == nil {
continue
}
switch exc := TypeDetect(schemas[name].Implies).(type) {
case string:
w.setFinger(name, schemas[name], 100, "")
break
case []string:
for i := 0; i < len(exc); i++ {
w.setFinger(name, schemas[exc[i]], 100, "")
}
break
default:
fmt.Println("found no implies type", schemas[name].Excludes)
}
}
for name, value := range w.Technologies {
if value.Icon == "" {
continue
}
value.Icon = icon_url + value.Icon
w.Technologies[name] = value
}
return w.Technologies
}
func ReadICON(filename string) string {
icon, err := fs.ReadFile(wappalyzer_fs, "src/images/icons/"+filename)
if err != nil {
icon, err = fs.ReadFile(wappalyzer_fs, "src/drivers/webextension/images/icons/"+filename)
if err != nil {
return ""
}
}
return string(icon)
}