-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwifitap_device.py
executable file
·100 lines (83 loc) · 3.12 KB
/
wifitap_device.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
########################################
#
# Copyright (C) 2011 Daniel Smith <viscous.liquid@gmail.com>
# Copyright (C) 2005 Cedric Blancher <sid@rstack.org>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation; version 2.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
#########################################
import os,sys,atexit,struct,re,string
from fcntl import ioctl
class WifiTapDevice:
TUN_CTL = '/dev/net/tun'
TUN_SET_IFF = 0x400454ca
IFF_TAP = 0x0002
def __init__(self):
self.__opened__ = False
self.inface = 'wlan0'
self.outface = 'wlan0'
self.bssid = ''
self.smac = ''
self.has_wep = False
self.wepkey = ''
self.keyid = 0
self.debug = False
self.verb = False
# Radiotap parameters for injection
self.rate = None # Legacy Rate
self.power = None # Tx Power
self.tx_flags = None # Tx Flags
self.retries = None # Data Retries
self.mcs = None # N Rate (MCS index)
self.hgi = False # HT Guard Interval
def open(self, name_format=''):
if name_format == '':
name_format = 'wj%d'
elif name_format.endswith('%d'):
name_format = name_format
else:
name_format = name_format + '%d'
self.__fd__ = os.open(self.TUN_CTL, os.O_RDWR)
ifs = ioctl(self.__fd__, self.TUN_SET_IFF,
struct.pack("16sH", name_format, self.IFF_TAP))
self.name = ifs[:16].strip("\x00")
self.__opened__ = True
atexit.register(self.close)
def fileno(self):
if self.__opened__:
return self.__fd__
else:
return 0
def is_open(self):
return self.__opened__
def close(self):
os.close(self.__fd__)
self.__fd__ = 0
self.__opened__ = False
def wep(self, key='', key_id=0):
# Match and parse WEP key
tmp_key = ""
if re.match('^([0-9a-fA-F]{2}){5}$', key) or re.match ('^([0-9a-fA-F]{2}){13}$', key):
tmp_key = key
elif re.match('^([0-9a-fA-F]{2}[:]){4}[0-9a-fA-F]{2}$', key) or re.match('^([0-9a-fA-F]{2}[:]){12}[0-9a-fA-F]{2}$', key):
tmp_key = re.sub(':', '', key)
elif re.match ('^([0-9a-fA-F]{4}[-]){2}[0-9a-fA-F]{2}$', key) or re.match ('^([0-9a-fA-F]{4}[-]){6}[0-9a-fA-F]{2}$', key):
tmp_key = re.sub('-', '', key)
else:
return
g = lambda x: chr(int(tmp_key[::2][x],16)*16+int(tmp_key[1::2][x],16))
for i in range(len(tmp_key)/2):
self.wepkey += g(i)
if key_id > 3 or key_id < 0:
self.key_id = 0
else:
self.key_id = key_id
self.has_wep = True
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 autoindent