#!/usr/bin/env python
#
#	Author: historypeats <iam.historypeats@gmail.com>
#	Description: Convert aircrack-ng .cap files to Hashcat .hccap format
#
import os
import sys
import argparse
import subprocess
from os.path import basename
from os.path import splitext


#
# globals
#
outcap = 'out.cap'
tmpfile = 'wpa2clean.cap'


#
# functions
#
def wpaclean(infile):
	''' run wpaclean command '''
	command = 'wpaclean {0} {1}'.format(tmpfile, infile)
	p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
	out, err = p.communicate()

def aircrack(outfile):
	''' run aircrack command '''
	command = 'aircrack-ng {0} -J {1}'.format(tmpfile, outfile)
	p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
	out, err = p.communicate()

	if 'No networks found' in out:
		print 'No networks found. Exiting.'
		cleanup()
		sys.exit(-1)

def cleanup():
	''' delete tmp files '''
	os.remove(tmpfile)

def init():
	''' initialize argparse '''
	parser = argparse.ArgumentParser(description='Convert aircrack-ng .cap files to Hashcat .hccap format.')
	parser.add_argument('infile', help='the input .cap file', type=str)

	return parser.parse_args()


#
# main
#
def main():
	''' main '''
	args = init()
	infile = args.infile
	outfile = splitext(basename(infile))[0]

	wpaclean(infile)
	aircrack(outfile)
	cleanup()


if __name__ == '__main__':
	main()