def InsertVariable(BotData,
                   isCapture,
                   recursive,
                   values,
                   variableName,
                   prefix="",
                   suffix="",
                   urlEncode=False,
                   createEmpty=True):
    # thisList = [ReplaceValues(prefix, bot_data) + str(v).strip() + ReplaceValues(suffix,bot_data) for v in values]
    thisList = values
    if urlEncode == False: pass

    variable = None

    if recursive:
        if len(thisList) == 0:
            if createEmpty:
                variable = CVar(variableName, thisList, isCapture)
        else:
            variable = CVar(variableName, thisList, isCapture)
    else:
        if len(thisList) == 0:
            if createEmpty:
                variable = CVar(variableName, "", isCapture)
        else:
            variable = CVar(variableName, thisList[0], isCapture)
    if variable:
        BotData.Variables.Set(variable)
    return True
Beispiel #2
0
    def __init__(self,
                 config: str,
                 data: BotData = None,
                 USER: str = None,
                 PASS: str = None,
                 output_path: str = None) -> None:

        self.blocks = []
        self.config = config
        if not data:
            self.data = BotData()
        else:
            self.data = data

        if USER:
            self.data.Variables.Set(CVar("USER", USER, False, True))
        if PASS:
            self.data.Variables.Set(CVar("PASS", PASS, False, True))

        # Current working dir
        if output_path:
            self.data.cwd = output_path
        else:
            self.data.cwd = os.getcwd()
    def Process(self, BotData):
        print(f"BLOCK: {self.block_type}, GROUP: {self.group}")

        replacedInput = ReplaceValues(self.InputString, BotData)
        if self.group == UtilityGroup.List:
            list1 = BotData.Variables.GetList(self.list_name) or []
            list2 = BotData.Variables.GetList(self.SecondListName) or []
            item = ReplaceValues(self.ListItem, BotData)
            index = int(ReplaceValues(self.ListIndex, BotData))

            if self.list_action == ListAction.Create:
                output = list1
                BotData.Variables.Set(
                    CVar(self.VariableName, output, self.isCapture))
                print(f"ACTION: {ListAction.Create}, output: {output}")

            elif self.list_action == ListAction.Length:
                output = str(len(list1))
                BotData.Variables.Set(
                    CVar(self.VariableName, output, self.isCapture))
                print(f"ACTION: {ListAction.Length}, output: {output}")

            elif self.list_action == ListAction.Join:
                output = self.Separator.join(list1)
                BotData.Variables.Set(
                    CVar(self.VariableName, output, self.isCapture))
                print(f"ACTION: {ListAction.Join}, output: {output}")

            elif self.list_action == ListAction.Sort:
                output = sorted(list1)
                if not self.Ascending:
                    output = list(reversed(output))
                BotData.Variables.Set(
                    CVar(self.VariableName, output, self.isCapture))
                print(f"ACTION: {ListAction.Sort}, output: {output}")

            elif self.list_action == ListAction.Concat:
                output = list1 + list2
                BotData.Variables.Set(
                    CVar(self.VariableName, output, self.isCapture))
                print(f"ACTION: {ListAction.Concat}, output: {output}")

            elif self.list_action == ListAction.Zip:
                output = zip(list1, list2)
                output = [f"{a}{b}" for a, b in output]
                BotData.Variables.Set(
                    CVar(self.VariableName, output, self.isCapture))
                print(f"ACTION: {ListAction.Zip}, output: {output}")

            elif self.list_action == ListAction.Map:
                output = zip(list1, list2)
                output = [{a: b} for a, b in output]
                BotData.Variables.Set(
                    CVar(self.VariableName, output, self.isCapture))
                print(f"ACTION: {ListAction.Map}, output: {output}")

            elif self.list_action == ListAction.Add:
                variable = BotData.Variables.GetWithNameAndType(
                    self.list_name, VarType.List)
                if not variable: return

                if len(variable.Value) == 0: index = 0
                elif index < 0: index += len(variable.Value)
                variable.Value.insert(index, item)

                print(f"ACTION: {ListAction.Add}, output: {variable.Value}")

            elif self.list_action == ListAction.Remove:
                variable = BotData.Variables.GetWithNameAndType(
                    self.list_name, VarType.List)
                if not variable: return

                if len(variable.Value) == 0: index = 0
                elif index < 0: index += len(variable.Value)
                variable.Value.pop(index)

                print(f"ACTION: {ListAction.Remove}, output: {variable.Value}")

            elif self.list_action == ListAction.RemoveValues:
                output = [
                    l for l in list1
                    if not Verify(ReplaceValues(l, BotData), self.
                                  ListElementComparer, self.ListComparisonTerm)
                ]
                BotData.Variables.Set(
                    CVar(self.VariableName, output, self.isCapture))
                print(f"ACTION: {ListAction.RemoveValues}, output: {output}")

            elif self.list_action == ListAction.RemoveDuplicates:
                output = list(dict.fromkeys(list1))
                BotData.Variables.Set(
                    CVar(self.VariableName, output, self.isCapture))
                print(
                    f"ACTION: {ListAction.RemoveDuplicates}, output: {output}")

            elif self.list_action == ListAction.Random:
                output = choice(list1)
                BotData.Variables.Set(
                    CVar(self.VariableName, output, self.isCapture))
                print(f"ACTION: {ListAction.Random}, output: {output}")

            elif self.list_action == ListAction.Shuffle:
                output = list1
                shuffle(output)
                BotData.Variables.Set(
                    CVar(self.VariableName, output, self.isCapture))
                print(f"ACTION: {ListAction.Shuffle}, output: {output}")

        elif self.group == UtilityGroup.Variable:

            if self.var_action == VarAction.Split:
                single = BotData.Variables.GetSingle(self.VarName)
                output = single.split(
                    ReplaceValues(self.SplitSeparator, BotData))
                BotData.Variables.Set(
                    CVar(self.VariableName, output, self.isCapture))
                print(
                    f"Executed action {self.var_action} on variable {self.VarName} with outcome {output}"
                )

        elif self.group == UtilityGroup.Conversion:
            conversionInputBytes = Conversion().ConvertFrom(
                replacedInput, self.ConversionFrom)
            conversionResult = Conversion().ConvertTo(conversionInputBytes,
                                                      self.ConversionTo)
            BotData.Variables.Set(
                CVar(self.VariableName, conversionResult, self.isCapture))
            print(
                f"Executed conversion {self.ConversionFrom} to {self.ConversionTo} on input {replacedInput} with outcome {conversionResult}"
            )

        elif self.group == UtilityGroup.File:

            file = ReplaceValues(self.FilePath, BotData)
            # If the file is not in the current dir, do nothing
            if NotInCWD(BotData.cwd, file) == True:
                print("File path is out of bounds")
                return

            if self.file_action == FileAction.Exists:
                output = os.path.isfile(file)
                BotData.Variables.Set(
                    CVar(self.VariableName, str(output), self.isCapture))

            elif self.file_action == FileAction.Read:
                try:
                    with open(file, "r", errors="ignore") as f:
                        output = f.read()
                        BotData.Variables.Set(
                            CVar(self.VariableName, str(output),
                                 self.isCapture))

                except Exception as e:
                    print(e)
                    return

            elif self.file_action == FileAction.ReadLines:
                try:
                    with open(file, "r", errors="ignore") as f:
                        output = f.readlines()
                        BotData.Variables.Set(
                            CVar(self.VariableName, output, self.isCapture))

                except Exception as e:
                    print(e)
                    return

            elif self.file_action == FileAction.Write:
                try:
                    with open(file, "w", errors="ignore") as f:
                        f.write(string_escape(replacedInput))
                except Exception as e:
                    print(e)
                    return

            elif self.file_action == FileAction.WriteLines:
                try:
                    with open(file, "w", errors="ignore") as f:
                        output = ReplaceValuesRecursive(
                            self.InputString, BotData)
                        if type(output) == str:
                            f.writelines(string_escape(output))
                        elif type(output) == list:
                            f.writelines([
                                string_escape(line) + "\n" for line in output
                            ])
                except Exception as e:
                    print(e)
                    return

            elif self.file_action == FileAction.Append:
                try:
                    with open(file, "a", errors="ignore") as f:
                        f.write(string_escape(replacedInput))

                except Exception as e:
                    print(e)
                    return

            elif self.file_action == FileAction.AppendLines:
                try:
                    with open(file, "a", errors="ignore") as f:
                        output = ReplaceValuesRecursive(
                            self.InputString, BotData)
                        if type(output) == str:
                            f.writelines(string_escape(output))
                        elif type(output) == list:
                            f.writelines([
                                string_escape(line) + "\n" for line in output
                            ])
                except Exception as e:
                    print(e)
                    return

            elif self.file_action == FileAction.Copy:
                fileCopyLocation = ReplaceValues(self.InputString, BotData)
                if NotInCWD(BotData.cwd, fileCopyLocation) == True:
                    print("File path is out of bounds")
                    return
                try:
                    copyfile(file, fileCopyLocation)
                except Exception as e:
                    print(e)
                    return

            elif self.file_action == FileAction.Move:
                fileMoveLocation = ReplaceValues(self.InputString, BotData)
                if NotInCWD(BotData.cwd, fileMoveLocation) == True:
                    print("File path is out of bounds")
                    return
                try:
                    move(file, fileMoveLocation)
                except Exception as e:
                    print(e)
                    return

            elif self.file_action == FileAction.Delete:
                if os.path.isfile(file):
                    os.remove(file)
                else:
                    return
            print(f"Executed action {self.file_action} on file {file}")
        elif self.group == UtilityGroup.Folder:
            folder = ReplaceValues(self.FolderPath, BotData)
            if NotInCWD(BotData.cwd, folder):
                print("File path is out of bounds")
                return

            if self.folder_action == FolderAction.Exists:
                output = os.path.isdir(folder)
                BotData.Variables.Set(
                    CVar(self.VariableName, str(output), self.isCapture))
                print(f"Executed action {self.folder_action} on file {folder}")

            elif self.folder_action == FolderAction.Create:
                if os.path.isdir(folder) == False:
                    os.mkdir(folder)
                    BotData.Variables.Set(
                        CVar(self.VariableName, str(folder), self.isCapture))
                    print(
                        f"Executed action {self.folder_action} on file {folder}"
                    )

            elif self.folder_action == FolderAction.Delete:
                if os.path.isdir(folder):
                    if input(
                            f"Are you sure you want to remove \"{folder}\" [y/n]: "
                    ).lower() == "y":
                        rmtree(folder)
                        print(
                            f"Executed action {self.folder_action} on file {folder}"
                        )
Beispiel #4
0
    def Process(self, BotData):
        cookies = BotData.CookiesGet()
        if cookies:
            cookies = cookies.Value
        else:
            cookies = {}
        for c in self.CustomCookies.items():
            cookies[ReplaceValues(c[0], BotData)] = cookies[ReplaceValues(
                c[1], BotData)]

        # Headers to be used for the request
        headers = {}
        for h in self.CustomHeaders.items():
            replacedKey = h[0].replace("-", "").lower()
            # Don't want brotli
            if replacedKey == "acceptencoding":
                headers["Accept"] = "*/*"
            else:
                headers[ReplaceValues(h[0],
                                      BotData)] = ReplaceValues(h[1], BotData)

        # Add the content type to headers if ContentType is not null
        if self.ContentType:
            headers["Content-Type"] = self.ContentType
        localUrl = ReplaceValues(self.Url, BotData)
        if self.RequestType == RequestType(
        ).Standard or self.RequestType == RequestType().BasicAuth:
            username = ReplaceValues(self.AuthUser, BotData)
            password = ReplaceValues(self.AuthPass, BotData)
            if self.Method in ["GET", "HEAD", "DELETE"]:
                print(f"{self.Method} {localUrl}")

                s = Session()
                if self.RequestType == RequestType().BasicAuth:
                    req = Request(self.Method,
                                  url=localUrl,
                                  headers=headers,
                                  cookies=cookies,
                                  auth=(username, password))
                else:
                    req = Request(self.Method,
                                  url=localUrl,
                                  headers=headers,
                                  cookies=cookies)
                prepped = s.prepare_request(req)
                try:
                    req = s.send(prepped,
                                 timeout=self.RequestTimeout,
                                 allow_redirects=self.AutoRedirect)
                except:
                    return

            elif self.Method in ["POST", "PUT", "PATCH"]:

                pData = ReplaceValues(self.PostData,
                                      BotData).encode("UTF-8", "replace")
                if self.encodeContent == True:
                    pData = requests.utils.quote(pData)
                print(f"{self.Method} {localUrl}")

                s = Session()
                if self.RequestType == RequestType().BasicAuth:
                    req = Request(self.Method,
                                  url=localUrl,
                                  data=pData,
                                  headers=headers,
                                  cookies=cookies,
                                  auth=(username, password))
                else:
                    req = Request(self.Method,
                                  url=localUrl,
                                  data=pData,
                                  headers=headers,
                                  cookies=cookies)
                prepped = s.prepare_request(req)
                try:
                    req = s.send(prepped,
                                 timeout=self.RequestTimeout,
                                 allow_redirects=self.AutoRedirect)
                except:
                    return

            ResponseCode = str(req.status_code)
            BotData.ResponseCodeSet(
                CVar("RESPONSECODE", ResponseCode, False, True))
            Address = str(req.url)
            BotData.ResponseSourceSet(CVar("ADDRESS", Address, False, True))
            Responce_Headers = dict(req.headers)
            BotData.ResponseHeadersSet(
                CVar("HEADERS", Responce_Headers, False, True))
            Responce_Cookies = dict(req.cookies)
            cookies = BotData.CookiesGet()
            if cookies:
                cookies = cookies.Value
            else:
                cookies = {}
            for cN, cV in Responce_Cookies.items():
                cookies[cN] = cV
            BotData.CookiesSet(CVar("COOKIES", cookies, False, True))

            if self.ResponseType == ResponseType().String:
                ResponseSource = str(req.text)
                BotData.ResponseSourceSet(
                    CVar("SOURCE", ResponseSource, False, True))
    def Process(self, BotData):

        local_url = ReplaceValues(self.url, BotData)
        request = OBRequest()
        request.Setup(self.auto_redirect)

        if self.request_type == RequestType.Standard:
            request.SetStandardContent(
                ReplaceValues(self.post_data, BotData),
                ReplaceValues(self.ContentType, BotData), self.method,
                self.encode_content)
        elif self.request_type == RequestType.BasicAuth:
            request.SetBasicAuth(ReplaceValues(self.auth_user, BotData),
                                 ReplaceValues(self.auth_pass, BotData))
        elif self.request_type == RequestType.Raw:
            request.SetRawContent(ReplaceValues(self.raw_data, BotData),
                                  ReplaceValues(self.ContentType, BotData))
        elif self.request_type == RequestType.Multipart:
            contents = []
            for m in self.multipart_contents:
                contents.append(
                    MultipartContent(Name=ReplaceValues(m.Name, BotData),
                                     Value=ReplaceValues(m.Value, BotData),
                                     ContentType=ReplaceValues(
                                         m.ContentType, BotData),
                                     Type=m.Type))
            request.SetMultipartContent(
                contents, ReplaceValues(self.multipart_boundary, BotData))

        # Set request cookies
        cookies = {}
        cookieJar = BotData.CookiesGet()
        if cookieJar:
            cookies = cookieJar.Value

        for c in self.custom_cookies.items():
            cookies[ReplaceValues(c[0], BotData)] = cookies[ReplaceValues(
                c[1], BotData)]
        request.SetCookies(cookies)

        # Set request headers
        headers = {}
        for headerName, headerValue in self.custom_headers.items():
            headers[ReplaceValues(headerName, BotData)] = ReplaceValues(
                headerValue, BotData)
        request.SetHeaders(headers, self.accept_encoding)

        try:
            (Address, ResponseCode, ResponseHeaders,
             ResponseCookies) = request.Perform(self.url, self.method)
            print(f"{self.method} {local_url}")
        except Exception as e:
            print(e)
            return

        BotData.ResponseCodeSet(CVar("RESPONSECODE", ResponseCode, False,
                                     True))
        BotData.AddressSet(CVar("ADDRESS", Address, False, True))
        BotData.ResponseHeadersSet(
            CVar("HEADERS", ResponseHeaders, False, True))

        # Add response cookies to cookie jar
        for cN, cV in ResponseCookies.items():
            cookies[cN] = cV
        BotData.CookiesSet(CVar("COOKIES", cookies, False, True))

        if self.response_type == ResponseType.String:
            ResponseSource = request.SaveString(self.read_response_source,
                                                ResponseHeaders)
            BotData.ResponseSourceSet(
                CVar("SOURCE", ResponseSource, False, True))
    def Process(self, BotData):
        localInputStrings = ReplaceValuesRecursive(self.InputString, BotData)
        outputs = []

        for localInputString in localInputStrings:
            # localInputString = localInputStrings[i]
            outputString = ""
            if self.function_type == FunctionType.Constant:
                outputString = localInputString

            elif self.function_type == FunctionType.Base64Encode:
                outputString = ToBase64(localInputString)

            elif self.function_type == FunctionType.Base64Decode:
                outputString = FromBase64(localInputString)

            elif self.function_type == FunctionType.Length:
                outputString = str(len(localInputString))

            elif self.function_type == FunctionType.ToLowercase:
                outputString = localInputString.lower()

            elif self.function_type == FunctionType.ToUppercase:
                outputString = localInputString.upper()

            elif self.function_type == FunctionType.Replace:
                if self.UseRegex:
                    outputString = re.sub(
                        ReplaceValues(self.ReplaceWhat, BotData),
                        ReplaceValues(self.ReplaceWith, BotData),
                        localInputString)
                else:
                    outputString = localInputString.replace(
                        ReplaceValues(self.ReplaceWhat, BotData),
                        ReplaceValues(self.ReplaceWith, BotData))

            elif self.function_type == FunctionType.URLEncode:
                outputString = quote(localInputString, errors="replace")

            elif self.function_type == FunctionType.URLDecode:
                outputString = unquote(localInputString)

            elif self.function_type == FunctionType.Hash:
                outputString = self.GetHash(localInputString, self.HashType,
                                            self.InputBase64).lower()

            elif self.function_type == FunctionType.HMAC:
                outputString = self.Hmac(localInputString, self.HashType,
                                         self.HmacKey, self.InputBase64,
                                         self.KeyBase64, self.HmacBase64)

            elif self.function_type == FunctionType.RandomNum:
                outputString = RandomNum(
                    ReplaceValues(self.RandomMin, BotData),
                    ReplaceValues(self.RandomMax, BotData), self.RandomZeroPad)

            elif self.function_type == FunctionType.RandomString:
                outputString = localInputString
                outputString = RandomString(outputString)

            elif self.function_type == FunctionType.CurrentUnixTime:
                outputString = str(int(time.time()))

            elif self.function_type == FunctionType.Ceil:
                outputString = str(math.ceil(float(localInputString)))

            elif self.function_type == FunctionType.Floor:
                outputString = str(math.floor(float(localInputString)))

            elif self.function_type == FunctionType.Round:
                outputString = str(round(float(localInputString)))

            elif self.function_type == FunctionType.CountOccurrences:
                outputString = str(localInputString.count(self.StringToFind))

            elif self.function_type == FunctionType.CharAt:
                outputString = str(localInputString[int(
                    ReplaceValues(self.charIndex, BotData))])

            elif self.function_type == FunctionType.ReverseString:
                charArray = list(localInputString)
                charArray.reverse()
                outputString = "".join(charArray)

            elif self.function_type == FunctionType.Substring:
                outputString = localInputString[
                    int(ReplaceValues(self.SubstringIndex, BotData)
                        ):int(ReplaceValues(self.SubstringIndex, BotData)) +
                    int(ReplaceValues(self.SubstringLength, BotData))]

            elif self.function_type == FunctionType.GetRandomUA:
                if self.UserAgentSpecifyBrowser:
                    outputString = UserAgent.ForBrowser(self.UserAgentBrowser)
                else:
                    outputString = UserAgent.Random()

            elif self.function_type == FunctionType.Trim:
                outputString = localInputString.strip()

            elif self.function_type == FunctionType.UnixTimeToDate:
                # Static dateformat because dates
                outputString = datetime.fromtimestamp(
                    int(localInputString),
                    timezone.utc).strftime("%Y-%m-%d:%H-%M-%S")

            elif self.function_type == FunctionType.PBKDF2PKCS5:
                outputString = Crypto.PBKDF2PKCS5(
                    localInputString, ReplaceValues(self.KdfSalt,
                                                    BotData), self.KdfSaltSize,
                    self.KdfIterations, self.KdfKeySize, self.KdfAlgorithm)

            elif self.function_type == FunctionType.Translate:
                outputString = localInputString
                for entryKey, entryValue in self.TranslationDictionary.items():
                    if entryKey in outputString:
                        outputString = outputString.replace(
                            entryKey, entryValue)
                        if self.StopAfterFirstMatch: break
            elif self.function_type == FunctionType.Unescape:
                outputString = Unescape(localInputString)

            elif self.function_type == FunctionType.UnixTimeToISO8601:
                outputString = datetime.fromtimestamp(
                    int(localInputString), timezone.utc).isoformat()

            elif self.function_type == FunctionType.ClearCookies:
                BotData.CookiesSet(CVar("COOKIES", {}, False, True))
            elif self.function_type == FunctionType.HTMLEntityEncode:
                outputString = escape(localInputString)
            elif self.function_type == FunctionType.HTMLEntityDecode:
                outputString = unescape(localInputString)
            else:
                pass
            outputs.append(outputString)

        print(
            f"Executed function {self.function_type} on input {localInputStrings} with outcome {outputString}"
        )
        isList = len(
            outputs
        ) > 1 or "[*]" in self.InputString or "(*)" in self.InputString or "{*}" in self.InputString
        InsertVariable(BotData, self.IsCapture, isList, outputs,
                       self.VariableName, self.CreateEmpty)