I wrote a script that would create and print the name of a new TUN interface on an ubuntu VM using python.
import fcntlimport structimport osimport subprocess#from scapy.all import *TUNSETIFF = 0x400454caIFF_TUN = 0x0001IFF_TAP = 0x0002IFF_NO_PI = 0x1000tun = os.open("/dev/net/tun", os.O_RDWR)ifr = struct.pack('16sH', b'tun%d', IFF_TUN | IFF_NO_PI)ifname_bytes = fcntl.ioctl(tun, TUNSETIFF, ifr)ifname = ifname_bytes.decode('UTF-8')[:16].strip('\x00')print("Interface Name: {}".format(ifname))proc = subprocess.Popen(['ifconfig'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)stdout, stdin = proc.communicate()print(stdout.decode())
the output
Interface Name: tun0 enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.204 ...... lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 ........
this creates a TUN interface and assigns it a name of tunX (X is the available number for a new name of the interface)after running this I usually get an answer of tun0.Then, I print out the output of 'ifconfig' (I also tried it manually) and and I cannot see that tun0.
Can someone explain to me if I am truly creating that TUN device and if so what happens to it after the script ends, I am also completely interested in theoretic material on it if someone's got any.
Thank you.