Exemplo n.º 1
0
 def send_content_to_file(self, command, content, file_out, append=True):
     if append:
         CommandRunner.run_command(command + " " + content + " >> " +
                                   file_out, silent=True)
     else:
         CommandRunner.run_command(command + " " + content + " > " +
                                   file_out, silent=True)
Exemplo n.º 2
0
 def run_hashcat_with_rule_and_source_file(self, rule, source_file):
     CommandRunner.run_command(
         self.hashcat_path + " -m " + self.hashcat_hash_option + " -w " +
         str(self.workload_profile) + " " + self.session + " " +
         self.hashes_file + " " + source_file + " -r " +
         self.hash_cat_rules_path + rule + " -o " + self.final_output_file,
         interuptable=True,
         is_add_force_flag=self.is_add_force_flag)
Exemplo n.º 3
0
 def log_actual_phase_in_output_file(self, phase):
     CommandRunner.run_command('echo ' + str(phase) +
                               ' > $HOME/.hashcat/sessions/' +
                               self.session.replace('--session ', '') +
                               '.phase', silent=True)
     CommandRunner.run_command(
         "echo Phase " + str(phase) + " starting " +
         self.return_formated_date_time() +
         " >> " + self.final_output_file, silent=True)
Exemplo n.º 4
0
    def run_child(self):
        self.log_actual_phase_in_output_file(1)

        print('Starting attack!')

        for rule in self.rules_to_run:
            self.run_hashcat_with_rule(rule)

        if self.show_when_done:
            CommandRunner.run_command("cat " + self.final_output_file)
Exemplo n.º 5
0
 def launch_hashcat(self, min_length, max_length, mask_start, mask_middle,
                    mask_end):
     for i in range(min_length, max_length + 1):
         mask_complete = mask_start
         for j in range(0, i):
             mask_complete += mask_middle
         mask_complete += mask_end
         CommandRunner.run_command(
             self.hashcat_path + " -a 3 -w 3 " + self.session + " -m " +
             self.hashcat_hash_option + " " + self.hashes_file + " " +
             mask_complete + " -o " + self.final_output_file,
             interuptable=True,
             is_add_force_flag=self.is_add_force_flag)
Exemplo n.º 6
0
    def run_child(self):
        self.log_actual_phase_in_output_file(self.phase_selection)

        if self.phase_selection < 2 or self.phase_selection > 6:
            print("Phases can only be 2 <= phase <= 6")
            exit(1)

        print('Starting mask attack! Stop anytime by pressing "CTRL+C".')
        if self.phase_selection == 2 or self.phase_selection == 5:
            mask_start = self.pure_brute_force_mask_start
            mask_middle = self.pure_brute_force_mask_middle
            if self.phase_selection == 2:
                print("\nStarting Brute force attack of 1-6 lenght passwords")
                mask_end = ""  # Leave empty so it run passwords of 1 lenght
                min_length = 0
            else:
                print("\nStarting Brute force attack of 7 lenght passwords")
                mask_end = self.pure_brute_force_mask_end
                min_length = 5
            max_length = 6
            self.launch_hashcat(min_length, max_length, mask_start,
                                mask_middle, mask_end)

        if self.phase_selection not in self.phase_to_speed_name:
            return

        print("\nStarting " + self.phase_to_speed_name[self.phase_selection] +
              " mask attack mode")
        for mask in self.masks_list:
            mask_start = self.masks_list[mask].start
            mask_middle = self.masks_list[mask].middle
            mask_end = self.masks_list[mask].end
            repeat_middle_min = self.masks_list[mask].length_groups[
                self.phase_to_speed_name[
                    self.phase_selection]]["repeat_middle_min"]
            repeat_middle_max = self.masks_list[mask].length_groups[
                self.phase_to_speed_name[
                    self.phase_selection]]["repeat_middle_max"]

            if repeat_middle_min == -1:
                print(mask + " not applicable for current phase, skipping")
            else:
                self.launch_hashcat(repeat_middle_min, repeat_middle_max,
                                    mask_start, mask_middle, mask_end)

        if self.show_when_done:
            CommandRunner.run_command("cat" + self.final_output_file)
Exemplo n.º 7
0
def print_hashcat_help_without_arguments(hashcat_path):
    help_text = CommandRunner.run_command(hashcat_path + " -h",
                                          silent=True,
                                          return_value=True)
    if not help_text:
        print("An error occured while getting hash modes. Please check your "
              "setup.")
    else:
        delimiter = '- [ Hash modes ] -'
        print(delimiter + (
            help_text.split(delimiter)[1]).split('- [ Outfile Formats ] -')[0])
