Ejemplo n.º 1
0
    def FromLS(self,input_line):
        input_line = input_line.strip()
        line.current = input_line
        if str(input_line).startswith("!"):
            return None
        KeyChains = []
        self.KeyChains_Objects = []
        self.Dict = {}
        
        self.Dict["Booleans"] = {}
        while Lookahead(line.current) == "Boolean":
            boolean_name, boolean_value = SetBool(line.current,self)
            self.Dict["Booleans"][boolean_name] = boolean_value

        while line.current:
            EnsureIdentifier(line.current,"KEYCHAIN")
            KC = KeyChain()
            kc = {}
            kc["Keys"] = []
            KeyChainType = ParseEnum(line.current)
            
            kc["Type"] = KeyChainType
            KC.Type = KeychainType[KeyChainType]
            if kc.get("Type") == "CUSTOM" and Lookahead(line.current) == "Literal":
                kc["CustomType"] = ParseLiteral(line.current)
            # kc["Mode"] = ParseEnum(line.current)
            KC_Mode = ParseEnum(line.current)
            kc["Mode"] = KC_Mode
            KC.Mode = KeychainMode[KC_Mode]

            while line.current and line.current.startswith("KEYCHAIN") == False:
                k = {}
                Key_Object = Key()
                EnsureIdentifier(line.current,"KEY")
                first = ParseLiteral(line.current)

                if CheckIdentifier(line.current,"KEY") or CheckIdentifier(line.current,"KEYCHAIN") or not line.current:
                    Key_Object.LeftTerm = "<SOURCE>"
                    Key_Object.Comparer = Comparer["Contains"]
                    Key_Object.RightTerm = first
                    k["LeftTerm"] = "<SOURCE>"
                    k["Comparer"] = "Contains"
                    k["RightTerm"] = first
                else:
                    Key_Object.LeftTerm = first
                    k["LeftTerm"] = first
                    Comparer__ = ParseEnum(line.current)
                    k["Comparer"] = Comparer__
                    Key_Object.Comparer = Comparer[Comparer__]
                    if Key_Object.Comparer.value != "Exists" and  Key_Object.Comparer.value != "DoesNotExist":
                        RightTerm__ = ParseLiteral(line.current)
                        k["RightTerm"] = RightTerm__
                        Key_Object.RightTerm = RightTerm__
                KC.Keys.append(Key_Object)
                kc["Keys"].append(k)
            self.KeyChains_Objects.append(KC)
            KeyChains.append(kc)

        self.Dict["KeyChains"] = KeyChains
class BlockUtility:
    def __init__(self) -> None:
        self.Dict = {}
        # List
        self.list_name = None
        self.list_action = None
        # List Join
        self.Separator = None
        # List Sort
        self.Ascending = True
        # List Map
        self.SecondListName = None
        # List Add
        self.ListIndex = None
        self.ListItem = None
        # List Add, Remove
        self.ListIndex = "0"
        # List RemoveValues
        self.ListComparisonTerm = None
        self.ListElementComparer = None

        # Variable
        self.VarName = None
        self.var_action = None
        self.SplitSeparator = None

        # Conversion
        self.ConversionFrom = None
        self.ConversionTo = None

        # File
        self.file_action = None
        self.folder_action = None
        # Folder
        self.FolderPath = None

        self.InputString = ""

        # All
        self.isCapture = False
        self.group = None
        self.VariableName = ""

    def FromLS(self, line: LineParser) -> None:
        if str(line.current).startswith("!"):
            return None

        self.group = ParseEnum(line)

        if self.group == UtilityGroup.List:

            self.list_name = ParseLiteral(line)
            self.list_action = ParseEnum(line)

            if self.list_action == ListAction.Join:
                self.Separator = ParseLiteral(line)

            elif self.list_action == ListAction.Sort:
                while Lookahead(line) == "Boolean":
                    boolean_name, boolean_value = SetBool(line, self)

            elif self.list_action == ListAction.Map or \
                self.list_action == ListAction.Zip or \
                self.list_action == ListAction.Concat:
                self.SecondListName = ParseLiteral(line)

            elif self.list_action == ListAction.Add:
                self.ListItem = ParseLiteral(line)
                self.ListIndex = ParseLiteral(line)

            elif self.list_action == ListAction.Remove:
                self.ListIndex = ParseLiteral(line)

            elif self.list_action == ListAction.RemoveValues:
                self.ListElementComparer = ParseEnum(line)
                self.ListComparisonTerm = ParseLiteral(line)
            else:
                pass

        elif self.group == UtilityGroup.Variable:
            self.VarName = ParseLiteral(line)
            self.var_action = ParseEnum(line)
            if self.var_action == VarAction.Split:
                self.SplitSeparator = ParseLiteral(line)

        elif self.group == UtilityGroup.Conversion:
            self.ConversionFrom = ParseEnum(line)
            self.ConversionTo = ParseEnum(line)
            self.InputString = ParseLiteral(line)

        elif self.group == UtilityGroup.File:
            self.FilePath = ParseLiteral(line)
            self.file_action = ParseEnum(line)

            if self.file_action in [
                    FileAction.Write, FileAction.WriteLines, FileAction.Append,
                    FileAction.AppendLines, FileAction.Copy, FileAction.Move
            ]:
                self.InputString = ParseLiteral(line)

        elif self.group == UtilityGroup.Folder:
            self.FolderPath = ParseLiteral(line)
            self.folder_action = ParseEnum(line)

        # Try to parse the arrow, otherwise just return the block as is with default var name and var / cap choice
        if not ParseToken(line, "Arrow", False, True):
            return self.Dict

        # Parse the VAR / CAP
        varType = ParseToken(line, "Parameter", True, True)
        if str(varType.upper()) == "VAR" or str(varType.upper()) == "CAP":
            if str(varType.upper()) == "CAP":
                self.Dict["IsCapture"] = True
                self.isCapture = True

        # Parse the variable/capture name
        VariableName = ParseToken(line, "Literal", True, True)
        self.VariableName = VariableName

    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}"
                        )
    def FromLS(self, line: LineParser) -> None:
        if str(line.current).startswith("!"):
            return None

        self.group = ParseEnum(line)

        if self.group == UtilityGroup.List:

            self.list_name = ParseLiteral(line)
            self.list_action = ParseEnum(line)

            if self.list_action == ListAction.Join:
                self.Separator = ParseLiteral(line)

            elif self.list_action == ListAction.Sort:
                while Lookahead(line) == "Boolean":
                    boolean_name, boolean_value = SetBool(line, self)

            elif self.list_action == ListAction.Map or \
                self.list_action == ListAction.Zip or \
                self.list_action == ListAction.Concat:
                self.SecondListName = ParseLiteral(line)

            elif self.list_action == ListAction.Add:
                self.ListItem = ParseLiteral(line)
                self.ListIndex = ParseLiteral(line)

            elif self.list_action == ListAction.Remove:
                self.ListIndex = ParseLiteral(line)

            elif self.list_action == ListAction.RemoveValues:
                self.ListElementComparer = ParseEnum(line)
                self.ListComparisonTerm = ParseLiteral(line)
            else:
                pass

        elif self.group == UtilityGroup.Variable:
            self.VarName = ParseLiteral(line)
            self.var_action = ParseEnum(line)
            if self.var_action == VarAction.Split:
                self.SplitSeparator = ParseLiteral(line)

        elif self.group == UtilityGroup.Conversion:
            self.ConversionFrom = ParseEnum(line)
            self.ConversionTo = ParseEnum(line)
            self.InputString = ParseLiteral(line)

        elif self.group == UtilityGroup.File:
            self.FilePath = ParseLiteral(line)
            self.file_action = ParseEnum(line)

            if self.file_action in [
                    FileAction.Write, FileAction.WriteLines, FileAction.Append,
                    FileAction.AppendLines, FileAction.Copy, FileAction.Move
            ]:
                self.InputString = ParseLiteral(line)

        elif self.group == UtilityGroup.Folder:
            self.FolderPath = ParseLiteral(line)
            self.folder_action = ParseEnum(line)

        # Try to parse the arrow, otherwise just return the block as is with default var name and var / cap choice
        if not ParseToken(line, "Arrow", False, True):
            return self.Dict

        # Parse the VAR / CAP
        varType = ParseToken(line, "Parameter", True, True)
        if str(varType.upper()) == "VAR" or str(varType.upper()) == "CAP":
            if str(varType.upper()) == "CAP":
                self.Dict["IsCapture"] = True
                self.isCapture = True

        # Parse the variable/capture name
        VariableName = ParseToken(line, "Literal", True, True)
        self.VariableName = VariableName
Ejemplo n.º 4
0
    def FromLS(self, input_line):
        input_line = input_line.strip()
        line.current = input_line
        MultipartContents = []
        CustomHeaders = {}
        CustomCookies = {}
        ResponseType = "String"

        if str(input_line).startswith("!"):
            return None

        self.Dict = {}

        Method = ParseEnum(line.current)
        self.Dict["Method"] = Method
        self.Method = Method

        Url = ParseLiteral(line.current)
        self.Dict["Url"] = Url
        self.Url = Url

        self.Dict["Booleans"] = {}
        while Lookahead(line.current) == "Boolean":
            boolean_name, boolean_value = SetBool(line.current, self)
            self.Dict["Booleans"][boolean_name] = boolean_value

        while len(str(
                line.current)) != 0 and line.current.startswith("->") == False:
            parsed = ParseToken(line.current, "Parameter", True, True).upper()
            if parsed == "MULTIPART":
                self.Dict["RequestType"] = "Multipart"

            elif parsed == "BASICAUTH":
                self.Dict["RequestType"] = "BasicAuth"

            elif parsed == "STANDARD":
                self.Dict["RequestType"] = "Standard"
                self.RequestType = RequestType.Standard

            elif parsed == "RAW":
                self.Dict["RequestType"] = "Raw"

            elif parsed == "CONTENT":
                PostData = ParseLiteral(line.current)
                self.Dict["PostData"] = PostData
                self.PostData = PostData

            elif parsed == "RAWDATA":
                RawData = ParseLiteral(line.current)
                self.Dict["RawData"] = RawData

            elif parsed == "STRINGCONTENT":
                stringContentPair = ParseString(ParseLiteral(line.current),
                                                ':', 2)
                MultipartContents.append({
                    "Type": "STRING",
                    "Name": stringContentPair[0],
                    "Value": stringContentPair[1]
                })

            elif parsed == "FILECONTENT":
                stringContentPair = ParseString(ParseLiteral(line.current),
                                                ':', 3)
                MultipartContents.append({
                    "Type": "FILE",
                    "Name": stringContentPair[0],
                    "Value": stringContentPair[1]
                })

            elif parsed == "COOKIE":
                cookiePair = ParseString(ParseLiteral(line.current), ':', 2)
                CustomCookies[cookiePair[0]] = cookiePair[1]

            elif parsed == "HEADER":
                headerPair = ParseString(ParseLiteral(line.current), ':', 2)
                CustomHeaders[headerPair[0]] = headerPair[1]
                self.CustomHeaders[headerPair[0]] = headerPair[1]

            elif parsed == "CONTENTTYPE":
                ContentType = ParseLiteral(line.current)
                self.Dict["ContentType"] = ContentType
                self.ContentType = ContentType

            elif parsed == "USERNAME":
                AuthUser = ParseLiteral(line.current)
                self.Dict["AuthUser"] = AuthUser
                self.AuthUser = AuthUser

            elif parsed == "PASSWORD":
                AuthPass = ParseLiteral(line.current)
                self.Dict["AuthUser"] = AuthPass
                self.AuthPass = AuthPass

            elif parsed == "BOUNDARY":
                MultipartBoundary = ParseLiteral(line.current)
                self.Dict["MultipartBoundary"] = MultipartBoundary

            elif parsed == "SECPROTO":
                SecurityProtocol = ParseLiteral(line.current)
                self.Dict["SecurityProtocol"] = SecurityProtocol

            else:
                pass

        if line.current.startswith("->"):
            EnsureIdentifier(line.current, "->")
            outType = ParseToken(line.current, "Parameter", True, True)

            if outType.upper() == "STRING":
                ResponseType = "String"

            elif outType.upper() == "File":
                ResponseType = "FILE"
                DownloadPath = ParseLiteral(line.current)
                self.Dict["DownloadPath"] = DownloadPath
                while Lookahead(line.current) == "Boolean":

                    boolean_name, boolean_value = SetBool(line.current, self)
                    self.Dict["Booleans"][boolean_name] = boolean_value

            elif outType.upper() == "BASE64":
                ResponseType = "BASE64"
                OutputVariable = ParseLiteral(line.current)
                self.Dict["OutputVariable"] = OutputVariable

        self.Dict["CustomCookies"] = CustomCookies
        self.Dict["CustomHeaders"] = CustomHeaders
