-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
155 lines (131 loc) · 3.45 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
//+build linux darwin
//Copyright (C) 2019 David Kröll
//
//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
package main
import (
"flag"
"fmt"
"github.com/mdlayher/ethernet"
"github.com/mdlayher/raw"
"net"
"os/user"
"sync"
"time"
)
var num = flag.Int("n", 1, "Amount of frames send")
var ifaceName = flag.String("i", "", "Interface to send")
var numThreads = flag.Int("t", 12, "Number of threads to use")
var seed = flag.Int("s", 0, "Seed for source MAC address")
var versionFlag = flag.Bool("v", false, "Print version")
const etherType = 0xbeef
const version = "flood v0.2.0"
func main() {
flag.Parse()
if !prerequisitesSatisfied() {
return
}
iface, err := net.InterfaceByName(*ifaceName)
if err != nil {
fmt.Println("No such network interface")
return
}
conn, err := raw.ListenPacket(iface, etherType, nil)
if err != nil {
fmt.Printf("cannot open connection: %v", err)
return
}
var wg sync.WaitGroup
// init channels
ch := make(chan *ethernet.Frame)
stats := make(chan int)
// create sender goroutines
for i := 0; i < *numThreads; i++ {
wg.Add(1)
// pass in Done() method from waitgroup
go frameWriter(conn, ch, stats, wg.Done)
}
// init stat vars
framesSend := 0
bytesWritten := 0
startTime := time.Now()
// stat collecting goroutine
go func() {
// no need for waitgroup here,
// goroutine gets automatically killed when any sender goroutine exits
for bytes := range stats {
bytesWritten += bytes
framesSend++
}
}()
for i := 1; i <= *num; i++ {
f := ðernet.Frame{
Destination: ethernet.Broadcast,
Source: net.HardwareAddr{
byte(*seed),
0x00,
// hacky method for power to 2 numbers
byte(i / (24 << 1)), uint8(i / (16 << 1)), uint8(i / (8 << 1)), uint8(i),
},
EtherType: etherType,
}
ch <- f
}
// close channel
close(ch)
wg.Wait() // wait for goroutines quit
fmt.Println("Execution summary:")
fmt.Printf("%d frames send\n", framesSend)
fmt.Printf("%d bytes written\n", bytesWritten)
fmt.Printf("Took a total time of %v\n", time.Since(startTime))
}
func frameWriter(c net.PacketConn, ch <-chan *ethernet.Frame, stats chan<- int, doneCall func()) {
for f := range ch {
// get frame and marshall it to binary
b, err := f.MarshalBinary()
if err != nil {
fmt.Printf("failed to marshal ethernet frame: %v", err)
}
// only necessary for WriteTo() method, does not change the frame
addr := &raw.Addr{
HardwareAddr: ethernet.Broadcast,
}
// write frame
n, err := c.WriteTo(b, addr)
if err != nil {
fmt.Printf("Cannot write to connection: %v", err)
}
// send to channel
stats <- n
}
doneCall()
}
func prerequisitesSatisfied() bool {
if *versionFlag {
fmt.Println(version)
return false
}
u, _ := user.Current()
if u.Uid != "0" {
fmt.Println("This program requires root (UID 0) access")
return false
}
// require iface index flag
if *ifaceName == "" {
flag.Usage()
return false
}
if *seed < 0 || *seed > 255 {
fmt.Printf("Seed (%d) must be between 0 and 255\n", *seed)
return false
}
return true
}