|
1 | | -package interpreter |
2 | | - |
3 | | -import ( |
4 | | - "bytes" |
5 | | - "context" |
6 | | - "encoding/base64" |
7 | | - "fmt" |
8 | | - "os" |
9 | | - "os/exec" |
10 | | - "strings" |
11 | | - |
12 | | - "github.com/RewstApp/agent-smith-go/internal/agent" |
13 | | - "github.com/RewstApp/agent-smith-go/internal/utils" |
14 | | - "github.com/RewstApp/agent-smith-go/internal/version" |
15 | | - "github.com/hashicorp/go-hclog" |
16 | | - "golang.org/x/text/encoding/unicode" |
17 | | - "golang.org/x/text/transform" |
18 | | -) |
19 | | - |
20 | | -const powershellVersionCheckCommand = "\"$($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor)\"" |
21 | | - |
22 | | -var utf8BOM = []byte{0xEF, 0xBB, 0xBF} |
23 | | - |
24 | | -func executeUsingPowershell(ctx context.Context, message *Message, device agent.Device, logger hclog.Logger, usePwsh bool) []byte { |
25 | | - // Parse the commands |
26 | | - commandBytes, err := base64.StdEncoding.DecodeString(message.Commands) |
27 | | - if err != nil { |
28 | | - return errorResultBytes(err) |
29 | | - } |
30 | | - |
31 | | - // Decode using UTF16LE |
32 | | - decoder := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewDecoder() |
33 | | - commands, _, err := transform.String(decoder, string(commandBytes)) |
34 | | - if err != nil { |
35 | | - return errorResultBytes(err) |
36 | | - } |
37 | | - |
38 | | - // Run the command in the system using powershell |
39 | | - shell := "powershell" |
40 | | - if usePwsh { |
41 | | - shell = "pwsh" |
42 | | - } |
43 | | - |
44 | | - if logger.IsDebug() { |
45 | | - cmd := exec.CommandContext(ctx, shell, "-Command", powershellVersionCheckCommand) |
46 | | - combinedOutputBytes, err := cmd.CombinedOutput() |
47 | | - combinedOutput := string(combinedOutputBytes) |
48 | | - if err != nil { |
49 | | - logger.Error("Shell version check failed", "error", err, "combined_output", combinedOutput) |
50 | | - } |
51 | | - |
52 | | - version := strings.TrimSpace(combinedOutput) |
53 | | - |
54 | | - logger.Debug("Shell version", "shell", shell, "version", version) |
55 | | - logger.Debug("Commands to execute", "commands", commands) |
56 | | - } |
57 | | - |
58 | | - if logger.IsDebug() { |
59 | | - cmd := exec.CommandContext(ctx, "whoami") |
60 | | - combinedOutputBytes, err := cmd.CombinedOutput() |
61 | | - combinedOutput := string(combinedOutputBytes) |
62 | | - if err != nil { |
63 | | - logger.Error("Whoami check failed", "error", err, "combined_output", combinedOutput) |
64 | | - } |
65 | | - |
66 | | - logger.Debug("Whomai", "user", combinedOutput) |
67 | | - } |
68 | | - |
69 | | - // Save commands to temporary file |
70 | | - scriptsDir := agent.GetScriptsDirectory(device.RewstOrgId) |
71 | | - err = utils.CreateFolderIfMissing(scriptsDir) |
72 | | - if err != nil { |
73 | | - return errorResultBytes(err) |
74 | | - } |
75 | | - |
76 | | - tempfile, err := os.CreateTemp(scriptsDir, "exec-*.ps1") |
77 | | - if err != nil { |
78 | | - return errorResultBytes(err) |
79 | | - } |
80 | | - |
81 | | - _, err = tempfile.Write(utf8BOM) |
82 | | - if err != nil { |
83 | | - logger.Error("Failed to write BOM", "error", err) |
84 | | - return errorResultBytes(err) |
85 | | - } |
86 | | - |
87 | | - _, err = tempfile.WriteString(commands) |
88 | | - if err != nil { |
89 | | - logger.Error("Failed to write command file", "error", err) |
90 | | - return errorResultBytes(err) |
91 | | - } |
92 | | - |
93 | | - logger.Info("Command saved to", "message_id", message.PostId, "path", tempfile.Name()) |
94 | | - |
95 | | - // Close the temporary file |
96 | | - tempfile.Close() |
97 | | - |
98 | | - var stdoutBuf, stderrBuf bytes.Buffer |
99 | | - cmd := exec.CommandContext(ctx, shell, "-File", tempfile.Name()) |
100 | | - cmd.Stdout = &stdoutBuf |
101 | | - cmd.Stderr = &stderrBuf |
102 | | - cmd.Env = os.Environ() |
103 | | - cmd.Env = append(cmd.Env, fmt.Sprintf("AGENT_SMITH_VERSION=%s", version.Version[1:])) |
104 | | - |
105 | | - err = cmd.Run() |
106 | | - if err != nil { |
107 | | - logger.Error("Command failed", "error", err) |
108 | | - logger.Debug("Command completed with outputs", "error", stderrBuf.String(), "info", stdoutBuf.String()) |
109 | | - return resultBytes(&result{Error: stderrBuf.String(), Output: stdoutBuf.String()}) |
110 | | - } |
111 | | - |
112 | | - // Remove successfully executed temporary filename |
113 | | - defer os.Remove(tempfile.Name()) |
114 | | - |
115 | | - logger.Info("Command completed", "message_id", message.PostId, "exit_code", cmd.ProcessState.ExitCode()) |
116 | | - logger.Debug("Command completed with outputs", "error", stderrBuf.String(), "info", stdoutBuf.String()) |
117 | | - |
118 | | - return resultBytes(&result{Error: stderrBuf.String(), Output: stdoutBuf.String()}) |
119 | | -} |
| 1 | +package interpreter |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/base64" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/RewstApp/agent-smith-go/internal/agent" |
| 13 | + "github.com/RewstApp/agent-smith-go/internal/utils" |
| 14 | + "github.com/RewstApp/agent-smith-go/internal/version" |
| 15 | + "github.com/hashicorp/go-hclog" |
| 16 | + "golang.org/x/text/encoding/unicode" |
| 17 | + "golang.org/x/text/transform" |
| 18 | +) |
| 19 | + |
| 20 | +var utf8BOM = []byte{0xEF, 0xBB, 0xBF} |
| 21 | + |
| 22 | +type baseExecutor struct { |
| 23 | + Shell string |
| 24 | + ShellVersionCheckCommand string |
| 25 | + WriteUtf8BOM bool |
| 26 | + BuildExecuteCommandArgs BuildExecuteCommandArgsFunc |
| 27 | + BuildExecuteFileArgs BuildExecuteFileArgsFunc |
| 28 | +} |
| 29 | + |
| 30 | +func (e *baseExecutor) Execute(ctx context.Context, message *Message, device agent.Device, logger hclog.Logger, sys agent.SystemInfoProvider, domain agent.DomainInfoProvider) []byte { |
| 31 | + // Parse the commands |
| 32 | + commandBytes, err := base64.StdEncoding.DecodeString(message.Commands) |
| 33 | + if err != nil { |
| 34 | + return errorResultBytes(err) |
| 35 | + } |
| 36 | + |
| 37 | + // Decode using UTF16LE |
| 38 | + decoder := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewDecoder() |
| 39 | + commands, _, err := transform.String(decoder, string(commandBytes)) |
| 40 | + if err != nil { |
| 41 | + return errorResultBytes(err) |
| 42 | + } |
| 43 | + |
| 44 | + // Run the command in the system using powershell |
| 45 | + if logger.IsDebug() { |
| 46 | + cmd := exec.CommandContext(ctx, e.Shell, e.BuildExecuteCommandArgs(e.ShellVersionCheckCommand)...) |
| 47 | + combinedOutputBytes, err := cmd.CombinedOutput() |
| 48 | + combinedOutput := string(combinedOutputBytes) |
| 49 | + if err != nil { |
| 50 | + logger.Error("Shell version check failed", "error", err, "combined_output", combinedOutput) |
| 51 | + } |
| 52 | + |
| 53 | + version := strings.TrimSpace(combinedOutput) |
| 54 | + |
| 55 | + logger.Debug("Shell version", "shell", e.Shell, "version", version) |
| 56 | + logger.Debug("Commands to execute", "commands", commands) |
| 57 | + } |
| 58 | + |
| 59 | + if logger.IsDebug() { |
| 60 | + cmd := exec.CommandContext(ctx, e.Shell, e.BuildExecuteCommandArgs("whoami")...) |
| 61 | + combinedOutputBytes, err := cmd.CombinedOutput() |
| 62 | + combinedOutput := string(combinedOutputBytes) |
| 63 | + if err != nil { |
| 64 | + logger.Error("Whoami check failed", "error", err, "combined_output", combinedOutput) |
| 65 | + } |
| 66 | + |
| 67 | + logger.Debug("Whomai", "user", combinedOutput) |
| 68 | + } |
| 69 | + |
| 70 | + // Save commands to temporary file |
| 71 | + scriptsDir := agent.GetScriptsDirectory(device.RewstOrgId) |
| 72 | + err = utils.CreateFolderIfMissing(scriptsDir) |
| 73 | + if err != nil { |
| 74 | + return errorResultBytes(err) |
| 75 | + } |
| 76 | + |
| 77 | + tempfile, err := os.CreateTemp(scriptsDir, "exec-*.ps1") |
| 78 | + if err != nil { |
| 79 | + return errorResultBytes(err) |
| 80 | + } |
| 81 | + |
| 82 | + if e.WriteUtf8BOM { |
| 83 | + _, err = tempfile.Write(utf8BOM) |
| 84 | + if err != nil { |
| 85 | + logger.Error("Failed to write BOM", "error", err) |
| 86 | + return errorResultBytes(err) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + _, err = tempfile.WriteString(commands) |
| 91 | + if err != nil { |
| 92 | + logger.Error("Failed to write command file", "error", err) |
| 93 | + return errorResultBytes(err) |
| 94 | + } |
| 95 | + |
| 96 | + logger.Info("Command saved to", "message_id", message.PostId, "path", tempfile.Name()) |
| 97 | + |
| 98 | + // Close the temporary file |
| 99 | + tempfile.Close() |
| 100 | + |
| 101 | + var stdoutBuf, stderrBuf bytes.Buffer |
| 102 | + cmd := exec.CommandContext(ctx, e.Shell, e.BuildExecuteFileArgs(tempfile.Name())...) |
| 103 | + cmd.Stdout = &stdoutBuf |
| 104 | + cmd.Stderr = &stderrBuf |
| 105 | + cmd.Env = os.Environ() |
| 106 | + cmd.Env = append(cmd.Env, fmt.Sprintf("AGENT_SMITH_VERSION=%s", version.Version[1:])) |
| 107 | + |
| 108 | + err = cmd.Run() |
| 109 | + if err != nil { |
| 110 | + logger.Error("Command failed", "error", err) |
| 111 | + logger.Debug("Command completed with outputs", "error", stderrBuf.String(), "info", stdoutBuf.String()) |
| 112 | + return resultBytes(&result{Error: stderrBuf.String(), Output: stdoutBuf.String()}) |
| 113 | + } |
| 114 | + |
| 115 | + // Remove successfully executed temporary filename |
| 116 | + defer os.Remove(tempfile.Name()) |
| 117 | + |
| 118 | + logger.Info("Command completed", "message_id", message.PostId, "exit_code", cmd.ProcessState.ExitCode()) |
| 119 | + logger.Debug("Command completed with outputs", "error", stderrBuf.String(), "info", stdoutBuf.String()) |
| 120 | + |
| 121 | + return resultBytes(&result{Error: stderrBuf.String(), Output: stdoutBuf.String()}) |
| 122 | +} |
| 123 | + |
| 124 | +func NewBaseExecutor(shell string, shellVersionCheckCommand string, writeUtf8BOM bool, buildExecuteCommandArgs BuildExecuteCommandArgsFunc, buildExecuteFileArgs BuildExecuteFileArgsFunc) Executor { |
| 125 | + return &baseExecutor{ |
| 126 | + Shell: shell, |
| 127 | + ShellVersionCheckCommand: shellVersionCheckCommand, |
| 128 | + WriteUtf8BOM: writeUtf8BOM, |
| 129 | + BuildExecuteCommandArgs: buildExecuteCommandArgs, |
| 130 | + BuildExecuteFileArgs: buildExecuteFileArgs, |
| 131 | + } |
| 132 | +} |
0 commit comments