Move Ifconfig and Iwconfig logic to separate classes.

This commit is contained in:
derv82
2018-04-18 06:15:14 -04:00
parent bd13bf69cf
commit 3542381b3e
7 changed files with 147 additions and 75 deletions

View File

@@ -2,6 +2,8 @@
# -*- coding: utf-8 -*-
from ..model.interface import Interface
from ..tools.ifconfig import Ifconfig
from ..tools.iwconfig import Iwconfig
from ..util.process import Process
from ..util.color import Color
from ..util.input import raw_input
@@ -63,7 +65,10 @@ class Airmon(object):
@staticmethod
def start_baddriver(iface): #fix for bad drivers like the rtl8812AU
os.system("ifconfig %s down; iwconfig %s mode monitor; ifconfig %s up" % (iface, iface, iface))
Ifconfig.down(iface)
Iwconfig.mode(iface, 'monitor')
Ifconfig.up(iface)
with open("/sys/class/net/" + iface + "/type", "r") as f:
if (int(f.read()) == Airmon.ARPHRD_IEEE80211_RADIOTAP):
return iface
@@ -72,7 +77,10 @@ class Airmon(object):
@staticmethod
def stop_baddriver(iface):
os.system("ifconfig %s down; iwconfig %s mode managed; ifconfig %s up" % (iface, iface, iface))
Ifconfig.down(iface)
Iwconfig.mode(iface, 'managed')
Ifconfig.up(iface)
with open("/sys/class/net/" + iface + "/type", "r") as f:
if (int(f.read()) == Airmon.ARPHRD_ETHER):
return iface
@@ -94,7 +102,7 @@ class Airmon(object):
'''
# Get interface name from input
if type(iface) == Interface:
iface = iface.name
iface = iface.interface
Airmon.base_interface = iface
# Call airmon-ng
@@ -181,17 +189,7 @@ class Airmon(object):
Returns:
List of interface names that are in monitor mode
'''
interfaces = []
(out, err) = Process.call("iwconfig")
for line in out.split("\n"):
if len(line) == 0: continue
if line[0] != ' ':
iface = line.split(' ')[0]
if '\t' in iface:
iface = iface.split('\t')[0]
if 'Mode:Monitor' in line and iface not in interfaces:
interfaces.append(iface)
return interfaces
return Iwconfig.get_interfaces(mode='Monitor')
@staticmethod
@@ -242,11 +240,11 @@ class Airmon(object):
iface = a.get(choice)
if a.get(choice).name in mon_ifaces:
Color.pl('{+} {G}%s{W} is already in monitor mode' % iface.name)
if a.get(choice).interface in mon_ifaces:
Color.pl('{+} {G}%s{W} is already in monitor mode' % iface.interface)
else:
iface.name = Airmon.start(iface)
return iface.name
iface.interface = Airmon.start(iface)
return iface.interface
@staticmethod
@@ -297,7 +295,7 @@ class Airmon(object):
@staticmethod
def put_interface_up(iface):
Color.p("{!} {O}putting interface {R}%s up{O}..." % (iface))
(out,err) = Process.call('ifconfig %s up' % (iface))
Ifconfig.up(iface)
Color.pl(" {R}done{W}")
@staticmethod

53
wifite/tools/ifconfig.py Normal file
View File

@@ -0,0 +1,53 @@
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
import re
class Ifconfig(object):
@classmethod
def up(cls, interface, args=[]):
'''Put interface up'''
from ..util.process import Process
command = ['ifconfig', interface, 'up']
if type(args) is list:
command.extend(args)
elif type(args) is 'str':
command.append(args)
pid = Process(command)
pid.wait()
return pid.poll() == 0
@classmethod
def down(cls, interface):
'''Put interface down'''
from ..util.process import Process
command = ['ifconfig', interface, 'down']
if type(args) is list:
command.extend(args)
elif type(args) is 'str':
command.append(args)
pid = Process(command)
pid.wait()
return pid.poll() == 0
@classmethod
def get_mac(cls, interface):
from ..util.process import Process
output = Process(['ifconfig', interface]).stdout()
mac_regex = ('[a-zA-Z0-9]{2}-' * 6)[:-1]
match = re.search(' (%s)' % mac_regex, output)
if not match:
raise Exception('Could not find the mac address for %s' % interface)
return match.groups()[0].replace('-', ':')

