Advertisement
LunaeStellsr

Untitled

Sep 19th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. import _winreg as reg
  2. import win32file
  3.  
  4.  
  5. # path to network adapter registery class
  6. adapter_key = r'SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}'
  7.  
  8.  
  9. def get_device_guid():
  10.  
  11.     with reg.OpenKey(reg.HKEY_LOCAL_MACHINE, adapter_key) as adapters:
  12.         try:
  13.       # enum each key under this register folder, 10000 should be big enough
  14.       # since we don't know how many keys it has
  15.             for i in range(10000):
  16.                 key_name = reg.EnumKey(adapters, i)
  17.  
  18.         # open the regedit machine code
  19.                 with reg.OpenKey(adapters, key_name) as adapter:
  20.                     try:
  21.  
  22.             # get device id of tap-win32 Virtual Network Driver
  23.                         component_id = reg.QueryValueEx(adapter, 'ComponentId')[0]
  24.                         if component_id == 'tap0801':
  25.                             return reg.QueryValueEx(adapter, 'NetCfgInstanceId')[0]
  26.                     except WindowsError:
  27.                             pass
  28.         except WindowsError:
  29.                 pass
  30.  
  31.  
  32. # return bitwise-formated tap control code in DWORD (int32)
  33. def CTL_CODE(device_type, function, method, access):
  34.     return (device_type << 16) | (access << 14) | (function << 2) | method
  35.  
  36. def TAP_CONTROL_CODE(request, method):
  37.     return CTL_CODE(34, request, method, 0)
  38.  
  39.  
  40. # tap driver config params
  41. TAP_IOCTL_CONFIG_POINT_TO_POINT = TAP_CONTROL_CODE(5, 0)
  42. TAP_IOCTL_SET_MEDIA_STATUS = TAP_CONTROL_CODE(6, 0)
  43. TAP_IOCTL_CONFIG_TUN = TAP_CONTROL_CODE(10, 0)
  44.  
  45. # Run tap driver in p2p mode
  46. TAP_P2P_MODE = False
  47.  
  48. if __name__ == '__main__':
  49.     guid = get_device_guid()
  50.  
  51.   # create tap config file in user mode device dir
  52.     handle = win32file.CreateFile(r'\\.\Global\%s.tap' % guid,
  53.                                                                 win32file.GENERIC_READ | win32file.GENERIC_WRITE,
  54.                                                                 win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE,
  55.                                                                 None, win32file.OPEN_EXISTING,
  56.                                                                 win32file.FILE_ATTRIBUTE_SYSTEM, # | win32file.FILE_FLAG_OVERLAPPED,
  57.                                                                 None)
  58.   # print target file handle
  59.     print(handle.handle)
  60.  
  61.     if TAP_P2P_MODE:
  62.         win32file.DeviceIoControl(handle, TAP_IOCTL_CONFIG_POINT_TO_POINT,
  63.                                                             '\xc0\xa8\x11\x01\xc0\xa8\x11\x10', None)
  64.     else:
  65.         win32file.DeviceIoControl(handle, TAP_IOCTL_SET_MEDIA_STATUS, '\x01\x00\x00\x00', None)
  66.         win32file.DeviceIoControl(handle, TAP_IOCTL_CONFIG_TUN,
  67.                                                             '\x0a\x03\x00\x01\x0a\x03\x00\x00\xff\xff\xff\x00', None)
  68.     while True:
  69.         l, p = win32file.ReadFile(handle, 2000)
  70.         q = p[:12] + p[16:20] + p[12:16] + p[20:]
  71.         win32file.WriteFile(handle, q)
  72.         print(p, q)
  73.     win32file.CloseHandle(handle)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement