Showing posts with label Fedora. Show all posts
Showing posts with label Fedora. Show all posts

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

Saturday, March 10, 2018

Linux: Configure VPN using NMCLI

Recently I was trying to connect to a PPTP VPN from my home workstation, which has fedora 30 installed and I wasn't able to. So I researched online about it and figured how to create a VPN connection using 'Network Manager' tool nmcli.

Below I have demonstrated the method of creating the connection.

Create Connection

 [spameggs@foobar ~]$ sudo nmcli connection add connection.id VPN_2 connection.type vpn connection.interface-name wlp2s0 connection.permissions spameggs vpn.service-type pptp vpn.data gateway=199.202.117.191   
 [spameggs@foobar ~]$ sudo nmcli connection modify VPN_2 vpn.user-name numb vpn.secrets password=damisk76  

We can check the connection details from the related file VPN_2.nmconnection created in the below location.

 [spameggs@foobar system-connections]$ pwd  
 /etc/NetworkManager/system-connections  

 [spameggs@foobar system-connections]$ sudo cat VPN_2.nmconnection   
 [sudo] password for spameggs:   
 [connection]  
 id=VPN_2  
 uuid=1342c044-108a-447c-8712-de988346fccb  
 type=vpn  
 interface-name=wlp2s0  
 permissions=user:spameggs:;  
   
 [vpn]  
 gateway=199.202.117.191  
 service-type=org.freedesktop.NetworkManager.pptp  
 user-name=numb  
 [vpn-secrets]  
 password=damisk76  
 [ipv4]  
 dns-search=  
 method=auto  
 [ipv6]  
 addr-gen-mode=stable-privacy  
 dns-search=  
 method=auto  

Test Connection


I could not connect the VPN and I see the following logs from journalctl.

 Started the VPN service, PID 6152  
 Saw the service appear; activating connection  
 VPN connection: (ConnectInteractive) reply received  
 VPN plugin: state changed: starting (3)  
 VPN plugin: failed: connect-failed (1)  
 VPN plugin: state changed: stopping (5)  
 VPN service disappeared  

Load Modules


I did further research and found that we need to load some modules. There are two methods for this process. For temporary use we can run the following command from a privileged user.

 [root@drone ~]# for module in nf_nat_pptp nf_conntrack_pptp
 > do  
 > modprobe $module;  
 > done  

Or we can load these modules on boot permanently by adding the above module names in the file /etc/modules-load.d/netfilter.conf as follows.

 [spameggs@foobar ~]$ sudo cat /etc/modules-load.d/netfilter.conf  
 nf_nat_pptp  
 nf_conntrack_pptp  

We need to reboot for the changes to occur.

Configure Firewall Rules


There is no need to make any changes in Selinux. However, we have to add few rules in the firewall to allow PPTP connections.

 [spameggs@foobar ~]$ sudo firewall-cmd --permanent --add-port=1723/tcp   
 [spameggs@foobar ~]$ sudo firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -p gre -j ACCEPT  
 [spameggs@foobar ~]$ sudo firewall-cmd --permanent --direct --add-rule ipv6 filter INPUT 0 -p gre -j ACCEPT  
 [spameggs@foobar ~]$ sudo firewall-cmd --permanent --add-masquerade  
 [spameggs@foobar ~]$ sudo firewall-cmd --reload  

Reconnect Successfully


We can see below that the connection is finally established successfully.

 [spameggs@foobar ~]$ nmcli connection up VPN_2   
 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)  

Verify Connection


We can verify by checking the output of ifconfig. A new ppp0 interface has been automatically created with the IP address 192.168.101.149.

 ppp0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST> mtu 1400  
     inet 192.168.101.149 netmask 255.255.255.255 destination 192.168.101.150  
     ppp txqueuelen 3 (Point-to-Point Protocol)  
     RX packets 1271 bytes 1398779 (1.3 MiB)  
     RX errors 0 dropped 0 overruns 0 frame 0  
     TX packets 1213 bytes 127555 (124.5 KiB)  
     TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0  

The command ip addr also shows the ppp0 interface.

 [foobar@drone system-connections]$ ip addr  
 12: ppp0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1400 qdisc fq_codel state UNKNOWN group default qlen 3  
   link/ppp   
   inet 192.168.101.149 peer 192.168.101.151/32 scope global ppp0  
     valid_lft forever preferred_lft forever  

We can check the routes for the VPN traffic by running ip route and route commands.

 [spameggs@foobar ~]$ ip route  
 default dev ppp0 proto static scope link metric 50   
 default via 192.168.100.1 dev wlp2s0 proto dhcp metric 600   
 192.168.100.0/24 dev wlp2s0 proto kernel scope link src 192.168.100.29 metric 600   
 192.168.100.1 dev wlp2s0 proto static scope link metric 600   
 192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown   
 192.168.101.149 dev ppp0 proto kernel scope link src 192.168.101.148 metric 50   
 199.202.117.191 via 192.168.100.1 dev wlp2s0 src 192.168.100.29   
 199.202.117.191 via 192.168.100.1 dev wlp2s0 proto static metric 600   

 [spameggs@foobar ~]$ route  
 Kernel IP routing table  
 Destination   Gateway     Genmask     Flags Metric Ref  Use Iface  
 default     0.0.0.0     0.0.0.0     U   50   0    0 ppp0  
 default     _gateway    0.0.0.0     UG  600  0    0 wlp2s0  
 192.168.100.0   0.0.0.0     255.255.255.0  U   600  0    0 wlp2s0  
 _gateway    0.0.0.0     255.255.255.255 UH  600  0    0 wlp2s0  
 192.168.122.0  0.0.0.0     255.255.255.0  U   0   0    0 virbr0  
 192.168.101.149 0.0.0.0     255.255.255.255 UH  50   0    0 ppp0  
 199.202.117.191 _gateway    255.255.255.255 UGH  0   0    0 wlp2s0  
 199.202.117.191 _gateway    255.255.255.255 UGH  600  0    0 wlp2s0  

Disconnect VPN


Now we disconnect the VPN connection by issuing the below command.

 [spameggs@foobar ~]$ nmcli connection down VPN_2   
 Connection 'VPN_2' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)