Issue WOL with Scapy of Python3 (LAN controller specified type)

I want to issue WOL by directly specifying the controller

The WOL specifications are as follows (WireShark Wiki).

https://wiki.wireshark.org/WakeOnLAN

In short, in the packet

It usually happens. It doesn't matter what the port is, you can see it by matching the pattern, so I think it's possible to detect it with hardware and interrupt it.

So, if you want to send this, I think it's easy to make a UDP packet. There seems to be such a tool, but as far as I did a quick search, there were many that let me specify by IP address. If this is the case, it will be difficult to issue the IP address appropriately. So I implemented a method to directly specify the controller and send it.

Scapy

Use Scapy to generate and send WOL packets. Scapy itself has been written briefly before. Less than

https://qiita.com/ken_hamada/items/736e1c22f6c40702d1a7

environment

I also use Scapy. I think it will probably work on Linux as well.

The site that I used as a reference

It's like referring to the code below, or cutting and pasting it based on these. Thank you very much!

code

It's quicker to look at the code to see how it's actually done, below.


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from scapy.all import *
import binascii

# ---->Please set here appropriately.

#Control target. Please specify the real thing for this.
TARGET_MAC_ADDRESS = "11:22:33:44:55:66"

#The IP address to put on the packet. This can be anything.
IP_ADDRESS = "66.77.88.99"

#A controller that creates packets. Please specify the real thing as well.
PCAP_ifname = "en0"

# <-----So far

def issue_WOL():

    '''
First, create the payload. As you can see from the code,
Password below(optional)I'm just making something that omits.

    https://wiki.wireshark.org/WakeOnLAN
    
    '''
    synchronization_stream = b"\xff" * 16
    mac_bin = binascii.unhexlify("".join(TARGET_MAC_ADDRESS.split(":")))
    payload = synchronization_stream + mac_bin * 16

    '''
Generate a WOL packet that sends the above payload as UDP.
    '''
    ether_layer = Ether(dst=TARGET_MAC_ADDRESS)
    ip_layer = IP(dst=IP_ADDRESS)
    udp_layer = UDP(sport=44444,dport=33333) #suitable
    raw_layer = Raw(load=payload) #The payload can be specified in bytes.
    wol_packet = ether_layer / ip_layer / udp_layer / raw_layer

    '''
You can use Layer2 because you only have to send the raw package.
If you specify the controller at this time, it should come out from there ...
    '''
    sendp(wol_packet, iface=PCAP_ifname)
    
    print ("sent WOL ", TARGET_MAC_ADDRESS, IP_ADDRESS)


if __name__ == "__main__":
    issue_WOL()


To use it, rewrite TARGET_MAC_ADDRESS, IP_ADDRESS, PCAP_ifname according to your environment and target, and execute this code, and a WOL packet should be output (sudo is required).

IP_ADDRESS does not have to think about the current target address. In fact, even if you specify a messy address, you will receive the following (check with the opposite WireShark). This is because the controller is directly connected because it can be accessed from layer 2.

wol.PNG

that's all. Well, with scapy it wasn't too difficult (While writing, Raw (although I was quite addicted to the data model specified by load = ...)

Recommended Posts

Issue WOL with Scapy of Python3 (LAN controller specified type)
[Python] Determine the type of iris with SVM
Network programming with Python Scapy
Tips: [Python] Calculate the average value of the specified area with bedgraph
Exclude specified cases with Python unittest
Getting Started with Python Basics of Python
Type specified in python. Throw exceptions
Life game with Python! (Conway's Game of Life)
10 functions of "language with battery" python
Master the type with Python [Python 3.9 compatible]
Implementation of Dijkstra's algorithm with python
Coexistence of Python2 and 3 with CircleCI (1.0)
Basic study of OpenCV with Python
[Examples of improving Python] Learning Python with Codecademy
[Python] Easy argument type check with dataclass
Execute Python script with cron of TS-220
Check the existence of the file with python
Algorithm learned with Python 8th: Evaluation of algorithm
Clogged with python update of GCP console ①
Easy introduction of speech recognition with Python
Receive date type (datetime) with ArgumentParser [python]
[python] [meta] Is the type of python a type?
UnicodeEncodeError struggle with standard output of python3
1. Statistics learned with Python 1-3. Calculation of various statistics (statistics)
Drawing with Matrix-Reinventor of Python Image Processing-
Recommendation of Altair! Data visualization with Python
Comparison of matrix transpose speeds with Python
Compare the sum of each element in two lists with the specified value in Python