Пример #1
0
import sys
import time
import uuid

from logging.config import fileConfig
from redis import Redis
from rq import use_connection, Queue
from rq.registry import StartedJobRegistry

#Setup logging
fileConfig('log_config.ini')
logger = logging.getLogger()

CRACK_CONF = crackq.conf.hc_conf()

crack_q = crackqueue.Queuer()
crack = crackq.run_hashcat.Crack()
rconf = CRACK_CONF['redis']
redis_con = Redis(rconf['host'], rconf['port'])
                               # password=rconf['password'])
q = crack_q.q_connect()
#redis_con = Redis()
log_dir = '/var/crackq/logs/'



def test_init_check():
    """Check the queue is empty first"""
    cur_list = rq.registry.StartedJobRegistry('default',
                                              connection=redis_con).get_job_ids()
    if len(cur_list) > 0:
Пример #2
0
 def runner(self,
            hash_file=None,
            hash_mode=1000,
            attack_mode=0,
            rules=None,
            mask=None,
            wordlist=None,
            session=None,
            outfile=None,
            restore=None,
            username=False,
            pot_path=None,
            show=False,
            brain=True,
            increment=False,
            increment_min=None,
            increment_max=False,
            speed=False,
            benchmark=False,
            benchmark_all=False,
            wordlist2=None):
     logger.info('Running hashcat')
     rconf = CRACK_CONF['redis']
     redis_con = Redis(rconf['host'], rconf['port'])
     redis_q = Queue(connection=redis_con)
     hc = Hashcat()
     logger.debug('Hashcat object ID: {}'.format(id(hc)))
     hc.session = session
     if benchmark:
         logger.debug('Running in benchmark mode')
         hc.benchmark = True
         if benchmark_all:
             hc.benchmark_all = True
         hc.hashcat_session_execute()
         return hc
     hc.potfile_disable = False
     hc.restore_disable = True
     hc.show = show
     if pot_path:
         hc.potfile_path = pot_path
     hc.quiet = False
     hc.optimized_kernel_enable = True
     hc.workload_profile = 4
     if username is True:
         hc.username = True
     if increment is True:
         hc.increment = True
     if increment_min:
         if isinstance(increment_min, int):
             hc.increment_min = increment_min
     if increment_max:
         if isinstance(increment_max, int):
             hc.increment_max = increment_max
     hc.hash = hash_file
     hc.attack_mode = attack_mode
     if rules:
         hc.rules = rules
         hc.rp_files_cnt = len(rules)
     hc.hash_mode = hash_mode
     if wordlist:
         hc.dict1 = wordlist
     if wordlist2:
         hc.dict2 = wordlist2
     if mask:
         hc.mask = mask
     if speed:
         hc.speed_only = True
         hc.hashcat_session_execute()
         return hc
     if brain:
         speed_q = Queue('speed_check', connection=redis_con)
         speed_session = '{}_speed'.format(session)
         job = redis_q.fetch_job(session)
         if 'brain_check' in job.meta:
             logger.debug('Restored job already has brain check state')
             speed_job = None
             if job.meta['brain_check'] is True:
                 hc.brain_client = brain
                 hc.brain_client_features = 3
                 ###***replace with random string
                 hc.brain_password = '******'
                 speed_job = None
             else:
                 speed_job = speed_q.fetch_job(speed_session)
         else:
             speed_job = speed_q.fetch_job(speed_session)
         wait_count = 0
         if speed_job:
             while len(speed_job.meta) < 1 and wait_count < 410:
                 logger.debug('RUNNER loop')
                 logger.debug('Speed meta not populated, waiting...')
                 if job:
                     if del_check(job):
                         return hc
                 if 'failed' in speed_job.get_status():
                     crack_q = crackqueue.Queuer()
                     err_msg = crack_q.error_parser(speed_job)
                     logger.error('Speed check failed: {}'.format(err_msg))
                     if job:
                         job.meta['brain_check'] = None
                         job.save_meta()
                     raise ValueError(
                         'Aborted, speed check failed: {}'.format(err_msg))
                 elif 'finished' in speed_job.get_status():
                     logger.debug(
                         'Breaking runner loop speed check job has finished'
                     )
                     if job:
                         if del_check(job):
                             return hc
                 elif 'CrackQ State' in speed_job.meta:
                     if del_check(speed_job):
                         return hc
                 time.sleep(5)
                 wait_count += 5
                 speed_job = speed_q.fetch_job(speed_session)
             logger.debug('RUNNER loop finished')
             if 'Mode Info' in speed_job.meta:
                 mode_info = speed_job.meta['Mode Info']
                 salts = mode_info[-1]
                 speed = int(mode_info[-2])
                 brain = self.brain_check(speed, salts)
                 hc.brain_client = brain
                 hc.brain_client_features = 3
                 ###***replace with random string
                 hc.brain_password = '******'
                 if brain is True:
                     if job:
                         job.meta['brain_check'] = True
                         job.save_meta()
                 if brain is False:
                     if job:
                         job.meta['brain_check'] = False
                         job.save_meta()
             else:
                 logger.error('Speed check error, disabling brain')
                 if job:
                     job.meta['brain_check'] = None
                     if not del_check(job):
                         job.meta['CrackQ State'] = 'Run/Restored'
                         job.save_meta()
         else:
             logger.error('No speed job to check')
             if job and not del_check(job):
                 job.meta['CrackQ State'] = 'Run/Restored'
                 job.save_meta()
     ###*** update this to config file path and try/except
     hc.markov_hcstat2 = "/var/crackq/files/crackq.hcstat"
     hc.custom_charset_1 = '?l?d'
     hc.custom_charset_2 = '?l?d?u'
     hc.custom_charset_3 = '?l?d?s'
     hc.outfile = outfile
     logger.debug('HC. Hashcat Rules: {}'.format(hc.rules))
     logger.debug('HC. Hashcat rp_files_cnt: {}'.format(hc.rp_files_cnt))
     if restore:
         hc.skip = int(restore)
     hc.hashcat_session_execute()
     speed_started = rq.registry.StartedJobRegistry('speed_check',
                                                    connection=redis_con)
     cur_speed = speed_started.get_job_ids()
     if len(cur_speed) > 0:
         job = redis_q.fetch_job(session)
         if job:
             if not del_check(job):
                 logger.debug(
                     'Speed job running, setting new job to Paused')
                 job.meta['CrackQ State'] = 'Pause'
                 job.save_meta()
     return hc