Exemplo n.º 8
0
def main():
    q = [
        'iquery', '-ocsv', '-q',
        'SELECT * FROM sort(list(\'instances\'),instance_id)'
    ]
    cr = CommandRunner.CommandRunner()
    exits, outputs = cr.waitForProcesses([cr.runSubProcess(q)], True)

    if (any(exits)):
        print 'Error: could not get scidb instance data!'
        errors = outputs[0][1]
        print errors
        sys.exit(1)
    lines = [line.strip() for line in outputs[0][0].split('\n')]
    lines = [line.replace('\'', '') for line in lines if len(line) > 0]
    lines = lines[1:]
    hostList = sorted([line.split(',') for line in lines],
                      key=lambda x: int(x[2]))
    hosts = reduce(  # Scidb hosts (machines)
        lambda x, y: x + y if y[0] not in x else x, [[x[0]] for x in hostList])

    nInst = len(lines) / len(hosts)

    user = pwd.getpwuid(os.getuid())[0]

    cq = BackupHelper.CREATE_200x200_ARRAYS

    bkpFolder = r'/tmp/bkpTest'

    arrays = [a + str(i + 1) for a in ['A', 'B', 'C'] for i in range(3)]

    bh = BackupHelper.BkpTestHelper(user, hosts, nInst, bkpFolder, arrays)

    bh.createArrays(arrays, [5, 6, 7, 8], True)

    S1 = BasicRestoreTests.getParallelTestSuite(user, hosts, nInst, bh, cq)

    runner = unittest.TextTestRunner(verbosity=2)
    ret = map(runner.run, [S1])

    bh.removeArrays(arrays, True)

    if (any([len(x.errors) > 0 for x in ret])
            or any([len(x.failures) > 0 for x in ret])
            or any([x.testsRun <= 0 for x in ret])):
        print 'FAIL'
        sys.exit(1)
Exemplo n.º 9
0
 def compile(src_path, compile_config, output_dir):
     command = compile_config
     exe_path = os.path.join(output_dir, command["exe_name"])
     compiler_output = os.path.join(output_dir, 'compiler.out')
     command = command['compile_command']
     command = command.format(src_path=src_path, exe_path=exe_path)
     env = ["PATH=" + os.getenv("PATH")]
     command = command.split(" ")
     result = CommandRunner.run(exe_path=command[0],
                                args=command[1::],
                                log_path=COMPILER_LOG_PATH,
                                env=env,
                                error_path=compiler_output)
     if result['result'] == CommandRunner.SUCCESS:
         os.remove(compiler_output)
         return exe_path
     else:
         if os.path.exists(compiler_output):
             with open(compiler_output, encoding='utf-8') as f:
                 error = f.read().strip()
                 print(error)
                 os.remove(compiler_output)
                 if error:
                     raise CompileError(error)
Exemplo n.º 10
0
def main():
    q = [
        'iquery',
        '-c',
        os.environ['IQUERY_HOST'],
        '-p',
        os.environ['IQUERY_PORT'],
        '-ocsv',
        '-q',
        'SELECT * FROM sort(list(\'instances\'),instance_id)'
        ]
    cr = CommandRunner.CommandRunner()
    exits,outputs = cr.waitForProcesses(
        [cr.runSubProcess(q)],
        True
        )

    if (any(exits)):
        sys.stderr.write('Error: could not get scidb instance data!\n')
        errors = outputs[0][1]
        print errors
        sys.exit(1)
    lines = [line.strip() for line in outputs[0][0].split('\n')]
    lines = [line.replace('\'','') for line in lines if len(line) > 0]
    lines = lines[1:]
    hostList = sorted(
        [line.split(',') for line in lines],
        key=lambda line_tokens: int(line_tokens[2])
        )
    hosts = reduce( # Scidb hosts (machines)
        lambda host_list,host_sublist: host_list + host_sublist if host_sublist[0] not in host_list else host_list,
        [[host_tokens[0]] for host_tokens in hostList]
        )

    nInst = len(lines) / len(hosts)

    user = pwd.getpwuid(os.getuid())[0]

    cq = BackupHelper.CREATE_400x400_ARRAYS

    bkpFolder = r'/tmp/bkpTest'

    arrays = [
        'A3',
        'B1',
        'C2'
        ]
    tempArrays = [
        'A1',
        'A2',
        'B2',
        'B3',
        'C1',
        'C3'
        ]

    bh = BackupHelper.BkpTestHelper(
        user,
        hosts,
        nInst,
        bkpFolder,
        arrays
        )
    bh.setCreateQuery(cq)
    bh.createArrays(arrays,[5,6,7,8],backup=True)
    bh.createArrays(tempArrays,backup=True,temp=True)

    suite = tempArraySkipTests.getNonParallelTestSuite(
        user,
        hosts,
        nInst,
        bh,
        arrays,
        tempArrays,
        cq
        )

    runner = unittest.TextTestRunner(verbosity=2)
    ret = map(runner.run,[suite])

    bh.removeArrays(arrays+tempArrays,True)

    if (
        any([len(x.errors) > 0 for x in ret]) or
        any([len(x.failures) > 0 for x in ret]) or
        any([x.testsRun <= 0 for x in ret])
            ):
        sys.stderr.write('FAIL\n')
        sys.exit(1)
