title: Audio streaming using metasploit
body: <p>I want to make a microphone audio stream in metasploit like webcam_stream,so I added some codes to the ruby script at : <strong><em>(/usr/share/metasploit-framework/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/webcam.rb</em></strong>), the added piece of code is:</p>
  
  <pre><code>def cmd_mic_stream(*args)
  
  print_status("Starting...")
  stream_path = Rex::Text.rand_text_alpha(8) + ".wav"
  player_path = Rex::Text.rand_text_alpha(8) + ".html"
  duration = 2
  view     = true
  timeout  = 1800
  
  record_mic_opts = Rex::Parser::Arguments.new(
    "-h" =&gt; [ false, "Help Banner" ],
    "-d" =&gt; [ true, "The stream duration in seconds (Default:2)" ], # 1/30 min
    "-s" =&gt; [ true, "The stream file path (Default: '#{stream_path}')" ],
    "-t" =&gt; [ true, "The stream player path (Default: #{player_path})"],
    "-v" =&gt; [ true, "Automatically view the stream (Default: '#{view}')" ]
  )
  
   record_mic_opts.parse(args) do |opt, _idx, val|
    case opt
    when "-h"
      print_line("Usage: record_mic [options]\n")
      print_line("Records audio from the default microphone.")
      print_line(record_mic_opts.usage)
      return
    when "-d" 
      duration = val.to_i
    when "-s"
      stream_path = val
    when "-t"
      player_path = val
    when "-v"
      view = true if val =~ /^(f|n|0)/i
    end
  end
  
  print_status("Preparing player...")
  &lt;!--
      html = %|&lt;html&gt;
  &lt;head&gt;
  &lt;META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE"&gt;
  &lt;META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE"&gt;
  &lt;title&gt;Metasploit webcam_stream - #{client.sock.peerhost}&lt;/title&gt;
  &lt;script language="javascript"&gt;
  function updateStatus(msg) {
    var status = document.getElementById("status");
    status.innerText = msg;
  }
  
  function noAudio() {
    document.getElementById("streamer").style = "display:none";
    updateStatus("Waiting");
  }
  
  var i = 0;
  function updateAudio() {
    var audio = document.getElementById("streamer");
    audio.src = "#{stream_path}#" + i;
    audio.style = "display:";
    updateStatus("Playing");
    i++;
    audio.load();
    audio.play();
  }
  
  setInterval(function() {
    updateAudio();
  },25);
  
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;noscript&gt;
    &lt;h2&gt;&lt;font color="red"&gt;Error: You need Javascript enabled to watch the stream.&lt;/font&gt;&lt;/h2&gt;
  &lt;/noscript&gt;
  &lt;pre&gt;
  Target IP  : #{client.sock.peerhost}
  Start time : #{Time.now}
  Status     : &lt;span id="status"&gt;&lt;/span&gt;
  &lt;/pre&gt;
  &lt;br&gt;
  &lt;audio controls autoplay&gt;
    &lt;source onerror="noAudio()" id="streamer" type="audio/wav"&gt;
  Your browser does not support the audio element.
  &lt;/audio&gt; 
  &lt;br&gt;&lt;br&gt;
  &lt;a href="http://www.metasploit.com" target="_blank"&gt;www.metasploit.com&lt;/a&gt;
  &lt;/body&gt;
  &lt;/html&gt;
      |
  --&gt;
  
  ::File.open(player_path, 'wb') do |f|
    f.write(html)
  end
  if view
    print_status("Opening player at: #{player_path}")
    Rex::Compat.open_file(player_path)
  else
    print_status("Please open the player manually with a browser: #{player_path}")
  end
  
  print_status("Streaming...")
  begin
    ::Timeout.timeout(timeout) do
      while client do
        data = client.webcam.record_mic(duration)
        if data
           ::File.open(stream_path, 'wb') do |d|
           d.write(data)
          end
          data = nil
          sleep(5)
        end
      end
    end
  rescue ::Timeout::Error
  end
  
  print_status("Stopped")
  end
  </code></pre>
  
  <p>you may notice that it is similar to webcam_stream command,the problem here is that the browser opens , status outputs "playing" , but the audio doesn't play anything , it's just empty what may have caused that ?</p>