CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/tools/dev/update_user_agent_strings.py
Views: 11768
1
#!/usr/bin/python3
2
import requests
3
import re
4
5
def replace_agent_string(lines, replace_marker, url, regex):
6
VALID_CHARS = 'a-zA-Z0-9\\(\\);:\\.,/_ '
7
regex = regex.replace('{VALID_CHARS}', VALID_CHARS)
8
print(f'Updating {replace_marker}')
9
for x in range(0, len(lines)):
10
if replace_marker in lines[x]:
11
break
12
else:
13
raise RuntimeError(f"Couldn't find marker {replace_marker}")
14
15
response = requests.get(url)
16
if response.status_code != 200:
17
raise RuntimeError(f"Can't retrieve {url}")
18
19
match = re.search(regex, response.text)
20
if match is None:
21
raise RuntimeError(f"Couldn't match regex {regex}")
22
23
new_string = match.groups()[0]
24
print(f'New value is: {new_string}')
25
old_line = lines[x]
26
if f"'{new_string}'" in old_line:
27
print('(This is unchanged from the previous value)')
28
else:
29
new_line = re.sub("'(.*)'", f"'{new_string}'", old_line)
30
if old_line == new_line:
31
raise RuntimeError(f"Line didn't change: {old_line}")
32
33
lines[x] = new_line
34
35
36
chrome_url = "https://www.whatismybrowser.com/guides/the-latest-user-agent/chrome"
37
edge_url = "https://www.whatismybrowser.com/guides/the-latest-user-agent/edge"
38
safari_url = "https://www.whatismybrowser.com/guides/the-latest-user-agent/safari"
39
firefox_url = "https://www.whatismybrowser.com/guides/the-latest-user-agent/firefox"
40
41
user_agent_filename = 'lib/rex/user_agent.rb'
42
with open(user_agent_filename,'r') as f:
43
lines = f.read().splitlines()
44
45
replace_agent_string(lines, 'Chrome Windows', chrome_url, '<td>Chrome \\(Standard\\)</td>\s*<td>\s*<ul>\s*<li><span class="code">([{VALID_CHARS}]*Windows NT[{VALID_CHARS}]*)</span>')
46
replace_agent_string(lines, 'Chrome MacOS', chrome_url, '<td>Chrome \\(Standard\\)</td>\s*<td>\s*<ul>\s*<li><span class="code">([{VALID_CHARS}]*Macintosh[{VALID_CHARS}]*)</span>')
47
replace_agent_string(lines, 'Edge Windows', edge_url, '<td>Edge \\(Standard\\)</td>\s*<td>\s*<ul>\s*<li><span class="code">([{VALID_CHARS}]*Windows NT[{VALID_CHARS}]*)</span>')
48
replace_agent_string(lines, 'Safari iPad', safari_url, '<td>\s*Safari on <b>Ipad</b>\s*</td>\s*<td>\s*<ul>\s*<li><span class="code">([{VALID_CHARS}]*iPad[{VALID_CHARS}]*)</span>')
49
replace_agent_string(lines, 'Safari MacOS', safari_url, '<td>Safari \\(Standard\\)</td>\s*<td>\s*<ul>\s*<li><span class="code">([{VALID_CHARS}]*Macintosh[{VALID_CHARS}]*)</span>')
50
replace_agent_string(lines, 'Firefox Windows', firefox_url, '<td>\s*Firefox on <b>Windows</b>\s*</td>\s*<td>\s*<ul>\s*<li><span class="code">([{VALID_CHARS}]*Windows NT[{VALID_CHARS}]*)</span>')
51
replace_agent_string(lines, 'Firefox MacOS', firefox_url, '<td>\s*Firefox on <b>Macos</b>\s*</td>\s*<td>\s*<ul>\s*<li><span class="code">([{VALID_CHARS}]*Macintosh[{VALID_CHARS}]*)</span>')
52
53
with open(user_agent_filename, 'w') as f:
54
f.write('\n'.join(lines) + '\n')
55
56
print('Done')
57
58