MeshCentral/agents/modules_meshcmd_min/amt-redir-duk.min.js

1 line
7.1 KiB
JavaScript
Raw Normal View History

2019-10-24 16:14:00 -04:00
module.exports=function CreateAmtRedirect(a){var b={};b.m=a;a.parent=b;b.State=0;b.net=require("net");b.tls=require("tls");b.socket=null;b.host=null;b.port=0;b.user=null;b.pass=null;b.connectstate=0;b.protocol=a.protocol;b.xtlsoptions=null;b.amtaccumulator=null;b.amtsequence=1;b.amtkeepalivetimer=null;b.authuri="/RedirectionService";b.digestRealmMatch=null;b.onStateChanged=null;b.Debug=function(d){console.log(d)};var c=null;b.Start=function(d,f,k,e,g,h,j){b.host=d;b.port=f;b.user=k;b.pass=e;b.xtls=g;b.xtlsoptions=j;b.xtlsFingerprint=h;b.connectstate=0;if(g==true){b.socket=b.tls.connect({host:d,port:f,rejectUnauthorized:false,checkServerIdentity:b.onCheckServerIdentity},b.xxOnSocketConnected)}else{b.socket=b.net.createConnection({host:d,port:f},b.xxOnSocketConnected)}b.socket.on("data",b.xxOnSocketData);b.socket.on("close",b.xxOnSocketClosed);b.socket.on("error",b.xxOnSocketClosed);b.xxStateChange(1)};b.onCheckServerIdentity=function(d){var e=d[0].fingerprint.split(":").join("").toLowerCase();if((b.xtlsFingerprint!=null)&&(b.xtlsFingerprint!=e)){console.log("Invalid TLS Cert, SHA384: "+e);process.exit(2);return}else{if(b.xtlsFingerprint==null){b.xtlsFingerprint=e;console.log("TLS Cert SHA384: "+e)}}};b.xxOnSocketConnected=function(){if(b.socket==null){return}if(c&&c.redirtrace){console.log("REDIR-CONNECTED")}b.xxStateChange(2);if(b.protocol==1){b.xxSend(b.RedirectStartSol)}else{if(b.protocol==2){b.xxSend(b.RedirectStartKvm)}else{if(b.protocol==3){b.xxSend(b.RedirectStartIder)}}}};b.xxOnSocketData=function(n){if(!n||b.connectstate==-1){return}if(c&&c.redirtrace){console.log("REDIR-RECV("+n.length+"): "+n.toString("hex"))}if((b.protocol==2||b.protocol==3)&&b.connectstate==1){return b.m.ProcessData(n)}if(b.amtaccumulator==null){b.amtaccumulator=n}else{b.amtaccumulator=Buffer.concat(b.amtaccumulator,n)}while(b.amtaccumulator!=null){var j=0;switch(b.amtaccumulator[0]){case 17:if(b.amtaccumulator.length<4){return}var z=b.amtaccumulator[1];switch(z){case 0:if(b.amtaccumulator.length<13){return}var s=b.amtaccumulator[12];if(b.amtaccumulator.length<13+s){return}b.xxSend(String.fromCharCode(19,0,0,0,0,0,0,0,0));j=(13+s);break;default:b.Stop();break}break;case 20:if(b.amtaccumulator.length<9){return}var f=b.amtaccumulator.readInt32LE(5);if(b.amtaccumulator.length<9+f){return}var y=b.amtaccumulator[1];var g=b.amtaccumulator[4];var d=[];for(i=0;i<f;i++){d.push(b.amtaccumulator[9+i])}var e=b.amtaccumulator.slice(9,9+f);j=9+f;if(g==0){if(d.indexOf(4)>=0){b.xxSend(String.fromCharCode(19,0,0,0,4)+IntToStrX(b.user.length+b.authuri.length+8)+String.fromCharCode(b.user.length)+b.user+String.fromCharCode(0,0)+String.fromCharCode(b.authuri.length)+b.authuri+String.fromCharCode(0,0,0,0))}else{if(d.indexOf(3)>=0){b.xxSend(String.fromCharCode(19,0,0,0,3)+IntToStrX(b.user.length+b.authuri.length+7)+String.fromCharCode(b.user.length)+b.user+String.fromCharCode(0,0)+String.fromCharCode(b.authuri.length)+b.authuri+String.fromCharCode(0,0,0))}else{if(d.indexOf(1)>=0){b.xxSend(String.fromCharCode(19,0,0,0,1)+IntToStrX(b.user.length+b.pass.length+2)+String.fromCharCode(b.user.length)+b.user+String.fromCharCode(b.pass.length)+b.pass)}else{b.Stop()}}}}else{if((g==3||g==4)&&y==1){var m=0;var w=e[m];var v=e.slice(m+1,m+1+w).toString();m+=(w+1);if(b.digestRealmMatch&&(b.digestRealmMatch!=v)){b.Stop();return}var r=e[m];var q=e.slice(m+1,m+1+r).toString();m+=(r+1);var u=0;var t=null;var k=b.xxRandomValueHex(32);var x="00000002";var p="";if(g==4){u=e[m];t=e.slice(m+1,m+1+u).toString();m+=(u+1);p=x+":"+k+":"+t+":"}var o=hex_md5(hex_md5(b.user+":"+v+":"+b.pass)+":"+q+":"+p+hex_md5("POST:"+b.authuri));var A=b.user.length+v.length+q.length+b.authuri.length+k.length+x.length+o.length+7;if(g==4){A+=(t.length+1)}var h=Buffer.concat([new Buffer([19,0,0,0,g]),new Buffer([A&255,(A>>8)&255,0,0]),new Buffer([b.user.length]),new Buffer(b.user),new Buffer([v.length]),new Buffer(v),new Buffer([q.length]),new Buffer(q),new Buffer([b.authuri.length]),new Buffer(b.authuri),new Buffer([k.length]),new Buffer(k),new Buffer([x.length]),new Buffer(x),new Buffer([o.length]),new Buffe