Skip to content

Commit

Permalink
cross platform service foo finished
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Levinson committed Feb 27, 2018
1 parent e758c4d commit ba76553
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 4 deletions.
2 changes: 1 addition & 1 deletion compiler/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions engine/builtin_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func TestVMAppendFile(t *testing.T) {
e.CreateVM()

e.VM.Run(testScript)
e.Logger.Infof("Function: function=%s msg='Appended local file at: %s'", CalledBy(), spew.Sdump(g_file_1))
e.Logger.Infof("Function: function=%s msg='Appended local file at: %s'", CalledBy(), spew.Sdump(g_file_2))
e.Logger.WithField("trace", "true").Infof("Function: function=%s msg='Appended local file at: %s'", CalledBy(), spew.Sdump(g_file_1))
e.Logger.WithField("trace", "true").Infof("Function: function=%s msg='Appended local file at: %s'", CalledBy(), spew.Sdump(g_file_2))
retVal, err := e.VM.Get("return_value1")
assert.Nil(t, err)
retValAsString, err := retVal.ToString()
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestVMRetrieveFileFromURL(t *testing.T) {
e.CreateVM()

e.VM.Run(testScript2)
e.Logger.Infof("Function: function=%s msg='wrote local file at: %s'", CalledBy(), spew.Sdump(file_3))
e.Logger.WithField("trace", "true").Infof("Function: function=%s msg='wrote local file at: %s'", CalledBy(), spew.Sdump(file_3))
retVal, err := e.VM.Get("return_value2")
assert.Nil(t, err)
retValAsString, err := retVal.ToString()
Expand Down
4 changes: 4 additions & 0 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ func (e *Engine) CreateVM() {
e.VM.Set("DelRegKey", e.VMDelRegKey)
e.VM.Set("GetHost", e.VMGetHostname)
e.VM.Set("LogTester", e.VMLogTester)
e.VM.Set("InstallSystemService", e.VMInstallSystemService)
e.VM.Set("StartServiceByName", e.VMStartServiceByName)
e.VM.Set("StopServiceByName", e.VMStopServiceByName)
e.VM.Set("RemoveServiceByName", e.VMRemoveServiceByName)
_, err := e.VM.Run(VMPreload)
if err != nil {
e.Logger.WithField("trace", "true").Fatalf("Syntax error in preload: %s", err.Error())
Expand Down
183 changes: 183 additions & 0 deletions engine/system_services.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package engine

import (
services "github.com/gen0cide/service-go"
"github.com/robertkrimen/otto"
)

func (e *Engine) InstallSystemService(path, name, displayName, description string) error {
c := &services.Config{
Path: path,
Name: name,
DisplayName: displayName,
Description: description,
}

s, err := services.NewServiceConfig(c)
if err != nil {
return err
}

err = s.Install()
if err != nil {
return err
}

return nil
}

func (e *Engine) StartServiceByName(name string) error {
c := &services.Config{
Name: name,
}

s, err := services.NewServiceConfig(c)
if err != nil {
return err
}

err = s.Start()
if err != nil {
return err
}

return nil
}

func (e *Engine) StopServiceByName(name string) error {
c := &services.Config{
Name: name,
}

s, err := services.NewServiceConfig(c)
if err != nil {
return err
}

err = s.Stop()
if err != nil {
return err
}

return nil
}

func (e *Engine) RemoveServiceByName(name string) error {
c := &services.Config{
Name: name,
}

s, err := services.NewServiceConfig(c)
if err != nil {
return err
}

err = s.Remove()
if err != nil {
return err
}

return nil
}

func (e *Engine) VMInstallSystemService(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) != 4 {
e.Logger.Errorf("Not enough arguments provided.")
return otto.FalseValue()
}

path, err := call.Argument(0).ToString()
if err != nil {
e.Logger.Errorf("Error converting path to string: %s", err.Error())
return otto.FalseValue()
}

name, err := call.Argument(1).ToString()
if err != nil {
e.Logger.Errorf("Error converting name to string: %s", err.Error())
return otto.FalseValue()
}

displayName, err := call.Argument(2).ToString()
if err != nil {
e.Logger.Errorf("Error converting displayName to string: %s", err.Error())
return otto.FalseValue()
}

description, err := call.Argument(3).ToString()
if err != nil {
e.Logger.Errorf("Error converting description to string: %s", err.Error())
return otto.FalseValue()
}

err = e.InstallSystemService(path, name, displayName, description)
if err != nil {
e.Logger.Errorf("Error installing system service: %s", err.Error())
return otto.FalseValue()
}

return otto.TrueValue()
}

func (e *Engine) VMStartServiceByName(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) != 1 {
e.Logger.Errorf("Not enough arguments provided.")
return otto.FalseValue()
}

name, err := call.Argument(0).ToString()
if err != nil {
e.Logger.Errorf("Error converting name to string: %s", err.Error())
return otto.FalseValue()
}

err = e.StartServiceByName(name)
if err != nil {
e.Logger.Errorf("Error starting system service: %s", err.Error())
return otto.FalseValue()
}

return otto.TrueValue()
}

func (e *Engine) VMStopServiceByName(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) != 1 {
e.Logger.Errorf("Not enough arguments provided.")
return otto.FalseValue()
}

name, err := call.Argument(0).ToString()
if err != nil {
e.Logger.Errorf("Error converting name to string: %s", err.Error())
return otto.FalseValue()
}

err = e.StopServiceByName(name)
if err != nil {
e.Logger.Errorf("Error starting system service: %s", err.Error())
return otto.FalseValue()
}

return otto.TrueValue()
}

func (e *Engine) VMRemoveServiceByName(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) != 1 {
e.Logger.Errorf("Not enough arguments provided.")
return otto.FalseValue()
}

name, err := call.Argument(0).ToString()
if err != nil {
e.Logger.Errorf("Error converting name to string: %s", err.Error())
return otto.FalseValue()
}

err = e.RemoveServiceByName(name)
if err != nil {
e.Logger.Errorf("Error starting system service: %s", err.Error())
return otto.FalseValue()
}

return otto.TrueValue()
}
69 changes: 69 additions & 0 deletions testscripts/darwin_service_test.gs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// -----------------------------------------------------
// GENESIS Script Engine
// https://github.com/gen0cide/gscript
//
// Example Script
//
// *Note: .gs files are just javascript! Enjoy!
// -----------------------------------------------------

