Skip to content

Instantly share code, notes, and snippets.

@anoduck
Created July 12, 2024 05:58
Show Gist options
  • Save anoduck/2cebdb53c10c1a318ef9cf1e2f386967 to your computer and use it in GitHub Desktop.
Save anoduck/2cebdb53c10c1a318ef9cf1e2f386967 to your computer and use it in GitHub Desktop.
A simply pthon script to create a tunnel interface.
#!/usr/bin/env python3
import fcntl
import struct
import os
from scapy.layers.inet import IP
from warnings import warn
from dataclasses import dataclass, field
from typing import List
import subprocess
@dataclass
class apData:
IFNAMSIZ: int = 16
IFF_TUN: int = 0x0001
IFF_TAP: int = 0x0002 # Should we want to tunnel layer 2...
IFF_NO_PI: int = 0x1000
TUNSETIFF: int = 0x400454ca
DEVICE: str = "tun0"
IP_ADDRESS: str = "10.1.1.1"
NETMASK: str = "255.255.255.0"
IP_NETWORK: str = "10.1.1"
NET_BROADCAST: str = "10.1.1.255"
def create_tun():
name = "rogue1"
if len(name) > apData.IFNAMSIZ:
raise Exception(
"Tun interface name it too big"
)
fd = open('/dev/net/tun', 'r+b')
ifr_flags = apData.IFF_TUN | apData.IFF_NO_PI
ifreq = struct.pack('16sH', name, ifr_flags)
fcntl.ioctl(fd, apData.TUNSETIFF, ifreq)
if subprocess.call(['ip', 'addr', 'add', apData.IP_ADDRESS, 'dev', apData.DEVICE]):
warn("Failed to assign ip address to dev")
if subprocess.call(['ip', 'link', 'set', 'dev', apData.DEVICE, 'up']):
warn("failed to bring device up")
if __name__ == "create_tun.py":
create_tun()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment