forked from rapid7/metasploit-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
msfupdate
executable file
·315 lines (282 loc) · 9.93 KB
/
msfupdate
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env ruby
# -*- coding: binary -*-
#
# $Id$
#
# This keeps the framework up-to-date
#
# $Revision$
#
msfbase = __FILE__
while File.symlink?(msfbase)
msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
end
class Msfupdate
attr_reader :stdin
attr_reader :stdout
attr_reader :stderr
def initialize(msfbase_dir, stdin = $stdin, stdout = $stdout, stderr = $stderr)
@msfbase_dir = msfbase_dir
@stdin = stdin
@stdout = stdout
@stderr = stderr
end
def usage(io = stdout)
help = "usage: msfupdate [options...]\n"
help << "Options:\n"
help << "-h, --help show help\n"
help << " --git-remote REMOTE git remote to use (default upstream)\n" if git?
help << " --git-branch BRANCH git branch to use (default master)\n" if git?
help << " --offline-file FILE offline update file to use\n" if binary_install?
io.print help
end
def parse_args(args)
begin
# GetoptLong uses ARGV, but we want to use the args parameter
# Copy args into ARGV, then restore ARGV after GetoptLong
real_args = ARGV.clone
ARGV.clear
args.each { |arg| ARGV << arg }
require 'getoptlong'
opts = GetoptLong.new(
['--help', '-h', GetoptLong::NO_ARGUMENT],
['--git-remote', GetoptLong::REQUIRED_ARGUMENT],
['--git-branch', GetoptLong::REQUIRED_ARGUMENT],
['--offline-file', GetoptLong::REQUIRED_ARGUMENT]
)
begin
opts.each do |opt, arg|
case opt
when '--help'
usage
maybe_wait_and_exit
when '--git-remote'
@git_remote = arg
when '--git-branch'
@git_branch = arg
when '--offline-file'
@offline_file = File.expand_path(arg)
end
end
rescue GetoptLong::Error
stderr.puts "#{$PROGRAM_NAME}: try 'msfupdate --help' for more information"
maybe_wait_and_exit 0x20
end
# Handle the old wait/nowait argument behavior
if ARGV[0] == 'wait' || ARGV[0] == 'nowait'
@actually_wait = (ARGV.shift == 'wait')
end
ensure
# Restore the original ARGV value
ARGV.clear
real_args.each { |arg| ARGV << arg }
end
end
def validate_args
valid = true
if binary_install? || apt?
if @git_branch
stderr.puts "[-] ERROR: git-branch is not supported on this installation"
valid = false
end
if @git_remote
stderr.puts "[-] ERROR: git-remote is not supported on this installation"
valid = false
end
end
if apt? || git?
if @offline_file
stderr.puts "[-] ERROR: offline-file option is not supported on this installation"
valid = false
end
end
valid
end
def apt?
File.exist?(File.expand_path(File.join(@msfbase_dir, '.apt')))
end
# Are you an installer, or did you get here via a source checkout?
def binary_install?
File.exist?(File.expand_path(File.join(@msfbase_dir, "..", "engine", "update.rb"))) && !apt?
end
def git?
File.directory?(File.join(@msfbase_dir, ".git"))
end
def run!
validate_args || maybe_wait_and_exit(0x13)
stderr.puts "[*]"
stderr.puts "[*] Attempting to update the Metasploit Framework..."
stderr.puts "[*]"
stderr.puts ""
# Bail right away, no waiting around for consoles.
unless Process.uid.zero? || File.stat(@msfbase_dir).owned?
stderr.puts "[-] ERROR: User running msfupdate does not own the Metasploit installation"
stderr.puts "[-] Please run msfupdate as the same user who installed Metasploit."
maybe_wait_and_exit 0x10
end
Dir.chdir(@msfbase_dir) do
if apt?
stderr.puts "[-] ERROR: msfupdate is not supported on Kali Linux."
stderr.puts "[-] Please run 'apt update; apt install metasploit-framework' instead."
elsif binary_install?
update_binary_install!
elsif git?
update_git!
else
raise "Cannot determine checkout type: `#{@msfbase_dir}'"
end
end
end
# We could also do this by running `git config --global user.name` and `git config --global user.email`
# and check the output of those. (it's a bit quieter)
def git_globals_okay?
output = ''
begin
output = `git config --list`
rescue Errno::ENOENT
stderr.puts '[-] ERROR: Failed to check git settings, git not found'
return false
end
unless output.include? 'user.name'
stderr.puts '[-] ERROR: user.name is not set in your global git configuration'
stderr.puts '[-] Set it by running: \'git config --global user.name "NAME HERE"\''
stderr.puts ''
return false
end
unless output.include? 'user.email'
stderr.puts '[-] ERROR: user.email is not set in your global git configuration'
stderr.puts '[-] Set it by running: \'git config --global user.email "[email protected]"\''
stderr.puts ''
return false
end
true
end
def update_git!
####### Since we're Git, do it all that way #######
stdout.puts "[*] Checking for updates via git"
stdout.puts "[*] Note: Updating from bleeding edge"
out = `git remote show upstream` # Actually need the output for this one.
add_git_upstream unless $?.success? &&
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
# will be told to set them if they aren't already set.
# Checks user.name and user.email
global_status = git_globals_okay?
maybe_wait_and_exit(1) unless global_status
# We shouldn't get here if the globals dont check out
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 !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..."
begin
require 'bundler'
rescue LoadError
stderr.puts '[*] Installing bundler'
system('gem', 'install', 'bundler')
Gem.clear_paths
require 'bundler'
end
Bundler.with_clean_env do
if File::exist? "Gemfile.local"
system("bundle", "install", "--gemfile", "Gemfile.local")
else
system("bundle", "install")
end
end
end
def update_binary_install!
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.exist? product_key
if File.readable? product_key
if @offline_file
system("ruby", update_script, @offline_file)
else
system("ruby", update_script)
end
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:"
stderr.puts "[-] https://localhost:3790"
stderr.puts "[-] (Note: Metasploit Community Edition is totally"
stderr.puts "[-] free and takes just a few seconds to register!)"
maybe_wait_and_exit 11
end
end
# 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
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."
end
# This only exits if you actually pass a wait option, otherwise
# just returns nil. This is likely unexpected, revisit this.
def maybe_wait_and_exit(exit_code = 0)
if @actually_wait
stdout.puts ""
stdout.puts "[*] Please hit enter to exit"
stdout.puts ""
stdin.readline
end
exit exit_code
end
def apt_upgrade_available(package)
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
end
end
if __FILE__ == $PROGRAM_NAME
cli = Msfupdate.new(File.dirname(msfbase))
cli.parse_args(ARGV.dup)
cli.run!
cli.maybe_wait_and_exit
end