<commit_before># Module: eviltwin.py
# Description: Creates an EAP based access point AKA EvilTwin.
# Author: <NAME>
# Contributors: 
# Version: v 1.08252017

try:
	import os, sys, time, signal, threading
	from subprocess import Popen, PIPE
	from theme import *
except Exception as e:
	print('\n [!] Error ' +str(e))

class evilTwin(threading.Thread):
	def __init__(self, interface, ssid, channel, macaddress, \
	 certname, band, server_cert, private_key,\
	 country, state, city, company, ou, email):
		threading.Thread.__init__(self)
		self.setDaemon(0) # Creates thread in non-daemon mode
		self.interface = interface
		self.wirelessInt = str(self.interface.get_ifname())
		self.macaddress = macaddress
		self.ssid = ssid
		self.channel = channel
		self.certname = certname
		self.band = band
		self.country = country
		self.state = state
		self.city = city
		self.company = company
		self.ou = ou
		self.email = email
		self.server_cert = server_cert
		self.private_key = private_key

	def run(self):
		self.datafolders_check()
		self.sslCert(self.country, self.state, self.city, self.company, self.ou, self.certname, self.email)
		self.dependency_check()
		self.hostapd_config()
		self.sanity_check()
		time.sleep(1.5)
		p1 = Popen(['hostapd-wpe', 'data/hostapd-wpe/hostapd-wpe.conf'], stdout=PIPE)
		print('\n[i] Real-time hostapd-wpe logs below:\n')
		counter = 0
		if counter <= 5:
			for line in iter(p1.stdout.readline, ''):
				counter+=1
				sys.stdout.write(line)
				# f.write(line)
		
		# Obtain hostapd-wpe Process ID
		global eviltwin_pid
		eviltwin_pid=p1.pid
		raw_input(red('!') + 'Press Enter to quit\n\n')
		# Send the signal terminate HTTPS Serverprocess
		os.kill(os.getpgid(eviltwin_pid), signal.SIGTERM)
		# self.driver_fix()
	
	def dependency_check(self):
		'''Checks if hostapd-wpe is installed, if not installs it'''
		p1 = Popen(['which', 'hostapd-wpe'], stdout=PIPE)
		if p1.communicate()[0]:
			p2 = Popen(["dpkg-query", "-W", "-f", "${version}", "hostapd-wpe"], stdout=PIPE)
			print(blue('i')+'Running hostapd-wpe %s' % (p2.communicate()[0]))
		else:
			print(blue('i')+'Installing hostapd-wpe ...')
			p3 = Popen(['apt-get install -y hostapd-wpe'], shell=True, stdin=None, stdout=open("/dev/null", "w"), stderr=None, executable="/bin/bash")
			p3.wait()
			print(' [*] Installation completed')
			os.system('which hostapd-wpe')
			os.system('hostapd-wpe -v')
			print('\n')
			# print(p2.communicate())
	
	def datafolders_check(self):
		'''Creates Cert and or Hostapd folder(s) if missing'''
		cert_directory = 'data/certs'
		hostapd_directory = 'data/hostapd-wpe'
		if not os.path.exists(cert_directory):
			os.makedirs(cert_directory)

		if not os.path.exists(hostapd_directory):
			os.makedirs(hostapd_directory)


	def sanity_check(self):
		'''Terminates conflicting processes'''
		p1 = Popen(['airmon-ng','check','kill'], stdout=PIPE)

	def driver_fix(self):
		p1 = Popen(['iwpriv', 'wlan0', 'reset', '0'], stdout=PIPE)


	def fix_broken_package(self):
		'''Fixes broken hostapd-wpe packages'''
		os.system('apt-get purge hostapd-wpe')
		os.system('apt-get update')
		os.system('apt-get install hostapd-wpe')
		#/var/run/hostapd-wpe/wlan0
		# iwpriv wlan0 reset 0; and if that is not
		# enough, change 0 to 1

	def hostapd_config(self):
		'''Creates hostadp-wpe config file based of parameter values'''
		# Open original hostapd-wpe configuration file.
		with open('/etc/hostapd-wpe/hostapd-wpe.conf', 'r') as f1:
		    # Read a List of lines into data
		    data = f1.readlines()

		# Lines to modify in hostapd-wpe config file
		data[3] = 'interface=%s\n' % (self.wirelessInt)
		# TESTING DRIVER BUG
		data[1] = 'driver=nl80211\n'
		data[14]= 'ssid=%s\n' % (self.ssid)
		data[15]= 'channel=%s\n' % (self.channel)
		data[8] = 'server_cert=%s\n' % (self.server_cert)
		data[9] = 'private_key=%s\n' % (self.private_key)
		data[183] = 'hw_mode=%s\n' % (self.band.lower())


		print(green('*')+'EvilTwin Details:')
		print('     AP Interface: %s' % (self.wirelessInt))
		print('     SSID: %s' % (self.ssid))
		print('     Band: %s' % (self.band))
		print('     Channel: %s' % (self.channel))
		print('     Certificate Name: %s' % (self.certname))
		# print('     Server Cert Path: %s' % (self.server_cert))
		# print('     Private Key Path: %s' % (self.private_key))
		# Design Reference: 
		# Line4: # interface=wlan0
		# Line7: # eap_user_file=/etc/hostapd-wpe/hostapd-wpe.eap_user
		# Line8: # ca_cert=/etc/hostapd-wpe/certs/ca.pem
		# Line9: # server_cert=/etc/hostapd-wpe/certs/server.pem
		# Line10:# private_key=/etc/hostapd-wpe/certs/server.key
		# Line11:# private_key_passwd=<PASSWORD>
		# Line12:# dh_file=/etc/hostapd-wpe/certs/dh
		# Line15:# ssid=hostapd-wpe
		# Line16:# channel=1
		# Line184:# hw_mode=g
		
		# Save new hostapd-wpe config file in WiFiSuite's data/hostapd-wpe directory
		with open('data/hostapd-wpe/hostapd-wpe.conf', 'w') as f1:
		    f1.writelines( data )


	def sslCert(self, country, state, city, company, orgUnit, fqdn, email):
		'''Create SSL Cert with Specified Values '''
		cert = """openssl \
		req \
		-nodes \<commit_msg>Updated log path in EvilTwin<commit_after># Module: eviltwin.py
# Description: Creates an EAP based access point AKA EvilTwin.
# Author: <NAME>
# Contributors: 
# Version: v 1.08252017

try:
	import os, sys, time, signal, threading
	from subprocess import Popen, PIPE
	from theme import *
except Exception as e:
	print('\n [!] Error ' +str(e))

class evilTwin(threading.Thread):
	def __init__(self, interface, ssid, channel, macaddress, \
	 certname, band, server_cert, private_key,\
	 country, state, city, company, ou, email):
		threading.Thread.__init__(self)
		self.setDaemon(0) # Creates thread in non-daemon mode
		self.interface = interface
		self.wirelessInt = str(self.interface.get_ifname())
		self.macaddress = macaddress
		self.ssid = ssid
		self.channel = channel
		self.certname = certname
		self.band = band
		self.country = country
		self.state = state
		self.city = city
		self.company = company
		self.ou = ou
		self.email = email
		self.server_cert = server_cert
		self.private_key = private_key

	def run(self):
		self.datafolders_check()
		self.sslCert(self.country, self.state, self.city, self.company, self.ou, self.certname, self.email)
		self.dependency_check()
		self.hostapd_config()
		self.sanity_check()
		time.sleep(1.5)
		p1 = Popen(['hostapd-wpe', 'data/hostapd-wpe/hostapd-wpe.conf'], stdout=PIPE)
		print('\n[i] Real-time hostapd-wpe logs below:\n')
		counter = 0
		if counter <= 5:
			for line in iter(p1.stdout.readline, ''):
				counter+=1
				sys.stdout.write(line)
				# f.write(line)
		
		# Obtain hostapd-wpe Process ID
		global eviltwin_pid
		eviltwin_pid=p1.pid
		raw_input(red('!') + 'Press Enter to quit\n\n')
		# Send the signal terminate HTTPS Serverprocess
		os.kill(os.getpgid(eviltwin_pid), signal.SIGTERM)
		# self.driver_fix()
	
	def dependency_check(self):
		'''Checks if hostapd-wpe is installed, if not installs it'''
		p1 = Popen(['which', 'hostapd-wpe'], stdout=PIPE)
		if p1.communicate()[0]:
			p2 = Popen(["dpkg-query", "-W", "-f", "${version}", "hostapd-wpe"], stdout=PIPE)
			print(blue('i')+'Running hostapd-wpe %s' % (p2.communicate()[0]))
		else:
			print(blue('i')+'Installing hostapd-wpe ...')
			p3 = Popen(['apt-get install -y hostapd-wpe'], shell=True, stdin=None, stdout=open("/dev/null", "w"), stderr=None, executable="/bin/bash")
			p3.wait()
			print(' [*] Installation completed')
			os.system('which hostapd-wpe')
			os.system('hostapd-wpe -v')
			print('\n')
			# print(p2.communicate())
	
	def datafolders_check(self):
		'''Creates Cert and or Hostapd folder(s) if missing'''
		cert_directory = 'data/certs'
		hostapd_directory = 'data/hostapd-wpe'
		eviltwin_directory = 'data/eviltwin'
		if not os.path.exists(cert_directory):
			os.makedirs(cert_directory)
		if not os.path.exists(hostapd_directory):
			os.makedirs(hostapd_directory)
		if not os.path.exists(eviltwin_directory):
			os.makedirs(eviltwin_directory)


	def sanity_check(self):
		'''Terminates conflicting processes'''
		p1 = Popen(['airmon-ng','check','kill'], stdout=PIPE)

	def driver_fix(self):
		p1 = Popen(['iwpriv', 'wlan0', 'reset', '0'], stdout=PIPE)


	def fix_broken_package(self):
		'''Fixes broken hostapd-wpe packages'''
		os.system('apt-get purge hostapd-wpe')
		os.system('apt-get update')
		os.system('apt-get install hostapd-wpe')
		#/var/run/hostapd-wpe/wlan0
		# iwpriv wlan0 reset 0; and if that is not
		# enough, change 0 to 1

	def hostapd_config(self):
		'''Creates hostadp-wpe config file based of parameter values'''
		# Open original hostapd-wpe configuration file.
		with open('/etc/hostapd-wpe/hostapd-wpe.conf', 'r') as f1:
		    # Read a List of lines into data
		    data = f1.readlines()

		# Lines to modify in hostapd-wpe config file
		data[3] = 'interface=%s\n' % (self.wirelessInt)
		# TESTING DRIVER BUG
		data[1] = 'driver=nl80211\n'
		data[14]= 'ssid=%s\n' % (self.ssid)
		data[15]= 'channel=%s\n' % (self.channel)
		data[8] = 'server_cert=%s\n' % (self.server_cert)
		data[9] = 'private_key=%s\n' % (self.private_key)
		data[19] = 'wpe_logfile=data/eviltwin/%s_eviltwin.log\n' % (self.ssid)
		data[183] = 'hw_mode=%s\n' % (self.band.lower())


		print(green('*')+'EvilTwin Details:')
		print('     AP Interface: %s' % (self.wirelessInt))
		print('     SSID: %s' % (self.ssid))
		print('     Band: %s' % (self.band))
		print('     Channel: %s' % (self.channel))
		print('     Certificate Name: %s' % (self.certname))
		print('     Creds saved to: data/eviltwin/%s_eviltwin.log' % (self.ssid))
		# print('     Server Cert Path: %s' % (self.server_cert))
		# print('     Private Key Path: %s' % (self.private_key))
		# Design Reference: 
		# Line4: # interface=wlan0
		# Line7: # eap_user_file=/etc/hostapd-wpe/hostapd-wpe.eap_user
		# Line8: # ca_cert=/etc/hostapd-wpe/certs/ca.pem
		# Line9: # server_cert=/etc/hostapd-wpe/certs/server.pem
		# Line10:# private_key=/etc/hostapd-wpe/certs/server.key
		# Line11:# private_key_passwd=<PASSWORD>
		# Line12:# dh_file=/etc/hostapd-wpe/certs/dh
		# Line15:# ssid=hostapd-wpe
		# Line16:# channel=1
		# Line20:# wpe_logfile=somefile              # (Default: ./hostapd-wpe.log)
		# Line184:# hw_mode=g
		
		# Save new hostapd-wpe config file in WiFiSuite's data/hostapd-wpe directory
		with open('data/hostapd-wpe/hostapd-wpe.conf', 'w') as f1:
		    f1.writelines( data )


	def sslCert(self, country, state, city, company, orgUnit, fqdn, email):
		'''Create SSL Cert with Specified Values '''
		cert = """openssl \
		req \
		-nodes \
{'fraction_non_alphanumeric': 0.1599469998107136, 'fraction_numerical': 0.014575052053757335, 'mean_word_length': 3.9610328638497654, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6789718', 'n_tokens_mistral': 4478, 'n_tokens_neox': 4053, 'n_words': 867}