2010-10-19 09:32:50 +02:00
|
|
|
#!/usr/bin/env ruby
|
2012-06-29 07:18:28 +02:00
|
|
|
# -*- coding: binary -*-
|
2013-10-10 14:25:00 +02:00
|
|
|
#
|
2011-10-23 14:04:41 +02:00
|
|
|
# $Id$
|
2013-10-10 14:25:00 +02:00
|
|
|
#
|
|
|
|
# This keeps the framework up-to-date
|
|
|
|
#
|
2011-10-23 14:04:41 +02:00
|
|
|
# $Revision$
|
2013-10-10 14:25:00 +02:00
|
|
|
#
|
2011-10-23 14:04:41 +02:00
|
|
|
|
2010-10-19 09:32:50 +02:00
|
|
|
msfbase = __FILE__
|
2012-10-04 00:06:38 +02:00
|
|
|
while File.symlink?(msfbase)
|
2013-09-30 20:47:53 +02:00
|
|
|
msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
|
2010-10-19 09:32:50 +02:00
|
|
|
end
|
|
|
|
|
2013-10-31 22:56:10 +01:00
|
|
|
@orig_dir = Dir.pwd
|
2012-11-05 18:48:09 +01:00
|
|
|
@msfbase_dir = File.dirname(msfbase)
|
2012-11-05 18:44:53 +01:00
|
|
|
|
2012-10-01 19:30:29 +02:00
|
|
|
@args = ARGV.dup
|
2010-10-19 09:32:50 +02:00
|
|
|
|
2012-11-05 18:48:09 +01:00
|
|
|
Dir.chdir(@msfbase_dir)
|
2010-10-19 09:32:50 +02:00
|
|
|
|
|
|
|
$stderr.puts "[*]"
|
|
|
|
$stderr.puts "[*] Attempting to update the Metasploit Framework..."
|
|
|
|
$stderr.puts "[*]"
|
2010-10-19 10:22:44 +02:00
|
|
|
$stderr.puts ""
|
|
|
|
|
2013-01-14 23:26:42 +01:00
|
|
|
# Bail right away, no waiting around for consoles.
|
2011-10-10 23:20:56 +02:00
|
|
|
if not (Process.uid == 0 or File.stat(msfbase).owned?)
|
2013-09-30 20:47:53 +02:00
|
|
|
$stderr.puts "[-] ERROR: User running msfupdate does not own the Metasploit installation"
|
|
|
|
$stderr.puts "[-] Please run msfupdate as the same user who installed Metasploit."
|
|
|
|
exit 0x10
|
2011-10-10 23:08:04 +02:00
|
|
|
end
|
|
|
|
|
2013-02-04 22:01:35 +01:00
|
|
|
def is_apt
|
2013-09-30 20:47:53 +02:00
|
|
|
File.exists?(File.expand_path(File.join(@msfbase_dir, '.apt')))
|
2013-02-04 22:01:35 +01:00
|
|
|
end
|
|
|
|
|
2013-01-14 22:58:16 +01:00
|
|
|
# Are you an installer, or did you get here via a source checkout?
|
|
|
|
def is_installed
|
2013-09-30 20:47:53 +02:00
|
|
|
File.exists?(File.expand_path(File.join(@msfbase_dir, "..", "engine", "update.rb"))) && !is_apt
|
2012-11-21 16:38:05 +01:00
|
|
|
end
|
2012-11-05 18:44:53 +01:00
|
|
|
|
2012-11-05 18:48:09 +01:00
|
|
|
def is_git
|
2013-09-30 20:47:53 +02:00
|
|
|
File.directory?(File.join(@msfbase_dir, ".git"))
|
2012-11-05 18:44:53 +01:00
|
|
|
end
|
|
|
|
|
2012-11-26 22:35:23 +01:00
|
|
|
# Adding an upstream enables msfupdate to pull updates from
|
|
|
|
# Rapid7's metasploit-framework repo instead of the repo
|
|
|
|
# the user originally cloned or forked.
|
|
|
|
def add_git_upstream
|
2013-09-30 20:47:53 +02:00
|
|
|
$stdout.puts "[*] Attempting to add remote 'upstream' to your local git repository."
|
|
|
|
system("git", "remote", "add", "upstream", "git://github.com/rapid7/metasploit-framework.git")
|
|
|
|
$stdout.puts "[*] Added remote 'upstream' to your local git repository."
|
2012-11-26 22:35:23 +01:00
|
|
|
end
|
|
|
|
|
2013-03-26 20:32:21 +01:00
|
|
|
# This only exits if you actually pass a wait option, otherwise
|
|
|
|
# just returns nil. This is likely unexpected, revisit this.
|
2013-01-14 23:26:42 +01:00
|
|
|
def maybe_wait_and_exit(exit_code=0)
|
2013-09-30 20:47:53 +02:00
|
|
|
if @actually_wait
|
|
|
|
$stdout.puts ""
|
|
|
|
$stdout.puts "[*] Please hit enter to exit"
|
|
|
|
$stdout.puts ""
|
|
|
|
$stdin.readline
|
|
|
|
exit exit_code
|
|
|
|
end
|
2013-01-14 23:26:42 +01:00
|
|
|
end
|
|
|
|
|
2013-02-04 22:01:35 +01:00
|
|
|
def apt_upgrade_available(package)
|
2013-09-30 20:47:53 +02:00
|
|
|
require 'open3'
|
|
|
|
installed = nil
|
|
|
|
upgrade = nil
|
|
|
|
::Open3.popen3({'LANG'=>'en_US.UTF-8'}, "apt-cache", "policy", package) do |stdin, stdout, stderr|
|
|
|
|
stdout.each do |line|
|
|
|
|
installed = $1 if line =~ /Installed: ([\w\-+.:~]+)$/
|
|
|
|
upgrade = $1 if line =~ /Candidate: ([\w\-+.:~]+)$/
|
|
|
|
break if installed && upgrade
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if installed && installed != upgrade
|
|
|
|
upgrade
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2013-02-04 22:01:35 +01:00
|
|
|
end
|
|
|
|
|
2012-10-01 19:41:36 +02:00
|
|
|
@args.each_with_index do |arg,i|
|
2013-09-30 20:47:53 +02:00
|
|
|
case arg
|
|
|
|
# Handle the old wait/nowait argument behavior
|
|
|
|
when "wait", "nowait"
|
|
|
|
@wait_index = i
|
|
|
|
@actually_wait = (arg == "wait")
|
|
|
|
when /--git-remote=([^\s]*)?/
|
|
|
|
@git_remote = $1
|
|
|
|
@git_remote_index = i
|
|
|
|
when /--git-branch=([^\s]*)?/
|
|
|
|
@git_branch = $1
|
|
|
|
@git_branch_index = i
|
|
|
|
end
|
2011-06-27 22:30:44 +02:00
|
|
|
end
|
|
|
|
|
2012-10-10 17:17:22 +02:00
|
|
|
@args[@wait_index] = nil if @wait_index
|
2012-11-05 18:56:05 +01:00
|
|
|
|
|
|
|
@args[@git_remote_index] = nil if @git_remote_index
|
|
|
|
@args[@git_branch_index] = nil if @git_branch_index
|
2011-06-27 22:30:44 +02:00
|
|
|
|
2013-10-10 14:25:00 +02:00
|
|
|
@args = @args.compact
|
2012-11-05 18:44:53 +01:00
|
|
|
|
|
|
|
####### Since we're Git, do it all that way #######
|
|
|
|
if is_git
|
2013-10-10 14:25:00 +02:00
|
|
|
$stdout.puts "[*] Checking for updates via git"
|
2013-10-10 20:59:03 +02:00
|
|
|
$stdout.puts "[*] Note: Updating from bleeding edge"
|
2013-09-30 20:47:53 +02:00
|
|
|
out = `git remote show upstream` # Actually need the output for this one.
|
|
|
|
add_git_upstream unless $?.success? and out =~ %r{(https|git|git@github\.com):(//github\.com/)?(rapid7/metasploit-framework\.git)}
|
|
|
|
|
|
|
|
remote = @git_remote || "upstream"
|
|
|
|
branch = @git_branch || "master"
|
|
|
|
|
|
|
|
# This will save local changes in a stash, but won't
|
|
|
|
# attempt to reapply them. If the user wants them back
|
|
|
|
# they can always git stash pop them, and that presumes
|
|
|
|
# they know what they're doing when they're editing local
|
|
|
|
# checkout, which presumes they're not using msfupdate
|
|
|
|
# to begin with.
|
|
|
|
#
|
|
|
|
# Note, this requires at least user.name and user.email
|
|
|
|
# to be configured in the global git config. Installers should
|
|
|
|
# take care that this is done. TODO: Enforce this in msfupdate
|
|
|
|
committed = system("git", "diff", "--quiet", "HEAD")
|
|
|
|
if committed.nil?
|
|
|
|
$stderr.puts "[-] ERROR: Failed to run git"
|
|
|
|
$stderr.puts ""
|
|
|
|
$stderr.puts "[-] If you used a binary installer, make sure you run the symlink in"
|
|
|
|
$stderr.puts "[-] /usr/local/bin instead of running this file directly (e.g.: ./msfupdate)"
|
|
|
|
$stderr.puts "[-] to ensure a proper environment."
|
|
|
|
maybe_wait_and_exit 1
|
|
|
|
elsif not committed
|
|
|
|
system("git", "stash")
|
|
|
|
$stdout.puts "[*] Stashed local changes to avoid merge conflicts."
|
|
|
|
$stdout.puts "[*] Run `git stash pop` to reapply local changes."
|
|
|
|
end
|
|
|
|
|
|
|
|
system("git", "reset", "HEAD", "--hard")
|
|
|
|
system("git", "checkout", branch)
|
|
|
|
system("git", "fetch", remote)
|
|
|
|
system("git", "merge", "#{remote}/#{branch}")
|
|
|
|
|
|
|
|
$stdout.puts "[*] Updating gems..."
|
|
|
|
require 'bundler'
|
|
|
|
Bundler.with_clean_env do
|
|
|
|
system("bundle", "install")
|
|
|
|
end
|
2012-11-05 18:44:53 +01:00
|
|
|
end
|
|
|
|
|
2013-01-14 22:58:16 +01:00
|
|
|
if is_installed
|
2013-09-30 20:47:53 +02:00
|
|
|
update_script = File.expand_path(File.join(@msfbase_dir, "..", "engine", "update.rb"))
|
|
|
|
product_key = File.expand_path(File.join(@msfbase_dir, "..", "engine", "license", "product.key"))
|
|
|
|
if File.exists? product_key
|
|
|
|
if File.readable? product_key
|
2013-10-31 22:56:10 +01:00
|
|
|
# if given, assume first argument is an offline update path (possibly a relative path)
|
|
|
|
if (@args[0])
|
|
|
|
system("ruby", update_script, File.expand_path(@args[0], @orig_dir))
|
|
|
|
else
|
|
|
|
system("ruby", update_script)
|
|
|
|
end
|
2013-09-30 20:47:53 +02:00
|
|
|
else
|
|
|
|
$stdout.puts "[-] ERROR: Failed to update Metasploit installation"
|
|
|
|
$stdout.puts ""
|
|
|
|
$stdout.puts "[-] You must be able to read the product key for the"
|
|
|
|
$stdout.puts "[-] Metasploit installation in order to run msfupdate."
|
|
|
|
$stdout.puts "[-] Usually, this means you must be root (EUID 0)."
|
|
|
|
maybe_wait_and_exit 10
|
|
|
|
end
|
|
|
|
else
|
|
|
|
$stdout.puts "[-] ERROR: Failed to update Metasploit installation"
|
|
|
|
$stdout.puts ""
|
|
|
|
$stdout.puts "[-] In order to update your Metasploit installation,"
|
|
|
|
$stdout.puts "[-] you must first register it through the UI, here:"
|
2013-10-10 14:25:00 +02:00
|
|
|
$stderr.puts "[-] https://localhost:3790"
|
2013-10-10 20:59:03 +02:00
|
|
|
$stderr.puts "[-] (Note: Metasploit Community Edition is totally"
|
2013-10-10 14:25:00 +02:00
|
|
|
$stderr.puts "[-] free and takes just a few seconds to register!)"
|
2013-09-30 20:47:53 +02:00
|
|
|
maybe_wait_and_exit 11
|
|
|
|
end
|
2012-11-21 16:38:05 +01:00
|
|
|
end
|
|
|
|
|
2013-02-04 22:01:35 +01:00
|
|
|
if is_apt
|
2013-10-10 14:25:00 +02:00
|
|
|
# For more information, see here:
|
|
|
|
# https://community.rapid7.com/community/metasploit/blog/2013/01/17/metasploit-updates-and-msfupdate
|
2013-10-10 20:59:03 +02:00
|
|
|
$stdout.puts "[*] Checking for updates via the APT repository"
|
|
|
|
$stdout.puts "[*] Note: expect weekly(ish) updates using this method"
|
2013-09-30 20:47:53 +02:00
|
|
|
system("apt-get", "-qq", "update")
|
2013-02-04 22:01:35 +01:00
|
|
|
|
2013-09-30 20:47:53 +02:00
|
|
|
packages = []
|
|
|
|
packages << 'metasploit-framework' if framework_version = apt_upgrade_available('metasploit-framework')
|
|
|
|
packages << 'metasploit' if pro_version = apt_upgrade_available('metasploit')
|
2013-02-04 22:01:35 +01:00
|
|
|
|
2013-09-30 20:47:53 +02:00
|
|
|
if packages.empty?
|
|
|
|
$stdout.puts "[*] No updates available"
|
|
|
|
else
|
|
|
|
$stdout.puts "[*] Updating to version #{pro_version || framework_version}"
|
|
|
|
system("apt-get", "install", "--assume-yes", *packages)
|
|
|
|
if packages.include?('metasploit')
|
|
|
|
start_cmd = File.expand_path(File.join(@msfbase_dir, '..', '..', '..', 'scripts', 'start.sh'))
|
|
|
|
system(start_cmd) if ::File.executable_real? start_cmd
|
|
|
|
end
|
|
|
|
end
|
2013-02-04 22:01:35 +01:00
|
|
|
end
|
|
|
|
|
2013-10-10 14:25:00 +02:00
|
|
|
unless is_git || is_installed || is_apt
|
2013-09-30 20:47:53 +02:00
|
|
|
raise RuntimeError, "Cannot determine checkout type: `#{@msfbase_dir}'"
|
2011-09-15 17:31:08 +02:00
|
|
|
end
|
2011-05-04 01:58:41 +02:00
|
|
|
|
2013-01-14 23:26:42 +01:00
|
|
|
maybe_wait_and_exit(0)
|
|
|
|
|