Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/msf/util/exe/osx/app.rb
36043 views
1
module Msf::Util::EXE::OSX::App
2
include Msf::Util::EXE::Common
3
include Msf::Util::EXE::OSX::Common
4
5
def self.included(base)
6
base.extend(ClassMethods)
7
end
8
9
module ClassMethods
10
11
# Create an OSX .app bundle containing the Mach-O executable provided in +exe+
12
# to_osx_app
13
#
14
# @param opts [Hash] The options hash
15
# @option opts [Hash] :exe_name (random) the name of the macho exe file (never seen by the user)
16
# @option opts [Hash] :app_name (random) the name of the OSX app
17
# @option opts [Hash] :hidden (true) hide the app when it is running
18
# @option opts [Hash] :plist_extra ('') some extra data to shove inside the Info.plist file
19
# @return [String] zip archive containing an OSX .app directory
20
def to_osx_app(exe, opts = {})
21
exe_name = opts.fetch(:exe_name) { Rex::Text.rand_text_alpha(8) }
22
app_name = opts.fetch(:app_name) { Rex::Text.rand_text_alpha(8) }
23
hidden = opts.fetch(:hidden, true)
24
plist_extra = opts.fetch(:plist_extra, '')
25
26
app_name.chomp!(".app")
27
app_name += ".app"
28
29
visible_plist = if hidden
30
%Q|
31
<key>LSBackgroundOnly</key>
32
<string>1</string>
33
|
34
else
35
''
36
end
37
38
info_plist = %Q|
39
<?xml version="1.0" encoding="UTF-8"?>
40
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
41
<plist version="1.0">
42
<dict>
43
<key>CFBundleExecutable</key>
44
<string>#{exe_name}</string>
45
<key>CFBundleIdentifier</key>
46
<string>com.#{exe_name}.app</string>
47
<key>CFBundleName</key>
48
<string>#{exe_name}</string>#{visible_plist}
49
<key>CFBundlePackageType</key>
50
<string>APPL</string>
51
#{plist_extra}
52
</dict>
53
</plist>
54
|
55
56
zip = Rex::Zip::Archive.new
57
zip.add_file("#{app_name}/", '')
58
zip.add_file("#{app_name}/Contents/", '')
59
zip.add_file("#{app_name}/Contents/Resources/", '')
60
zip.add_file("#{app_name}/Contents/MacOS/", '')
61
# Add the macho and mark it as executable
62
zip.add_file("#{app_name}/Contents/MacOS/#{exe_name}", exe).last.attrs = 0o777
63
zip.add_file("#{app_name}/Contents/Info.plist", info_plist)
64
zip.add_file("#{app_name}/Contents/PkgInfo", 'APPLaplt')
65
zip.pack
66
end
67
end
68
69
class << self
70
include ClassMethods
71
end
72
73
end
74
75