mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2024-12-18 19:32:18 +03:00
1 line
8.7 KiB
JavaScript
1 line
8.7 KiB
JavaScript
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,atob,btoa,hex2rstr,rstr2hex,random,rstr_md5,MakeToArray,ReadShort,ReadShortX,ReadInt,ReadSInt,ReadIntX,ShortToStr,ShortToStrX,IntToStr,IntToStrX];function MakeToArray(a){if(!a||a==null||typeof a=="object"){return a}return[a]}function ReadShort(b,a){return(b[a]<<8)+b[a+1]}function ReadShortX(b,a){return(b[a+1]<<8)+b[a]}function ReadInt(b,a){return(b[a]*16777216)+(b[a+1]<<16)+(b[a+2]<<8)+b[a+3]}function ReadSInt(b,a){return(b[a]<<24)+(b[a+1]<<16)+(b[a+2]<<8)+b[a+3]}function ReadIntX(b,a){return(b[a+3]*16777216)+(b[a+2]<<16)+(b[a+1]<<8)+b[a]}function ShortToStr(a){return String.fromCharCode((a>>8)&255,a&255)}function ShortToStrX(a){return String.fromCharCode(a&255,(a>>8)&255)}function IntToStr(a){return String.fromCharCode((a>>24)&255,(a>>16)&255,(a>>8)&255,a&255)}function IntToStrX(a){return String.fromCharCode(a&255,(a>>8)&255,(a>>16)&255,(a>>24)&255)}function btoa(a){return Buffer.from(a).toString("base64")}function atob(b){var c=null;try{c=Buffer.from(b,"base64").toString()}catch(a){console.log(a)}return c}function passwordcheck(g){if(g.length<8){return false}var h=0,d=0,f=0,e=0;for(var b in g){var a=g.charCodeAt(b);if((a>64)&&(a<91)){h=1}else{if((a>96)&&(a<123)){d=1}else{if((a>47)&&(a<58)){f=1}else{e=1}}}}return((h+d+f+e)==4)}function hex2rstr(a){Buffer.from(a,"hex").toString()}function rstr2hex(a){Buffer.from(a).toString("hex")}function random(){return Math.floor(Math.random()*max)}function rstr_md5(a){return hex2rstr(hex_md5(a))}function getItem(b,c,d){for(var a in b){if(b[a][c]==d){return b[a]}}return null}var httpErrorTable={200:"OK",401:"Authentication Error",408:"Timeout Error",601:"WSMAN Parsing Error",602:"Unable to parse HTTP response header",603:"Unexpected HTTP enum response",604:"Unexpected HTTP pull response",998:"Invalid TLS certificate"};module.exports.setup=function(a,c){var b={startvars:c,onCompleted:null};if(a.length<6){console.error("Invalid script length");return null}if(ReadInt(a,0)!=612182341){console.error("Invalid binary script");return null}if(ReadShort(a,4)>1){console.error("Unsupported script version");return null}b.script=a.slice(6);b.reset=function(d){b.stop();b.ip=0;b.variables=c;b.state=1};b.start=function(d){b.stop();if(d==null){b.stepspeed=10}else{b.stepspeed=d}if(b.stepspeed>0){b.timer=setInterval(function(){b.step()},b.stepspeed)}};b.stop=function(){if(b.timer!=null){clearInterval(b.timer)}b.timer=null;b.stepspeed=0};b.getVar=function(d){if(d==undefined){return undefined}return b.getVarEx(d.split("."),b.variables)};b.getVarEx=function(f,g){try{if(f==undefined){return undefined}if(f.length==0){return g}return b.getVarEx(f.slice(1),g[f[0]])}catch(d){return null}};b.setVar=function(d,e){b.setVarEx(d.split("."),b.variables,e)};b.setVarEx=function(d,f,e){if(d.length==1){f[d[0]]=e}else{b.setVarEx(d.slice(1),f[d[0]],e)}};b.step=function(){if(b.state!=1){return}if(b.ip<b.script.length){var m=ReadShort(b.script,b.ip);var n=ReadShort(b.script,b.ip+2);var d=ReadShort(b.script,b.ip+4);var g=b.ip+6;var h=[];for(var p in b.variables){if(p.startsWith("__")){delete b.variables[p]}}for(var p=0;p<d;p++){var f=ReadShort(b.script,g);var l=b.script.slice(g+2,g+2+f);var k=l[0];l=l.slice(1);if(k<2){l=l.toString();if(l!=null){while(l.split("{").length>1){var r=l.split("{").pop().split("}").shift();l=l.replace("{"+r+"}",b.getVar(r))}}if(k==1){b.variables["__"+p]=decodeURI(l);l="__"+p}h.push(l)}if(k==2||k==3){b.variables["__"+p]=ReadSInt(l,0);h.push("__"+p)}g+=(2+f)}b.ip+=n;var j=[];for(var p=0;p<10;p++){j.push(b.getVar(h[p]))}var q;try{if(m<10000){switch(m){case 0:break;case 1:if(j[2]){if((j[2]=="<"&&j[1]<j[3])||(j[2]=="<="&&j[1]<=j[3])||(j[2]=="!="&&j[1]!=j[3])||(j[2]=="="&&j[1]==j[3])||(j[2]==">="&&j[1]>=j[3])||(j[2]==">"&&j[1]>j[3])){b.ip=j[0]}}else{b.ip=j[0]}break;case 2:if(h[1]==undefined){delete b.variables[h[0]]}else{b.setVar(h[0],j[1])}break;case 3:var s=b.toString(j[0]);if(s.indexOf("INFO: ")==0){s=s.substring(6)}if(s.indexOf("SUCCESS: ")==0){s=s.substring(9)}if(b.onConsole){b.onConsole(s,b)}else{console.log(s)}break;case 4:b.state=2;b.dialog=true;setDialogMode(11,j[0],j[2],b.xxStepDialogOk,j[1],b);break;case 5:for(var p in j[1]){if(j[1][p][j[2]]==j[3]){q=p}}break;case 6:q=j[1].substr(j[2],j[3]);break;case 7:q=j[1].indexOf(j[2]);break;case 8:q=j[1].split(j[2]);break;case 9:q=j[1].join(j[2]);break;case 10:if(j[1]==null){q=0}else{q=j[1].length}break;case 11:q=JSON.parse(j[1]);break;case 12:q=JSON.stringify(j[1]);break;case 13:q=(j[1]+j[2]);break;case 14:q=(j[1]-j[2]);break;case 15:q=parseInt(j[1]);break;case 16:b.state=2;b.amtstack.BatchEnum(j[0],j[1],b.xxWsmanReturn,b);break;case 17:b.state=2;b.amtstack.Put(j[0],j[1],b.xxWsmanReturn,b);break;case 18:b.state=2;b.amtstack.Create(j[0],j[1],b.xxWsmanReturn,b);break;case 19:b.state=2;b.amtstack.Delete(j[0],j[1],b.xxWsmanReturn,b);break;case 20:b.state=2;b.amtstack.Exec(j[0],j[1],j[2],b.xxWsmanReturn,b,0,j[3]);break;case 21:b.stepspeed=j[0];if(b.timer!=null){clearInterval(b.timer);b.timer=setInterval(function(){b.step()},b.stepspeed)}break;case 22:b.state=2;b.amtstack.Subscribe(j[0],j[1],j[2],b.xxWsmanReturn,b,0,j[3],j[4],j[5],j[6]);break;case 23:b.state=2;b.amtstack.UnSubscribe(j[0],b.xxWsmanReturn,b,0,j[1]);break;case 24:console.log(j[1],j[2],j[1].charCodeAt(j[2]));q=j[1].charCodeAt(j[2]);break;case 25:break;default:b.state=9;console.error("Script Error, unknown command: "+m)}}else{if(m<20000){q=script_functionTableX2[m-10000](j[1],j[2],j[3],j[4],j[5],j[6])}else{}}if(q!=undefined){b.setVar(h[0],q)}}catch(o){if(typeof o=="object"){o=o.message}b.setVar("_exception",o)}}if(b.state==1&&b.ip>=b.script.length){b.state=0;b.stop();if(b.onCompleted){b.onCompleted()}}if(b.onStep){b.onStep(b)}return b};b.xxStepDialogOk=function(d){b.variables.DialogSelect=d;b.state=1;b.dialog=false;if(b.onStep){b.onStep(b)}};b.xxWsmanReturn=function(f,d,e,g){b.setVar(d,e);b.setVar("wsman_result",g);b.setVar("wsman_result_str",((httpErrorTable[g])?(httpErrorTable[g]):("Error #"+g)));b.state=1;if(b.onStep){b.onStep(b)}};b.toString=function(d){if(typeof d=="object"){return JSON.stringify(d)}return d};b.reset();return b};module.exports.compile=function(o,l){var n="",q=o.split("\n"),h={},k=[],s=[];for(var d in q){var p=q[d];if(p.startsWith("##SWAP ")){var u=p.split(" ");if(u.length==3){s[u[1]]=u[2]}}if(p[0]=="#"||p.length==0){continue}for(var u in s){p=p.split(u).join(s[u])}var f=p.match(/"[^"]*"|[^\s"]+/g);if(f.length==0){continue}if(p[0]==":"){h[f[0].toUpperCase()]=n.length;continue}var c=script_functionTable1.indexOf(f[0].toLowerCase());if(c==-1){c=script_functionTable2.indexOf(f[0].toLowerCase());if(c>=0){c+=10000}}if(c==-1){if(l){l("Unabled to compile, unknown command: "+f[0])}return""}var b=ShortToStr(f.length-1);for(var e in f){if(e==0){continue}if(f[e][0]==":"){k.push([f[e],n.length+b.length+7]);b+=ShortToStr(5)+String.fromCharCode(3)+IntToStr(4294967295)}else{var a=parseInt(f[e]);if(a==f[e]){b+=ShortToStr(5)+String.fromCharCode(2)+IntToStr(a)}else{if(f[e][0]=='"'&&f[e][f[e].length-1]=='"'){b+=ShortToStr(f[e].length-1)+String.fromCharCode(1)+f[e].substring(1,f[e].length-1)}else{b+=ShortToStr(f[e].length+1)+String.fromCharCode(0)+f[e]}}}}b=ShortToStr(c)+ShortToStr(b.length+4)+b;n+=b}for(d in k){var g=k[d][0].toUpperCase(),m=k[d][1],t=h[g];if(t==undefined){if(l){l("Unabled to compile, unknown label: "+g)}return""}n=n.substr(0,m)+IntToStr(t)+n.substr(m+4)}return IntToStr(612182341)+ShortToStr(1)+n};module.exports.decompile=function(g,q){var t="",s=6,m=0,n={};if(q>=0){s=q}else{if(g.length<6){return"# Invalid script length"}var p=ReadInt(g,0);var w=ReadShort(g,4);if(p!=612182341){return"# Invalid binary script: "+p}if(w!=1){return"# Invalid script version"}}while(s<g.length){var h=ReadShort(g,s);var j=ReadShort(g,s+2);var a=ReadShort(g,s+4);var c=s+6;var d="";if(!(q>=0)){t+=":label"+(s-6)+"\n"}for(var k=0;k<a;k++){var b=ReadShort(g,c);var f=g.substring(c+2,c+2+b);var e=f.charCodeAt(0);if(e==0){d+=" "+f.substring(1)}else{if(e==1){d+=' "'+f.substring(1)+'"'}else{if(e==2){d+=" "+ReadInt(f,1)}else{if(e==3){var v=ReadInt(f,1);var l=n[v];if(!l){l=":label"+v;n[l]=v}d+=" "+l}}}}c+=(2+b)}if(h<10000){t+=script_functionTable1[h]+d+"\n"}else{if((h>=10000)&&(h<10000)){t+=script_functionTable2[h-10000]+d+"\n"}}s+=j;if(q>=0){return t}}var u=t.split("\n");t="";for(var k in u){var o=u[k];if(o[0]!=":"){t+=o+"\n"}else{if(n[o]){t+=o+"\n"}}}return t}; |