Exemplo n.º 11
0
import CommandRunner

CommandRunner.run(
    exe_path="/usr/bin/g++",
    args=[
        '/home/amir/Documents/Project/AOJ/new AOJ/AOJ/SandboxCommandRunner/bindings/python/tests/main.cpp',
        '-o',
        '/home/amir/Documents/Project/AOJ/new AOJ/AOJ/SandboxCommandRunner/bindings/python/tests/boom'
    ],
    env=['PATH=$PATH:/usr/bin'],
    output_path="output")
CommandRunner.run(
    exe_path=
    '/home/amir/Documents/Project/AOJ/new AOJ/AOJ/SandboxCommandRunner/bindings/python/tests/boom',
    env=['PATH=$PATH:/usr/bin'],
    output_path="output")
Exemplo n.º 12
0
def main():
    args = parse_args()

    if args.clear and args.clear > 0:
        print("Clearing all unnecessary files!")
        CommandRunner.run_command("rm -R tmp/ > /dev/null", silent=True)
        CommandRunner.run_command("rm -R outputs/ > /dev/null", silent=True)
        CommandRunner.run_command("rm -R $HOME/.hashcat > /dev/null",
                                  silent=True)
        CommandRunner.run_command("rmdir hashcat* > /dev/null", silent=True)
        CommandRunner.run_command("rm hashcat.* > /dev/null", silent=True)
        CommandRunner.run_command("rmdir hashcat* > /dev/null", silent=True)
        CommandRunner.run_command("rm hashcat.* > /dev/null", silent=True)
        CommandRunner.run_command("rm *.pyc > /dev/null", silent=True)
        CommandRunner.run_command("rm custom_list.txt > /dev/null",
                                  silent=True)
        CommandRunner.run_command("rm -R __pycache__ > /dev/null", silent=True)
        print('Done!')
        exit(0)

    if args.hashes and args.hashes > 0:
        print_hashcat_help_without_arguments(args.hashcat_path)
        exit(1)

    if args.session:
        print("Restoring from session " + args.session)
        CommandRunner.run_command(args.hashcat_path + " --session " +
                                  args.session + " --restore",
                                  interuptable=True)
        phase = CommandRunner.run_command('cat $HOME/.hashcat/sessions/' +
                                          args.session + '.phase',
                                          interuptable=False,
                                          return_value=True,
                                          silent=True)
        args.phase = int(phase)

    attacker = Attacker.SmartHCAttacker()

    attacker.session = "--session " + get_random_token(8)

    if not os.path.exists('tmp'):
        CommandRunner.run_command("mkdir tmp", silent=True)
    if not os.path.exists('outputs'):
        CommandRunner.run_command("mkdir outputs", silent=True)

    if args.workload_profile < 0 or args.workload_profile > 4:
        print("The workload profile can only be 1 <= w <= 4")
        exit(1)
    else:
        attacker.workload_profile = args.workload_profile

    if args.company_name:
        attacker.company_name = args.company_name
    if args.url:
        attacker.url = args.url
    if args.cewl_depth:
        attacker.cewl_depth = args.cewl_depth
    if args.force and args.force > 0:
        attacker.is_add_force_flag = True

    if args.hashcat_hash_type:
        attacker.hashcat_hash_option = args.hashcat_hash_type
    else:
        hash_type = '5600'
        attacker.hashcat_hash_option = hash_type
        print("Using default hash type NetNTLMv2 (-m {})".format(hash_type))

    if args.show:
        attacker.show_when_done = args.show > 0

    if args.smart_dict:
        attacker.smart_file = args.smart_dict

    if args.custom_list:
        attacker.custom_list = args.custom_list

    if not args.hash_file:
        print_error_and_usage_then_exit(
            "Hash file path (-f) needed for phase 1 to 6!")
    else:
        attacker.hashes_file = args.hash_file

    if args.phase <= 0:
        if not args.company_name:
            print_error_and_usage_then_exit(
                "Company name (-n) needed for phase 0!")
        attacker.phase_zero()

    if args.phase <= 1:
        attacker.attack_dictio()

    for i in range(2, 7):
        if (args.phase <= i):
            attacker.attack_mask(phase_selection=i)
Exemplo n.º 13
0
    def run_child(self):
        print("Sending default data to dictionary")
        self.log_actual_phase_in_output_file(0)

        self.copy_file_content_to_other_file(self.user_list,
                                             self.smart_file_temp,
                                             append=False)
        self.copy_file_content_to_other_file(self.modifier_list,
                                             self.smart_file_temp)
        self.copy_file_content_to_other_file(self.most_common_pass,
                                             self.smart_file_temp)

        if self.custom_list:
            self.copy_file_content_to_other_file(self.custom_list,
                                                 self.smart_file_temp)

        if not self.company_name:
            print("NO COMPANY NAME GIVEN (-n), WILL APPEND NOTHING TO THE "
                  "DICT FILE!!")
        else:
            self.write_text_to_file(self.company_name, self.smart_file_temp)

        self.copy_file_content_to_other_file(self.custom_dict_en,
                                             self.smart_file_temp)

        self.copy_file_content_to_other_file(self.custom_dict_fr,
                                             self.smart_file_temp)

        if self.url:
            print('Starting cewl. You can force stop anytime with ' '"CTRL+C"')
            CommandRunner.run_command("cewl -d " + self.cewl_depth +
                                      " -e -v " + self.url + " -w " +
                                      self.cewl_file_output,
                                      interuptable=True)
            self.print_date_time()
            self.copy_file_content_to_other_file(self.cewl_file_output,
                                                 self.smart_file_temp)
        else:
            print("NO URL GIVEN (-u), SKIPPING CEWL!!")

        # Append everything to finish file
        self.copy_file_content_to_other_file(self.smart_file_temp,
                                             self.smart_file)

        with open(self.smart_file_temp, "r") as file:
            lines_1 = list(set([l.strip().lower() for l in file.readlines()]))
        with open(self.user_list, "r") as file:
            lines_2 = list(set([l.strip().lower() for l in file.readlines()]))
        with open(self.modifier_list, "r") as file:
            lines_3 = list(set([l.strip().lower() for l in file.readlines()]))

        total = 2 * len(lines_1) * len(lines_2) + 2 * len(lines_1) * \
            len(lines_3) + 2 * len(lines_2) * len(lines_3) \
            + 6 * len(lines_1) * len(lines_2) * len(lines_3)
        count = 0
        self.print_date_time()
        print("Starting combinations")
        sys.stdout.write("{} / {}\r".format(count, total))
        sys.stdout.flush()
        with open(self.smart_file, "a", 10000000) as file_smart:
            for l1 in lines_1:
                for l2 in lines_2:
                    file_smart.write("{}{}\n".format(l1, l2))
                    file_smart.write("{}{}\n".format(l2, l1))
                    count += 2
                sys.stdout.write("{} / {}\r".format(count, total))
                sys.stdout.flush()
            for l1 in lines_1:
                for l3 in lines_3:
                    file_smart.write("{}{}\n".format(l1, l3))
                    file_smart.write("{}{}\n".format(l3, l1))
                    count += 2
                sys.stdout.write("{} / {}\r".format(count, total))
                sys.stdout.flush()
            for l2 in lines_2:
                for l3 in lines_3:
                    file_smart.write("{}{}\n".format(l2, l3))
                    file_smart.write("{}{}\n".format(l3, l2))
                    count += 2
                sys.stdout.write("{} / {}\r".format(count, total))
                sys.stdout.flush()
            for l1 in lines_1:
                for l2 in lines_2:
                    for l3 in lines_3:
                        file_smart.write("{}{}{}\n".format(l1, l2, l3))
                        file_smart.write("{}{}{}\n".format(l1, l3, l2))
                        file_smart.write("{}{}{}\n".format(l2, l1, l3))
                        file_smart.write("{}{}{}\n".format(l2, l3, l1))
                        file_smart.write("{}{}{}\n".format(l3, l1, l2))
                        file_smart.write("{}{}{}\n".format(l3, l2, l1))
                        count += 6
                sys.stdout.write("{} / {}\r".format(count, total))
                sys.stdout.flush()
        sys.stdout.write("{} / {}\n".format(count, total))
        sys.stdout.flush()
