def choosePrefStub(self, stubs, sizePref, timePref, binaryPref, filePref, userStub=None): """ Choose a stub which is of the desired sizeRating, timeRating, and uses desired binaries. If the userStub parameter is passed, the specific stub defined by userStub is searched for and is checked to make sure it aligns with the users preferences for used binaries. :param stubs: list of Stubs to choose from :param sizePref: payload size user preference :type sizePref: int :param timePref: execution time user preference :type timePref: int :param binaryPref: list of binaries that the chosen Mutator should or should not use :type binaryPref: tuple containing a list of strs, and a bool :param userStub: the specific Stub the user chose to use :type userStub: lowercase str :returns: a :class:`bashfuscator.common.objects.Stub` object """ selStub = None if binaryPref is not None: binList = binaryPref[0] includeBinary = binaryPref[1] # attempt to find the specific stub the user wants if userStub is not None: for stub in stubs: if stub.longName == userStub: if binaryPref is not None: for binary in stub.binariesUsed: if (binary in binList) != includeBinary: printWarning( f"'{userStub}' stub contains an unwanted binary" ) if filePref is False and stub.fileWrite != filePref: printWarning(f"'{userStub}' stub preforms file writes") selStub = stub if selStub is None: printError(f"'{userStub}' stub not found") else: selStub = self.randGen.randSelect(stubs) return selStub
def checkMutatorList(self): reverseableMutator = "" nonReadableWarning = False for i, mutator in enumerate(self.mutatorList): if mutator.mutatorType == "command" and mutator.reversible: if reverseableMutator == mutator.longName: printWarning( f"{mutator.longName} used twice in a row, part of the output may be in the clear" ) reverseableMutator = "" else: reverseableMutator = mutator.longName else: reverseableMutator = ""
def checkMutatorList(self): reverseableMutator = "" nonReadableWarning = False for i, mutator in enumerate(self.mutatorList): if self.clip and ((mutator.unreadableOutput and not nonReadableWarning) or self.full_ascii_strings): printWarning("Output may consist of unreadable ASCII characters and probably won't execute from your clipboard correctly. Saving output with '-o' is recommended") nonReadableWarning = True if mutator.mutatorType == "command" and mutator.reversible: if reverseableMutator == mutator.longName: printWarning(f"{mutator.longName} used twice in a row, part of the output may be in the clear") reverseableMutator = "" else: reverseableMutator = mutator.longName else: reverseableMutator = ""
def choosePrefMutator(self, mutators, sizePref=None, timePref=None, binaryPref=None, filePref=None, prevCmdOb=None, userMutator=None, userStub=None): """ Chooses a Mutator from a list of mutators which is of the desired preferences, with a stub that uses desired binaries if appropriate. If called with the userMutator or userStub parameters, the Mutator and/or Stub specified by userMutator and/or userStub will be chosen. If those parameters are not used, a Mutator and Stub (if appropriate) will be chosen automatically based off of the values of the other parameters. :param mutators: list of Mutators to choose a Mutator from :param sizePref: payload size user preference :type sizePref: int :param timePref: execution time user preference :type timePref: int :param binaryPref: list of binaries that the chosen Mutator should or should not use :type binaryPref: tuple containing a list of strs, and a bool :param filePref: file write user preference :type filePref: bool :param prevCmdOb: the previous CommandObfuscator used. Should only be passed if a CommandObfuscator was used to generate the most recent obfuscation layer :type prevCmdOb: :class:`bashfuscator.lib.command_mutators.CommandObfuscator` :param userMutator: the specific Mutator the user chose to use :type userMutator: lowercase str :param userStub: the specific Stub the user chose to use :type userStub: lowercase str :returns: a :class:`bashfuscator.common.objects.Mutator` object """ selMutator = None if userMutator: if binaryPref: binList = binaryPref[0] includeBinary = binaryPref[1] for mutator in mutators: if mutator.longName == userMutator: if filePref is False and mutator.mutatorType != "command" and mutator.fileWrite != filePref: printWarning( f"'{userMutator}' mutator preforms file writes") elif binaryPref and mutator.mutatorType != "command": for binary in mutator.binariesUsed: if (binary in binList) != includeBinary: printWarning( f"'{userMutator}' mutator contains an unwanted binary" ) selMutator = mutator if selMutator.mutatorType == "command": selMutator.prefStubs = self.getPrefStubs( selMutator.stubs, sizePref, timePref, binaryPref, filePref) break if selMutator is None: printError(f"Selected mutator '{userMutator}' not found") else: prefMutators = self.getPrefMutators(mutators, sizePref, timePref, binaryPref, filePref, prevCmdOb) selMutator = self.randGen.randSelect(prefMutators) if selMutator is not None and selMutator.mutatorType == "command": selMutator.deobStub = self.choosePrefStub(selMutator.prefStubs, sizePref, timePref, binaryPref, filePref, userStub) if selMutator.deobStub: selMutator.deobStub.mangler = selMutator.mangler selMutator.deobStub.randGen = selMutator.mangler.randGen else: printError( f"All of '{selMutator.longName}'s Stubs do not fulfil your requirements" ) return selMutator