Monday, April 2, 2018

Linux: Creating Startup Service Using SYSTEMD UNIT

Sometimes we might have a requirement to create a custom script or service that needs to be run at startup. In this post we will be testing the creation of a custom service that changes the MAC Address of the wireless and ethernet Network Interface Card on boot. This process is achieved by creating a Systemd Unit file. This test was performed on Fedora Linux version 31.

Creating the Systemd Unit File

To create a Systemd Unit File we will create the following file.

 vim /etc/systemd/system/macchange@.service  

The contents of the above file are as follows.

 [Unit]  
 Description=macchanger on %I  
 Wants=network-pre.target  
 Before=network-pre.target  
 After=sys-subsystem-net-devices-%i.device  

 [Service]  
 ExecStart=/usr/bin/macchanger -r %I  
 Type=oneshot  

 [Install]  
 WantedBy=multi-user.target  

Starting Services

We will now enable the services for both the wireless and as well as ethernet NIC. Enabling the services will start the services on boot. 

 # systemctl enable macchange@wlp2s0.service  

 # systemctl enable macchange@enp1s0.service  

We will test the service by rebooting the device.

Testing the Setup

Before rebooting, we check the MAC Address of the interfaces enp1s0 (c8:d3:ff:fd:f2:a4) and wlp2s0 (94:53:30:78:5f:d1).

 $ ifconfig  
 enp1s0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500  
     ether c8:d3:ff:fd:f2:a4 txqueuelen 1000 (Ethernet)  
     RX packets 0 bytes 0 (0.0 B)  
     RX errors 0 dropped 0 overruns 0 frame 0  
     TX packets 0 bytes 0 (0.0 B)  
     TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0  

 wlp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500  
     inet 192.168.20.4 netmask 255.255.255.0 broadcast 192.168.200.255  
     inet6 fe80::f016:771c:2a61:f3d3 prefixlen 64 scopeid 0x20<link>  
     ether 94:53:30:78:5f:d1 txqueuelen 1000 (Ethernet)  
     RX packets 80646 bytes 82143411 (78.3 MiB)  
     RX errors 0 dropped 0 overruns 0 frame 0  
     TX packets 40453 bytes 6154019 (5.8 MiB)  
     TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0  

After rebooting, we can see that the MAC Addresses have been changed to enp1s0 (96:af:17:f0:9e:1f) and wlp2s0 (aa:5f:09:97:64:06).


 $ ifconfig  
 enp1s0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500  
     ether 96:af:17:f0:9e:1f txqueuelen 1000 (Ethernet)  
     RX packets 0 bytes 0 (0.0 B)  
     RX errors 0 dropped 0 overruns 0 frame 0  
     TX packets 0 bytes 0 (0.0 B)  
     TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0  


 wlp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500  
     inet 192.168.20.4 netmask 255.255.255.0 broadcast 192.168.20.255  
     inet6 fe80::f016:771c:2a61:f3d3 prefixlen 64 scopeid 0x20<link>  
     ether aa:5f:09:97:64:06 txqueuelen 1000 (Ethernet)  
     RX packets 80646 bytes 82143411 (78.3 MiB)  
     RX errors 0 dropped 0 overruns 0 frame 0  
     TX packets 40453 bytes 6154019 (5.8 MiB)  
     TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0  

So we have successfully configured Systemd service to randomly change the MAC Address of WLAN and LAN NIC on boot by utilizing the macchanger tool.

References


Creating a Linux service with systemd - Benjamin Morel - Medium
Use systemd to Start a Linux Service at Boot | Linode
[SOLVED] Could not change MAC. & Setup Macchanger auto spoofing/randomization in Kali
10.6. Creating and Modifying systemd Unit Files Red Hat Enterprise Linux 7 | Red Hat Customer Portal

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.