Exemplo n.º 14
0
    def __init__(self, protocolFile, stage, piped):

        #how are we and our protocol called?
        self.name=sys.argv[0]
        self.protocolName=protocolFile
        #parse the protocol
        self.stage=stage
        self.piped=piped
        self.MyProtocol=Protocol(protocolFile)
        self.runCmd = CommandRunner(self.MyProtocol.cmdTemplate, \
                                    self.MyProtocol.nJobs)
        
        
        #detect in what iteration we are currently at
        iterationFolders=[i.replace('ITERATION_', '') \
                          for i in glob.glob(os.path.join(self.MyProtocol.outDir,\
                                                          'ITERATION_*'))]
        #catch exception if theres no iterationfolder yet
        try:
            self.iteration=max(sorted(map(lambda f: int(f), \
                                          map(lambda f: os.path.basename(f),\
                                              iterationFolders))))
        except ValueError:
            self.iteration=1
            
        self.baseDir=os.path.join(self.MyProtocol.outDir,\
                                  "ITERATION_{}".format(self.iteration))
        if not os.path.exists(self.baseDir):
            os.mkdir(self.baseDir)
        
        self.stages=['setup', 'index', 'map', 'merge', 'prepvarcall', 'varcall', \
                'varintegration', 'reindex', 'remap', 'remerge', 'correction']
        self.stage=stage
        #try to get the next stage:
        try:
            self.nextStage=self.stages[self.stages.index(stage)+1]
        except IndexError:
            #if the stage is correction, the next one is index in next iter 
            self.nextStage='index'
        #try to get the previsprevious stage:
        try:
            previousStage=self.stages[self.stages.index(stage)-1]
        except IndexError:
            previousStage=None

        #get/create the stage specific directory
        if previousStage is not None:
            self.previousStageDir=os.path.join(self.baseDir, previousStage)
        self.stageDir=os.path.join(self.baseDir, stage)
        #if stage is setup, set the thing outside of the nestings:
        if stage=='setup':
            self.stageDir=os.path.join(self.MyProtocol.outDir, 'setup')
        #we're gonna need the path to the index directory sometimes:
        self.indexDir=os.path.join(self.baseDir, 'index')
        #create the directory for the current stage, put setup outside of nestings
        if not os.path.exists(self.stageDir):
            os.mkdir(self.stageDir)
        #if the directory exists and is not empty, we're not touching it..
        elif os.listdir(self.stageDir) != []:
            logging.critical('Stage directory {} for iteration {} exists and '\
                             'is not empty! Please check its contents and '\
                             'delete them if necessary.'.format(self.stage, self.iteration))
            logging.critical('Exiting...')
            sys.exit(1)

        
        #run stage
        if stage=="setup":
            commands, wDir=self.setup()
        elif stage=="index":
            commands, wDir=self.index(self.iteration, reindex=False, piped=self.piped)
        elif stage=="reindex":
            commands, wDir=self.index(self.iteration, reindex=True, piped=self.piped)
        elif stage=="map":
            commands, wDir=self.mapping(self.iteration, remap=False)
        elif stage=="remap":
            commands, wDir=self.mapping(self.iteration, remap=True)
        elif stage=="merge":
            commands, wDir=self.merge_bam(self.iteration, remerge=False, piped=self.piped)
        elif stage=="remerge":
            commands, wDir=self.merge_bam(self.iteration, remerge=True, piped=self.piped)
        elif stage=="prepvarcall":
            commands, wDir=self.prep_varcall(self.iteration, piped=self.piped)
        elif stage=="varcall":
            commands, wDir=self.varcall(self.iteration)
        elif stage=="varintegration":
            commands, wDir=self.varintegration(self.iteration, piped=self.piped)
        elif stage=="correction":
            commands, wDir=self.correction(self.iteration, piped=self.piped)
        else:
            #because calls to non existing stages should be filtered by argparser
            logging.warning('Man..this error should really really not occur...')
            sys.exit(1)
        
        #autocall next stage for unscattered stages
        if self.piped and wDir==None:
            nextStageCmd=self.selfCall(self.nextStage)
            commands=self.constructCommand(self.stage, commands+nextStageCmd)
            wDir=self.stageDir
        
        logging.debug("CommandRunner Returned: " + 
            str(self.runCmd(commands, wDir, self.stage )) )
        
        logging.info("Finished %s Stage: %s" % (self.runCmd.runType, self.stage))