Skip to content

Instantly share code, notes, and snippets.

@Backtalk2022
Created March 10, 2023 07:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Backtalk2022/31b147c7d209d1e3519b4f22e100ca08 to your computer and use it in GitHub Desktop.
Save Backtalk2022/31b147c7d209d1e3519b4f22e100ca08 to your computer and use it in GitHub Desktop.
golang wol use syscall
package main
import (
"flag"
"log"
"net"
"syscall"
)
var ifName = flag.String("i", "eth0", "interface to use")
var targetMac = flag.String("t", "00:00:00:00:00:00", "target mac address")
func main() {
flag.Parse()
// Open a raw socket
fd, err := syscall.Socket(syscall.AF_PACKET, syscall.SOCK_RAW, syscall.ETH_P_ALL)
if err != nil {
log.Fatal(err)
}
defer syscall.Close(fd)
// Get the MAC address of the network interface
ifi, err := net.InterfaceByName(*ifName)
if err != nil {
log.Fatal(err)
}
srcAddr := syscall.SockaddrLinklayer{
Protocol: syscall.ETH_P_ALL,
Ifindex: ifi.Index,
Halen: 6,
}
copy(srcAddr.Addr[:], ifi.HardwareAddr)
// Create the Ethernet frame
dstAddr, _ := net.ParseMAC(*targetMac)
ethFrame := make([]byte, 14)
copy(ethFrame[0:6], dstAddr)
copy(ethFrame[6:12], ifi.HardwareAddr)
ethFrame[12] = 0x08
ethFrame[13] = 0x42 // Ethernet type WOL
// create wol payload
payload := genPayload(dstAddr)
// Send the Ethernet frame
err = syscall.Sendto(fd, append(ethFrame, payload...), 0, &srcAddr)
if err != nil {
log.Fatal(err)
}
}
func genPayload(targetMAC net.HardwareAddr) []byte {
// Create the magic packet payload
payload := make([]byte, 6+16*6)
copy(payload[0:6], []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff})
for i := 1; i <= 16; i++ {
start := i * 6
end := start + 6
copy(payload[start:end], targetMAC)
}
return payload
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment