Debian Codegraph @

modules/wol/wol.go

raw symbols SCIP
1 package wol
2
3 import (
4 "fmt"
5 "net"
6 "regexp"
7
8 "github.com/bettercap/bettercap/packets"
9 "github.com/bettercap/bettercap/session"
10
11 "github.com/google/gopacket/layers"
12
13 "github.com/evilsocket/islazy/str"
14 "github.com/evilsocket/islazy/tui"
15 )
16
17 var (
18 reMAC = regexp.MustCompile(`^([0-9a-fA-F]{2}[:-]){5}([0-9a-fA-F]{2})$`)
19 )
20
21 type WOL struct {
22 session.SessionModule
23 }
24
25 func NewWOL(s *session.Session) *WOL {
26 mod := &WOL{
27 SessionModule: session.NewSessionModule("wol", s),
28 }
29
30 mod.AddHandler(session.NewModuleHandler("wol.eth MAC", "wol.eth(\\s.+)?",
31 "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).",
32 func(args []string) error {
33 if mac, err := parseMAC(args); err != nil {
34 return err
35 } else {
36 return mod.wolETH(mac)
37 }
38 }))
39
40 mod.AddHandler(session.NewModuleHandler("wol.udp MAC", "wol.udp(\\s.+)?",
41 "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).",
42 func(args []string) error {
43 if mac, err := parseMAC(args); err != nil {
44 return err
45 } else {
46 return mod.wolUDP(mac)
47 }
48 }))
49
50 return mod
51 }
52
53 func parseMAC(args []string) (string, error) {
54 mac := "ff:ff:ff:ff:ff:ff"
55 if len(args) == 1 {
56 tmp := str.Trim(args[0])
57 if tmp != "" {
58 if !reMAC.MatchString(tmp) {
59 return "", fmt.Errorf("%s is not a valid MAC address.", tmp)
60 } else {
61 mac = tmp
62 }
63 }
64 }
65
66 return mac, nil
67 }
68
69 func (mod *WOL) Name() string {
70 return "wol"
71 }
72
73 func (mod *WOL) Description() string {
74 return "A module to send Wake On LAN packets in broadcast or to a specific MAC."
75 }
76
77 func (mod *WOL) Author() string {
78 return "Simone Margaritelli <[email protected]>"
79 }
80
81 func (mod *WOL) Configure() error {
82 return nil
83 }
84
85 func (mod *WOL) Start() error {
86 return nil
87 }
88
89 func (mod *WOL) Stop() error {
90 return nil
91 }
92
93 func buildPayload(mac string) []byte {
94 raw, _ := net.ParseMAC(mac)
95 payload := []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
96 for i := 0; i < 16; i++ {
97 payload = append(payload, raw...)
98 }
99 return payload
100 }
101
102 func (mod *WOL) wolETH(mac string) error {
103 mod.SetRunning(true, nil)
104 defer mod.SetRunning(false, nil)
105
106 payload := buildPayload(mac)
107 mod.Info("sending %d bytes of ethernet WOL packet to %s", len(payload), tui.Bold(mac))
108 eth := layers.Ethernet{
109 SrcMAC: mod.Session.Interface.HW,
110 DstMAC: layers.EthernetBroadcast,
111 EthernetType: 0x0842,
112 }
113
114 err, raw := packets.Serialize(&eth)
115 if err != nil {
116 return err
117 }
118
119 raw = append(raw, payload...)
120 return mod.Session.Queue.Send(raw)
121 }
122
123 func (mod *WOL) wolUDP(mac string) error {
124 mod.SetRunning(true, nil)
125 defer mod.SetRunning(false, nil)
126
127 payload := buildPayload(mac)
128 mod.Info("sending %d bytes of UDP WOL packet to %s", len(payload), tui.Bold(mac))
129
130 eth := layers.Ethernet{
131 SrcMAC: mod.Session.Interface.HW,
132 DstMAC: layers.EthernetBroadcast,
133 EthernetType: layers.EthernetTypeIPv4,
134 }
135
136 ip4 := layers.IPv4{
137 Protocol: layers.IPProtocolUDP,
138 Version: 4,
139 TTL: 64,
140 SrcIP: mod.Session.Interface.IP,
141 DstIP: net.ParseIP("255.255.255.255"),
142 }
143
144 udp := layers.UDP{
145 SrcPort: layers.UDPPort(32767),
146 DstPort: layers.UDPPort(9),
147 }
148
149 udp.SetNetworkLayerForChecksum(&ip4)
150
151 err, raw := packets.Serialize(&eth, &ip4, &udp)
152 if err != nil {
153 return err
154 }
155
156 raw = append(raw, payload...)
157 return mod.Session.Queue.Send(raw)
158 }