38
wifite/tools/iwconfig.py Normal file
View File

@@ -0,0 +1,38 @@
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
class Iwconfig(object):
@classmethod
def exists(cls):
pass
@classmethod
def mode(cls, iface, mode_name):
from ..util.process import Process
pid = Process(['iwconfig', iface, 'mode', mode_name])
pid.wait()
return pid.poll()
@classmethod
def get_interfaces(cls, mode=None):
from ..util.process import Process
interfaces = set()
(out, err) = Process.call('iwconfig')
for line in out.split('\n'):
if len(line) == 0: continue
if not line.startswith(' '):
iface = line.split(' ')[0]
if '\t' in iface:
iface = iface.split('\t')[0]
if mode is None:
interfaces.add(iface)
if mode is not None and 'Mode:{}'.format(mode) in line:
interfaces.add(iface)
return list(interfaces)

View File

@@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
from ..model.interface import Interface
from ..tools.ifconfig import Ifconfig
from ..util.color import Color
class Macchanger(object):
@@ -15,22 +16,22 @@ class Macchanger(object):
from ..config import Configuration
iface = Configuration.interface
if type(iface) == Interface:
iface = iface.name
cls.original_mac = Interface.get_mac(iface)
iface = iface.interface
cls.original_mac = Ifconfig.get_mac(iface)
cls.is_init = True
@classmethod
def down_macch_up(cls, macch_option):
cls.init()
from ..util.process import Process
from ..config import Configuration
iface = Configuration.interface
cmd = ["ifconfig", iface, "down"]
Color.clear_entire_line()
Color.p("\r{+} {C}macchanger{W}: Taking interface {C}%s{W} down..." % iface)
ifdown = Process(cmd)
ifdown.wait()
if ifdown.poll() != 0:
if Ifconfig.down(iface) != 0:
Color.pl("{!} {C}macchanger{W}: Error running %s" % " ".join(cmd))
Color.pl("{!} Output: %s, %s" % (ifdown.stdout(), ifdown.stderr()))
return False
@@ -45,12 +46,11 @@ class Macchanger(object):
Color.pl("{!} Output: %s, %s" % (macch.stdout(), macch.stderr()))
return False
cmd = ["ifconfig", iface, "up"]
Color.clear_entire_line()
Color.p("\r{+} {C}macchanger{W}: Bringing interface {C}%s{W} up..." % iface)
ifup = Process(cmd)
ifup.wait()
if ifup.poll() != 0:
if Ifconfig.up(iface) != 0:
Color.pl("{!} {C}macchanger{W}: Error running %s" % " ".join(cmd))
Color.pl("{!} Output: %s, %s" % (ifup.stdout(), ifup.stderr()))
return False
@@ -62,7 +62,7 @@ class Macchanger(object):
if not cls.down_macch_up("-p"): return
Color.pl("\r{+} {C}macchanger{W}: Resetting MAC address...")
from ..config import Configuration
new_mac = Interface.get_mac(Configuration.interface)
new_mac = Ifconfig.get_mac(Configuration.interface)
Color.clear_entire_line()
Color.pl("\r{+} {C}macchanger{W}: Reset MAC address back to {C}%s{W}" % new_mac)
@@ -72,7 +72,7 @@ class Macchanger(object):
if not cls.down_macch_up("-r"): return
cls.is_changed = True
from ..config import Configuration
new_mac = Interface.get_mac(Configuration.interface)
new_mac = Ifconfig.get_mac(Configuration.interface)
Color.clear_entire_line()
Color.pl("\r{+} {C}macchanger{W}: Changed MAC address to {C}%s{W}" % new_mac)