welsonjs/lib/powershell.js

149 lines
3.7 KiB
JavaScript
Raw Normal View History

////////////////////////////////////////////////////////////////////////
2023-03-09 10:16:00 +00:00
// Powershell Interface API
///////////////////////////////////////////////////////////////////////
2020-07-18 20:02:49 +00:00
var SHELL = require("lib/shell");
2023-03-10 02:36:36 +00:00
var PowershellObject = function() {
2023-03-10 02:26:53 +00:00
this.execType = "ps1";
this.dataType = -1;
2023-03-09 10:16:00 +00:00
this.target = null;
2023-03-10 02:26:53 +00:00
2023-03-10 02:28:23 +00:00
this.setExecType = function(execType) {
2023-03-10 02:26:53 +00:00
this.execType = execType;
2023-03-10 02:36:36 +00:00
return this;
2023-03-10 02:26:53 +00:00
};
2023-03-09 10:16:00 +00:00
this.load = function(script) {
this.target = script;
2023-03-10 02:26:53 +00:00
this.dataType = 0;
2023-03-10 02:36:36 +00:00
return this;
2023-03-10 02:26:53 +00:00
};
this.loadCommand = function(command) {
this.target = command;
this.dataType = 1;
2023-03-10 02:36:36 +00:00
return this;
2023-03-09 10:16:00 +00:00
};
this.loadFile = function(filename) {
this.target = filename;
2023-03-10 02:26:53 +00:00
this.dataType = 2;
2023-03-10 02:36:36 +00:00
return this;
2023-03-09 10:16:00 +00:00
};
2023-03-10 02:26:53 +00:00
this.loadUrl = function(url) {
2023-03-09 10:16:00 +00:00
this.target = url;
2023-03-10 02:26:53 +00:00
this.dataType = 3;
2023-03-10 02:36:36 +00:00
return this;
2023-03-09 10:16:00 +00:00
};
// For example:
// file:C:\\a\\b\\c
// http://
// https://
// data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==
this.loadUri = function(uri) {
var pos = uri.indexOf(':');
var scheme = (pos < 0 ? '' : url.substring(0, pos));
var target = (pos < 0 ? uri : url.substring(pos + 1));
2020-07-18 20:02:49 +00:00
2023-03-09 10:16:00 +00:00
switch (scheme) {
case 'http':
case 'https':
2023-03-10 02:26:53 +00:00
this.loadUrl(target);
2023-03-09 10:16:00 +00:00
break;
2023-03-10 02:26:53 +00:00
2023-03-09 10:16:00 +00:00
case 'file':
2023-03-10 02:26:53 +00:00
this.loadFile(target);
break;
case 'data':
2023-03-09 10:16:00 +00:00
this.load(target);
break;
2023-03-10 02:26:53 +00:00
default:
console.error("Invalid scheme");
2023-03-09 10:16:00 +00:00
}
2023-03-10 02:36:36 +00:00
return this;
2023-03-09 10:16:00 +00:00
};
2023-03-10 02:43:35 +00:00
this.build = function(args) {
2023-03-10 02:26:53 +00:00
if (this.execType != "ps1") {
2023-03-10 02:28:02 +00:00
console.warn("The execType is not set 'ps1'. Will be forward it to the default shell.");
2023-03-10 02:43:35 +00:00
return SHELL.build(this.target);
2023-03-10 02:26:53 +00:00
}
var cmd = [
"powershell.exe",
"-NoProfile",
"-ExecutionPolicy",
"ByPass",
"-nologo"
2023-03-10 02:43:35 +00:00
]; // default arguments
2023-03-10 02:26:53 +00:00
switch (this.dataType) {
2023-03-10 02:36:36 +00:00
case 3: // dataType: URL(3)\
2023-03-10 02:43:35 +00:00
// todo
2023-03-09 10:16:00 +00:00
break;
2023-03-10 02:26:53 +00:00
case 2: // dataType: file(2)
cmd.push("-file");
cmd.push(scriptName + ".ps1");
2023-03-09 10:16:00 +00:00
break;
2023-03-10 02:26:53 +00:00
case 1: // dataType: command(1)
cmd.push("-Command");
if (typeof this.target === "string") {
2023-03-10 02:36:36 +00:00
cmd.push("& {" + this.target + "}");
2023-03-10 02:26:53 +00:00
} else {
2023-03-10 02:36:36 +00:00
cmd.push("& {" + SHELL.build(this.target) + "}");
2023-03-10 02:26:53 +00:00
}
break;
case 0: // dataType: script(0)
2023-03-09 10:16:00 +00:00
// todo
break;
2023-03-10 02:26:53 +00:00
2023-03-09 10:16:00 +00:00
default:
break;
}
2023-03-10 02:26:53 +00:00
if (typeof(cmd) !== "undefined") {
cmd = cmd.concat(args);
}
2023-03-10 02:43:35 +00:00
return cmd;
2023-03-09 10:16:00 +00:00
};
2023-03-10 02:43:35 +00:00
this.exec = function(args) {
return SHELL.exec(this.build(args));
};
this.runAs = function(args) {
return this.exec("Start-Process cmd \"/q /c " + SHELL.addslashes(this.build(args)) + "\" -Verb RunAs");
2023-03-10 02:26:53 +00:00
};
}
2023-03-09 10:16:00 +00:00
function execScript(scriptName, args) {
2023-03-10 02:36:36 +00:00
return (new PowershellObject()).loadFile(scriptName).exec(args);
2020-07-18 20:02:49 +00:00
};
2023-03-09 10:16:00 +00:00
function execCommand(cmd) {
2023-03-10 02:36:36 +00:00
return (new PowershellObject()).loadCommand(cmd).exec();
2020-07-18 21:06:41 +00:00
};
2020-08-04 09:29:42 +00:00
2023-03-09 10:16:00 +00:00
function runAs(cmd) {
2023-03-10 02:36:36 +00:00
return (new PowershellObject()).setExecType("cmd").runAs();
2020-08-04 09:29:42 +00:00
};
2023-03-09 10:16:00 +00:00
exports.execScript = execScript;
exports.execCommand = execCommand;
exports.runAs = runAs;
2023-03-10 02:28:02 +00:00
exports.VERSIONINFO = "Powershell Interface (powershell.js) version 0.1.2";
2023-03-09 10:16:00 +00:00
exports.AUTHOR = "abuse@catswords.net";
exports.global = global;
exports.require = global.require;