Handle processes a shell module command.
(ctx ModuleContext, sessionID string, taskID uint32, spite *implantpb.Spite)
| 42 | |
| 43 | // Handle processes a shell module command. |
| 44 | func (m *ShellModule) Handle(ctx ModuleContext, sessionID string, taskID uint32, spite *implantpb.Spite) { |
| 45 | sess, toolName := acquireShellSession(ctx, sessionID, taskID, m.name) |
| 46 | if sess == nil { |
| 47 | return |
| 48 | } |
| 49 | |
| 50 | // Determine OS from User-Agent. |
| 51 | info := parseUserAgentFull(sess.UserAgent) |
| 52 | osName := info.osName |
| 53 | |
| 54 | // Extract request args. |
| 55 | var req *implantpb.Request |
| 56 | if r := spite.GetRequest(); r != nil { |
| 57 | req = r |
| 58 | } else { |
| 59 | req = &implantpb.Request{} |
| 60 | } |
| 61 | |
| 62 | // Build the OS-specific command. |
| 63 | command := m.buildCommand(osName, req) |
| 64 | if command == "" { |
| 65 | ctx.SendSpite(sessionID, taskID, execSpite(fmt.Sprintf("cannot build command for module %s on %s", m.name, osName))) |
| 66 | ctx.Tasks.Fail(sessionID, taskID, "empty command") |
| 67 | return |
| 68 | } |
| 69 | |
| 70 | result := enqueueAndAwait(ctx, sessionID, taskID, sess, toolName, command) |
| 71 | if result == nil { |
| 72 | return |
| 73 | } |
| 74 | |
| 75 | output := extractPlainOutput(result.Output) |
| 76 | |
| 77 | var resultSpite *implantpb.Spite |
| 78 | if m.buildResponse != nil { |
| 79 | resultSpite = m.buildResponse(output, osName, req) |
| 80 | } |
| 81 | if resultSpite == nil { |
| 82 | // Fallback to ExecResponse. |
| 83 | resp := parseToolOutput(result.Output) |
| 84 | resp.End = true |
| 85 | resultSpite = &implantpb.Spite{ |
| 86 | Name: consts.ModuleExecute, |
| 87 | Body: &implantpb.Spite_ExecResponse{ExecResponse: resp}, |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if err := ctx.SendSpite(sessionID, taskID, resultSpite); err != nil { |
| 92 | log.Errorf("[bridge] failed to forward module %s task %d: %v", m.name, taskID, err) |
| 93 | ctx.Tasks.Fail(sessionID, taskID, err.Error()) |
| 94 | } else { |
| 95 | log.Infof("[bridge] forwarded module %s task %d result for session %s", m.name, taskID, sessionID) |
| 96 | ctx.Tasks.Complete(sessionID, taskID) |
| 97 | } |
| 98 | } |
nothing calls this directly
no test coverage detected