def create_tun_interface(): from pytun import TunTapDevice tun1 = TunTapDevice(name="gtw") tun1.addr = '192.168.13.10' tun1.persist(True) tun2 = TunTapDevice(name='tun_socket') tun2.addr = '192.168.13.1' tun2.persist(True) server_program(tun2.addr)
def __init__(self, side): self.side = int(side) if self.side not in [1, 2]: print("Side must be 1 or 2") raise Exception("Invalid Side") self.tun = TunTapDevice(flags=IFF_TUN | IFF_NO_PI, name='qrtun%d' % self.side) self.tun.addr = '10.0.8.%d' % (self.side) if self.side == 1: self.other_side = 2 else: self.other_side = 1 self.tun.netmask = '255.255.255.0' #MTU must be set low enough to fit in a single qrcode self.tun.mtu = 500 self.epoll = select.epoll() self.epoll.register(self.tun.fileno(), select.EPOLLIN) self.tun.up() #self.outfile = 'resources/toscreen%d.png'%(self.side) self.outfile = None self.infile = 'resources/toscreen%d.png' % (self.other_side) self.indata = None self.olddata = "" self.outdata = "" self.running = False self.qr = qrtools.QR() self.vc = cv2.VideoCapture(0) self.vc.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 720) self.vc.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 1280) pygame.init() pygame.event.set_allowed(None) pygame.event.set_allowed([pygame.KEYDOWN, pygame.QUIT]) self.screen = pygame.display.set_mode((SIZE, SIZE)) self.scale = 12 pygame.display.set_caption("qrtun - QR Code scale %d" % (self.scale))
def __init__(self, name, address, netmask, mtu): self.name = name self.tun = TunTapDevice(self.name) self.tun.addr = address self.tun.netmask = netmask self.tun.mtu = mtu self.tun.up()
def __init__(self, side): self.side = int(side) if self.side not in [1, 2]: print("Side must be 1 or 2") raise Exception("Invalid Side") self.tun = TunTapDevice(flags=IFF_TUN | IFF_NO_PI, name='qrtun%d' % self.side) self.tun.addr = '10.0.8.%d' % (self.side) if self.side == 1: self.other_side = 2 else: self.other_side = 1 self.tun.netmask = '255.255.255.0' #MTU must be set low enough to fit in a single qrcode self.tun.mtu = 500 self.epoll = select.epoll() self.epoll.register(self.tun.fileno(), select.EPOLLIN) self.tun.up() self.seq = 0 self.ack = 0 self.outfile = 'resources/toscreen%d.png' % (self.side) self.infile = 'resources/toscreen%d.png' % (self.other_side) self.indata = None self.olddata = "" self.outdata = "" self.msg_read = True self.sent_time = None self.running = False self.qr = qrtools.QR() self.vc = cv2.VideoCapture(0) self.vc.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 720) self.vc.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 1280)
def __init__(self, ip='192.168.1.0', netmask='255.255.255.0'): tap = TunTapDevice(flags=IFF_TAP | IFF_NO_PI, name="tap0") tap.mtu = 1500 tap.addr = ip tap.netmask = netmask self.tap = tap self.tap.up()
def __init__(self, *args, **kwargs): super(TunThread, self).__init__(*args, **kwargs) self.running = True self.tun = TunTapDevice(flags=(IFF_TAP | IFF_NO_PI)) self.tun.addr = '10.5.0.1' self.tun.netmask = '255.255.0.0' self.tun.mtu = 1500 self.tun.up()
def __init__(self, name, ip, netmask): self.tun = TunTapDevice(name=name, flags=pytun.IFF_TUN | pytun.IFF_NO_PI) self.tun.addr = ip self.tun.netmask = netmask self.mtu = 1500 self.tun.persist(True) self.tun.up()
def main(): global tap, my_mac, my_ip, server if len(sys.argv) != 3: print("Usage: %s url password" % sys.argv[0]) sys.exit(1) server = sys.argv[1] password = sys.argv[2] while server.endswith('/'): server = server[:-1] session = requests.Session() if os.path.exists("/tmp/tap0cache"): with open("/tmp/tap0cache", "rb") as f: data = f.read(10) my_mac = data[:6] my_ip = data[6:10] else: ans = session.post(server + '/connect', password) res = ans.content if ans.status_code != 200: raise ValueError("Failed to connect: " + str(res)) my_mac = res[:6] my_ip = res[6:10] with open("/tmp/tap0cache", "wb") as f: f.write(res) tap = TunTapDevice(flags=IFF_TAP) tap.addr = ".".join(map(str, my_ip)) print("My ip is:", tap.addr) tap.netmask = '255.255.0.0' tap.mtu = 1300 tap.hwaddr = my_mac tap.up() tap_reader = threading.Thread(target=read_data, daemon=True) tap_reader.start() sender = threading.Thread(target=send_data, daemon=True) sender.start() while True: ans = session.post(server + '/recv', my_mac) if ans.status_code == 204: continue if ans.status_code == 403: os.remove("/tmp/tap0cache") if ans.status_code != 200: print("recv: received status code " + str(ans.status_code) + ": " + ans.text) sys.exit(1) def process_packet(data): packet_mac = get_mac(data) if packet_mac == my_mac or packet_mac == BROADCAST: tap.write(data) parse_packets(BytesIO(ans.content), process_packet)
def setup_devs(name, if_idx): tap = TunTapDevice(name="{}-tap{}".format(name, if_idx), flags=IFF_TAP | IFF_NO_PI) tap.up() sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW) sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 33554432) sock.bind(("{}-eth{}".format(name, if_idx), socket.SOCK_RAW)) return tap, sock
def open(self): self.set_nodelay(True) self.tun = TunTapDevice(flags= (IFF_TAP | IFF_NO_PI)) self.tun.addr = '192.168.1.1' self.tun.dstaddr = '192.168.1.2' self.tun.netmask = '255.255.255.0' self.tun.mtu = 1500 self.tun.up() self.thread = TunThread(tun=self.tun, socket = self) self.thread.start()
def setup_tap(addr,netmask): tap = TunTapDevice(flags=IFF_TAP) #tap = TapTunnel() print tap.name tap.addr = addr #tun.dstaddr = '10.8.0.2' tap.netmask = netmask tap.mtu = 1500 print tap #tap.persist(True) tap.up() return tap
def create_tap(self): # identify interface for bridging, will may take some time to be created def get_interface_for_ip(addr): mon_if_name = '' addrs = psutil.net_if_addrs() for netif in addrs: for iface in addrs[netif]: #print (str(iface.family)) if str(iface.family) == 'AddressFamily.AF_INET': ip_addr = ipaddress.IPv4Network((addrs[netif][0].address, addrs[netif][0].netmask), strict=False) print(ip_addr.network_address) if self.args.priv_net_addr.split('/')[0] == str(addrs[netif][0].address): mon_if_name = netif break return mon_if_name mon_if_name = get_interface_for_ip (self.args.priv_net_addr) while mon_if_name == '' or mon_if_name is None: print("waiting for interface to be active " + self.args.priv_net_addr) r("/bin/sleep 2s") mon_if_name = get_interface_for_ip (self.args.priv_net_addr) # create tap self.tap_interface = 'tap0' addrs = psutil.net_if_addrs() if mon_if_name == '': print ('No interface specified') elif self.tap_interface not in addrs: print('Creating tap ' + self.tap_interface + ' for ' + mon_if_name) #os.system("/root/environments/tls/start-tap.sh " + mon_if_name + " 10.0.2.2/24") r("mkdir /dev/net") r("mknod /dev/net/tun c 10 200") r("ip tuntap add tap0 mode tap") r("brctl addbr br0") r("ip link set tap0 up") r("ip link set br0 up") r("brctl addif br0 tap0") r("brctl addif br0 " + mon_if_name) r("ip addr del " + self.args.priv_net_addr + " dev " + mon_if_name) r("ip addr add " + self.args.priv_net_addr + " dev br0") else: print('Tap already created') # Initialize a TAP interface and make it non-blocking self.tap = TunTapDevice(flags = IFF_TAP|IFF_NO_PI, name=self.tap_interface); self.tap.up() fcntl.fcntl(self.tap.fileno(), fcntl.F_SETFL, os.O_NONBLOCK) self.tap.mtu = self.tap_mtu
def create_tun(tun_name): tun = TunTapDevice(name=tun_name, flags=(IFF_TUN | IFF_NO_PI)) tun.addr = '10.6.0.1' tun.dstaddr = '0.0.0.0' tun.netmask = '0.0.0.0' tun.mtu = 1500 ''' res = os.system('route add default gw' + ' 10.6.0.1 ' + tun_name) if res == 0: return tun else: return None''' return tun
def main(): global tap, password if len(sys.argv) != 2: print("Usage: %s password" % sys.argv[0]) sys.exit(1) password = sys.argv[1] tap = TunTapDevice(flags=IFF_TAP) tap.addr = ".".join(map(str, IP_PREFIX + (0, 1))) tap.netmask = '255.255.0.0' tap.mtu = 1300 tap.hwaddr = MYMAC tap.up() tap_reader = threading.Thread(target=read_data, daemon=True) tap_reader.start() print('Serving on 8088...') WSGIServer(application, port=8088, numthreads=1000).start()
def __init__(self, tkns): self.clients = [] self.iface = TunTapDevice(flags=IFF_TAP | IFF_NO_PI, name=iface) if addr is not None and netmask is not None: self.iface.addr = addr if addr is not None and netmask is not None: self.iface.netmask = netmask self.iface.hwaddr = macaddr self.iface.mtu = mtu self.iface.persist(True) self.iface.up() self.loop = asyncio.get_event_loop() os.system(ifup) self.ifacethread = IFaceThread(self.iface) self.ifacethread.start() if not sys.platform == 'win32': self.loop.add_signal_handler(signal.SIGINT, lambda: self.loop.stop()) self.loop.add_signal_handler(signal.SIGTERM, lambda: self.loop.stop()) i = 0 for tkn in tkns: c = VPNBot(self, self.ifacethread, i) i += 1 future = asyncio.ensure_future(c.start(tkn), loop=self.loop) future.add_done_callback(lambda f: self.loop.stop()) self.clients.append(c) self.loop.call_later(.1, self.poll_thread) print("Tap interface started") print( f"VARS: MTU{mtu}, ADDR{addr}, HWADDR{macaddr}, DSCVPN{TUNVER} - {len(tkns)} clients" ) try: self.loop.run_forever() except KeyboardInterrupt: print('Received signal to terminate bot and event loop.') finally: self.loop = asyncio.get_event_loop() for x in self.clients: asyncio.ensure_future(x.close(), loop=self.loop) self.loop.run_forever() self.ifacethread.stop()
def __init__(self): self.tun = TunTapDevice(name='strangenet', flags=pytun.IFF_TUN | pytun.IFF_NO_PI) logging.debug('Created Linux tun device ' + self.tun.name) self.tun.addr = os.getenv('STRANGENET_IP', '10.0.0.1') logging.info('Assigned Linux tun device IP: ' + self.tun.addr) self.tun.netmask = os.getenv('STRANGENET_NETMASK', '255.255.255.0') logging.info('Configured Linux tun device netmask: ' + self.tun.netmask) self.tun.persist( True) # TODO figure out what this does and if we need it self.tun.mtu = 256 self.tun.up() logging.info('Linux tun device is up!') # setup polling self.poller = select.poll() self.poller.register(self.tun, select.POLLIN)
def start_main(rec, loc, dst, passwd,ifname): tunnel = WhatsAppMessageTunnel() # Create TUN device for network capture and injections tun = TunTapDevice(name='socialtun') print(tun.name + ' has been created, information follows:') tun.addr = loc tun.dstaddr = dst tun.netmask = '255.255.255.0' tun.mtu = 40000 password = passwd print('Address: ' + tun.addr) print('Dest.-Address: ' + tun.dstaddr) print('Netmask: ' + tun.netmask) print('MTU: ' + str(tun.mtu)) # Start TUN device tun.up() up = True print('TUN is up') # Create the receive thread via our helper method # thread = threading.Thread(target=main_loop_starter) thread = threading.Thread(target=recv_loop, args=(tunnel, rec, password, tun)) thread.start() thread = threading.Thread(target=ping_server, args=(loc,)) thread.start() while True: # Continually read from the tunnel and write data in base64 buf = tun.read(tun.mtu) rc4obj = ARC4.new(password) tunnel.send(rec+"@s.whatsapp.net", base64.b64encode(rc4obj.encrypt(buf))) #print (buf) # Cleanup and stop application up = False tun.down() receiver.stop()
async def start(self): print('Starting...') print(f'Creating {len(self.tokens)} clients') for client in range(len(self.tokens)): print(f'Creating client #{client}') self.clients.append(VPNBot(self, client)) # Set the first client as the receiver self.clients[0].receiver = True for client, token in enumerate(self.tokens): print(f'Starting client #{client} with token {token}') self.loop.create_task(self.clients[client].start(token)) self.session = aiohttp.ClientSession() self.iface = TunTapDevice(flags=IFF_TAP | IFF_NO_PI, name=iface) if addr is not None and netmask is not None: self.iface.addr = addr if addr is not None and netmask is not None: self.iface.netmask = netmask self.iface.hwaddr = macaddr self.iface.mtu = mtu self.iface.persist(True) self.iface.up() os.system(ifup) self.ifacethread = IFaceThread(self.iface) self.ifacethread.start() self.lastsend = time() print("Tap interface started") print(f"VARS: MTU{mtu}, ADDR{addr}, HWADDR{macaddr}") self.loop.call_later(.1, self.send_pkts) while any(client.ready == False for client in self.clients): # Wait for all clients to come up await asyncio.sleep(1) continue self.channels = [ client.get_channel(ispchan) for client in self.clients ] self.ready = True
def main(): parser = argparse.ArgumentParser() parser.add_argument('-s', '--source-addr', default='10.8.0.1') parser.add_argument('-d', '--dest-addr', default='10.8.0.2') parser.add_argument('-m', '--netmask', default='255.255.255.0') parser.add_argument('-l', '--mtu', default=1500, type=int) args = parser.parse_args() tun = TunTapDevice() try: tun.addr = args.source_addr tun.dstaddr = args.dest_addr tun.netmask = args.netmask tun.mtu = args.mtu while True: buf = tun.read(tun.mtu) print(buf) tun.write(buf) finally: tun.close()
import hexdump from pytun import TunTapDevice, IFF_TAP tun = TunTapDevice(name='rfc6214') with open("captured_in.hex", "r") as the_file: hex = the_file.readline() buf = hexdump.dehex(hex) tun.write(buf) buf = tun.read(tun.mtu) hex = hexdump.dump(buf, 2, '') print(hex) with open('captured_out.hex', 'w') as the_file: the_file.write(hex)
from socket import * from fcntl import ioctl from select import select import getopt, struct from scapy.all import * from pytun import TunTapDevice, IFF_TUN, IFF_NO_PI def change_dst_ip(packet, new_ip): new_packet = packet.copy() new_packet[IP].dst = new_ip return new_packet tun0 = TunTapDevice(name='tun0', flags=IFF_TUN + IFF_NO_PI) #'127.0.0.1' tun0.addr = '10.1.8.82' tun0.dstaddr = '10.1.8.83' tun0.netmask = '255.255.255.0' tun0.mtu = 1500 ip = IP(src="192.168.1.2", dst="192.168.1.1") udp = UDP(sport=4444, dport=4444) payload = Raw(load="Hi! I'm UDP-packet") packet = ip / udp / payload packet.show() new_packet = change_dst_ip(packet, "192.168.1.3")
from pytun import TunTapDevice tun = TunTapDevice(name='mytun') print(tun.name) tun.addr = '192.168.137.1' tun.dstaddr = '192.168.137.10' tun.netmask = '255.255.255.255' tun.mtu = 1500 tap.hwaddr = '\x00\x11\x22\x33\x44\x55' print(tap.hwaddr) tun.persists(True) tun.up() buf = tun.read(tun.mtu) tun.write(buf) tun.close()
import fcntl import struct import os import socket import threading import sys import pytun from pytun import TunTapDevice try: import uvloop except ImportError: uvloop = None COUNT = 0 tun = TunTapDevice(name="mytun_serv", flags=pytun.IFF_TUN | pytun.IFF_NO_PI) tun.addr = "10.10.10.2" tun.dstaddr = "10.10.10.1" tun.netmask = "255.255.255.0" tun.mtu = 1048 tun.persist(True) tun.up() STREAM_ID = 100 class VPNServerProtocol(QuicConnectionProtocol): # -00 specifies 'dq', 'doq', and 'doq-h00' (the latter obviously tying to # the version of the draft it matches). This is confusing, so we'll just # support them all, until future drafts define conflicting behaviour. SUPPORTED_ALPNS = ["dq", "doq", "doq-h00"]
exit(1) # Ask user to choose contact i = input('Telegram online, please enter contact to connect to (by number): ') if i not in contacts: print 'Please enter a number in the above range!' exit(1) # Print username username = unicode(contacts[i]['print_name']) peer_id = contacts[i]['peer_id'] print 'Connecting to partner: ' + username # Create TUN device for network capture and injections tun = TunTapDevice(name='teletun-device') print tun.name + ' has been created, information follows:' # Set IP address based on --server flag if '--server' in sys.argv: tun.addr = '10.8.0.1' tun.dstaddr = '10.8.0.2' else: tun.addr = '10.8.0.2' tun.dstaddr = '10.8.0.1' tun.netmask = '255.255.255.0' tun.mtu = 1500 print 'Address: ' + tun.addr
def setUp(self): tap = TunTapDevice(name=TAP_NAME, flags=IFF_TAP) tap.up() self.tap = tap
from tornado import ioloop from ws4py.client.tornadoclient import TornadoWebSocketClient from pytun import TunTapDevice, IFF_TAP, IFF_NO_PI import sys io_loop = ioloop.IOLoop.instance() tap = TunTapDevice(flags=IFF_TAP | IFF_NO_PI, name='vpn-ws%d') class VpnWSClient(TornadoWebSocketClient): def received_message(self, m): tap.write(str(m)) def closed(self, code, reason=None): print "ooops" ws = VpnWSClient(sys.argv[1]) ws.connect() def tap_callback(fd, event): ws.send(tap.read(tap.mtu), binary=True) io_loop.add_handler(tap.fileno(), tap_callback, io_loop.READ) io_loop.start()
from pytun import TunTapDevice, IFF_NO_PI, IFF_TUN tun = TunTapDevice(name="tun", flags=(IFF_TUN | IFF_NO_PI)) tun.addr = '192.168.13.10' tun.dstaddr = '192.168.13.1' tun.netmask = '255.255.255.0' tun.persist(True) tun.mtu = 1500 buf = tun.read(tun.mtu) tun.write(buf)
def __init__(self): os.system('sudo ip tuntap add dev esp32 mode tap user ds') time.sleep(1) self.tap = TunTapDevice(name='esp32', flags=IFF_TAP)
from pytun import TunTapDevice def read(tun): while True: try: buf = tun.read(tun.mtu) tun.write(buf) print(buf) except KeyboardInterrupt: print('Stop') break if __name__ == '__main__': tun = TunTapDevice(name='tun_device') print('Tun name:', tun.name) tun.addr = '192.168.137.1' tun.dstaddr = '192.168.137.10' tun.netmask = '255.255.255.0' tun.mtu = 1500 tun.persist(True) tun.up() read(tun) tun.close()
#tun IP server import multiprocessing import binascii import json from pytun import TunTapDevice import os from nat_bridge_functions import * tun = TunTapDevice(name='tun0') tun.mtu = 1500 while True: buf = tun.read(tun.mtu) #p = multiprocessing.Process(target=data_processor.parse_message(addr, data)) #p.start() packet = binascii.hexlify(buf).decode() dest_ip = get_dest_ip_from_scapy(packet) dest_udp_port = get_dest_udp_port_from_scapy(packet) #ethernet = packet[0:8] #ip_headers = packet[8:24] mes_type, local_id, data = get_json_content_from_scapy(packet) if int(dest_udp_port, 16) == 40001: if (mes_type == 'directive'): print "TUN SERVER DIRECTIVE %s %s %s %s" % ( dest_ip, mes_type, str(local_id), str(data)) p = multiprocessing.Process( target=os.system("./nat_bridge_out.py %s %s %s %s" %