UNPKG

meshcentral

Version:

Web based remote computer management server

1 lines • 8.02 kB
function script_setup(e,t){var r={startvars:t};return e.length<6?(console.error("Invalid script length"),null):612182341!=ReadInt(e,0)?(console.error("Invalid binary script"),null):ReadShort(e,4)>1?(console.error("Unsupported script version"),null):(r.script=e.substring(6),r.reset=function(e){r.stop(),r.ip=0,r.variables=t,r.state=1},r.start=function(e){r.stop(),r.stepspeed=e,e>0&&(r.timer=setInterval((function(){r.step()}),e))},r.stop=function(){null!=r.timer&&clearInterval(r.timer),r.timer=null,r.stepspeed=0},r.getVar=function(e){if(null!=e)return r.getVarEx(e.split("."),r.variables)},r.getVarEx=function(e,t){try{if(null==e)return;return 0==e.length?t:r.getVarEx(e.slice(1),t[e[0]])}catch(e){return null}},r.setVar=function(e,t){r.setVarEx(e.split("."),r.variables,t)},r.setVarEx=function(e,t,a){1==e.length?t[e[0]]=a:r.setVarEx(e.slice(1),t[e[0]],a)},r.step=function(){if(1==r.state){if(r.ip<r.script.length){var e=ReadShort(r.script,r.ip),t=ReadShort(r.script,r.ip+2),a=ReadShort(r.script,r.ip+4),n=r.ip+6,s=[];for(var i in r.variables)i.startsWith("__")&&delete r.variables[i];for(i=0;i<a;i++){var o=ReadShort(r.script,n),l=r.script.substring(n+2,n+2+o),c=l.charCodeAt(0);if(l=l.substring(1),c<2){for(;l.split("{").length>1;){var u=l.split("{").pop().split("}").shift();l=l.replace("{"+u+"}",r.getVar(u))}1==c&&(r.variables["__"+i]=decodeURI(l),l="__"+i),s.push(l)}2!=c&&3!=c||(r.variables["__"+i]=ReadSInt(l,0),s.push("__"+i)),n+=2+o}r.ip+=t;var p,d=[];for(i=0;i<10;i++)d.push(r.getVar(s[i]));try{if(e<1e4)switch(e){case 0:break;case 1:d[2]?("<"==d[2]&&d[1]<d[3]||"<="==d[2]&&d[1]<=d[3]||"!="==d[2]&&d[1]!=d[3]||"="==d[2]&&d[1]==d[3]||">="==d[2]&&d[1]>=d[3]||">"==d[2]&&d[1]>d[3])&&(r.ip=d[0]):r.ip=d[0];break;case 2:null==s[1]?delete r.variables[s[0]]:r.setVar(s[0],d[1]);break;case 3:r.onConsole?r.onConsole(r.toString(d[0]),r):console.log(r.toString(d[0]));break;case 4:r.state=2,r.dialog=!0,setDialogMode(11,d[0],d[2],r.xxStepDialogOk,d[1],r);break;case 5:for(var i in d[1])d[1][i][d[2]]==d[3]&&(p=i);break;case 6:p=d[1].substr(d[2],d[3]);break;case 7:p=d[1].indexOf(d[2]);break;case 8:p=d[1].split(d[2]);break;case 9:p=d[1].join(d[2]);break;case 10:p=d[1].length;break;case 11:p=JSON.parse(d[1]);break;case 12:p=JSON.stringify(d[1]);break;case 13:p=d[1]+d[2];break;case 14:p=d[1]-d[2];break;case 15:p=parseInt(d[1]);break;case 16:r.state=2,r.amtstack.BatchEnum(d[0],d[1],r.xxWsmanReturn,r);break;case 17:r.state=2,r.amtstack.Put(d[0],d[1],r.xxWsmanReturn,r);break;case 18:r.state=2,r.amtstack.Create(d[0],d[1],r.xxWsmanReturn,r);break;case 19:r.state=2,r.amtstack.Delete(d[0],d[1],r.xxWsmanReturn,r);break;case 20:r.state=2,r.amtstack.Exec(d[0],d[1],d[2],r.xxWsmanReturn,r,0,d[3]);break;case 21:r.stepspeed=d[0],null!=r.timer&&(clearInterval(r.timer),r.timer=setInterval((function(){r.step()}),r.stepspeed));break;case 22:r.state=2,r.amtstack.Subscribe(d[0],d[1],d[2],r.xxWsmanReturn,r,0,d[3],d[4],d[5],d[6]);break;case 23:r.state=2,r.amtstack.UnSubscribe(d[0],r.xxWsmanReturn,r,0,d[1]);break;case 24:console.log(d[1],d[2],d[1].charCodeAt(d[2])),p=d[1].charCodeAt(d[2]);break;case 25:r.state=2,amtcert_signWithCaKey(d[0],null,d[1],{CN:"Untrusted Root Certificate"},r.xxSignWithDummyCaReturn);break;default:r.state=9,console.error("Script Error, unknown command: "+e)}else e<2e4?p=script_functionTableX2[e-1e4](d[1],d[2],d[3],d[4],d[5],d[6]):script_functionTableX3&&script_functionTableX3[e-2e4]&&(p=script_functionTableX3[e-2e4](r,d[1],d[2],d[3],d[4],d[5],d[6]));null!=p&&r.setVar(s[0],p)}catch(e){"object"==typeof e&&(e=e.message),r.setVar("_exception",e)}}return 1==r.state&&r.ip>=r.script.length&&(r.state=0,r.stop()),r.onStep&&r.onStep(r),r}},r.xxStepDialogOk=function(e){r.variables.DialogSelect=e,r.state=1,r.dialog=!1,r.onStep&&r.onStep(r)},r.xxWsmanReturnFix=function(e){e&&null!=e&&(e.Header&&(e.Header=e.Header,delete e.Header),e.Body&&(e.Body=e.Body,delete e.Body),e.Responses&&(e.Responses=e.Responses,delete e.Responses),e.Response&&(e.Response=e.Response,delete e.Response),e.ReturnValueStr&&(e.ReturnValueStr=e.ReturnValueStr,delete e.ReturnValueStr))},r.xxWsmanReturn=function(e,t,a,n){if(a)for(var s in r.xxWsmanReturnFix(a),a)for(var i in r.xxWsmanReturnFix(a[s]),a[s])r.xxWsmanReturnFix(a[s][i]);r.setVar(t,a),r.setVar("wsman_result",n),r.setVar("wsman_result_str",httpErrorTable[n]?httpErrorTable[n]:"Error #"+n),r.state=1,r.onStep&&r.onStep(r)},r.xxSignWithDummyCaReturn=function(e){r.setVar("signed_cert",btoa(_arrayBufferToString(e))),r.state=1,r.onStep&&r.onStep(r)},r.toString=function(e){return"object"==typeof e?JSON.stringify(e):e},r.reset(),r)}function script_compile(e,t){var r="",a=e.split("\n"),n={},s=[],i=[];for(var o in a){var l=a[o];if(l.startsWith("##SWAP "))3==(c=l.split(" ")).length&&(i[c[1]]=c[2]);if("#"!=l[0]&&0!=l.length){for(var c in i)l=l.split(c).join(i[c]);var u=l.match(/"[^"]*"|[^\s"]+/g);if(0!=u.length)if(":"!=l[0]){var p=script_functionTable1.indexOf(u[0].toLowerCase());if(-1==p&&(p=script_functionTable2.indexOf(u[0].toLowerCase()))>=0&&(p+=1e4),-1==p&&(p=script_functionTable3.indexOf(u[0].toLowerCase()))>=0&&(p+=2e4),-1==p)return t&&t("Unabled to compile, unknown command: "+u[0]),"";var d=ShortToStr(u.length-1);for(var f in u)if(0!=f)if(":"==u[f][0])s.push([u[f],r.length+d.length+7]),d+=ShortToStr(5)+String.fromCharCode(3)+IntToStr(4294967295);else{var h=parseInt(u[f]);h==u[f]?d+=ShortToStr(5)+String.fromCharCode(2)+IntToStr(h):'"'==u[f][0]&&'"'==u[f][u[f].length-1]?d+=ShortToStr(u[f].length-1)+String.fromCharCode(1)+u[f].substring(1,u[f].length-1):d+=ShortToStr(u[f].length+1)+String.fromCharCode(0)+u[f]}r+=d=ShortToStr(p)+ShortToStr(d.length+4)+d}else n[u[0].toUpperCase()]=r.length}}for(o in s){var b=s[o][0].toUpperCase(),g=s[o][1],S=n[b];if(null==S)return t&&t("Unabled to compile, unknown label: "+b),"";r=r.substr(0,g)+IntToStr(S)+r.substr(g+4)}return IntToStr(612182341)+ShortToStr(1)+r}function script_decompile(e,t){var r="",a=6,n={};if(t>=0)a=t;else{if(e.length<6)return"# Invalid script length";var s=ReadInt(e,0),i=ReadShort(e,4);if(612182341!=s)return"# Invalid binary script: "+s;if(1!=i)return"# Invalid script version"}for(;a<e.length;){var o=ReadShort(e,a),l=ReadShort(e,a+2),c=ReadShort(e,a+4),u=a+6,p="";t>=0||(r+=":label"+(a-6)+"\n");for(var d=0;d<c;d++){var f=ReadShort(e,u),h=e.substring(u+2,u+2+f),b=h.charCodeAt(0);if(0==b)p+=" "+h.substring(1);else if(1==b)p+=' "'+h.substring(1)+'"';else if(2==b)p+=" "+ReadInt(h,1);else if(3==b){var g=ReadInt(h,1),S=n[g];S||(n[S=":label"+g]=g),p+=" "+S}u+=2+f}if(r+=o<1e4?script_functionTable1[o]+p+"\n":o>=2e4?script_functionTable3[o-2e4]+p+"\n":script_functionTable2[o-1e4]+p+"\n",a+=l,t>=0)return r}var m=r.split("\n");for(var d in r="",m){var x=m[d];(":"!=x[0]||n[x])&&(r+=x+"\n")}return r}script_functionTable1=["nop","jump","set","print","dialog","getitem","substr","indexof","split","join","length","jsonparse","jsonstr","add","substract","parseint","wsbatchenum","wsput","wscreate","wsdelete","wsexec","scriptspeed","wssubscribe","wsunsubscribe","readchar","signwithdummyca"],script_functionTable2=["encodeuri","decodeuri","passwordcheck","atob","btoa","hex2str","str2hex","random","md5","maketoarray","readshort","readshortx","readint","readsint","readintx","shorttostr","shorttostrx","inttostr","inttostrx"],script_functionTableX2=[encodeURI,decodeURI,passwordcheck,window.atob.bind(window),window.btoa.bind(window),hex2rstr,rstr2hex,random,rstr_md5,MakeToArray,ReadShort,ReadShortX,ReadInt,ReadSInt,ReadIntX,ShortToStr,ShortToStrX,IntToStr,IntToStrX],script_functionTable3=["pullsystemstatus","pulleventlog","pullauditlog","pullcertificates","pullwatchdog","pullsystemdefense","pullhardware","pulluserinfo","pullremoteaccess","highlightblock","disconnect","getsidstring","getsidbytearray"],script_functionTableX3=[PullSystemStatus,PullEventLog,PullAuditLog,PullCertificates,PullWatchdog,PullSystemDefense,PullHardware,PullUserInfo,PullRemoteAccess,script_HighlightBlock,disconnect,function(e,t){return GetSidString(t)},function(e,t){return GetSidByteArray(t)}]