Ejemplo n.º 5
0
    def FromLS(self, line: LineParser):

        if str(line.current).startswith("!"):
            return None

        self.Dict = {}

        ParseTarget = ParseLiteral(line)
        self.Dict["ParseTarget"] = ParseTarget
        self.ParseTarget = ParseTarget

        parse_type = ParseEnum(line)
        self.Dict["parse_type"] = parse_type
        self.ParseType = parse_type

        if parse_type == ParseType.REGEX:
            regex_pattern = ParseLiteral(line)
            self.Dict["regex_pattern"] = regex_pattern
            self.RegexString = regex_pattern

            regex_output = ParseLiteral(line)
            self.Dict["regex_output"] = regex_output
            self.RegexOutput = regex_output

            self.Dict["Booleans"] = {}
            while Lookahead(line) == "Boolean":
                boolean_name, boolean_value = SetBool(line, self)
                self.Dict["Booleans"][boolean_name] = boolean_value

        elif parse_type == ParseType.CSS:
            CssSelector = ParseLiteral(line)
            self.CssSelector = CssSelector
            self.Dict["CssSelector"] = CssSelector

            AttributeName = ParseLiteral(line)
            self.AttributeName = AttributeName
            self.Dict["AttributeName"] = AttributeName

            if Lookahead(line) == "Boolean":
                SetBool(line, self)
            elif Lookahead(line) == "Integer":
                CssElementIndex = ParseInt(line)
                self.CssElementIndex = CssElementIndex
                self.Dict["CssElementIndex"] = CssElementIndex
            self.Dict["Booleans"] = {}
            while Lookahead(line) == "Boolean":
                boolean_name, boolean_value = SetBool(line, self)
                self.Dict["Booleans"][boolean_name] = boolean_value

        elif parse_type == ParseType.JSON:
            JsonField = ParseLiteral(line)
            self.Dict["JsonField"] = JsonField
            self.JsonField = JsonField
            self.Dict["Booleans"] = {}
            while Lookahead(line) == "Boolean":
                boolean_name, boolean_value = SetBool(line, self)
                self.Dict["Booleans"][boolean_name] = boolean_value

        elif parse_type == ParseType.LR:
            LeftString = ParseLiteral(line)
            self.Dict["LeftString"] = LeftString
            self.LeftString = LeftString
            RightString = ParseLiteral(line)
            self.RightString = RightString
            self.Dict["RightString"] = RightString
            self.Dict["Booleans"] = {}
            while Lookahead(line) == "Boolean":
                boolean_name, boolean_value = SetBool(line, self)
                self.Dict["Booleans"][boolean_name] = boolean_value

        else:
            return None

        arrow = ParseToken(line, "Arrow", True, True)

        var_type = ParseToken(line, "Parameter", True, True)

        IsCapture = False
        if str(var_type.upper()) == "VAR" or str(var_type.upper()) == "CAP":
            if str(var_type.upper()) == "CAP": IsCapture = True
        self.Dict["IsCapture"] = IsCapture
        self.IsCapture = IsCapture

        variable_name = ParseLiteral(line)
        self.Dict["variable_name"] = variable_name
        self.VariableName = variable_name

        prefix = ParseLiteral(line)
        self.Dict["prefix"] = prefix
        self.Prefix = prefix

        suffix = ParseLiteral(line)
        self.Dict["suffix"] = suffix
        self.Suffix = suffix
    def FromLS(self, line: LineParser):
        if str(line.current).startswith("!"):
            return None

        method = ParseEnum(line)
        self.method = method

        url = ParseLiteral(line)
        self.url = url

        while Lookahead(line) == "Boolean":
            boolean_name, boolean_value = SetBool(line, self)

        while len(str(
                line.current)) != 0 and line.current.startswith("->") == False:
            parsed = ParseToken(line, "Parameter", True, True).upper()
            if parsed == "MULTIPART":
                self.request_type = RequestType.Multipart

            elif parsed == "BASICAUTH":
                self.request_type = RequestType.BasicAuth

            elif parsed == "STANDARD":
                self.request_type = RequestType.Standard

            elif parsed == "RAW":
                self.request_type = RequestType.Raw

            elif parsed == "CONTENT":
                post_data = ParseLiteral(line)
                self.post_data = post_data

            elif parsed == "RAWDATA":
                raw_data = ParseLiteral(line)
                self.raw_data = raw_data

            elif parsed == "STRINGCONTENT":
                stringContentPair = ParseString(ParseLiteral(line), ':', 2)
                self.multipart_contents.append(
                    MultipartContent(stringContentPair[0],
                                     stringContentPair[1],
                                     MultipartContentType.String))

            elif parsed == "FILECONTENT":
                fileContentTriplet = ParseString(ParseLiteral(line), ':', 3)
                self.multipart_contents.append(
                    MultipartContent(fileContentTriplet[0],
                                     fileContentTriplet[1],
                                     MultipartContentType.File,
                                     fileContentTriplet[3]))

            elif parsed == "COOKIE":
                cookiePair = ParseString(ParseLiteral(line), ':', 2)
                self.custom_cookies[cookiePair[0]] = cookiePair[1]

            elif parsed == "HEADER":
                headerPair = ParseString(ParseLiteral(line), ':', 2)
                self.custom_headers[headerPair[0]] = headerPair[1]

            elif parsed == "CONTENTTYPE":
                ContentType = ParseLiteral(line)
                self.ContentType = ContentType

            elif parsed == "USERNAME":
                auth_user = ParseLiteral(line)
                self.auth_user = auth_user

            elif parsed == "PASSWORD":
                auth_pass = ParseLiteral(line)
                self.auth_pass = auth_pass

            elif parsed == "BOUNDARY":
                multipart_boundary = ParseLiteral(line)
                self.multipart_boundary = multipart_boundary

            elif parsed == "SECPROTO":
                SecurityProtocol = ParseLiteral(line)
                self.SecurityProtocol = SecurityProtocol

            else:
                pass

        if line.current.startswith("->"):
            EnsureIdentifier(line, "->")
            outType = ParseToken(line, "Parameter", True, True)

            if outType.upper() == "STRING":
                self.response_type = ResponseType.String

            elif outType.upper() == "FILE":
                self.response_type = ResponseType.File
                download_path = ParseLiteral(line)
                self.download_path = download_path
                while Lookahead(line) == "Boolean":
                    boolean_name, boolean_value = SetBool(line, self)

            elif outType.upper() == "BASE64":
                self.response_type = ResponseType.Base64String
                output_variable = ParseLiteral(line)
                self.output_variable = output_variable
