forked from projectdiscovery/wappalyzergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtech.go
273 lines (234 loc) · 7.74 KB
/
tech.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
package wappalyzer
import (
"bytes"
"encoding/json"
"os"
"strings"
)
// Wappalyze is a client for working with tech detection
type Wappalyze struct {
original *Fingerprints
fingerprints *CompiledFingerprints
}
// New creates a new tech detection instance
func New(customFilePaths ...string) (*Wappalyze, error) {
wappalyze := &Wappalyze{
fingerprints: &CompiledFingerprints{
Apps: make(map[string]*CompiledFingerprint),
},
}
var customFilePath string
if len(customFilePaths) > 0 {
customFilePath = customFilePaths[0]
}
err := wappalyze.loadFingerprints(customFilePath)
if err != nil {
return nil, err
}
return wappalyze, nil
}
// GetFingerprints returns the original fingerprints
func (s *Wappalyze) GetFingerprints() *Fingerprints {
return s.original
}
// GetCompiledFingerprints returns the compiled fingerprints
func (s *Wappalyze) GetCompiledFingerprints() *CompiledFingerprints {
return s.fingerprints
}
// loadFingerprints loads the fingerprints from the embedded JSON and optionally from a custom JSON file
func (s *Wappalyze) loadFingerprints(customFilePath string) error {
var fingerprintsStruct Fingerprints
// Load embedded fingerprints
err := json.Unmarshal([]byte(fingerprints), &fingerprintsStruct)
if err != nil {
return err
}
// Load custom fingerprints if a file path is provided
if customFilePath != "" {
customData, err := os.ReadFile(customFilePath)
if err != nil {
return err
}
var customFingerprints Fingerprints
err = json.Unmarshal(customData, &customFingerprints)
if err != nil {
return err
}
// Merge custom fingerprints into the existing ones
for app, fingerprint := range customFingerprints.Apps {
fingerprintsStruct.Apps[app] = fingerprint
}
}
s.original = &fingerprintsStruct
for i, fingerprint := range fingerprintsStruct.Apps {
s.fingerprints.Apps[i] = compileFingerprint(fingerprint)
}
return nil
}
// Fingerprint identifies technologies on a target,
// based on the received response headers and body.
//
// Body should not be mutated while this function is being called, or it may
// lead to unexpected things.
func (s *Wappalyze) Fingerprint(headers map[string][]string, body []byte) map[string]struct{} {
uniqueFingerprints := NewUniqueFingerprints()
// Lowercase everything that we have received to check
normalizedBody := bytes.ToLower(body)
normalizedHeaders := s.normalizeHeaders(headers)
// Run header based fingerprinting if the number
// of header checks if more than 0.
for _, app := range s.checkHeaders(normalizedHeaders) {
uniqueFingerprints.SetIfNotExists(app.application, app.version, app.confidence)
}
cookies := s.findSetCookie(normalizedHeaders)
// Run cookie based fingerprinting if we have a set-cookie header
if len(cookies) > 0 {
for _, app := range s.checkCookies(cookies) {
uniqueFingerprints.SetIfNotExists(app.application, app.version, app.confidence)
}
}
// Check for stuff in the body finally
bodyTech := s.checkBody(normalizedBody)
for _, app := range bodyTech {
uniqueFingerprints.SetIfNotExists(app.application, app.version, app.confidence)
}
return uniqueFingerprints.GetValues()
}
type UniqueFingerprints struct {
values map[string]uniqueFingerprintMetadata
}
type uniqueFingerprintMetadata struct {
confidence int
version string
}
func NewUniqueFingerprints() UniqueFingerprints {
return UniqueFingerprints{
values: make(map[string]uniqueFingerprintMetadata),
}
}
func (u UniqueFingerprints) GetValues() map[string]struct{} {
values := make(map[string]struct{}, len(u.values))
for k, v := range u.values {
if v.confidence == 0 {
continue
}
values[FormatAppVersion(k, v.version)] = struct{}{}
}
return values
}
const versionSeparator = ":"
func (u UniqueFingerprints) SetIfNotExists(value, version string, confidence int) {
if _, ok := u.values[value]; ok {
new := u.values[value]
updatedConfidence := new.confidence + confidence
if updatedConfidence > 100 {
updatedConfidence = 100
}
new.confidence = updatedConfidence
if new.version == "" && version != "" {
new.version = version
}
u.values[value] = new
return
}
u.values[value] = uniqueFingerprintMetadata{
confidence: confidence,
version: version,
}
}
type matchPartResult struct {
application string
confidence int
version string
}
// FingerprintWithTitle identifies technologies on a target,
// based on the received response headers and body.
// It also returns the title of the page.
//
// Body should not be mutated while this function is being called, or it may
// lead to unexpected things.
func (s *Wappalyze) FingerprintWithTitle(headers map[string][]string, body []byte) (map[string]struct{}, string) {
uniqueFingerprints := NewUniqueFingerprints()
// Lowercase everything that we have received to check
normalizedBody := bytes.ToLower(body)
normalizedHeaders := s.normalizeHeaders(headers)
// Run header based fingerprinting if the number
// of header checks if more than 0.
for _, app := range s.checkHeaders(normalizedHeaders) {
uniqueFingerprints.SetIfNotExists(app.application, app.version, app.confidence)
}
cookies := s.findSetCookie(normalizedHeaders)
// Run cookie based fingerprinting if we have a set-cookie header
if len(cookies) > 0 {
for _, app := range s.checkCookies(cookies) {
uniqueFingerprints.SetIfNotExists(app.application, app.version, app.confidence)
}
}
// Check for stuff in the body finally
if strings.Contains(normalizedHeaders["content-type"], "text/html") {
bodyTech := s.checkBody(normalizedBody)
for _, app := range bodyTech {
uniqueFingerprints.SetIfNotExists(app.application, app.version, app.confidence)
}
title := s.getTitle(body)
return uniqueFingerprints.GetValues(), title
}
return uniqueFingerprints.GetValues(), ""
}
// FingerprintWithInfo identifies technologies on a target,
// based on the received response headers and body.
// It also returns basic information about the technology, such as description
// and website URL as well as icon.
//
// Body should not be mutated while this function is being called, or it may
// lead to unexpected things.
func (s *Wappalyze) FingerprintWithInfo(headers map[string][]string, body []byte) map[string]AppInfo {
apps := s.Fingerprint(headers, body)
result := make(map[string]AppInfo, len(apps))
for app := range apps {
if fingerprint, ok := s.fingerprints.Apps[app]; ok {
result[app] = AppInfoFromFingerprint(fingerprint)
}
// Handle colon separated values
if strings.Contains(app, versionSeparator) {
if parts := strings.Split(app, versionSeparator); len(parts) == 2 {
if fingerprint, ok := s.fingerprints.Apps[parts[0]]; ok {
result[app] = AppInfoFromFingerprint(fingerprint)
}
}
}
}
return result
}
func AppInfoFromFingerprint(fingerprint *CompiledFingerprint) AppInfo {
categories := make([]string, 0, len(fingerprint.cats))
for _, cat := range fingerprint.cats {
if category, ok := categoriesMapping[cat]; ok {
categories = append(categories, category.Name)
}
}
return AppInfo{
Description: fingerprint.description,
Website: fingerprint.website,
Icon: fingerprint.icon,
CPE: fingerprint.cpe,
Categories: categories,
}
}
// FingerprintWithCats identifies technologies on a target,
// based on the received response headers and body.
// It also returns categories information about the technology, is there's any
// Body should not be mutated while this function is being called, or it may
// lead to unexpected things.
func (s *Wappalyze) FingerprintWithCats(headers map[string][]string, body []byte) map[string]CatsInfo {
apps := s.Fingerprint(headers, body)
result := make(map[string]CatsInfo, len(apps))
for app := range apps {
if fingerprint, ok := s.fingerprints.Apps[app]; ok {
result[app] = CatsInfo{
Cats: fingerprint.cats,
}
}
}
return result
}