forked from bettercap/bettercap
/
wol.go
158 lines (127 loc) · 3.32 KB
/
wol.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
package wol
import (
"fmt"
"net"
"regexp"
"github.com/bettercap/bettercap/packets"
"github.com/bettercap/bettercap/session"
"github.com/google/gopacket/layers"
"github.com/evilsocket/islazy/str"
"github.com/evilsocket/islazy/tui"
)
var (
reMAC = regexp.MustCompile(`^([0-9a-fA-F]{2}[:-]){5}([0-9a-fA-F]{2})$`)
)
type WOL struct {
session.SessionModule
}
func NewWOL(s *session.Session) *WOL {
mod := &WOL{
SessionModule: session.NewSessionModule("wol", s),
}
mod.AddHandler(session.NewModuleHandler("wol.eth MAC", "wol.eth(\\s.+)?",
"Send a WOL as a raw ethernet packet of type 0x0847 (if no MAC is specified, ff:ff:ff:ff:ff:ff will be used).",
func(args []string) error {
if mac, err := parseMAC(args); err != nil {
return err
} else {
return mod.wolETH(mac)
}
}))
mod.AddHandler(session.NewModuleHandler("wol.udp MAC", "wol.udp(\\s.+)?",
"Send a WOL as an IPv4 broadcast packet to UDP port 9 (if no MAC is specified, ff:ff:ff:ff:ff:ff will be used).",
func(args []string) error {
if mac, err := parseMAC(args); err != nil {
return err
} else {
return mod.wolUDP(mac)
}
}))
return mod
}
func parseMAC(args []string) (string, error) {
mac := "ff:ff:ff:ff:ff:ff"
if len(args) == 1 {
tmp := str.Trim(args[0])
if tmp != "" {
if !reMAC.MatchString(tmp) {
return "", fmt.Errorf("%s is not a valid MAC address.", tmp)
} else {
mac = tmp
}
}
}
return mac, nil
}
func (mod *WOL) Name() string {
return "wol"
}
func (mod *WOL) Description() string {
return "A module to send Wake On LAN packets in broadcast or to a specific MAC."
}
func (mod *WOL) Author() string {
return "Simone Margaritelli <evilsocket@gmail.com>"
}
func (mod *WOL) Configure() error {
return nil
}
func (mod *WOL) Start() error {
return nil
}
func (mod *WOL) Stop() error {
return nil
}
func buildPayload(mac string) []byte {
raw, _ := net.ParseMAC(mac)
payload := []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
for i := 0; i < 16; i++ {
payload = append(payload, raw...)
}
return payload
}
func (mod *WOL) wolETH(mac string) error {
mod.SetRunning(true, nil)
defer mod.SetRunning(false, nil)
payload := buildPayload(mac)
mod.Info("sending %d bytes of ethernet WOL packet to %s", len(payload), tui.Bold(mac))
eth := layers.Ethernet{
SrcMAC: mod.Session.Interface.HW,
DstMAC: layers.EthernetBroadcast,
EthernetType: 0x0842,
}
err, raw := packets.Serialize(ð)
if err != nil {
return err
}
raw = append(raw, payload...)
return mod.Session.Queue.Send(raw)
}
func (mod *WOL) wolUDP(mac string) error {
mod.SetRunning(true, nil)
defer mod.SetRunning(false, nil)
payload := buildPayload(mac)
mod.Info("sending %d bytes of UDP WOL packet to %s", len(payload), tui.Bold(mac))
eth := layers.Ethernet{
SrcMAC: mod.Session.Interface.HW,
DstMAC: layers.EthernetBroadcast,
EthernetType: layers.EthernetTypeIPv4,
}
ip4 := layers.IPv4{
Protocol: layers.IPProtocolUDP,
Version: 4,
TTL: 64,
SrcIP: mod.Session.Interface.IP,
DstIP: net.ParseIP("255.255.255.255"),
}
udp := layers.UDP{
SrcPort: layers.UDPPort(32767),
DstPort: layers.UDPPort(9),
}
udp.SetNetworkLayerForChecksum(&ip4)
err, raw := packets.Serialize(ð, &ip4, &udp)
if err != nil {
return err
}
raw = append(raw, payload...)
return mod.Session.Queue.Send(raw)
}