From d081396d4bc9cdc17335c17a814541ae5aeb38e3 Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Mon, 18 Feb 2019 18:08:36 -0800 Subject: [PATCH] Added server side agent msh config option. --- agents/modules_meshcore/win-terminal.js | 37 ++++++++++++------- .../modules_meshcore_min/win-terminal.min.js | 2 +- package.json | 2 +- sample-config.json | 13 ++++--- webserver.js | 4 ++ 5 files changed, 38 insertions(+), 20 deletions(-) diff --git a/agents/modules_meshcore/win-terminal.js b/agents/modules_meshcore/win-terminal.js index dbe2fc30..6a63b782 100644 --- a/agents/modules_meshcore/win-terminal.js +++ b/agents/modules_meshcore/win-terminal.js @@ -132,7 +132,9 @@ function windows_terminal() { this._kernel32.SetConsoleWindowInfo(this._stdoutput, 1, rect); } - this.Start = function Start(CONSOLE_SCREEN_WIDTH, CONSOLE_SCREEN_HEIGHT) { + this.Start = function Start(CONSOLE_SCREEN_WIDTH, CONSOLE_SCREEN_HEIGHT) + { + this.stopping = null; if (this._kernel32.GetConsoleWindow().Val == 0) { if (this._kernel32.AllocConsole().Val == 0) { throw ('AllocConsole failed with: ' + this._kernel32.GetLastError().Val); @@ -209,10 +211,12 @@ function windows_terminal() { this._stop = function () { if (this.stopping) { return (this.stopping); } //console.log('Stopping Terminal...'); + this._ConsoleWinEventProc.removeAllListeners('GlobalCallback'); this.stopping = new promise(function (res, rej) { this._res = res; this._rej = rej; }); var threadID = this._kernel32.GetThreadId(this._user32.SetWinEventHook.async.thread()).Val; this._user32.PostThreadMessageA(threadID, WM_QUIT, 0, 0); + this._stream.emit('end'); return (this.stopping); } @@ -272,8 +276,10 @@ function windows_terminal() { //SendConsoleEvent(dwEvent, idObject, idChild); break; case EVENT_CONSOLE_END_APPLICATION: - if (idObject.Val == this.terminal._hProcessID) { + if (idObject.Val == this.terminal._hProcessID) + { //console.log('END APPLICATION: [PID: ' + idObject.Val + ' CID: ' + idChild.Val + ']'); + this.terminal._hProcess = null; this.terminal._stop().then(function () { console.log('STOPPED'); }); } break; @@ -306,18 +312,23 @@ function windows_terminal() { }, console.log); }, console.log); } - } else { + } else + { this.nativeProxy.UnhookWinEvent.async(this.nativeProxy.terminal._user32.SetWinEventHook.async, this.nativeProxy.terminal.hwinEventHook) - .then(function () { - this.nativeProxy.terminal.stopping._res(); - if (this.nativeProxy.terminal._kernel32.TerminateProcess(this.nativeProxy.terminal._hProcess, 1067).Val == 0) { - var e = this.nativeProxy.terminal._kernel32.GetLastError().Val; - console.log('Unable to kill Terminal Process, error: ' + e); - } - this.nativeProxy.terminal.stopping = null; - }, function (err) { - console.log('REJECTED_UnhookWinEvent: ' + err); - }); + .then(function () + { + if (this.nativeProxy.terminal._hProcess == null) { return; } + + this.nativeProxy.terminal.stopping._res(); + if (this.nativeProxy.terminal._kernel32.TerminateProcess(this.nativeProxy.terminal._hProcess, 1067).Val == 0) { + var e = this.nativeProxy.terminal._kernel32.GetLastError().Val; + console.log('Unable to kill Terminal Process, error: ' + e); + } + this.nativeProxy.terminal.stopping = null; + }, function (err) + { + console.log('REJECTED_UnhookWinEvent: ' + err); + }); } }, function (err) { // Get Message Failed diff --git a/agents/modules_meshcore_min/win-terminal.min.js b/agents/modules_meshcore_min/win-terminal.min.js index 0277748f..a954e934 100644 --- a/agents/modules_meshcore_min/win-terminal.min.js +++ b/agents/modules_meshcore_min/win-terminal.min.js @@ -1 +1 @@ -var promise=require("promise");var duplex=require("stream").Duplex;var SW_HIDE=0;var SW_MINIMIZE=6;var STARTF_USESHOWWINDOW=1;var STD_INPUT_HANDLE=-10;var STD_OUTPUT_HANDLE=-11;var EVENT_CONSOLE_CARET=16385;var EVENT_CONSOLE_END_APPLICATION=16391;var WINEVENT_OUTOFCONTEXT=0;var WINEVENT_SKIPOWNPROCESS=2;var CREATE_NEW_PROCESS_GROUP=512;var EVENT_CONSOLE_UPDATE_REGION=16386;var EVENT_CONSOLE_UPDATE_SIMPLE=16387;var EVENT_CONSOLE_UPDATE_SCROLL=16388;var EVENT_CONSOLE_LAYOUT=16389;var EVENT_CONSOLE_START_APPLICATION=16390;var KEY_EVENT=1;var MAPVK_VK_TO_VSC=0;var WM_QUIT=18;var GM=require("_GenericMarshal");var si=GM.CreateVariable(GM.PointerSize==4?68:104);var pi=GM.CreateVariable(GM.PointerSize==4?16:24);si.Deref(0,4).toBuffer().writeUInt32LE(GM.PointerSize==4?68:104);si.Deref(GM.PointerSize==4?48:64,2).toBuffer().writeUInt16LE(SW_HIDE|SW_MINIMIZE);si.Deref(GM.PointerSize==4?44:60,4).toBuffer().writeUInt32LE(STARTF_USESHOWWINDOW);var MSG=GM.CreateVariable(GM.PointerSize==4?28:48);function windows_terminal(){this._ObjectID="windows_terminal";this._user32=GM.CreateNativeProxy("User32.dll");this._user32.CreateMethod("DispatchMessageA");this._user32.CreateMethod("GetMessageA");this._user32.CreateMethod("MapVirtualKeyA");this._user32.CreateMethod("PostThreadMessageA");this._user32.CreateMethod("SetWinEventHook");this._user32.CreateMethod("ShowWindow");this._user32.CreateMethod("TranslateMessage");this._user32.CreateMethod("UnhookWinEvent");this._user32.CreateMethod("VkKeyScanA");this._user32.terminal=this;this._kernel32=GM.CreateNativeProxy("Kernel32.dll");this._kernel32.CreateMethod("AllocConsole");this._kernel32.CreateMethod("CreateProcessA");this._kernel32.CreateMethod("CloseHandle");this._kernel32.CreateMethod("FillConsoleOutputAttribute");this._kernel32.CreateMethod("FillConsoleOutputCharacterA");this._kernel32.CreateMethod("GetConsoleScreenBufferInfo");this._kernel32.CreateMethod("GetConsoleWindow");this._kernel32.CreateMethod("GetLastError");this._kernel32.CreateMethod("GetStdHandle");this._kernel32.CreateMethod("GetThreadId");this._kernel32.CreateMethod("ReadConsoleOutputA");this._kernel32.CreateMethod("SetConsoleCursorPosition");this._kernel32.CreateMethod("SetConsoleScreenBufferSize");this._kernel32.CreateMethod("SetConsoleWindowInfo");this._kernel32.CreateMethod("TerminateProcess");this._kernel32.CreateMethod("WaitForSingleObject");this._kernel32.CreateMethod("WriteConsoleInputA");var b=0;var c=0;this._scrx=0;this._scry=0;this.SendCursorUpdate=function(){var f=GM.CreateVariable(22);if(this._kernel32.GetConsoleScreenBufferInfo(this._stdoutput,f).Val==0){return}if(f.Deref(4,2).toBuffer().readUInt16LE()!=this.currentX||f.Deref(6,2).toBuffer().readUInt16LE()!=this.currentY){this.currentX=f.Deref(4,2).toBuffer().readUInt16LE();this.currentY=f.Deref(6,2).toBuffer().readUInt16LE()}};this.ClearScreen=function(){var g=GM.CreateVariable(22);if(this._kernel32.GetConsoleScreenBufferInfo(this._stdoutput,g).Val==0){return}var h=GM.CreateVariable(4);var i=g.Deref(0,2).toBuffer().readUInt16LE(0)*g.Deref(2,2).toBuffer().readUInt16LE(0);var f=GM.CreateVariable(4);if(this._kernel32.FillConsoleOutputCharacterA(this._stdoutput,32,i,h.Deref(0,4).toBuffer().readUInt32LE(),f).Val==0){return}if(this._kernel32.GetConsoleScreenBufferInfo(this._stdoutput,g).Val==0){return}if(this._kernel32.FillConsoleOutputAttribute(this._stdoutput,g.Deref(8,2).toBuffer().readUInt16LE(0),i,h.Deref(0,4).toBuffer().readUInt32LE(),f).Val==0){return}this._kernel32.SetConsoleCursorPosition(this._stdoutput,h.Deref(0,4).toBuffer().readUInt32LE());var j=GM.CreateVariable(8);var k=g.Deref(10,8).toBuffer();j.Deref(4,2).toBuffer().writeUInt16LE(k.readUInt16LE(4)-k.readUInt16LE(0));j.Deref(6,2).toBuffer().writeUInt16LE(k.readUInt16LE(6)-k.readUInt16LE(2));this._kernel32.SetConsoleWindowInfo(this._stdoutput,1,j)};this.Start=function d(g,f){if(this._kernel32.GetConsoleWindow().Val==0){if(this._kernel32.AllocConsole().Val==0){throw ("AllocConsole failed with: "+this._kernel32.GetLastError().Val)}}this._stdinput=this._kernel32.GetStdHandle(STD_INPUT_HANDLE);this._stdoutput=this._kernel32.GetStdHandle(STD_OUTPUT_HANDLE);this._connected=false;var h=GM.CreateVariable(4);h.Deref(0,2).toBuffer().writeUInt16LE(g);h.Deref(2,2).toBuffer().writeUInt16LE(f);var i=GM.CreateVariable(8);i.Deref(4,2).toBuffer().writeUInt16LE(g-1);i.Deref(6,2).toBuffer().writeUInt16LE(f-1);if(this._kernel32.SetConsoleWindowInfo(this._stdoutput,1,i).Val==0){throw ("Failed to set Console Screen Size")}if(this._kernel32.SetConsoleScreenBufferSize(this._stdoutput,h.Deref(0,4).toBuffer().readUInt32LE()).Val==0){throw ("Failed to set Console Buffer Size")}this._user32.ShowWindow(this._kernel32.GetConsoleWindow().Val,SW_HIDE);this.ClearScreen();this._hookThread().then(function(){this.terminal.StartCommand()},console.log);this._stream=new duplex({write:function(j,k){if(!this.terminal.connected){if(!this._promise.chunk){this._promise.chunk=[]}if(typeof(j)=="string"){this._promise.chunk.push(j)}else{this._promise.chunk.push(Buffer.alloc(j.length));j.copy(this._promise.chunk.peek())}this._promise.chunk.peek().flush=k;this._promise.then(function(){var l;while(this.chunk.length>0){l=this.chunk.shift();this.terminal._WriteBuffer(l);l.flush()}})}else{this.terminal._WriteBuffer(j);k()}return(true)},"final":function(j){var k=this.terminal._stop();k.__flush=j;k.then(function(){this.__flush()})}});this._stream.terminal=this;this._stream._promise=new promise(function(k,j){this._res=k;this._rej=j});this._stream._promise.terminal=this;return(this._stream)};this._stop=function(){if(this.stopping){return(this.stopping)}this.stopping=new promise(function(h,g){this._res=h;this._rej=g});var f=this._kernel32.GetThreadId(this._user32.SetWinEventHook.async.thread()).Val;this._user32.PostThreadMessageA(f,WM_QUIT,0,0);return(this.stopping)};this._hookThread=function(){var g=new promise(function(i,h){this._res=i;this._rej=h});g.terminal=this;this._ConsoleWinEventProc=GM.GetGenericGlobalCallback(7);this._ConsoleWinEventProc.terminal=this;var f=this._user32.SetWinEventHook.async(EVENT_CONSOLE_CARET,EVENT_CONSOLE_END_APPLICATION,0,this._ConsoleWinEventProc,0,0,WINEVENT_OUTOFCONTEXT|WINEVENT_SKIPOWNPROCESS);f.ready=g;f.terminal=this;f.then(function(h){if(h.Val==0){this.ready._rej("Error calling SetWinEventHook")}else{this.terminal.hwinEventHook=h;this.ready._res();this.terminal._GetMessage()}});this._ConsoleWinEventProc.on("GlobalCallback",function(j,i,k,n,l,m,p){if(!this.terminal.hwinEventHook||this.terminal.hwinEventHook.Val!=j.Val){return}var h=null;switch(i.Val){case EVENT_CONSOLE_CARET:break;case EVENT_CONSOLE_UPDATE_REGION:if(!this.terminal.connected){this.terminal.connected=true;this.terminal._stream._promise._res()}if(this.terminal._scrollTimer==null){h=this.terminal._GetScreenBuffer(LOWORD(n.Val),HIWORD(n.Val),LOWORD(l.Val),HIWORD(l.Val));this.terminal._SendDataBuffer(h)}break;case EVENT_CONSOLE_UPDATE_SIMPLE:var o={data:[Buffer.alloc(1,LOWORD(l.Val))],attributes:[HIWORD(l.Val)],width:1,height:1,x:LOWORD(n.Val),y:HIWORD(n.Val)};this.terminal._SendDataBuffer(o);break;case EVENT_CONSOLE_UPDATE_SCROLL:this.terminal._SendScroll(n.Val,l.Val);break;case EVENT_CONSOLE_LAYOUT:break;case EVENT_CONSOLE_START_APPLICATION:break;case EVENT_CONSOLE_END_APPLICATION:if(n.Val==this.terminal._hProcessID){this.terminal._stop().then(function(){console.log("STOPPED")})}break;default:console.log("Unknown event: "+i.Val);break}});return(g)};this._GetMessage=function(){if(this._user32.abort){console.log("aborting loop");return}this._user32.GetMessageA.async(this._user32.SetWinEventHook.async,MSG,0,0,0).then(function(f){if(f.Val!=0){if(f.Val==-1){}else{this.nativeProxy._user32.TranslateMessage.async(this.nativeProxy.user32.SetWinEventHook.async,MSG).then(function(){this.nativeProxy._user32.DispatchMessageA.async(this.nativeProxy.user32.SetWinEventHook.async,MSG).then(function(){this.nativeProxy.terminal._GetMessage()},console.log)},console.log)}}else{this.nativeProxy.UnhookWinEvent.async(this.nativeProxy.terminal._user32.SetWinEventHook.async,this.nativeProxy.terminal.hwinEventHook).then(function(){this.nativeProxy.terminal.stopping._res();if(this.nativeProxy.terminal._kernel32.TerminateProcess(this.nativeProxy.terminal._hProcess,1067).Val==0){var g=this.nativeProxy.terminal._kernel32.GetLastError().Val;console.log("Unable to kill Terminal Process, error: "+g)}this.nativeProxy.terminal.stopping=null},function(g){console.log("REJECTED_UnhookWinEvent: "+g)})}},function(f){console.log("REJECTED_GETMessage: "+f)})};this._WriteBuffer=function(f){for(var g=0;gl;++j){this._stream.push(Buffer.from("\r\n"))}var f=this._GetScreenBuffer(0,0,m-1,l-1);this._SendDataBuffer(f);this._scrollTimer=setTimeout(function(p,o,n){var i=p._GetScreenBuffer(0,0,o-1,n-1);p._SendDataBuffer(i);p._scrollTimer=null},250,this,m,l)};this.StartCommand=function e(){if(this._kernel32.CreateProcessA(GM.CreateVariable(process.env.windir+"\\system32\\cmd.exe"),0,0,0,1,CREATE_NEW_PROCESS_GROUP,0,0,si,pi).Val==0){console.log("Error Spawning CMD");return}this._kernel32.CloseHandle(pi.Deref(GM.PointerSize,GM.PointerSize).Deref());this._hProcess=pi.Deref(0,GM.PointerSize).Deref();this._hProcessID=pi.Deref(GM.PointerSize==4?8:16,4).toBuffer().readUInt32LE()}}function LOWORD(a){return(a&65535)}function HIWORD(a){return((a>>16)&65535)}function GetEsc(b,a){return(Buffer.from("\x1B["+a.join(";")+b))}function MeshConsole(a){require("MeshAgent").SendCommand({action:"msg",type:"console",value:JSON.stringify(a)})}function TranslateLine(r,s,f,a){var m,l,e,q,j,c,n,k,d,p,h,b,g=[],o=[GetEsc("H",[s,r])];if(typeof a=="number"){a=[a]}for(m=0;m>2);d=(a[m]&112)>>4;d=((d&1)<<2)+(d&2)+((d&4)>>2);p=(a[m]&16384);h=(a[m]&8)>>3;b=(a[m]&128);if(p!=q){if(p!=0){g.push(7)}else{g.push(0);l=7;e=0;j=0;c=0}q=p}if(k!=l){g.push(k+30);l=k}if(d!=e){g.push(d+40);e=d}if(h!=j){g.push(2-h);j=h}if(b!=c){if(b==0){g.push(e+40)}else{g.push(e+100);c=b}}if(g.length>0){o.push(GetEsc("m",g));g=[]}n=a[m]}o.push(Buffer.from(String.fromCharCode(f[m])))}return Buffer.concat(o)}module.exports=new windows_terminal(); \ No newline at end of file +var promise=require("promise");var duplex=require("stream").Duplex;var SW_HIDE=0;var SW_MINIMIZE=6;var STARTF_USESHOWWINDOW=1;var STD_INPUT_HANDLE=-10;var STD_OUTPUT_HANDLE=-11;var EVENT_CONSOLE_CARET=16385;var EVENT_CONSOLE_END_APPLICATION=16391;var WINEVENT_OUTOFCONTEXT=0;var WINEVENT_SKIPOWNPROCESS=2;var CREATE_NEW_PROCESS_GROUP=512;var EVENT_CONSOLE_UPDATE_REGION=16386;var EVENT_CONSOLE_UPDATE_SIMPLE=16387;var EVENT_CONSOLE_UPDATE_SCROLL=16388;var EVENT_CONSOLE_LAYOUT=16389;var EVENT_CONSOLE_START_APPLICATION=16390;var KEY_EVENT=1;var MAPVK_VK_TO_VSC=0;var WM_QUIT=18;var GM=require("_GenericMarshal");var si=GM.CreateVariable(GM.PointerSize==4?68:104);var pi=GM.CreateVariable(GM.PointerSize==4?16:24);si.Deref(0,4).toBuffer().writeUInt32LE(GM.PointerSize==4?68:104);si.Deref(GM.PointerSize==4?48:64,2).toBuffer().writeUInt16LE(SW_HIDE|SW_MINIMIZE);si.Deref(GM.PointerSize==4?44:60,4).toBuffer().writeUInt32LE(STARTF_USESHOWWINDOW);var MSG=GM.CreateVariable(GM.PointerSize==4?28:48);function windows_terminal(){this._ObjectID="windows_terminal";this._user32=GM.CreateNativeProxy("User32.dll");this._user32.CreateMethod("DispatchMessageA");this._user32.CreateMethod("GetMessageA");this._user32.CreateMethod("MapVirtualKeyA");this._user32.CreateMethod("PostThreadMessageA");this._user32.CreateMethod("SetWinEventHook");this._user32.CreateMethod("ShowWindow");this._user32.CreateMethod("TranslateMessage");this._user32.CreateMethod("UnhookWinEvent");this._user32.CreateMethod("VkKeyScanA");this._user32.terminal=this;this._kernel32=GM.CreateNativeProxy("Kernel32.dll");this._kernel32.CreateMethod("AllocConsole");this._kernel32.CreateMethod("CreateProcessA");this._kernel32.CreateMethod("CloseHandle");this._kernel32.CreateMethod("FillConsoleOutputAttribute");this._kernel32.CreateMethod("FillConsoleOutputCharacterA");this._kernel32.CreateMethod("GetConsoleScreenBufferInfo");this._kernel32.CreateMethod("GetConsoleWindow");this._kernel32.CreateMethod("GetLastError");this._kernel32.CreateMethod("GetStdHandle");this._kernel32.CreateMethod("GetThreadId");this._kernel32.CreateMethod("ReadConsoleOutputA");this._kernel32.CreateMethod("SetConsoleCursorPosition");this._kernel32.CreateMethod("SetConsoleScreenBufferSize");this._kernel32.CreateMethod("SetConsoleWindowInfo");this._kernel32.CreateMethod("TerminateProcess");this._kernel32.CreateMethod("WaitForSingleObject");this._kernel32.CreateMethod("WriteConsoleInputA");var b=0;var c=0;this._scrx=0;this._scry=0;this.SendCursorUpdate=function(){var f=GM.CreateVariable(22);if(this._kernel32.GetConsoleScreenBufferInfo(this._stdoutput,f).Val==0){return}if(f.Deref(4,2).toBuffer().readUInt16LE()!=this.currentX||f.Deref(6,2).toBuffer().readUInt16LE()!=this.currentY){this.currentX=f.Deref(4,2).toBuffer().readUInt16LE();this.currentY=f.Deref(6,2).toBuffer().readUInt16LE()}};this.ClearScreen=function(){var g=GM.CreateVariable(22);if(this._kernel32.GetConsoleScreenBufferInfo(this._stdoutput,g).Val==0){return}var h=GM.CreateVariable(4);var i=g.Deref(0,2).toBuffer().readUInt16LE(0)*g.Deref(2,2).toBuffer().readUInt16LE(0);var f=GM.CreateVariable(4);if(this._kernel32.FillConsoleOutputCharacterA(this._stdoutput,32,i,h.Deref(0,4).toBuffer().readUInt32LE(),f).Val==0){return}if(this._kernel32.GetConsoleScreenBufferInfo(this._stdoutput,g).Val==0){return}if(this._kernel32.FillConsoleOutputAttribute(this._stdoutput,g.Deref(8,2).toBuffer().readUInt16LE(0),i,h.Deref(0,4).toBuffer().readUInt32LE(),f).Val==0){return}this._kernel32.SetConsoleCursorPosition(this._stdoutput,h.Deref(0,4).toBuffer().readUInt32LE());var j=GM.CreateVariable(8);var k=g.Deref(10,8).toBuffer();j.Deref(4,2).toBuffer().writeUInt16LE(k.readUInt16LE(4)-k.readUInt16LE(0));j.Deref(6,2).toBuffer().writeUInt16LE(k.readUInt16LE(6)-k.readUInt16LE(2));this._kernel32.SetConsoleWindowInfo(this._stdoutput,1,j)};this.Start=function d(g,f){this.stopping=null;if(this._kernel32.GetConsoleWindow().Val==0){if(this._kernel32.AllocConsole().Val==0){throw ("AllocConsole failed with: "+this._kernel32.GetLastError().Val)}}this._stdinput=this._kernel32.GetStdHandle(STD_INPUT_HANDLE);this._stdoutput=this._kernel32.GetStdHandle(STD_OUTPUT_HANDLE);this._connected=false;var h=GM.CreateVariable(4);h.Deref(0,2).toBuffer().writeUInt16LE(g);h.Deref(2,2).toBuffer().writeUInt16LE(f);var i=GM.CreateVariable(8);i.Deref(4,2).toBuffer().writeUInt16LE(g-1);i.Deref(6,2).toBuffer().writeUInt16LE(f-1);if(this._kernel32.SetConsoleWindowInfo(this._stdoutput,1,i).Val==0){throw ("Failed to set Console Screen Size")}if(this._kernel32.SetConsoleScreenBufferSize(this._stdoutput,h.Deref(0,4).toBuffer().readUInt32LE()).Val==0){throw ("Failed to set Console Buffer Size")}this._user32.ShowWindow(this._kernel32.GetConsoleWindow().Val,SW_HIDE);this.ClearScreen();this._hookThread().then(function(){this.terminal.StartCommand()},console.log);this._stream=new duplex({write:function(j,k){if(!this.terminal.connected){if(!this._promise.chunk){this._promise.chunk=[]}if(typeof(j)=="string"){this._promise.chunk.push(j)}else{this._promise.chunk.push(Buffer.alloc(j.length));j.copy(this._promise.chunk.peek())}this._promise.chunk.peek().flush=k;this._promise.then(function(){var l;while(this.chunk.length>0){l=this.chunk.shift();this.terminal._WriteBuffer(l);l.flush()}})}else{this.terminal._WriteBuffer(j);k()}return(true)},"final":function(j){var k=this.terminal._stop();k.__flush=j;k.then(function(){this.__flush()})}});this._stream.terminal=this;this._stream._promise=new promise(function(k,j){this._res=k;this._rej=j});this._stream._promise.terminal=this;return(this._stream)};this._stop=function(){if(this.stopping){return(this.stopping)}this._ConsoleWinEventProc.removeAllListeners("GlobalCallback");this.stopping=new promise(function(h,g){this._res=h;this._rej=g});var f=this._kernel32.GetThreadId(this._user32.SetWinEventHook.async.thread()).Val;this._user32.PostThreadMessageA(f,WM_QUIT,0,0);this._stream.emit("end");return(this.stopping)};this._hookThread=function(){var g=new promise(function(i,h){this._res=i;this._rej=h});g.terminal=this;this._ConsoleWinEventProc=GM.GetGenericGlobalCallback(7);this._ConsoleWinEventProc.terminal=this;var f=this._user32.SetWinEventHook.async(EVENT_CONSOLE_CARET,EVENT_CONSOLE_END_APPLICATION,0,this._ConsoleWinEventProc,0,0,WINEVENT_OUTOFCONTEXT|WINEVENT_SKIPOWNPROCESS);f.ready=g;f.terminal=this;f.then(function(h){if(h.Val==0){this.ready._rej("Error calling SetWinEventHook")}else{this.terminal.hwinEventHook=h;this.ready._res();this.terminal._GetMessage()}});this._ConsoleWinEventProc.on("GlobalCallback",function(j,i,k,n,l,m,p){if(!this.terminal.hwinEventHook||this.terminal.hwinEventHook.Val!=j.Val){return}var h=null;switch(i.Val){case EVENT_CONSOLE_CARET:break;case EVENT_CONSOLE_UPDATE_REGION:if(!this.terminal.connected){this.terminal.connected=true;this.terminal._stream._promise._res()}if(this.terminal._scrollTimer==null){h=this.terminal._GetScreenBuffer(LOWORD(n.Val),HIWORD(n.Val),LOWORD(l.Val),HIWORD(l.Val));this.terminal._SendDataBuffer(h)}break;case EVENT_CONSOLE_UPDATE_SIMPLE:var o={data:[Buffer.alloc(1,LOWORD(l.Val))],attributes:[HIWORD(l.Val)],width:1,height:1,x:LOWORD(n.Val),y:HIWORD(n.Val)};this.terminal._SendDataBuffer(o);break;case EVENT_CONSOLE_UPDATE_SCROLL:this.terminal._SendScroll(n.Val,l.Val);break;case EVENT_CONSOLE_LAYOUT:break;case EVENT_CONSOLE_START_APPLICATION:break;case EVENT_CONSOLE_END_APPLICATION:if(n.Val==this.terminal._hProcessID){this.terminal._hProcess=null;this.terminal._stop().then(function(){console.log("STOPPED")})}break;default:console.log("Unknown event: "+i.Val);break}});return(g)};this._GetMessage=function(){if(this._user32.abort){console.log("aborting loop");return}this._user32.GetMessageA.async(this._user32.SetWinEventHook.async,MSG,0,0,0).then(function(f){if(f.Val!=0){if(f.Val==-1){}else{this.nativeProxy._user32.TranslateMessage.async(this.nativeProxy.user32.SetWinEventHook.async,MSG).then(function(){this.nativeProxy._user32.DispatchMessageA.async(this.nativeProxy.user32.SetWinEventHook.async,MSG).then(function(){this.nativeProxy.terminal._GetMessage()},console.log)},console.log)}}else{this.nativeProxy.UnhookWinEvent.async(this.nativeProxy.terminal._user32.SetWinEventHook.async,this.nativeProxy.terminal.hwinEventHook).then(function(){if(this.nativeProxy.terminal._hProcess==null){return}this.nativeProxy.terminal.stopping._res();if(this.nativeProxy.terminal._kernel32.TerminateProcess(this.nativeProxy.terminal._hProcess,1067).Val==0){var g=this.nativeProxy.terminal._kernel32.GetLastError().Val;console.log("Unable to kill Terminal Process, error: "+g)}this.nativeProxy.terminal.stopping=null},function(g){console.log("REJECTED_UnhookWinEvent: "+g)})}},function(f){console.log("REJECTED_GETMessage: "+f)})};this._WriteBuffer=function(f){for(var g=0;gl;++j){this._stream.push(Buffer.from("\r\n"))}var f=this._GetScreenBuffer(0,0,m-1,l-1);this._SendDataBuffer(f);this._scrollTimer=setTimeout(function(p,o,n){var i=p._GetScreenBuffer(0,0,o-1,n-1);p._SendDataBuffer(i);p._scrollTimer=null},250,this,m,l)};this.StartCommand=function e(){if(this._kernel32.CreateProcessA(GM.CreateVariable(process.env.windir+"\\system32\\cmd.exe"),0,0,0,1,CREATE_NEW_PROCESS_GROUP,0,0,si,pi).Val==0){console.log("Error Spawning CMD");return}this._kernel32.CloseHandle(pi.Deref(GM.PointerSize,GM.PointerSize).Deref());this._hProcess=pi.Deref(0,GM.PointerSize).Deref();this._hProcessID=pi.Deref(GM.PointerSize==4?8:16,4).toBuffer().readUInt32LE()}}function LOWORD(a){return(a&65535)}function HIWORD(a){return((a>>16)&65535)}function GetEsc(b,a){return(Buffer.from("\x1B["+a.join(";")+b))}function MeshConsole(a){require("MeshAgent").SendCommand({action:"msg",type:"console",value:JSON.stringify(a)})}function TranslateLine(r,s,f,a){var m,l,e,q,j,c,n,k,d,p,h,b,g=[],o=[GetEsc("H",[s,r])];if(typeof a=="number"){a=[a]}for(m=0;m>2);d=(a[m]&112)>>4;d=((d&1)<<2)+(d&2)+((d&4)>>2);p=(a[m]&16384);h=(a[m]&8)>>3;b=(a[m]&128);if(p!=q){if(p!=0){g.push(7)}else{g.push(0);l=7;e=0;j=0;c=0}q=p}if(k!=l){g.push(k+30);l=k}if(d!=e){g.push(d+40);e=d}if(h!=j){g.push(2-h);j=h}if(b!=c){if(b==0){g.push(e+40)}else{g.push(e+100);c=b}}if(g.length>0){o.push(GetEsc("m",g));g=[]}n=a[m]}o.push(Buffer.from(String.fromCharCode(f[m])))}return Buffer.concat(o)}module.exports=new windows_terminal(); \ No newline at end of file diff --git a/package.json b/package.json index 11161818..876a9f5e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "meshcentral", - "version": "0.2.8-r", + "version": "0.2.8-s", "keywords": [ "Remote Management", "Intel AMT", diff --git a/sample-config.json b/sample-config.json index 2b6dfaa9..5c954f86 100644 --- a/sample-config.json +++ b/sample-config.json @@ -42,11 +42,14 @@ "_AgentAllowedIP": "192.168.0.100/24", "_AgentBlockedIP": "127.0.0.1,::1", "_Limits": { - "MaxUserAccounts": 100, - "MaxUserSessions": 100, - "MaxAgentSessions": 100 + "_MaxUserAccounts": 100, + "_MaxUserSessions": 100, + "_MaxAgentSessions": 100, + "MaxSingleUserSessions": 10 }, - "_yubikey": { "id": "0000", "secret": "xxxxxxxxxxxxxxxxxxxxx", "_proxy": "http://myproxy.domain.com:80" } + "_yubikey": { "id": "0000", "secret": "xxxxxxxxxxxxxxxxxxxxx", "_proxy": "http://myproxy.domain.com:80" }, + "_httpheaders": { "Strict-Transport-Security": "max-age=360000" }, + "_agentConfig": [ "webSocketMaskOverride=1" ] }, "customer1": { "DNS": "customer1.myserver.com", @@ -62,7 +65,7 @@ } }, "_letsencrypt": { - "__comment__": "If your are having problems with Let's Encrypt, use https://letsdebug.net/ first to debug it.", + "__comment__": "Go to https://letsdebug.net/ first before trying Let's Encrypt.", "email": "myemail@myserver.com ", "names": "myserver.com,customer1.myserver.com", "rsaKeySize": 3072, diff --git a/webserver.js b/webserver.js index f91bec76..322c8d1c 100644 --- a/webserver.js +++ b/webserver.js @@ -1927,6 +1927,8 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) { if (req.query.tag != null) { meshsettings += "Tag=" + req.query.tag + "\r\n"; } if ((req.query.installflags != null) && (req.query.installflags != 0)) { meshsettings += "InstallFlags=" + req.query.installflags + "\r\n"; } if (domain.agentnoproxy === true) { meshsettings += "ignoreProxyFile=1\r\n"; } + if (obj.args.agentconfig) { for (var i in obj.args.agentconfig) { meshsettings += obj.args.agentconfig[i] + "\r\n"; } } + if (domain.agentconfig) { for (var i in domain.agentconfig) { meshsettings += domain.agentconfig[i] + "\r\n"; } } res.set({ 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'Content-Type': 'application/octet-stream', 'Content-Disposition': 'attachment; filename=' + argentInfo.rname }); obj.parent.exeHandler.streamExeWithMeshPolicy({ platform: 'win32', sourceFileName: obj.parent.meshAgentBinaries[req.query.id].path, destinationStream: res, msh: meshsettings, peinfo: obj.parent.meshAgentBinaries[req.query.id].pe }); @@ -2147,6 +2149,8 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) { if (req.query.tag != null) { meshsettings += "Tag=" + req.query.tag + "\r\n"; } if ((req.query.installflags != null) && (req.query.installflags != 0)) { meshsettings += "InstallFlags=" + req.query.installflags + "\r\n"; } if (domain.agentnoproxy === true) { meshsettings += "ignoreProxyFile=1\r\n"; } + if (obj.args.agentconfig) { for (var i in obj.args.agentconfig) { meshsettings += obj.args.agentconfig[i] + "\r\n"; } } + if (domain.agentconfig) { for (var i in domain.agentconfig) { meshsettings += domain.agentconfig[i] + "\r\n"; } } res.set({ 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'Content-Type': 'application/octet-stream', 'Content-Disposition': 'attachment; filename=meshagent.msh' }); res.send(meshsettings);