forked from projectdiscovery/wappalyzergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtech.go
205 lines (176 loc) · 5.87 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
package wappalyzer
import (
"bytes"
"encoding/json"
"strings"
)
// Wappalyze is a client for working with tech detection
type Wappalyze struct {
fingerprints *CompiledFingerprints
}
// New creates a new tech detection instance
func New() (*Wappalyze, error) {
wappalyze := &Wappalyze{
fingerprints: &CompiledFingerprints{
Apps: make(map[string]*CompiledFingerprint),
},
}
err := wappalyze.loadFingerprints()
if err != nil {
return nil, err
}
return wappalyze, nil
}
// loadFingerprints loads the fingerprints and compiles them
func (s *Wappalyze) loadFingerprints() error {
var fingerprintsStruct Fingerprints
err := json.Unmarshal([]byte(fingerprints), &fingerprintsStruct)
if err != nil {
return err
}
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 _, application := range s.checkHeaders(normalizedHeaders) {
uniqueFingerprints.setIfNotExists(application)
}
cookies := s.findSetCookie(normalizedHeaders)
// Run cookie based fingerprinting if we have a set-cookie header
if len(cookies) > 0 {
for _, application := range s.checkCookies(cookies) {
uniqueFingerprints.setIfNotExists(application)
}
}
// Check for stuff in the body finally
bodyTech := s.checkBody(normalizedBody)
for _, application := range bodyTech {
uniqueFingerprints.setIfNotExists(application)
}
return uniqueFingerprints.getValues()
}
type uniqueFingerprints struct {
values map[string]struct{}
}
func newUniqueFingerprints() uniqueFingerprints {
return uniqueFingerprints{
values: make(map[string]struct{}),
}
}
func (u uniqueFingerprints) getValues() map[string]struct{} {
return u.values
}
const versionSeparator = ":"
// separateAppVersion returns app name and version
func separateAppVersion(value string) (string, string) {
if strings.Contains(value, versionSeparator) {
if parts := strings.Split(value, versionSeparator); len(parts) == 2 {
return parts[0], parts[1]
}
}
return value, ""
}
func (u uniqueFingerprints) setIfNotExists(value string) {
app, version := separateAppVersion(value)
if _, ok := u.values[app]; ok {
// Handles case when we get additional version information next
if version != "" {
delete(u.values, app)
u.values[strings.Join([]string{app, version}, versionSeparator)] = struct{}{}
}
return
}
// Handle duplication for : based values
for k := range u.values {
if strings.Contains(k, versionSeparator) {
if parts := strings.Split(k, versionSeparator); len(parts) == 2 && parts[0] == value {
return
}
}
}
u.values[value] = struct{}{}
}
// 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 _, application := range s.checkHeaders(normalizedHeaders) {
uniqueFingerprints.setIfNotExists(application)
}
cookies := s.findSetCookie(normalizedHeaders)
// Run cookie based fingerprinting if we have a set-cookie header
if len(cookies) > 0 {
for _, application := range s.checkCookies(cookies) {
uniqueFingerprints.setIfNotExists(application)
}
}
// Check for stuff in the body finally
if strings.Contains(normalizedHeaders["content-type"], "text/html") {
bodyTech := s.checkBody(normalizedBody)
for _, application := range bodyTech {
uniqueFingerprints.setIfNotExists(application)
}
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.
//
// 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] = AppInfo{
Description: fingerprint.description,
Website: fingerprint.website,
}
}
}
return result
}
// 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
}