// -----------------------------------------------------
// COMPILER MACROS (Remove comment and modify to use)
// //priority:100 (lower will execute first)
// //timeout:30 (time in seconds)
// //import:/path/to/local/file.exe
// //import_url:https://website.com/with/local/file.exe
// In both of those examples, you can retrieve the asset
// by using Asset("file.exe") (returns byte array).

// -----------------------------------------------------
// GLOBALS
var test_var = "hello, world";
var test_num = 5;

// -----------------------------------------------------
// HOOKS
// Your final script must implement these methods.
// If any method returns false, the VM will cease
// execution and not continue to the subsequent
// functions.

// -----------------------------------------------------
// BeforeDeploy() is meant to allow you the opportunity
// to investigate the target system to determine if you
// even want to proceed to the Deploy() step. You might:
// - Check to see if you have the right architecture.
// - Check to see if apache is installed.
// - Check to see if your payload has already been dropped.
function BeforeDeploy() {
LogInfo("This is an example of a debug command.");
LogWarn("This will only show when you use the \"run\" subcommand.");
LogError("It will *not* print when run from a compiled binary.");
return true;
}

// -----------------------------------------------------
// Deploy() is where you actually deploy your payload.
// Remember to return true if it deploys successfully.
function Deploy() {
InstallSystemService("/usr/local/bin/testsvc", "gscriptdaemon", "Gscript Daemon", "this is a test daemon.")
LogInfo("service installed");
StartServiceByName("gscriptdaemon");
LogInfo("service started");
return true;
}

// -----------------------------------------------------
// AfterDeploy() allows you to clean up or validate deployment.
function AfterDeploy() {
Sleep(10);
StopServiceByName("gscriptdaemon");
LogInfo("service stopped");
Sleep(5)
RemoveServiceByName("gscriptdaemon");
LogInfo("service removed");
return true;
}



0 comments on commit ba76553

Please sign in to comment.