#!/usr/bin/env python
"""enumerator is a script built to assist in
automating the tedious task of enumerating 
a target or list of targets during a penetration
test.

@author: Maleus
@date: 2014-07-28
@author: Steve Coward (steve<at>sugarstack.io)
@version: 0.1
@notes: Built for Kali Linux, not tested on other distros. 
        The current version of enumerator requires the 
        following applications to be installed:
            - nmap
            - nikto, dirb (http enumeration)
            - hydra (ftp enumeration)
            - enum4linux (netbios enumeration)
"""
import argparse
import sys
import os
import subprocess
import multiprocessing
from multiprocessing import Process

from enumerator.lib import nmap

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='This is intended to simplify the common enumeration \
        actions taken during a pentest engagement. The only parameter required is a path to a list of \
        host IP addresses. nmap processes are then kicked off which in turn intelligently kick off ftp \
        scanning (anonymous & hydra), enum4linux, dirb or nikto processes depending on nmap service \
        enumeration output. All output is organized and saved to local folders named by IP address.')

    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('-f', '--file', metavar='file path', type=argparse.FileType(
        'r'), help='path to file of IP addresses')
    group.add_argument('-s', '--single', metavar='single IP', type=str, help='Check single IP')
    args = parser.parse_args()

    results_dir = 'results'
    ip_list = []

    if args.file:
        file_contents = args.file.read()
        
        # Handle empty file
        if file_contents == '':
            print '[!] The contents of the provided file are empty.'
            sys.exit(1)
        else:
            # Turn the file contents into a list of ip addresses
            # Trim blank lines out of list
            ip_list = file_contents.split('\n')
            ip_list = [ip for ip in ip_list if ip != '']
    elif args.single:
        ip_list = [args.single]

    print '[+] IP list parsed, sending hosts to nmap...'
    pool = multiprocessing.Pool(
        multiprocessing.cpu_count() * 2, maxtasksperchild=2)
    pool.map_async(nmap.scan, [(ip, results_dir) for ip in ip_list])
    pool.close()
    pool.join()
