-
Notifications
You must be signed in to change notification settings - Fork 8
/
Wappalyzer.py
268 lines (218 loc) · 7.64 KB
/
Wappalyzer.py
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
import json
import re
import warnings
import os
import logging
import pkgutil
import pkg_resources
import pprint
import requests
from bs4 import BeautifulSoup
logger = logging.getLogger(name=__name__)
class WappalyzerError(Exception):
pass
class WebPage(object):
def __init__(self, url, html, headers):
"""
Parameters
----------
url : str
The web page URL.
html : str
The web page content (HTML)
headers : dict
The HTTP response headers
"""
self.url = url
self.html = html
self.headers = headers
try:
self.headers.keys()
except AttributeError:
raise ValueError("Headers must be a dictionary-like object")
self._parse_html()
def _parse_html(self):
"""
Parse the HTML with BeautifulSoup to find <script> and <meta> tags.
"""
self.parsed_html = soup = BeautifulSoup(self.html, "lxml")
self.scripts = [script['src'] for script in
soup.findAll('script', src=True)]
self.meta = {
meta['name'].lower():
meta['content'] for meta in soup.findAll(
'meta', attrs=dict(name=True, content=True))
}
@classmethod
def new_from_url(cls, url, verify=True):
"""
Constructs a new WebPage object for the URL,
using the `requests` module to fetch the HTML.
Parameters
----------
url : str
verify: bool
"""
response = requests.get(url, verify=verify, timeout=2.5)
return cls.new_from_response(response)
@classmethod
def new_from_response(cls, response):
"""
Constructs a new WebPage object for the response,
using the `BeautifulSoup` module to parse the HTML.
Parameters
----------
response : requests.Response object
"""
return cls(response.url, html=response.text, headers=response.headers)
class Wappalyzer(object):
"""
Python Wappalyzer driver.
"""
def __init__(self, categories, apps):
"""
Initialize a new Wappalyzer instance.
Parameters
----------
categories : dict
Map of category ids to names, as in apps.json.
apps : dict
Map of app names to app dicts, as in apps.json.
"""
self.categories = categories
self.apps = apps
for name, app in self.apps.items():
self._prepare_app(app)
@classmethod
def latest(cls, apps_file=None):
"""
Construct a Wappalyzer instance using a apps db path passed in via
apps_file, or alternatively the default in data/apps.json
"""
if apps_file:
with open(apps_file, 'r') as fd:
obj = json.load(fd)
else:
apps_data = pkg_resources.resource_string(__name__, "data/apps.json")
obj = json.loads(apps_data.decode('utf-8'))
return cls(categories=obj['categories'], apps=obj['apps'])
def _prepare_app(self, app):
"""
Normalize app data, preparing it for the detection phase.
"""
# Ensure these keys' values are lists
for key in ['url', 'html', 'script', 'implies']:
try:
value = app[key]
except KeyError:
app[key] = []
else:
if not isinstance(value, list):
app[key] = [value]
# Ensure these keys exist
for key in ['headers', 'meta']:
try:
value = app[key]
except KeyError:
app[key] = {}
# Ensure the 'meta' key is a dict
obj = app['meta']
if not isinstance(obj, dict):
app['meta'] = {'generator': obj}
# Ensure keys are lowercase
for key in ['headers', 'meta']:
obj = app[key]
app[key] = {k.lower(): v for k, v in obj.items()}
# Prepare regular expression patterns
for key in ['url', 'html', 'script']:
app[key] = [self._prepare_pattern(pattern) for pattern in app[key]]
for key in ['headers', 'meta']:
obj = app[key]
for name, pattern in obj.items():
obj[name] = self._prepare_pattern(obj[name])
def _prepare_pattern(self, pattern):
"""
Strip out key:value pairs from the pattern and compile the regular
expression.
"""
regex, _, rest = pattern.partition('\\;')
try:
return re.compile(regex, re.I)
except re.error as e:
warnings.warn(
"Caught '{error}' compiling regex: {regex}"
.format(error=e, regex=regex)
)
# regex that never matches:
# http://stackoverflow.com/a/1845097/413622
return re.compile(r'(?!x)x')
def _has_app(self, app, webpage):
"""
Determine whether the web page matches the app signature.
"""
# Search the easiest things first and save the full-text search of the
# HTML for last
for regex in app['url']:
if regex.search(webpage.url):
return True
for name, regex in app['headers'].items():
if name in webpage.headers:
content = webpage.headers[name]
if regex.search(content):
return True
for regex in app['script']:
for script in webpage.scripts:
if regex.search(script):
return True
for name, regex in app['meta'].items():
if name in webpage.meta:
content = webpage.meta[name]
if regex.search(content):
return True
for regex in app['html']:
if regex.search(webpage.html):
return True
def _get_implied_apps(self, detected_apps):
"""
Get the set of apps implied by `detected_apps`.
"""
def __get_implied_apps(apps):
_implied_apps = set()
for app in apps:
try:
_implied_apps.update(set(self.apps[app]['implies']))
except KeyError:
pass
return _implied_apps
implied_apps = __get_implied_apps(detected_apps)
all_implied_apps = set()
# Descend recursively until we've found all implied apps
while not all_implied_apps.issuperset(implied_apps):
all_implied_apps.update(implied_apps)
implied_apps = __get_implied_apps(all_implied_apps)
return all_implied_apps
def get_categories(self, app_name):
"""
Returns a list of the categories for an app name.
"""
cat_nums = self.apps.get(app_name, {}).get("cats", [])
cat_names = [self.categories.get("%s" % cat_num, "")
for cat_num in cat_nums]
return cat_names
def analyze(self, webpage):
"""
Return a list of applications that can be detected on the web page.
"""
detected_apps = set()
for app_name, app in self.apps.items():
if self._has_app(app, webpage):
detected_apps.add(app_name)
detected_apps |= self._get_implied_apps(detected_apps)
return detected_apps
def analyze_with_categories(self, webpage):
detected_apps = self.analyze(webpage)
categorised_apps = {}
for app_name in detected_apps:
cat_names = self.get_categories(app_name)
categorised_apps[app_name] = {"categories": cat_names}
return categorised_apps