Ejemplo n.º 7
0
    def FromLS(self, input_line):
        input_line = input_line.strip()

        if str(input_line).startswith("!"):
            return None
        line.current = input_line
        self.Dict = {}

        self.Dict["IsCapture"] = False

        self.Dict["VariableName"] = ""

        self.Dict["InputString"] = ""

        self.Dict["Booleans"] = {}

        FunctionType = ParseEnum(line.current)
        self.Dict["FunctionType"] = FunctionType
        self.FunctionType = FunctionType

        if FunctionType == Function().Constant:
            pass

        elif FunctionType == Function().Hash:
            HashType = ParseEnum(line.current)
            self.Dict["HashType"] = HashType
            self.HashType = HashType

            while Lookahead(line.current) == "Boolean":
                boolean_name, boolean_value = SetBool(line.current, self)
                self.Dict["Booleans"][boolean_name] = boolean_value

        elif FunctionType == Function().HMAC:
            HashType = ParseEnum(line.current)
            self.Dict["HashType"] = HashType
            self.HashType = HashType
            HmacKey = ParseLiteral(line.current)
            self.Dict["HmacKey"] = HmacKey
            self.HmacKey = HmacKey
            while Lookahead(line.current) == "Boolean":
                boolean_name, boolean_value = SetBool(line.current, self)
                self.Dict["Booleans"][boolean_name] = boolean_value

        elif FunctionType == Function().Translate:
            while Lookahead(line.current) == "Boolean":
                boolean_name, boolean_value = SetBool(line.current, self)
                self.Dict["Booleans"][boolean_name] = boolean_value
            self.Dict["TranslationDictionary"] = {}
            while line.current and Lookahead(line.current) == "Parameter":
                EnsureIdentifier(line.current, "KEY")
                k = ParseLiteral(line.current)
                EnsureIdentifier(line.current, "VALUE")
                v = ParseLiteral(line.current)
                self.Dict["TranslationDictionary"][k] = v

        elif FunctionType == Function().DateToUnixTime:
            self.Dict["DateFormat"] = ParseLiteral(line.current)

        elif FunctionType == Function().UnixTimeToDate:
            self.Dict["DateFormat"] = ParseLiteral(line.current)
            if Lookahead(line.current) != "Literal":
                self.Dict["InputString"] = "yyyy-MM-dd:HH-mm-ss"

        elif FunctionType == Function().Replace:
            ReplaceWhat = ParseLiteral(line.current)
            self.Dict["ReplaceWhat"] = ReplaceWhat
            self.ReplaceWhat = ReplaceWhat

            ReplaceWith = ParseLiteral(line.current)
            self.Dict["ReplaceWith"] = ReplaceWith
            self.ReplaceWith = ReplaceWith

            if Lookahead(line.current) == "Boolean":
                boolean_name, boolean_value = SetBool(line.current, self)
                self.Dict["Booleans"][boolean_name] = boolean_value

        elif FunctionType == Function().RegexMatch:
            self.Dict["RegexMatch"] = ParseLiteral(line.current)

        elif FunctionType == Function().RandomNum:
            if Lookahead(line.current) == "Literal":
                RandomMin = ParseLiteral(line.current)
                RandomMax = ParseLiteral(line.current)
                self.Dict["RandomMin"] = RandomMin
                self.Dict["RandomMax"] = RandomMax
                self.RandomMin = RandomMin
                self.RandomMax = RandomMax
            # Support for old integer definition of Min and Max
            else:
                RandomMin = ParseLiteral(line.current)
                RandomMax = ParseLiteral(line.current)
                self.Dict["RandomMin"] = RandomMin
                self.Dict["RandomMax"] = RandomMax
                self.RandomMin = RandomMin
                self.RandomMax = RandomMax

            if Lookahead(line.current) == "Boolean":
                boolean_name, boolean_value = SetBool(line.current, self)
                self.Dict["Booleans"][boolean_name] = boolean_value

        elif FunctionType == Function().CountOccurrences:
            self.Dict["StringToFind"] = ParseLiteral(line.current)

        elif FunctionType == Function().CharAt:
            self.Dict["CharIndex"] = ParseLiteral(line.current)

        elif FunctionType == Function().Substring:
            self.Dict["SubstringIndex"] = ParseLiteral(line.current)
            self.Dict["SubstringLength"] = ParseLiteral(line.current)

        elif FunctionType == Function().RSAEncrypt:
            self.Dict["RsaN"] = ParseLiteral(line.current)
            self.Dict["RsaE"] = ParseLiteral(line.current)
            if Lookahead(line.current) == "Boolean":
                boolean_name, boolean_value = SetBool(line.current, self)
                self.Dict["Booleans"][boolean_name] = boolean_value

        elif FunctionType == Function().RSAPKCS1PAD2:
            self.Dict["RsaN"] = ParseLiteral(line.current)
            self.Dict["RsaE"] = ParseLiteral(line.current)

        elif FunctionType == Function().GetRandomUA:
            if ParseToken(line.current, "Parameter", False,
                          False) == "BROWSER":
                EnsureIdentifier(line.current, "BROWSER")
                self.Dict["Booleans"]["UserAgentSpecifyBrowser"] = True
                self.Dict["UserAgentBrowser"] = ParseEnum(line.current)

        elif FunctionType == Function().AESDecrypt:
            pass

        elif FunctionType == Function().AESEncrypt:
            self.Dict["AesKey"] = ParseLiteral(line.current)
            self.Dict["AesIV"] = ParseLiteral(line.current)
            self.Dict["AesMode"] = ParseEnum(line.current)
            self.Dict["AesPadding"] = ParseEnum(line.current)

        elif FunctionType == Function().PBKDF2PKCS5:
            if Lookahead(line.current) == "Literal":
                self.Dict["KdfSalt"] = ParseLiteral(line.current)
            else:
                self.Dict["KdfSaltSize"] = ParseInt(line.current)
                self.Dict["KdfIterations"] = ParseInt(line.current)
                self.Dict["KdfKeySize"] = ParseInt(line.current)
                self.Dict["KdfAlgorithm"] = ParseEnum(line.current)

        else:
            pass
        if Lookahead(line.current) == "Literal":
            inputString = ParseLiteral(line.current)
            self.InputString = inputString
            self.Dict["InputString"] = inputString

        # Try to parse the arrow, otherwise just return the block as is with default var name and var / cap choice
        if not ParseToken(line.current, "Arrow", False, True):
            return self.Dict

        # Parse the VAR / CAP
        varType = ParseToken(line.current, "Parameter", True, True)
        if str(varType.upper()) == "VAR" or str(varType.upper()) == "CAP":
            if str(varType.upper()) == "CAP":
                self.Dict["IsCapture"] = True
                self.isCapture = True

        # Parse the variable/capture name
        VariableName = ParseToken(line.current, "Literal", True, True)
        self.VariableName = VariableName
        self.Dict["VariableName"] = VariableName