Fixing eviltwin. Lots of changes.

This commit is contained in:
derv82
2018-05-13 12:39:28 -04:00
parent 94dd02b3ab
commit 1dcb23659b
11 changed files with 424 additions and 131 deletions

View File

@@ -6,6 +6,7 @@ import os
from .dependency import Dependency
from ..config import Configuration
from ..util.process import Process
class Hostapd(Dependency):
process_name = 'hostapd'
@@ -14,6 +15,7 @@ class Hostapd(Dependency):
dependency_name = process_name
dependency_url = 'apt-get install hostapd'
@classmethod
def exists(cls):
return Process.exists(cls.process_name)
@@ -23,52 +25,71 @@ class Hostapd(Dependency):
self.target = target
self.interface = interface
self.pid = None
# Save hostapd state?
self.config_file = None
self.output_file = None
self.output_write = None
self.state = 'Initializing'
def create_config_file(self):
if not self.target.essid_known:
self.state = 'Error: Target ESSID is not known'
raise Exception('Cannot start hostapd if target has unknown SSID')
config_file = os.path.abspath(os.path.join(Configuration.temp(), 'hostapd.conf'))
self.config_file = os.path.abspath(os.path.join(Configuration.temp(), 'hostapd.conf'))
with open(config_file, 'w') as config:
with open(self.config_file, 'w') as config:
config.write('driver=nl80211\n')
config.write('ssid={}\n'.format(self.target.essid))
config.write('hw_mode=g\n') # TODO: support 5ghz
# TODO: support 5ghz
config.write('hw_mode=g\n')
config.write('channel={}\n'.format(self.target.channel))
config.write('logger_syslog=-1\n')
config.write('logger_syslog_level=2\n')
return config_file
def start(self):
config_file = self.create_config_file()
self.create_config_file()
self.killall()
temp = Configuration.temp()
self.output_file = os.path.abspath(os.path.join(temp, 'hostapd.out'))
self.output_write = open(self.output_file, 'a')
self.pid = Process([
self.process_name,
'-C', config_file,
'-i', self.interface
])
self.process_name,
'-C', self.config_file,
'-i', self.interface
],
stdout=self.output_write,
cwd=temp
)
def stop(self):
if self.pid:
if self.pid and self.pid.poll() is not None:
self.pid.interrupt()
self.killall()
# TODO: Wait until hostapd is completely stopped.
if self.output_write:
self.output_write.close()
def check(self):
# TODO: Check if hostapd is still running, if there's any errors in the logs, etc.
if self.pid.poll() is not None:
# Process stopped
pass
if self.config_file and os.path.exists(self.config_file):
os.remove(self.config_file)
if self.output_file and os.path.exists(self.output_file):
os.remove(self.output_file)
def killall(self):
Process(['killall', self.process_name]).wait()
def check(self):
if self.pid.poll() is not None:
raise Exception('hostapd stopped running, exit code: %d, output: %s' % (self.pid.poll(), self.pid.stdout()))
# TODO: Check hostapd logs / output for any problems.