// // Copyright (c) 2006-2026 Wade Alcorn - wade@bindshell.net // Browser Exploitation Framework (BeEF) - https://beefproject.com // See the file 'doc/COPYING' for copying permission // /** * Provides functionality to manipulate the DOM. * @namespace beef.dom */ beef.dom = { /** * Generates a random ID for HTML elements * @param {String} prefix a custom prefix before the random id. defaults to "beef-" * @return {String} generated id */ generateID: function(prefix) { return ((prefix == null) ? 'beef-' : prefix)+Math.floor(Math.random()*99999); }, /** * Creates a new element but does not append it to the DOM. * @param {String} type the name of the element. * @param {Array} attributes the attributes of that element. * @return {Array} the created element. */ createElement: function(type, attributes) { var el = document.createElement(type); for(index in attributes) { if(typeof attributes[index] == 'string') { el.setAttribute(index, attributes[index]); } } return el; }, /** * Removes element from the DOM. * @param {Object} el the target element to be removed. */ removeElement: function(el) { if (!beef.dom.isDOMElement(el)) { el = document.getElementById(el); } try { el.parentNode.removeChild(el); } catch (e) { } }, /** * Tests if the object is a DOM element. * @param {Object} the DOM element. * @return {boolean} true if the object is a DOM element. */ isDOMElement: function(obj) { return (obj.nodeType) ? true : false; }, /** * Creates an invisible iframe on the hook browser's page. * @return {array} the iframe. */ createInvisibleIframe: function() { var iframe = this.createElement('iframe', { width: '1px', height: '1px', style: 'visibility:hidden;' }); document.body.appendChild(iframe); return iframe; }, /** * Returns the highest current z-index * @param {Boolean} whether to return an associative array with the height AND the ID of the element * @return {Integer} Highest z-index in the DOM * OR * @return {Hash} A hash with the height and the ID of the highest element in the DOM {'height': INT, 'elem': STRING} */ getHighestZindex: function(include_id) { var highest = {'height':0, 'elem':''}; $j('*').each(function() { var current_high = parseInt($j(this).css("zIndex"),10); if (current_high > highest.height) { highest.height = current_high; highest.elem = $j(this).attr('id'); } }); if (include_id) { return highest; } else { return highest.height; } }, /** * Create an iFrame element and prepend to document body. URI passed via 'src' property of function's 'params' parameter * is assigned to created iframe tag's src attribute resulting in GET request to that URI. * example usage in the code: beef.dom.createIframe('fullscreen', {'src':$j(this).attr('href')}, {}, null); * @param {String} type: can be 'hidden' or 'fullScreen'. defaults to normal * @param {Hash} params: list of params that will be sent in request. * @param {Hash} styles: css styling attributes, these are merged with the defaults specified in the type parameter * @param {Function} a callback function to fire once the iFrame has loaded * @return {Object} the inserted iFrame * */ createIframe: function(type, params, styles, onload) { var css = {}; if (type == 'hidden') { css = $j.extend(true, {'border':'none', 'width':'1px', 'height':'1px', 'display':'none', 'visibility':'hidden'}, styles); } else if (type == 'fullscreen') { css = $j.extend(true, {'border':'none', 'background-color':'white', 'width':'100%', 'height':'100%', 'position':'absolute', 'top':'0px', 'left':'0px', 'z-index':beef.dom.getHighestZindex()+1}, styles); $j('body').css({'padding':'0px', 'margin':'0px'}); } else { css = styles; $j('body').css({'padding':'0px', 'margin':'0px'}); } var iframe = $j('