Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/tools/dev/update_user_agent_strings.rb
Views: 15959
#!/usr/bin/env ruby1# -*- coding: binary -*-23require 'optparse'4require 'net/http'5require 'uri'6optparse = OptionParser.new do |opts|7opts.banner = 'Usage: ruby tools/dev/update_user_agent_strings.rb [options]'8opts.separator "This program updates lib/rex/user_agent.rb so Metasploit uses the most up-to-date User Agent strings across the framework."9opts.separator ""10opts.on('-h', '--help', 'Display this screen.') do11puts opts12exit13end14end15optparse.parse!1617# colors and puts templates from msftidy.rb1819class String20def red21"\e[1;31;40m#{self}\e[0m"22end2324def yellow25"\e[1;33;40m#{self}\e[0m"26end2728def green29"\e[1;32;40m#{self}\e[0m"30end3132def cyan33"\e[1;36;40m#{self}\e[0m"34end35end3637#38# Display an error message, given some text39#40def error(txt)41puts "[#{'ERROR'.red}] #{cleanup_text(txt)}"42end4344#45# Display a warning message, given some text46#47def warning(txt)48puts "[#{'WARNING'.yellow}] #{cleanup_text(txt)}"49end5051#52# Display a info message, given some text53#54def info(txt)55puts "[#{'INFO'.cyan}] #{cleanup_text(txt)}"56end5758def cleanup_text(txt)59# remove line breaks60txt = txt.gsub(/[\r\n]/, ' ')61# replace multiple spaces by one space62txt.gsub(/\s{2,}/, ' ')63end6465def replace_agent_string(lines, replace_marker, url, regex)66valid_chars = 'a-zA-Z0-9\(\);:\.,/_ '67regex = regex.gsub('{VALID_CHARS}', valid_chars)68info "Checking: #{replace_marker}"6970index = lines.index { |line| line.include?(replace_marker) }71raise "Couldn't find marker #{replace_marker}" if index.nil?7273uri = URI(url)74response = Net::HTTP.get_response(uri)75raise "Can't retrieve #{url}" unless response.is_a?(Net::HTTPSuccess)7677match = response.body.match(/#{regex}/)78raise "Couldn't match regex #{regex}" if match.nil?7980new_string = match[1]8182old_line = lines[index]83if old_line.include?("'#{new_string}'")84puts " (Unchanged): #{new_string}"85else86new_line = old_line.gsub(/'(.*)'/, "'#{new_string}'")87if old_line == new_line88raise " Line didn't change: #{old_line}"89end90puts " New value is: #{new_string}"91lines[index] = new_line92end93end9495chrome_url = "https://www.whatismybrowser.com/guides/the-latest-user-agent/chrome"96edge_url = "https://www.whatismybrowser.com/guides/the-latest-user-agent/edge"97safari_url = "https://www.whatismybrowser.com/guides/the-latest-user-agent/safari"98firefox_url = "https://www.whatismybrowser.com/guides/the-latest-user-agent/firefox"99100user_agent_filename = 'lib/rex/user_agent.rb'101lines = File.read(user_agent_filename).split("\n")102103replace_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>')104replace_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>')105replace_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>')106replace_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>')107replace_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>')108replace_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>')109replace_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>')110111File.write(user_agent_filename, lines.join("\n") + "\n")112113114