Пример #1
0
        /// <inheritdoc />
        public override void Process(BotData data)
        {
            // Get easy handles
            var tcp = data.GetCustomObject("TCPClient") as TcpClient;
            var net = data.GetCustomObject("NETStream") as NetworkStream;
            var ssl = data.GetCustomObject("SSLStream") as SslStream;

            byte[] buffer   = new byte[2048];
            int    bytes    = -1;
            string response = "";

            switch (TCPCommand)
            {
            case TCPCommand.Connect:
                // Replace the Host and Port
                var h = ReplaceValues(host, data);
                var p = int.Parse(ReplaceValues(port, data));

                // Initialize the TCP client, connect to the host and get the SSL stream
                tcp = new TcpClient();
                tcp.Connect(h, p);

                if (tcp.Connected)
                {
                    net = tcp.GetStream();

                    if (UseSSL)
                    {
                        ssl = new SslStream(net);
                        ssl.AuthenticateAsClient(h);
                    }

                    if (WaitForHello)
                    {
                        // Read the stream to make sure we are connected
                        if (UseSSL)
                        {
                            bytes = ssl.Read(buffer, 0, buffer.Length);
                        }
                        else
                        {
                            bytes = net.Read(buffer, 0, buffer.Length);
                        }

                        // Save the response as ASCII in the SOURCE variable
                        response = Encoding.ASCII.GetString(buffer, 0, bytes);
                    }

                    // Save the TCP client and the streams
                    data.CustomObjects["TCPClient"] = tcp;
                    data.CustomObjects["NETStream"] = net;
                    data.CustomObjects["SSLStream"] = ssl;
                    data.CustomObjects["TCPSSL"]    = UseSSL;

                    data.Log(new LogEntry($"Succesfully connected to host {h} on port {p}. The server says:", Colors.Green));
                    data.Log(new LogEntry(response, Colors.GreenYellow));
                }

                if (VariableName != string.Empty)
                {
                    data.Variables.Set(new CVar(VariableName, response, IsCapture));
                    data.Log(new LogEntry($"Saved Response in variable {VariableName}", Colors.White));
                }
                break;

            case TCPCommand.Disconnect:
                if (tcp == null)
                {
                    throw new Exception("Make a connection first!");
                }

                tcp.Close();
                tcp = null;
                if (net != null)
                {
                    net.Close();
                }
                if (ssl != null)
                {
                    ssl.Close();
                }
                data.Log(new LogEntry($"Succesfully closed the stream", Colors.GreenYellow));
                break;

            case TCPCommand.Send:
                if (tcp == null)
                {
                    throw new Exception("Make a connection first!");
                }

                var    msg     = ReplaceValues(Message, data);
                byte[] b       = { };
                var    payload = Encoding.ASCII.GetBytes(msg.Replace(@"\r\n", "\r\n"));

                // Manual implementation of the WebSocket frame
                if (WebSocket)
                {
                    #region WebSocket
                    List <byte> bl = new List <byte>();

                    // (FIN=1) (RSV1=0) (RSV2=0) (RSV3=0) (OPCODE=0001) = 128 + 1 = 129
                    bl.Add(129);

                    ulong pllen = (ulong)payload.Length;

                    // We add 128 because the mask bit (MSB) is always 1. In this case the payload len is 7 bits long
                    if (pllen <= 125)
                    {
                        bl.Add((byte)(pllen + 128));
                    }

                    // Payload len set to 126 -> Next 2 bytes are payload len
                    else if (pllen <= ushort.MaxValue)
                    {
                        bl.Add(126 + 128);
                        bl.Add((byte)(pllen >> 8));     // Shift by 1 byte
                        bl.Add((byte)(pllen % 255));    // Take LSB
                    }

                    // Payload len set to 127 -> Next 4 bytes are payload len
                    else if (pllen <= ulong.MaxValue)
                    {
                        bl.Add(127 + 128);
                        bl.Add((byte)(pllen >> 24));         // Shift by 3 bytes
                        bl.Add((byte)((pllen >> 16) % 255)); // Shift by 2 bytes and take LSB
                        bl.Add((byte)((pllen >> 8) % 255));  // Shift by 1 byte and take LSB
                        bl.Add((byte)(pllen % 255));         // Take LSB
                    }

                    // Set the mask used for this message
                    byte[] mask = new byte[4] {
                        61, 84, 35, 6
                    };
                    bl.AddRange(mask);

                    // Finally we add the payload XORed with the mask
                    for (int i = 0; i < payload.Length; i++)
                    {
                        bl.Add((byte)(payload[i] ^ mask[i % 4]));
                    }

                    b = bl.ToArray();
                    #endregion
                }
                else
                {
                    b = payload;
                }
                data.Log(new LogEntry("> " + msg, Colors.White));

                var TCPSSL = data.GetCustomObject("TCPSSL") as bool?;
                if (TCPSSL.HasValue && TCPSSL.Value)
                {
                    ssl.Write(b);
                    bytes = ssl.Read(buffer, 0, buffer.Length);
                }
                else
                {
                    net.Write(b, 0, b.Length);
                    bytes = net.Read(buffer, 0, buffer.Length);
                }

                // Save the response as ASCII in the SOURCE variable and log it
                response = Encoding.ASCII.GetString(buffer, 0, bytes);
                data.Log(new LogEntry("> " + response, Colors.GreenYellow));

                if (VariableName != string.Empty)
                {
                    data.Variables.Set(new CVar(VariableName, response, IsCapture));
                    data.Log(new LogEntry($"Saved Response in variable {VariableName}.", Colors.White));
                }
                break;
            }
        }
Пример #2
0
 /// <summary>
 /// Executes the actual block logic.
 /// </summary>
 /// <param name="data">The BotData needed for variable replacement</param>
 public virtual void Process(BotData data)
 {
     // Clear log buffer
     data.LogBuffer.Clear();
     data.Log(new LogEntry(string.Format("<--- Executing Block {0} --->", Label), Colors.Orange));
 }
Пример #3
0
        /// <inheritdoc />
        public override void Process(BotData data)
        {
            base.Process(data);

            try
            {
                var replacedInput = ReplaceValues(inputString, data);
                switch (group)
                {
                case UtilityGroup.List:
                    var  list     = data.Variables.GetList(listName);
                    var  list2    = data.Variables.GetList(secondListName);
                    var  item     = ReplaceValues(listItem, data);
                    var  index    = int.Parse(ReplaceValues(listIndex, data));
                    CVar variable = null;

                    switch (listAction)
                    {
                    case ListAction.Create:
                        data.Variables.Set(new CVar(variableName, new List <string>(), isCapture));
                        break;

                    case ListAction.Length:
                        data.Variables.Set(new CVar(variableName, list.Count().ToString(), isCapture));
                        break;

                    case ListAction.Join:
                        data.Variables.Set(new CVar(variableName, string.Join(separator, list), isCapture));
                        break;

                    case ListAction.Sort:
                        var sorted = list.Select(e => e).ToList();         // Clone the list so we don't edit the original one
                        if (Numeric)
                        {
                            var nums = sorted.Select(e => double.Parse(e, CultureInfo.InvariantCulture)).ToList();
                            nums.Sort();
                            sorted = nums.Select(e => e.ToString()).ToList();
                        }
                        else
                        {
                            sorted.Sort();
                        }
                        if (!Ascending)
                        {
                            sorted.Reverse();
                        }
                        data.Variables.Set(new CVar(variableName, sorted, isCapture));
                        break;

                    case ListAction.Concat:
                        data.Variables.Set(new CVar(variableName, list.Concat(list2).ToList(), isCapture));
                        break;

                    case ListAction.Zip:
                        data.Variables.Set(new CVar(variableName, list.Zip(list2, (a, b) => a + b).ToList(), isCapture));
                        break;

                    case ListAction.Map:
                        data.Variables.Set(new CVar(variableName, list.Zip(list2, (k, v) => new { k, v }).ToDictionary(x => x.k, x => x.v), isCapture));
                        break;

                    case ListAction.Add:
                        // TODO: Refactor this
                        variable = data.Variables.Get(listName, CVar.VarType.List);
                        if (variable == null)
                        {
                            variable = data.GlobalVariables.Get(listName, CVar.VarType.List);
                        }
                        if (variable == null)
                        {
                            break;
                        }
                        if (variable.Value.Count == 0)
                        {
                            index = 0;
                        }
                        else if (index < 0)
                        {
                            index += variable.Value.Count;
                        }
                        variable.Value.Insert(index, item);
                        break;

                    case ListAction.Remove:
                        // TODO: Refactor this
                        variable = data.Variables.Get(listName, CVar.VarType.List);
                        if (variable == null)
                        {
                            variable = data.GlobalVariables.Get(listName, CVar.VarType.List);
                        }
                        if (variable == null)
                        {
                            break;
                        }
                        if (variable.Value.Count == 0)
                        {
                            index = 0;
                        }
                        else if (index < 0)
                        {
                            index += variable.Value.Count;
                        }
                        variable.Value.RemoveAt(index);
                        break;

                    case ListAction.RemoveValues:
                        data.Variables.Set(new CVar(variableName, list.Where(l => !Condition.Verify(new KeycheckCondition
                        {
                            Left     = ReplaceValues(l, data),
                            Comparer = ListElementComparer,
                            Right    = ListComparisonTerm
                        })).ToList(), isCapture));
                        break;

                    case ListAction.RemoveDuplicates:
                        data.Variables.Set(new CVar(variableName, list.Distinct().ToList(), isCapture));
                        break;

                    case ListAction.Random:
                        data.Variables.Set(new CVar(variableName, list[data.random.Next(list.Count)], isCapture));
                        break;

                    case ListAction.Shuffle:
                        // This makes a copy of the original list
                        var listCopy = list.ToArray().ToList();
                        listCopy.Shuffle(data.random);
                        data.Variables.Set(new CVar(variableName, listCopy, isCapture));
                        break;

                    default:
                        break;
                    }
                    data.Log(new LogEntry($"Executed action {listAction} on file {listName}", isCapture ? Colors.Tomato : Colors.Yellow));
                    break;

                case UtilityGroup.Variable:
                    string single;
                    switch (varAction)
                    {
                    case VarAction.Split:
                        single = data.Variables.GetSingle(varName);
                        data.Variables.Set(new CVar(variableName, single.Split(new string[] { ReplaceValues(splitSeparator, data) }, StringSplitOptions.None).ToList(), isCapture));
                        break;
                    }
                    data.Log(new LogEntry($"Executed action {varAction} on variable {varName}", isCapture ? Colors.Tomato : Colors.Yellow));
                    break;

                case UtilityGroup.Conversion:
                    byte[] conversionInputBytes = replacedInput.ConvertFrom(conversionFrom);
                    var    conversionResult     = conversionInputBytes.ConvertTo(conversionTo);
                    data.Variables.Set(new CVar(variableName, conversionResult, isCapture));
                    data.Log(new LogEntry($"Executed conversion {conversionFrom} to {conversionTo} on input {replacedInput} with outcome {conversionResult}", isCapture ? Colors.Tomato : Colors.Yellow));
                    break;

                case UtilityGroup.File:
                    var file = ReplaceValues(filePath, data);
                    Files.ThrowIfNotInCWD(file);

                    switch (fileAction)
                    {
                    case FileAction.Exists:
                        data.Variables.Set(new CVar(variableName, File.Exists(file).ToString(), isCapture));
                        break;

                    case FileAction.Read:
                        lock (FileLocker.GetLock(file))
                            data.Variables.Set(new CVar(variableName, File.ReadAllText(file), isCapture));
                        break;

                    case FileAction.ReadLines:
                        lock (FileLocker.GetLock(file))
                            data.Variables.Set(new CVar(variableName, File.ReadAllLines(file).ToList(), isCapture));
                        break;

                    case FileAction.Write:
                        Files.CreatePath(file);
                        lock (FileLocker.GetLock(file))
                            File.WriteAllText(file, replacedInput.Unescape());
                        break;

                    case FileAction.WriteLines:
                        Files.CreatePath(file);
                        lock (FileLocker.GetLock(file))
                            File.WriteAllLines(file, ReplaceValuesRecursive(inputString, data).Select(i => i.Unescape()));
                        break;

                    case FileAction.Append:
                        Files.CreatePath(file);
                        lock (FileLocker.GetLock(file))
                            File.AppendAllText(file, replacedInput.Unescape());
                        break;

                    case FileAction.AppendLines:
                        Files.CreatePath(file);
                        lock (FileLocker.GetLock(file))
                            File.AppendAllLines(file, ReplaceValuesRecursive(inputString, data).Select(i => i.Unescape()));
                        break;

                    case FileAction.Copy:
                        var fileCopyLocation = ReplaceValues(inputString, data);
                        Files.ThrowIfNotInCWD(fileCopyLocation);
                        Files.CreatePath(fileCopyLocation);
                        lock (FileLocker.GetLock(file))
                            lock (FileLocker.GetLock(fileCopyLocation))
                                File.Copy(file, fileCopyLocation);
                        break;

                    case FileAction.Move:
                        var fileMoveLocation = ReplaceValues(inputString, data);
                        Files.ThrowIfNotInCWD(fileMoveLocation);
                        Files.CreatePath(fileMoveLocation);
                        lock (FileLocker.GetLock(file))
                            lock (FileLocker.GetLock(fileMoveLocation))
                                File.Move(file, fileMoveLocation);
                        break;

                    case FileAction.Delete:
                        // No deletion if the file is in use (DB/OpenBullet.db cannot be deleted but instead DB/OpenBullet-BackupCopy.db)
                        // If another process is just reading the file it will be deleted
                        lock (FileLocker.GetLock(file))
                            File.Delete(file);
                        break;
                    }
                    data.Log(new LogEntry($"Executed action {fileAction} on file {file}", isCapture ? Colors.Tomato : Colors.Yellow));
                    break;

                case UtilityGroup.Folder:
                    var folder = ReplaceValues(folderPath, data);
                    Files.ThrowIfNotInCWD(folder);

                    switch (folderAction)
                    {
                    case FolderAction.Exists:
                        data.Variables.Set(new CVar(variableName, Directory.Exists(folder).ToString(), isCapture));
                        break;

                    case FolderAction.Create:
                        data.Variables.Set(new CVar(variableName, Directory.CreateDirectory(folder).ToString(), isCapture));
                        break;

                    case FolderAction.Delete:
                        // All files in the folder will be deleted expect the ones that are in use
                        // DB/OpenBullet.db cannot be deleted but instead DB/OpenBullet-BackupCopy.db
                        // If another process is just reading a file in the folder it will be deleted
                        Directory.Delete(folder, true);
                        break;
                    }
                    data.Log(new LogEntry($"Executed action {folderAction} on folder {folder}", isCapture ? Colors.Tomato : Colors.Yellow));
                    break;

                case UtilityGroup.Telegram:
                    lock (tlgLocker)
                    {
                        var bot = new TelegramBotClient(BotToken);
                        Telegram.Bot.Types.ChatId chatId = null;
                        try
                        {
                            if (ChatId.StartsWith("@"))
                            {
                                chatId = new Telegram.Bot.Types.ChatId(ChatId);
                            }
                            else
                            {
                                chatId = new Telegram.Bot.Types.ChatId(int.Parse(ChatId));
                            }
                        }
                        catch { data.Log(new LogEntry("Chat Id is invalid", Colors.Red)); }
                        var s = 0;
                        while (s < 20)
                        {
                            var result = bot.SendTextMessageAsync(chatId, ReplaceValues(string.Join("\n", Messages), data), ParseMode).Result;
                            if (result == null)
                            {
                                data.Log(new LogEntry("Message sent not successfully!!", Colors.Red));
                                data.Log(new LogEntry("Sleep 100ms", Colors.Yellow));
                                Thread.Sleep(100);
                                s++;
                                continue;
                            }
                            data.Log(new LogEntry("Message sent successfully!", Colors.LimeGreen));
                            break;
                        }
                    }
                    break;

                default:
                    break;
                }
            }
            catch (Exception ex) { data.Log(new LogEntry(ex.Message, Colors.Tomato)); if (ex.InnerException != null)
                                   {
                                       data.Log(new LogEntry(ex.InnerException.Message, Colors.Tomato));
                                   }
            }
        }
Пример #4
0
        /// <inheritdoc />
        public override void Process(BotData data)
        {
            base.Process(data);

            // If the clearance info is already set and we're not getting it fresh each time, skip
            if (data.UseProxies)
            {
                if (data.Proxy.Clearance != "" && !data.GlobalSettings.Proxies.AlwaysGetClearance)
                {
                    data.Log(new LogEntry("Skipping CF Bypass because there is already a valid cookie", Colors.White));
                    data.Cookies.Add("cf_clearance", data.Proxy.Clearance);
                    return;
                }
            }

            var localUrl = ReplaceValues(url, data);
            var uri      = new Uri(localUrl);

            var timeout = data.GlobalSettings.General.RequestTimeout * 1000;

            // We initialize the solver basing on the captcha service available
            CloudflareSolver cf = null;

            switch (data.GlobalSettings.Captchas.CurrentService)
            {
            case BlockCaptcha.CaptchaService.AntiCaptcha:
                cf = new CloudflareSolver(new AntiCaptchaProvider(data.GlobalSettings.Captchas.AntiCapToken));
                break;

            case BlockCaptcha.CaptchaService.TwoCaptcha:
                cf = new CloudflareSolver(new TwoCaptchaProvider(data.GlobalSettings.Captchas.TwoCapToken));
                break;

            default:
                cf = new CloudflareSolver();
                break;
            }

            // Initialize the handler with the Proxy and the previous cookies
            HttpClientHandler handler = null;

            CookieContainer cookies = new CookieContainer();

            foreach (var cookie in data.Cookies)
            {
                cookies.Add(new Cookie(cookie.Key, cookie.Value, "/", uri.Host));
            }

            if (data.UseProxies)
            {
                if (data.Proxy.Type != Extreme.Net.ProxyType.Http)
                {
                    throw new Exception($"The proxy type {data.Proxy.Type} is not supported by this block yet");
                }

                var proxy = new WebProxy(data.Proxy.Proxy, false);

                if (data.Proxy.Username != "")
                {
                    proxy.Credentials = new NetworkCredential(data.Proxy.Username, data.Proxy.Password);
                }

                handler = new HttpClientHandler()
                {
                    Proxy                  = proxy,
                    CookieContainer        = cookies,
                    AllowAutoRedirect      = true,
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
                };
            }
            else
            {
                handler = new HttpClientHandler()
                {
                    CookieContainer        = cookies,
                    AllowAutoRedirect      = true,
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
                };
            }

            // Initialize the HttpClient with the given handler, timeout, user-agent
            var httpClient = new HttpClient(handler);

            httpClient.Timeout = TimeSpan.FromMinutes(timeout);
            httpClient.DefaultRequestHeaders.Add("User-Agent", ReplaceValues(userAgent, data));

            // Solve the CF challenge
            var result = cf.Solve(httpClient, handler, uri).Result;

            if (result.Success)
            {
                data.Log(new LogEntry($"[Success] Protection bypassed: {result.DetectResult.Protection}", Colors.GreenYellow));
            }
            else if (result.DetectResult.Protection == CloudflareProtection.Unknown)
            {
                data.Log(new LogEntry("Unknown protection, skipping the bypass!", Colors.Tomato));
            }
            else
            {
                throw new Exception($"CF Bypass Failed: {result.FailReason}");
            }

            // Once the protection has been bypassed we can use that httpClient to send the requests as usual
            var response = httpClient.GetAsync(uri).Result;

            // Save the cookies
            var ck = cookies.GetCookies(uri);

            var clearance = "";
            var cfduid    = "";

            try
            {
                clearance = ck["cf_clearance"].Value;
                cfduid    = ck["__cfduid"].Value;
            }
            catch { }

            if (data.UseProxies)
            {
                data.Proxy.Clearance = clearance;
                data.Proxy.Cfduid    = cfduid;
            }

            if (clearance != "")
            {
                data.Log(new LogEntry("Got Cloudflare clearance!", Colors.GreenYellow));
                data.Log(new LogEntry(clearance + Environment.NewLine + cfduid + Environment.NewLine, Colors.White));
            }

            // Get code
            data.ResponseCode = ((int)response.StatusCode).ToString();
            data.Log(new LogEntry("Response code: " + data.ResponseCode, Colors.Cyan));

            // Get headers
            data.Log(new LogEntry("Received headers:", Colors.DeepPink));
            var headerList      = new List <KeyValuePair <string, string> >();
            var receivedHeaders = response.Headers.GetEnumerator();

            data.ResponseHeaders.Clear();

            while (receivedHeaders.MoveNext())
            {
                var header = receivedHeaders.Current;
                var value  = header.Value.GetEnumerator();
                if (value.MoveNext())
                {
                    data.ResponseHeaders.Add(header.Key, value.Current);
                    data.Log(new LogEntry(header.Key + ": " + value.Current, Colors.LightPink));
                }
            }

            // Get cookies
            data.Log(new LogEntry("Received cookies:", Colors.Goldenrod));
            foreach (Cookie cookie in ck)
            {
                if (data.Cookies.ContainsKey(cookie.Name))
                {
                    data.Cookies[cookie.Name] = cookie.Value;
                }
                else
                {
                    data.Cookies.Add(cookie.Name, cookie.Value);
                }
                data.Log(new LogEntry(cookie.Name + ": " + cookie.Value, Colors.LightGoldenrodYellow));
            }

            // Save the content
            data.ResponseSource = response.Content.ReadAsStringAsync().Result;
            data.Log(new LogEntry("Response Source:", Colors.Green));
            data.Log(new LogEntry(data.ResponseSource, Colors.GreenYellow));
        }
Пример #5
0
        /// <summary>
        /// Replaces variables in a given input string.
        /// </summary>
        /// <param name="input">The string to replace variables into</param>
        /// <param name="data">The BotData needed for variable replacement</param>
        /// <returns>The string where variables have been replaced</returns>
        public static string ReplaceValues(string input, BotData data)
        {
            if (!input.Contains("<") && !input.Contains(">"))
            {
                return(input);
            }

            var previous = "";
            var output   = input;

            do
            {
                previous = output;

                // Replace all the fixed quantities (this needs to go away inside BotData.cs, initialized as hidden vars)
                output = output.Replace("<INPUT>", data.Data.Data);
                output = output.Replace("<STATUS>", data.Status.ToString());
                output = output.Replace("<BOTNUM>", data.BotNumber.ToString());
                output = output.Replace("<RETRIES>", data.Data.Retries.ToString());
                if (data.Proxy != null)
                {
                    output = output.Replace("<PROXY>", data.Proxy.Proxy);
                }

                // Get all the inner (max. 1 level of nesting) variables
                var matches = Regex.Matches(output, @"<([^<>]*)>");

                foreach (Match match in matches)
                {
                    var full = match.Groups[0].Value;
                    var m    = match.Groups[1].Value;

                    // Parse the variable name
                    var name = Regex.Match(m, @"^[^\[\{\(]*").Value;

                    // Try to get the variable (first local, then global, then if none was found go to the next iteration)
                    // We don't throw an error here because it could be some HTML or XML code e.g. <br> that triggers this, and we dont' want to spam the user with unneeded errors
                    var v = data.Variables.Get(name);
                    if (v == null)
                    {
                        v = data.GlobalVariables.Get(name);
                    }
                    if (v == null)
                    {
                        continue;
                    }

                    // Parse the arguments
                    var args = m.Replace(name, "");

                    switch (v.Type)
                    {
                    case CVar.VarType.Single:
                        output = output.Replace(full, v.Value);
                        break;

                    case CVar.VarType.List:

                        // If it's just the list name, replace it with its string representation
                        if (string.IsNullOrEmpty(args))
                        {
                            output = output.Replace(full, v.ToString());
                            break;
                        }

                        var index = 0;
                        int.TryParse(ParseArguments(args, '[', ']')[0], out index);
                        var item = v.GetListItem(index);     // Can return null
                        if (item != null)
                        {
                            output = output.Replace(full, item);
                        }
                        break;

                    case CVar.VarType.Dictionary:

                        if (args.Contains("(") && args.Contains(")"))
                        {
                            var dicKey = ParseArguments(args, '(', ')')[0];
                            try { output = output.Replace(full, v.GetDictValue(dicKey)); } catch { }
                        }
                        else if (args.Contains("{") && args.Contains("}"))
                        {
                            var dicVal = ParseArguments(args, '{', '}')[0];
                            try { output = output.Replace(full, v.GetDictKey(dicVal)); } catch { }
                        }
                        else     // If it's just the dictionary name, replace it with its string representation
                        {
                            output = output.Replace(full, v.ToString());
                            break;
                        }
                        break;
                    }
                }
            }while (input.Contains("<") && input.Contains(">") && output != previous);

            return(output);
        }
Пример #6
0
        /// <inheritdoc />
        public override void Process(BotData data)
        {
            base.Process(data);

            try
            {
                switch (group)
                {
                case UtilityGroup.List:

                    var  list     = data.Variables.GetList(listName);
                    var  list2    = data.Variables.GetList(secondListName);
                    var  item     = ReplaceValues(listItem, data);
                    var  index    = int.Parse(ReplaceValues(listIndex, data));
                    CVar variable = null;

                    switch (listAction)
                    {
                    case ListAction.Create:
                        data.Variables.Set(new CVar(variableName, new List <string>(), isCapture));
                        break;

                    case ListAction.Join:
                        data.Variables.Set(new CVar(variableName, string.Join(separator, list), isCapture));
                        break;

                    case ListAction.Concat:
                        data.Variables.Set(new CVar(variableName, list.Concat(list2).ToList(), isCapture));
                        break;

                    case ListAction.Zip:
                        data.Variables.Set(new CVar(variableName, list.Zip(list2, (a, b) => a + b).ToList(), isCapture));
                        break;

                    case ListAction.Map:
                        data.Variables.Set(new CVar(variableName, list.Zip(list2, (k, v) => new { k, v }).ToDictionary(x => x.k, x => x.v), isCapture));
                        break;

                    case ListAction.Add:
                        variable = data.Variables.Get(listName, CVar.VarType.List);
                        if (variable == null)
                        {
                            variable = data.GlobalVariables.Get(listName, CVar.VarType.List);
                        }
                        if (variable == null)
                        {
                            break;
                        }
                        if (variable.Value.Count == 0)
                        {
                            index = 0;
                        }
                        else if (index < 0)
                        {
                            index += variable.Value.Count;
                        }
                        variable.Value.Insert(index, item);
                        break;

                    case ListAction.Remove:
                        variable = data.Variables.Get(listName, CVar.VarType.List);
                        if (variable == null)
                        {
                            variable = data.GlobalVariables.Get(listName, CVar.VarType.List);
                        }
                        if (variable == null)
                        {
                            break;
                        }
                        if (variable.Value.Count == 0)
                        {
                            index = 0;
                        }
                        else if (index < 0)
                        {
                            index += variable.Value.Count;
                        }
                        variable.Value.RemoveAt(index);
                        break;

                    case ListAction.Random:
                        data.Variables.Set(new CVar(variableName, list[data.rand.Next(list.Count)], isCapture));
                        break;

                    default:
                        break;
                    }

                    data.Log(new LogEntry(string.Format("Executed action {0} on list {1}", listAction, listName), Colors.White));
                    break;

                case UtilityGroup.Variable:
                    string single;
                    switch (varAction)
                    {
                    case VarAction.Split:
                        single = data.Variables.GetSingle(varName);
                        data.Variables.Set(new CVar(variableName, single.Split(new string[] { ReplaceValues(separator, data) }, StringSplitOptions.None).ToList(), isCapture));
                        break;
                    }
                    data.Log(new LogEntry(string.Format("Executed action {0} on variable {1}", varAction, varName), Colors.White));
                    break;

                case UtilityGroup.Conversion:
                    byte[] convertedBytes = ConvertFrom(ReplaceValues(inputString, data), conversionFrom);
                    data.Variables.Set(new CVar(variableName, ConvertTo(convertedBytes, conversionTo), isCapture));
                    data.Log(new LogEntry(string.Format("Converted input from {0} to {1}", conversionFrom, conversionTo), Colors.White));
                    break;

                case UtilityGroup.File:
                    var file   = ReplaceValues(filePath, data);
                    var input  = ReplaceValues(inputString, data).Replace("\\r\\n", "\r\n").Replace("\\n", "\n");
                    var inputs = ReplaceValuesRecursive(inputString, data);
                    switch (fileAction)
                    {
                    case FileAction.Read:
                        data.Variables.Set(new CVar(variableName, File.ReadAllText(file), isCapture));
                        break;

                    case FileAction.ReadLines:
                        data.Variables.Set(new CVar(variableName, File.ReadAllLines(file).ToList(), isCapture));
                        break;

                    case FileAction.Write:
                        File.WriteAllText(file, input);
                        break;

                    case FileAction.WriteLines:
                        File.WriteAllLines(file, inputs);
                        break;

                    case FileAction.Append:
                        File.AppendAllText(file, input);
                        break;

                    case FileAction.AppendLines:
                        File.AppendAllLines(file, inputs);
                        break;
                    }
                    break;

                default:
                    break;
                }
            }
            catch (Exception ex) { data.Log(new LogEntry(ex.Message, Colors.Tomato)); }
        }
Пример #7
0
        /// <inheritdoc />
        public override void Process(BotData data)
        {
            base.Process(data);

            try
            {
                switch (group)
                {
                case UtilityGroup.List:

                    var  list     = data.Variables.GetList(listName);
                    var  list2    = data.Variables.GetList(secondListName);
                    var  item     = ReplaceValues(listItem, data);
                    var  index    = int.Parse(ReplaceValues(listIndex, data));
                    CVar variable = null;

                    switch (listAction)
                    {
                    case ListAction.Create:
                        data.Variables.Set(new CVar(variableName, new List <string>(), isCapture));
                        break;

                    case ListAction.Length:
                        data.Variables.Set(new CVar(variableName, list.Count().ToString(), isCapture));
                        break;

                    case ListAction.Join:
                        data.Variables.Set(new CVar(variableName, string.Join(separator, list), isCapture));
                        break;

                    case ListAction.Sort:
                        var sorted = list.Select(e => e).ToList();         // Clone the list so we don't edit the original one
                        if (Numeric)
                        {
                            var nums = sorted.Select(e => double.Parse(e, CultureInfo.InvariantCulture)).ToList();
                            nums.Sort();
                            sorted = nums.Select(e => e.ToString()).ToList();
                        }
                        else
                        {
                            sorted.Sort();
                        }
                        if (!Ascending)
                        {
                            sorted.Reverse();
                        }
                        data.Variables.Set(new CVar(variableName, sorted, isCapture));
                        break;

                    case ListAction.Concat:
                        data.Variables.Set(new CVar(variableName, list.Concat(list2).ToList(), isCapture));
                        break;

                    case ListAction.Zip:
                        data.Variables.Set(new CVar(variableName, list.Zip(list2, (a, b) => a + b).ToList(), isCapture));
                        break;

                    case ListAction.Map:
                        data.Variables.Set(new CVar(variableName, list.Zip(list2, (k, v) => new { k, v }).ToDictionary(x => x.k, x => x.v), isCapture));
                        break;

                    case ListAction.Add:
                        // TODO: Refactor this
                        variable = data.Variables.Get(listName, CVar.VarType.List);
                        if (variable == null)
                        {
                            variable = data.GlobalVariables.Get(listName, CVar.VarType.List);
                        }
                        if (variable == null)
                        {
                            break;
                        }
                        if (variable.Value.Count == 0)
                        {
                            index = 0;
                        }
                        else if (index < 0)
                        {
                            index += variable.Value.Count;
                        }
                        variable.Value.Insert(index, item);
                        break;

                    case ListAction.Remove:
                        // TODO: Refactor this
                        variable = data.Variables.Get(listName, CVar.VarType.List);
                        if (variable == null)
                        {
                            variable = data.GlobalVariables.Get(listName, CVar.VarType.List);
                        }
                        if (variable == null)
                        {
                            break;
                        }
                        if (variable.Value.Count == 0)
                        {
                            index = 0;
                        }
                        else if (index < 0)
                        {
                            index += variable.Value.Count;
                        }
                        variable.Value.RemoveAt(index);
                        break;

                    case ListAction.RemoveValues:
                        data.Variables.Set(new CVar(variableName, list.Where(l => !Condition.Verify(new KeycheckCondition
                        {
                            Left     = ReplaceValues(l, data),
                            Comparer = ListElementComparer,
                            Right    = ListComparisonTerm
                        })).ToList(), isCapture));
                        break;

                    case ListAction.RemoveDuplicates:
                        data.Variables.Set(new CVar(variableName, list.Distinct().ToList(), isCapture));
                        break;

                    case ListAction.Random:
                        data.Variables.Set(new CVar(variableName, list[data.random.Next(list.Count)], isCapture));
                        break;

                    case ListAction.Shuffle:
                        // This makes a copy of the original list
                        var listCopy = list.ToArray().ToList();
                        listCopy.Shuffle(data.random);
                        data.Variables.Set(new CVar(variableName, listCopy, isCapture));
                        break;

                    default:
                        break;
                    }
                    data.Log(new LogEntry($"Executed action {listAction} on file {listName}", Colors.White));
                    break;

                case UtilityGroup.Variable:
                    string single;
                    switch (varAction)
                    {
                    case VarAction.Split:
                        single = data.Variables.GetSingle(varName);
                        data.Variables.Set(new CVar(variableName, single.Split(new string[] { ReplaceValues(splitSeparator, data) }, StringSplitOptions.None).ToList(), isCapture));
                        break;
                    }
                    data.Log(new LogEntry($"Executed action {varAction} on variable {varName}", Colors.White));
                    break;

                case UtilityGroup.Conversion:
                    byte[] convertedBytes = ReplaceValues(inputString, data).ConvertFrom(conversionFrom);
                    data.Variables.Set(new CVar(variableName, convertedBytes.ConvertTo(conversionTo), isCapture));
                    data.Log(new LogEntry($"Converted input {conversionFrom} to {conversionTo}", Colors.White));
                    break;

                case UtilityGroup.File:
                    var file   = ReplaceValues(filePath, data);
                    var input  = ReplaceValues(inputString, data).Replace("\\r\\n", "\r\n").Replace("\\n", "\n");
                    var inputs = ReplaceValuesRecursive(inputString, data).Select(i => i.Replace("\\r\\n", "\r\n").Replace("\\n", "\n"));
                    switch (fileAction)
                    {
                    case FileAction.Read:
                        data.Variables.Set(new CVar(variableName, File.ReadAllText(file), isCapture));
                        break;

                    case FileAction.ReadLines:
                        data.Variables.Set(new CVar(variableName, File.ReadAllLines(file).ToList(), isCapture));
                        break;

                    case FileAction.Write:
                        File.WriteAllText(file, input);
                        break;

                    case FileAction.WriteLines:
                        File.WriteAllLines(file, inputs);
                        break;

                    case FileAction.Append:
                        File.AppendAllText(file, input);
                        break;

                    case FileAction.AppendLines:
                        File.AppendAllLines(file, inputs);
                        break;
                    }
                    data.Log(new LogEntry($"Executed action {fileAction} on file {file}", Colors.White));
                    break;

                default:
                    break;
                }
            }
            catch (Exception ex) { data.Log(new LogEntry(ex.Message, Colors.Tomato)); }
        }
Пример #8
0
        private List <string> Parse(BotData data)
        {
            var original = ReplaceValues(parseTarget, data);
            var partial  = original;
            var list     = new List <string>();

            // Parse the value
            switch (Type)
            {
            case ParseType.LR:
                var ls    = ReplaceValues(leftString, data);
                var rs    = ReplaceValues(rightString, data);
                var pFrom = 0;
                var pTo   = 0;

                // No L and R = return full input
                if (ls == "" && rs == "")
                {
                    list.Add(original);
                    break;
                }

                // L or R not present and not empty
                else if (((!partial.Contains(ls) && ls != "") || (!partial.Contains(rs) && rs != "")))
                {
                    list.Add("");
                    break;
                }

                // Instead of the mess below, we could simply use Extreme.NET's Substring extensions
                // return original.Substrings(ls, rs); // Recursive
                // return original.Substring(ls, rs); // Not recursive

                if (recursive)
                {
                    if (useRegexLR)
                    {
                        try
                        {
                            var             pattern = BuildLRPattern(ls, rs);
                            MatchCollection mc      = Regex.Matches(partial, pattern);
                            foreach (Match m in mc)
                            {
                                list.Add(m.Value);
                            }
                        }
                        catch { }
                    }
                    else
                    {
                        try
                        {
                            while ((partial.Contains(ls) || ls == "") && (partial.Contains(rs) || rs == ""))
                            {
                                // Search for left delimiter and Calculate offset
                                pFrom = ls == "" ? 0 : partial.IndexOf(ls) + ls.Length;
                                // Move right of offset
                                partial = partial.Substring(pFrom);
                                // Search for right delimiter and Calculate length to parse
                                pTo = rs == "" ? partial.Length : partial.IndexOf(rs);
                                // Parse it
                                var parsed = partial.Substring(0, pTo);
                                list.Add(parsed);
                                // Move right of parsed + right
                                partial = partial.Substring(parsed.Length + rs.Length);
                            }
                        }
                        catch { }
                    }
                }

                // Non-recursive
                else
                {
                    if (useRegexLR)
                    {
                        var             pattern = BuildLRPattern(ls, rs);
                        MatchCollection mc      = Regex.Matches(partial, pattern);
                        if (mc.Count > 0)
                        {
                            list.Add(mc[0].Value);
                        }
                    }
                    else
                    {
                        try
                        {
                            pFrom   = ls == "" ? 0 : partial.IndexOf(ls) + ls.Length;
                            partial = partial.Substring(pFrom);
                            pTo     = rs == "" ? (partial.Length - 1) : partial.IndexOf(rs);
                            list.Add(partial.Substring(0, pTo));
                        }
                        catch { }
                    }
                }

                break;

            case ParseType.CSS:

                HtmlParser parser = new HtmlParser();
                AngleSharp.Dom.Html.IHtmlDocument document = null;
                try { document = parser.Parse(original); } catch { list.Add(""); }

                try
                {
                    if (recursive)
                    {
                        foreach (var element in document.QuerySelectorAll(ReplaceValues(cssSelector, data)))
                        {
                            switch (ReplaceValues(attributeName, data))
                            {
                            case "innerHTML":
                                list.Add(element.InnerHtml);
                                break;

                            case "outerHTML":
                                list.Add(element.OuterHtml);
                                break;

                            default:
                                foreach (var attr in element.Attributes)
                                {
                                    if (attr.Name == ReplaceValues(attributeName, data))
                                    {
                                        list.Add(attr.Value);
                                        break;
                                    }
                                }
                                break;
                            }
                        }
                    }
                    else
                    {
                        switch (ReplaceValues(attributeName, data))
                        {
                        case "innerHTML":
                            list.Add(document.QuerySelectorAll(ReplaceValues(cssSelector, data))[cssElementIndex].InnerHtml);
                            break;

                        case "outerHTML":
                            list.Add(document.QuerySelectorAll(ReplaceValues(cssSelector, data))[cssElementIndex].OuterHtml);
                            break;

                        default:
                            foreach (var attr in document.QuerySelectorAll(ReplaceValues(cssSelector, data))[cssElementIndex].Attributes)
                            {
                                if (attr.Name == ReplaceValues(attributeName, data))
                                {
                                    list.Add(attr.Value);
                                    break;
                                }
                            }
                            break;
                        }
                    }
                }
                catch { list.Add(""); }

                break;

            case ParseType.JSON:
                var jsonlist = new List <KeyValuePair <string, string> >();
                parseJSON("", original, jsonlist);
                foreach (var j in jsonlist)
                {
                    if (j.Key == ReplaceValues(jsonField, data))
                    {
                        list.Add(j.Value);
                    }
                }

                if (list.Count == 0)
                {
                    list.Add("");
                }
                break;

            case ParseType.XPATH:

                // NOT IMPLEMENTED YET
                break;

            case ParseType.REGEX:
REGEXBEGIN:
                try
                {
                    var matches = Regex.Matches(partial, ReplaceValues(regexString, data));
                    foreach (Match match in matches)
                    {
                        var output = ReplaceValues(regexOutput, data);
                        for (var i = 0; i < match.Groups.Count; i++)
                        {
                            output = output.Replace("[" + i + "]", match.Groups[i].Value);
                        }
                        list.Add(output);
                        if (recursive && match.Index + match.Length <= partial.Length)
                        {
                            partial = partial.Substring(match.Index + match.Length); goto REGEXBEGIN;
                        }
                    }
                }
                catch { }
                break;
            }

            return(list);
        }
Пример #9
0
        /// <inheritdoc />
        public override void Process(BotData data)
        {
            if (!data.GlobalSettings.Captchas.BypassBalanceCheck)
            {
                base.Process(data);
            }

            var localUrl = ReplaceValues(url, data);

            data.Log(new LogEntry("Downloading image...", Colors.White));

            // Download captcha
            var captchaFile = string.Format("Captchas/captcha{0}.jpg", data.BotNumber);

            if (base64)
            {
                var bytes = Convert.FromBase64String(localUrl);
                using (var imageFile = new FileStream(captchaFile, FileMode.Create))
                {
                    imageFile.Write(bytes, 0, bytes.Length);
                    imageFile.Flush();
                }
            }
            else if (sendScreenshot && data.Screenshots.Count > 0)
            {
                Bitmap image = new Bitmap(data.Screenshots.Last());
                image.Save(captchaFile);
            }
            else
            {
                try
                {
                    DownloadRemoteImageFile(captchaFile, data, localUrl);
                }
                catch (Exception ex) { data.Log(new LogEntry(ex.Message, Colors.Tomato)); throw; }
            }

            string response = "";

            CaptchaServices.CaptchaService service = null;
            var bitmap = new Bitmap(captchaFile);

            try
            {
                switch (data.GlobalSettings.Captchas.CurrentService)
                {
                case CaptchaService.ImageTypers:
                    service = new ImageTyperz(data.GlobalSettings.Captchas.ImageTypToken, data.GlobalSettings.Captchas.Timeout);
                    break;

                case CaptchaService.AntiCaptcha:
                    service = new AntiCaptcha(data.GlobalSettings.Captchas.AntiCapToken, data.GlobalSettings.Captchas.Timeout);
                    break;

                case CaptchaService.DBC:
                    service = new DeathByCaptcha(data.GlobalSettings.Captchas.DBCUser, data.GlobalSettings.Captchas.DBCPass, data.GlobalSettings.Captchas.Timeout);
                    break;

                case CaptchaService.TwoCaptcha:
                    service = new TwoCaptcha(data.GlobalSettings.Captchas.TwoCapToken, data.GlobalSettings.Captchas.Timeout);
                    break;

                case CaptchaService.DeCaptcher:
                    service = new DeCaptcher(data.GlobalSettings.Captchas.DCUser, data.GlobalSettings.Captchas.DCPass, data.GlobalSettings.Captchas.Timeout);
                    break;

                case CaptchaService.AZCaptcha:
                    service = new AZCaptcha(data.GlobalSettings.Captchas.AZCapToken, data.GlobalSettings.Captchas.Timeout);
                    break;

                case CaptchaService.CaptchasIO:
                    service = new CaptchasIO(data.GlobalSettings.Captchas.CIOToken, data.GlobalSettings.Captchas.Timeout);
                    break;

                default:
                    throw new Exception("This service cannot solve normal captchas!");
                }
                response = service.SolveCaptcha(bitmap);
            }
            catch (Exception ex) { data.Log(new LogEntry(ex.Message, Colors.Tomato)); throw; }
            finally { bitmap.Dispose(); }

            data.CaptchaService = service;
            data.Log(response == "" ? new LogEntry("Couldn't get a response from the service", Colors.Tomato) : new LogEntry("Succesfully got the response: " + response, Colors.GreenYellow));

            if (VariableName != "")
            {
                data.Log(new LogEntry("Response stored in variable: " + variableName, Colors.White));
                data.Variables.Set(new CVar(variableName, response));
            }
        }
Пример #10
0
        /// <inheritdoc/>
        public override void Process(BotData data)
        {
            base.Process(data);

            var  ws         = data.GetCustomObject(nameof(WebSocket)) as WebSocket;
            var  wsMessages = data.GetCustomObject(nameof(WebSocket) + "Messages") as List <string>;
            bool onMsg      = false;

            switch (Command)
            {
            case WSCommand.Connect:
            {
                var inputs = ReplaceValues(Url, data);
                try { ws?.Close(CloseStatusCode.NoStatus); } catch { }
                ws = new WebSocket(inputs)
                {
                    Compression       = Compression,
                    Origin            = Origin,
                    WaitTime          = WaitTime,
                    EmitOnPing        = EmitOnPing,
                    EnableRedirection = Redirection,
                };
                ws.SslConfiguration.EnabledSslProtocols = SslProtocols;

                if (data.UseProxies)
                {
                    if (data.Proxy.Type != Extreme.Net.ProxyType.Http)
                    {
                        throw new NotSupportedException("Only HTTP proxy is supported");
                    }
                    ws.SetProxy($"http://{data.Proxy.Host}:{data.Proxy.Port}", data.Proxy.Username, data.Proxy.Password);
                }

                if (Credentials)
                {
                    ws.SetCredentials(ReplaceValues(Username, data), ReplaceValues(Password, data), PreAuth);
                }

                // Set cookies
                data.Log(new LogEntry("Sent Cookies:", Colors.MediumTurquoise));

                foreach (var cookie in CustomCookies)         // Add new user-defined custom cookies to the bot's cookie jar
                {
                    ws.SetCookie(new WebSocketSharp.Net.Cookie(ReplaceValues(cookie.Key, data), ReplaceValues(cookie.Value, data), "/"));
                    data.LogBuffer.Add(new LogEntry($"{cookie.Key}: {cookie.Value}", Colors.MediumTurquoise));
                }
                foreach (var cookie in data.Cookies)
                {
                    ws.SetCookie(new WebSocketSharp.Net.Cookie(ReplaceValues(cookie.Key, data), ReplaceValues(cookie.Value, data), "/"));
                    data.LogBuffer.Add(new LogEntry($"{cookie.Key}: {cookie.Value}", Colors.MediumTurquoise));
                }
#if DEBUG
                ws.Log.Level = WebSocketSharp.LogLevel.Trace;
#endif

                if (wsMessages == null)
                {
                    wsMessages = new List <string>();
                }
                data.CustomObjects[nameof(WebSocket) + "Messages"] = wsMessages;
                ws.OnMessage += (sender, e) =>
                {
                    lock (wsMessages)
                    {
                        wsMessages.Add(e.Data);
                    }
                };

                // Connect
                ws.Connect();

                if (!WaitForConnect(ws))
                {
                    throw new Exception($"Connection Status: {ws.ReadyState}");
                }

                data.CustomObjects[nameof(WebSocket)] = ws;
                data.Log(new LogEntry($"Succesfully connected to url: {inputs}.", Colors.LimeGreen));
                data.Log(new LogEntry($"Connection Status: {ws.ReadyState}", Colors.LimeGreen));
            }
            break;

            case WSCommand.Disconnect:
            {
                if (ws == null)
                {
                    throw new NullReferenceException("Make a connection first!");
                }
                ws.Close();
                ws = null;
                data.Log(new LogEntry($"Succesfully closed", Colors.GreenYellow));
                data.CustomObjects[nameof(WebSocket)] = null;
            }
            break;

            case WSCommand.Send:
            {
                if (ws == null)
                {
                    throw new NullReferenceException("Make a connection first!");
                }
                var msg = ReplaceValues(Message, data);
                ws.Send(msg);
                data.Log(new LogEntry($"Sent {msg} to the server", Colors.LimeGreen));
            }
            break;

            case WSCommand.Read:
                if (ws == null)
                {
                    throw new NullReferenceException("Make a connection first!");
                }
                wsMessages = data.CustomObjects[nameof(WebSocket) + "Messages"] as List <string>;
                data.Log(new LogEntry($"Unread messages from server", Colors.LimeGreen));
                break;
            }

            if (!string.IsNullOrEmpty(VariableName))
            {
                InsertVariable(data, IsCapture, wsMessages, VariableName);
                wsMessages.Clear();
            }
        }
Пример #11
0
        /// <inheritdoc />
        public override void Process(BotData data)
        {
            base.Process(data);

            InsertVariables(data, isCapture, recursive, Parse(data), variableName, prefix, suffix);
        }
Пример #12
0
        /// <inheritdoc />
        public override void Process(BotData data)
        {
            base.Process(data);

            var style    = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
            var provider = new CultureInfo("en-US");

            var localInputStrings = ReplaceValuesRecursive(inputString, data);
            var outputs           = new List <string>();

            for (int i = 0; i < localInputStrings.Count; i++)
            {
                var localInputString = localInputStrings[i];
                var outputString     = "";

                switch (FunctionType)
                {
                case Function.Constant:
                    outputString = localInputString;
                    break;

                case Function.Base64Encode:
                    outputString = localInputString.ToBase64();
                    break;

                case Function.Base64Decode:
                    outputString = localInputString.FromBase64();
                    break;

                case Function.HTMLEntityEncode:
                    outputString = WebUtility.HtmlEncode(localInputString);
                    break;

                case Function.HTMLEntityDecode:
                    outputString = WebUtility.HtmlDecode(localInputString);
                    break;

                case Function.Hash:
                    outputString = GetHash(localInputString, hashType).ToLower();
                    break;

                case Function.HMAC:
                    outputString = Hmac(localInputString, hashType, ReplaceValues(hmacKey, data), hmacBase64);
                    break;

                case Function.Translate:
                    outputString = localInputString;
                    foreach (var entry in TranslationDictionary.OrderBy(e => e.Key.Length).Reverse())
                    {
                        if (outputString.Contains(entry.Key))
                        {
                            outputString = outputString.Replace(entry.Key, entry.Value);
                            if (StopAfterFirstMatch)
                            {
                                break;
                            }
                        }
                    }
                    break;

                case Function.DateToUnixTime:
                    outputString = localInputString.ToDateTime(dateFormat).ToUnixTimeSeconds().ToString();
                    break;

                case Function.Length:
                    outputString = localInputString.Length.ToString();
                    break;

                case Function.ToLowercase:
                    outputString = localInputString.ToLower();
                    break;

                case Function.ToUppercase:
                    outputString = localInputString.ToUpper();
                    break;

                case Function.Replace:
                    if (useRegex)
                    {
                        outputString = Regex.Replace(localInputString, ReplaceValues(replaceWhat, data), ReplaceValues(replaceWith, data));
                    }
                    else
                    {
                        outputString = localInputString.Replace(ReplaceValues(replaceWhat, data), ReplaceValues(replaceWith, data));
                    }
                    break;

                case Function.RegexMatch:
                    outputString = Regex.Match(localInputString, ReplaceValues(regexMatch, data)).Value;
                    break;

                case Function.Unescape:
                    outputString = Regex.Unescape(localInputString);
                    break;

                case Function.URLEncode:
                    // The maximum allowed Uri size is 2083 characters, we use 2080 as a precaution
                    outputString = string.Join("", SplitInChunks(localInputString, 2080).Select(s => Uri.EscapeDataString(s)));
                    break;

                case Function.URLDecode:
                    outputString = Uri.UnescapeDataString(localInputString);
                    break;

                case Function.UnixTimeToDate:
                    outputString = double.Parse(localInputString).ToDateTime().ToShortDateString();
                    break;

                case Function.CurrentUnixTime:
                    outputString = DateTime.UtcNow.ToUnixTimeSeconds().ToString();
                    break;

                case Function.UnixTimeToISO8601:
                    outputString = double.Parse(localInputString).ToDateTime().ToISO8601();
                    break;

                case Function.RandomNum:
                    outputString = (data.Random.Next(randomMin, randomMax)).ToString();
                    break;

                case Function.RandomString:
                    var reserved  = new string[] { "?l", "?u", "?d", "?s", "?h", "?a", "?m", "?i" };
                    var lowercase = "abcdefghijklmnopqrstuvwxyz";
                    var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                    var digits    = "0123456789";
                    var symbols   = "\\!\"£$%&/()=?^'{}[]@#,;.:-_*+";
                    var hex       = digits + "abcdef";
                    var allchars  = lowercase + uppercase + digits + symbols;
                    var udchars   = uppercase + digits;
                    var ludchars  = lowercase + uppercase + digits;

                    outputString = localInputString;
                    while (reserved.Any(r => outputString.Contains(r)))
                    {
                        if (outputString.Contains("?l"))
                        {
                            outputString = ReplaceFirst(outputString, "?l", lowercase[data.Random.Next(0, lowercase.Length)].ToString());
                        }
                        else if (outputString.Contains("?u"))
                        {
                            outputString = ReplaceFirst(outputString, "?u", uppercase[data.Random.Next(0, uppercase.Length)].ToString());
                        }
                        else if (outputString.Contains("?d"))
                        {
                            outputString = ReplaceFirst(outputString, "?d", digits[data.Random.Next(0, digits.Length)].ToString());
                        }
                        else if (outputString.Contains("?s"))
                        {
                            outputString = ReplaceFirst(outputString, "?s", symbols[data.Random.Next(0, symbols.Length)].ToString());
                        }
                        else if (outputString.Contains("?h"))
                        {
                            outputString = ReplaceFirst(outputString, "?h", hex[data.Random.Next(0, hex.Length)].ToString());
                        }
                        else if (outputString.Contains("?a"))
                        {
                            outputString = ReplaceFirst(outputString, "?a", allchars[data.Random.Next(0, allchars.Length)].ToString());
                        }
                        else if (outputString.Contains("?m"))
                        {
                            outputString = ReplaceFirst(outputString, "?m", udchars[data.Random.Next(0, udchars.Length)].ToString());
                        }
                        else if (outputString.Contains("?i"))
                        {
                            outputString = ReplaceFirst(outputString, "?i", ludchars[data.Random.Next(0, ludchars.Length)].ToString());
                        }
                    }
                    break;

                case Function.Ceil:
                    outputString = Math.Ceiling(Decimal.Parse(localInputString, style, provider)).ToString();
                    break;

                case Function.Floor:
                    outputString = Math.Floor(Decimal.Parse(localInputString, style, provider)).ToString();
                    break;

                case Function.Round:
                    outputString = Math.Round(Decimal.Parse(localInputString, style, provider), 0, MidpointRounding.AwayFromZero).ToString();
                    break;

                case Function.Compute:
                    outputString = new DataTable().Compute(localInputString.Replace(',', '.'), null).ToString();
                    break;

                case Function.CountOccurrences:
                    outputString = CountStringOccurrences(localInputString, stringToFind).ToString();
                    break;

                case Function.ClearCookies:
                    data.Cookies.Clear();
                    break;

                case Function.RSAEncrypt:
                    outputString = Crypto.RSAEncrypt(
                        localInputString,
                        ReplaceValues(RsaKey, data),
                        ReplaceValues(RsaMod, data),
                        ReplaceValues(RsaExp, data),
                        RsaOAEP
                        );
                    break;

                case Function.RSADecrypt:
                    outputString = Crypto.RSADecrypt(
                        localInputString,
                        ReplaceValues(RsaKey, data),
                        ReplaceValues(RsaMod, data),
                        ReplaceValues(RsaExp, data),
                        RsaOAEP
                        );
                    break;

                case Function.Delay:
                    try { Thread.Sleep(int.Parse(localInputString)); } catch { }
                    break;

                case Function.CharAt:
                    outputString = localInputString.ToCharArray()[int.Parse(ReplaceValues(charIndex, data))].ToString();
                    break;

                case Function.Substring:
                    outputString = localInputString.Substring(int.Parse(ReplaceValues(substringIndex, data)), int.Parse(ReplaceValues(substringLength, data)));
                    break;

                case Function.ReverseString:
                    char[] charArray = localInputString.ToCharArray();
                    Array.Reverse(charArray);
                    outputString = new string(charArray);
                    break;

                case Function.Trim:
                    outputString = localInputString.Trim();
                    break;

                case Function.GetRandomUA:
                    outputString = RandomUserAgent(data.Random);
                    break;

                case Function.AESEncrypt:
                    outputString = Crypto.AESEncrypt(localInputString, ReplaceValues(aesKey, data), ReplaceValues(aesIV, data), AesMode, AesPadding);
                    break;

                case Function.AESDecrypt:
                    outputString = Crypto.AESDecrypt(localInputString, ReplaceValues(aesKey, data), ReplaceValues(aesIV, data), AesMode, AesPadding);
                    break;

                case Function.PBKDF2PKCS5:
                    outputString = Crypto.PBKDF2PKCS5(localInputString, ReplaceValues(KdfSalt, data), KdfSaltSize, KdfIterations, KdfKeySize, KdfAlgorithm);
                    break;
                }

                data.Log(new LogEntry(string.Format("Executed function {0} on input {1} with outcome {2}", functionType, localInputString, outputString), Colors.GreenYellow));

                // Add to the outputs
                outputs.Add(outputString);
            }

            var isList = outputs.Count > 1 || InputString.Contains("[*]") || InputString.Contains("(*)") || InputString.Contains("{*}");

            InsertVariables(data, isCapture, isList, outputs, variableName, "", "", false, true);
        }
Пример #13
0
 /// <summary>
 /// Adds a list variable with the given value.
 /// </summary>
 /// <param name="data">The BotData used for variable replacement and insertion</param>
 /// <param name="isCapture">Whether the variable should be marked for Capture</param>
 /// <param name="values">The list of values</param>
 /// <param name="variableName">The name of the variable to create</param>
 /// <param name="prefix">The string to add at the start of the value</param>
 /// <param name="suffix">The string to add at the end of the value</param>
 /// <param name="urlEncode">Whether to URLencode the values before creating the variables</param>
 /// <param name="createEmpty">Whether to create an empty (single) variable if the list of values is empty</param>
 public static void InsertVariable(BotData data, bool isCapture, IEnumerable <string> values, string variableName,
                                   string prefix = "", string suffix = "", bool urlEncode = false, bool createEmpty = true)
 {
     InsertVariable(data, isCapture, true, values, variableName, prefix, suffix, urlEncode, createEmpty);
 }
Пример #14
0
 /// <summary>
 /// Adds a single variable with the given value.
 /// </summary>
 /// <param name="data">The BotData used for variable replacement and insertion</param>
 /// <param name="isCapture">Whether the variable should be marked for Capture</param>
 /// <param name="value">The value of the variable</param>
 /// <param name="variableName">The name of the variable to create</param>
 /// <param name="prefix">The string to add at the start of the value</param>
 /// <param name="suffix">The string to add at the end of the value</param>
 /// <param name="urlEncode">Whether to URLencode the values before creating the variables</param>
 /// <param name="createEmpty">Whether to create an empty (single) variable if the list of values is empty</param>
 public static void InsertVariable(BotData data, bool isCapture, string value, string variableName,
                                   string prefix = "", string suffix = "", bool urlEncode = false, bool createEmpty = true)
 {
     InsertVariable(data, isCapture, false, new string[] { value }, variableName, prefix, suffix, urlEncode, createEmpty);
 }
Пример #15
0
        /// <inheritdoc />
        public override void Process(BotData data)
        {
            base.Process(data);

            var localInputStrings = ReplaceValuesRecursive(inputString, data);
            var outputs           = new List <string>();

            for (int i = 0; i < localInputStrings.Count; i++)
            {
                var localInputString = localInputStrings[i];
                var outputString     = "";

                switch (FunctionType)
                {
                case Function.Constant:
                    outputString = localInputString;
                    break;

                case Function.Base64Encode:
                    outputString = localInputString.ToBase64();
                    break;

                case Function.Base64Decode:
                    outputString = localInputString.FromBase64();
                    break;

                case Function.HTMLEntityEncode:
                    outputString = WebUtility.HtmlEncode(localInputString);
                    break;

                case Function.HTMLEntityDecode:
                    outputString = WebUtility.HtmlDecode(localInputString);
                    break;

                case Function.Hash:
                    outputString = GetHash(localInputString, hashType).ToLower();
                    break;

                case Function.HMAC:
                    outputString = Hmac(localInputString, hashType, ReplaceValues(hmacKey, data), hmacBase64, keyBase64);
                    break;

                case Function.Translate:
                    outputString = localInputString;
                    foreach (var entry in TranslationDictionary.OrderBy(e => e.Key.Length).Reverse())
                    {
                        if (outputString.Contains(entry.Key))
                        {
                            outputString = outputString.Replace(entry.Key, entry.Value);
                            if (StopAfterFirstMatch)
                            {
                                break;
                            }
                        }
                    }
                    break;

                case Function.DateToUnixTime:
                    outputString = localInputString.ToDateTime(dateFormat).ToUnixTimeSeconds().ToString();
                    break;

                case Function.Length:
                    outputString = localInputString.Length.ToString();
                    break;

                case Function.ToLowercase:
                    outputString = localInputString.ToLower();
                    break;

                case Function.ToUppercase:
                    outputString = localInputString.ToUpper();
                    break;

                case Function.Replace:
                    if (useRegex)
                    {
                        outputString = Regex.Replace(localInputString, ReplaceValues(replaceWhat, data), ReplaceValues(replaceWith, data));
                    }
                    else
                    {
                        outputString = localInputString.Replace(ReplaceValues(replaceWhat, data), ReplaceValues(replaceWith, data));
                    }
                    break;

                case Function.RegexMatch:
                    outputString = Regex.Match(localInputString, ReplaceValues(regexMatch, data)).Value;
                    break;

                case Function.Unescape:
                    outputString = Regex.Unescape(localInputString);
                    break;

                case Function.URLEncode:
                    // The maximum allowed Uri size is 2083 characters, we use 2080 as a precaution
                    outputString = string.Join("", SplitInChunks(localInputString, 2080).Select(s => Uri.EscapeDataString(s)));
                    break;

                case Function.URLDecode:
                    outputString = Uri.UnescapeDataString(localInputString);
                    break;

                case Function.UnixTimeToDate:
                    outputString = double.Parse(localInputString).ToDateTime().ToString(dateFormat);
                    break;

                case Function.CurrentUnixTime:
                    outputString = DateTime.UtcNow.ToUnixTimeSeconds().ToString();
                    break;

                case Function.UnixTimeToISO8601:
                    outputString = double.Parse(localInputString).ToDateTime().ToISO8601();
                    break;

                case Function.RandomNum:
                    var min             = int.Parse(ReplaceValues(randomMin, data));
                    var max             = int.Parse(ReplaceValues(randomMax, data));
                    var randomNumString = data.random.Next(min, max).ToString();
                    outputString = randomZeroPad ? randomNumString.PadLeft(max.ToString().Length, '0') : randomNumString;
                    break;

                case Function.RandomString:
                    outputString = localInputString;
                    outputString = Regex.Replace(outputString, @"\?l", m => _lowercase[data.random.Next(_lowercase.Length)].ToString());
                    outputString = Regex.Replace(outputString, @"\?u", m => _uppercase[data.random.Next(_uppercase.Length)].ToString());
                    outputString = Regex.Replace(outputString, @"\?d", m => _digits[data.random.Next(_digits.Length)].ToString());
                    outputString = Regex.Replace(outputString, @"\?s", m => _symbols[data.random.Next(_symbols.Length)].ToString());
                    outputString = Regex.Replace(outputString, @"\?h", m => _hex[data.random.Next(_hex.Length)].ToString());
                    outputString = Regex.Replace(outputString, @"\?a", m => _allChars[data.random.Next(_allChars.Length)].ToString());
                    outputString = Regex.Replace(outputString, @"\?m", m => _udChars[data.random.Next(_udChars.Length)].ToString());
                    outputString = Regex.Replace(outputString, @"\?i", m => _ludChars[data.random.Next(_ludChars.Length)].ToString());
                    break;

                case Function.Ceil:
                    outputString = Math.Ceiling(Decimal.Parse(localInputString, _style, _provider)).ToString();
                    break;

                case Function.Floor:
                    outputString = Math.Floor(Decimal.Parse(localInputString, _style, _provider)).ToString();
                    break;

                case Function.Round:
                    outputString = Math.Round(Decimal.Parse(localInputString, _style, _provider), 0, MidpointRounding.AwayFromZero).ToString();
                    break;

                case Function.Compute:
                    outputString = new DataTable().Compute(localInputString.Replace(',', '.'), null).ToString();
                    break;

                case Function.CountOccurrences:
                    outputString = CountStringOccurrences(localInputString, stringToFind).ToString();
                    break;

                case Function.ClearCookies:
                    data.Cookies.Clear();
                    break;

                case Function.RSAEncrypt:
                    outputString = Crypto.RSAEncrypt(
                        localInputString,
                        ReplaceValues(RsaN, data),
                        ReplaceValues(RsaE, data),
                        RsaOAEP
                        );
                    break;

                /*
                 * case Function.RSADecrypt:
                 * outputString = Crypto.RSADecrypt(
                 *  localInputString,
                 *  ReplaceValues(RsaN, data),
                 *  ReplaceValues(RsaD, data),
                 *  RsaOAEP
                 *  );
                 * break;
                 */

                case Function.Delay:
                    try { Thread.Sleep(int.Parse(localInputString)); } catch { }
                    break;

                case Function.CharAt:
                    outputString = localInputString.ToCharArray()[int.Parse(ReplaceValues(charIndex, data))].ToString();
                    break;

                case Function.Substring:
                    outputString = localInputString.Substring(int.Parse(ReplaceValues(substringIndex, data)), int.Parse(ReplaceValues(substringLength, data)));
                    break;

                case Function.ReverseString:
                    char[] charArray = localInputString.ToCharArray();
                    Array.Reverse(charArray);
                    outputString = new string(charArray);
                    break;

                case Function.Trim:
                    outputString = localInputString.Trim();
                    break;

                case Function.GetRandomUA:
                    if (UserAgentSpecifyBrowser)
                    {
                        outputString = UserAgent.ForBrowser(UserAgentBrowser);
                    }
                    else
                    {
                        outputString = UserAgent.Random(data.random);
                    }
                    break;

                case Function.AESEncrypt:
                    outputString = Crypto.AESEncrypt(localInputString, ReplaceValues(aesKey, data), ReplaceValues(aesIV, data), AesMode, AesPadding);
                    break;

                case Function.AESDecrypt:
                    outputString = Crypto.AESDecrypt(localInputString, ReplaceValues(aesKey, data), ReplaceValues(aesIV, data), AesMode, AesPadding);
                    break;

                case Function.PBKDF2PKCS5:
                    outputString = Crypto.PBKDF2PKCS5(localInputString, ReplaceValues(KdfSalt, data), KdfSaltSize, KdfIterations, KdfKeySize, KdfAlgorithm);
                    break;
                }

                data.Log(new LogEntry(string.Format("Executed function {0} on input {1} with outcome {2}", functionType, localInputString, outputString), Colors.GreenYellow));

                // Add to the outputs
                outputs.Add(outputString);
            }

            var isList = outputs.Count > 1 || InputString.Contains("[*]") || InputString.Contains("(*)") || InputString.Contains("{*}");

            InsertVariable(data, isCapture, isList, outputs, variableName, "", "", false, true);
        }
Пример #16
0
        /// <inheritdoc />
        public override void Process(BotData data)
        {
            base.Process(data);

            // Setup
            var request = new Request();

            request.Setup(data.GlobalSettings, securityProtocol, AutoRedirect, data.ConfigSettings.MaxRedirects, AcceptEncoding);

            var localUrl = ReplaceValues(Url, data);

            data.Log(new LogEntry($"Calling URL: {localUrl}", Colors.MediumTurquoise));

            // Set content
            switch (RequestType)
            {
            case RequestType.Standard:
                request.SetStandardContent(ReplaceValues(PostData, data), ReplaceValues(ContentType, data), Method, EncodeContent, GetLogBuffer(data));
                break;

            case RequestType.BasicAuth:
                request.SetBasicAuth(ReplaceValues(AuthUser, data), ReplaceValues(AuthPass, data));
                break;

            case RequestType.Multipart:
                var contents = MultipartContents.Select(m =>
                                                        new MultipartContent()
                {
                    Name        = ReplaceValues(m.Name, data),
                    Value       = ReplaceValues(m.Value, data),
                    ContentType = ReplaceValues(m.Value, data),
                    Type        = m.Type
                });
                request.SetMultipartContent(contents, ReplaceValues(MultipartBoundary, data), GetLogBuffer(data));
                break;
            }

            // Set proxy
            if (data.UseProxies)
            {
                request.SetProxy(data.Proxy);
            }

            // Set headers
            data.Log(new LogEntry("Sent Headers:", Colors.DarkTurquoise));
            var headers = CustomHeaders.Select(h =>
                                               new KeyValuePair <string, string> (ReplaceValues(h.Key, data), ReplaceValues(h.Value, data))
                                               ).ToDictionary(h => h.Key, h => h.Value);

            request.SetHeaders(headers, AcceptEncoding, GetLogBuffer(data));

            // Set cookies
            data.Log(new LogEntry("Sent Cookies:", Colors.MediumTurquoise));

            foreach (var cookie in CustomCookies) // Add new user-defined custom cookies to the bot's cookie jar
            {
                data.Cookies[ReplaceValues(cookie.Key, data)] = ReplaceValues(cookie.Value, data);
            }

            request.SetCookies(data.Cookies, GetLogBuffer(data));

            // End the request part
            data.LogNewLine();

            // Perform the request
            try
            {
                (data.Address, data.ResponseCode, data.ResponseHeaders, data.Cookies) = request.Perform(localUrl, Method, GetLogBuffer(data));
            }
            catch (Exception ex)
            {
                if (data.ConfigSettings.IgnoreResponseErrors)
                {
                    data.Log(new LogEntry(ex.Message, Colors.Tomato));
                    data.ResponseSource = ex.Message;
                    return;
                }
                throw;
            }

            // Save the response content
            switch (ResponseType)
            {
            case ResponseType.String:
                data.ResponseSource = request.SaveString(ReadResponseSource, data.ResponseHeaders, GetLogBuffer(data));
                break;

            case ResponseType.File:
                if (SaveAsScreenshot)
                {
                    Files.SaveScreenshot(request.GetResponseStream(), data);     // Read the stream
                    data.Log(new LogEntry("File saved as screenshot", Colors.Green));
                }
                else
                {
                    request.SaveFile(ReplaceValues(DownloadPath, data), GetLogBuffer(data));
                }
                break;

            case ResponseType.Base64String:
                var base64 = Convert.ToBase64String(request.GetResponseStream().ToArray());
                InsertVariable(data, false, base64, OutputVariable);
                break;

            default:
                break;
            }
        }
Пример #17
0
        /// <inheritdoc />
        public override void Process(BotData data)
        {
            base.Process(data);

            var style    = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
            var provider = new CultureInfo("en-US");

            var localInputStrings = ReplaceValuesRecursive(inputString, data);
            var outputs           = new List <string>();

            for (int i = 0; i < localInputStrings.Count; i++)
            {
                var localInputString = localInputStrings[i];
                var outputString     = "";

                switch (FunctionType)
                {
                case Function.Constant:
                    outputString = localInputString;
                    break;

                case Function.Base64Encode:
                    outputString = Base64Encode(localInputString);
                    break;

                case Function.Base64Decode:
                    outputString = Base64Decode(localInputString);
                    break;

                case Function.Hash:
                    outputString = GetHash(localInputString, hashType).ToLower();
                    break;

                case Function.HMAC:
                    outputString = Hmac(localInputString, hashType, ReplaceValues(hmacKey, data), hmacBase64);
                    break;

                case Function.Translate:
                    outputString = localInputString;
                    foreach (var entry in TranslationDictionary.OrderBy(e => e.Key.Length).Reverse())
                    {
                        if (outputString.Contains(entry.Key))
                        {
                            outputString = outputString.Replace(entry.Key, entry.Value);
                            if (StopAfterFirstMatch)
                            {
                                break;
                            }
                        }
                    }
                    break;

                case Function.DateToUnixTime:
                    outputString = (DateTime.ParseExact(localInputString, dateFormat, new CultureInfo("en-US"), DateTimeStyles.AllowWhiteSpaces)
                                    .Subtract(new DateTime(1970, 1, 1)))
                                   .TotalSeconds
                                   .ToString();
                    break;

                case Function.Length:
                    outputString = localInputString.Length.ToString();
                    break;

                case Function.ToLowercase:
                    outputString = localInputString.ToLower();
                    break;

                case Function.ToUppercase:
                    outputString = localInputString.ToUpper();
                    break;

                case Function.Replace:
                    if (useRegex)
                    {
                        outputString = Regex.Replace(localInputString, ReplaceValues(replaceWhat, data), ReplaceValues(replaceWith, data));
                    }
                    else
                    {
                        outputString = localInputString.Replace(ReplaceValues(replaceWhat, data), ReplaceValues(replaceWith, data));
                    }
                    break;

                case Function.RegexMatch:
                    outputString = Regex.Match(localInputString, ReplaceValues(regexMatch, data)).Value;
                    break;

                case Function.URLEncode:
                    outputString = System.Uri.EscapeDataString(localInputString);
                    break;

                case Function.URLDecode:
                    outputString = System.Uri.UnescapeDataString(localInputString);
                    break;

                case Function.UnixTimeToDate:
                    try
                    {
                        var loc = localInputString;
                        if (localInputString.Length > 10)
                        {
                            loc = localInputString.Substring(0, 10);
                        }
                        DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                        dtDateTime   = dtDateTime.AddSeconds(int.Parse(loc)).ToLocalTime();
                        outputString = dtDateTime.ToShortDateString();
                    }
                    catch { }

                    break;

                case Function.CurrentUnixTime:
                    outputString = Math.Round((DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds).ToString();
                    break;

                case Function.UnixTimeToISO8601:
                    var loc2 = localInputString;
                    if (localInputString.Length > 10)
                    {
                        loc2 = localInputString.Substring(0, 10);
                    }
                    DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                    dateTime     = dateTime.AddSeconds(int.Parse(loc2)).ToLocalTime();
                    outputString = dateTime.ToString("yyyy-MM-ddTHH\\:mm\\:ss.fffZ");
                    break;

                case Function.RandomNum:
                    outputString = (data.rand.Next(randomMin, randomMax)).ToString();
                    break;

                case Function.Ceil:
                    outputString = Math.Ceiling(Decimal.Parse(localInputString, style, provider)).ToString();
                    break;

                case Function.Floor:
                    outputString = Math.Floor(Decimal.Parse(localInputString, style, provider)).ToString();
                    break;

                case Function.Round:
                    outputString = Math.Round(Decimal.Parse(localInputString, style, provider), 0, MidpointRounding.AwayFromZero).ToString();
                    break;

                case Function.Compute:
                    outputString = new DataTable().Compute(localInputString.Replace(',', '.'), null).ToString();
                    break;

                case Function.CountOccurrences:
                    outputString = CountStringOccurrences(localInputString, stringToFind).ToString();
                    break;

                case Function.ClearCookies:
                    data.Cookies.Clear();
                    break;

                case Function.RSA:
                    try
                    {
                        while (outputString.Length < 2 || outputString.Substring(outputString.Length - 2) != "==")
                        {
                            outputString = SteamRSAEncrypt(new RsaParameters {
                                Exponent = ReplaceValues(RSAExp, data), Modulus = ReplaceValues(RSAMod, data), Password = localInputString
                            });
                        }
                    }
                    catch (Exception ex) { outputString = ex.ToString(); }
                    break;

                case Function.Delay:
                    try { Thread.Sleep(int.Parse(localInputString)); } catch { }
                    break;

                case Function.CharAt:
                    outputString = localInputString.ToCharArray()[int.Parse(ReplaceValues(charIndex, data))].ToString();
                    break;

                case Function.Substring:
                    outputString = localInputString.Substring(int.Parse(ReplaceValues(substringIndex, data)), int.Parse(ReplaceValues(substringLength, data)));
                    break;

                case Function.ReverseString:
                    char[] charArray = localInputString.ToCharArray();
                    Array.Reverse(charArray);
                    outputString = new string(charArray);
                    break;

                case Function.Trim:
                    outputString = localInputString.Trim();
                    break;

                case Function.GetRandomUA:
                    outputString = RandomUserAgent(data.rand);
                    break;

                case Function.AESEncrypt:
                    outputString = AESEncrypt(ReplaceValues(aesKey, data), localInputString);
                    break;

                case Function.AESDecrypt:
                    outputString = AESDecrypt(ReplaceValues(aesKey, data), localInputString);
                    break;
                }

                data.Log(new LogEntry(string.Format("Executed function {0} on input {1} with outcome {2}", functionType, localInputString, outputString), Colors.GreenYellow));

                // Add to the outputs
                outputs.Add(outputString);
            }

            InsertVariables(data, isCapture, outputs.Count > 1, outputs, variableName, "", "");
        }
Пример #18
0
 private List <LogEntry> GetLogBuffer(BotData data) => data.GlobalSettings.General.EnableBotLog || data.IsDebug ? data.LogBuffer : null;
Пример #19
0
 /// <inheritdoc/>
 public override void Process(BotData data)
 {
     base.Process(data);
     //InsertVariable(data, IsCapture, Run(data), VariableName);
 }
Пример #20
0
        /// <inheritdoc />
        public override void Process(BotData data)
        {
            base.Process(data);

            if (data.Driver == null)
            {
                data.Log(new LogEntry("Open a browser first!", Colors.White));
                throw new Exception("Browser not open");
            }

            // Find the element
            IWebElement element = null;
            ReadOnlyCollection <IWebElement> elements = null;

            try
            {
                if (action != ElementAction.WaitForElement)
                {
                    elements = FindElements(data);
                    element  = elements[elementIndex];
                }
            }
            catch { data.Log(new LogEntry("Cannot find element on the page", Colors.White)); }

            List <string> outputs = new List <string>();

            try
            {
                switch (action)
                {
                case ElementAction.Clear:
                    element.Clear();
                    break;

                case ElementAction.SendKeys:
                    element.SendKeys(ReplaceValues(input, data));
                    break;

                case ElementAction.Click:
                    element.Click();
                    UpdateSeleniumData(data);
                    break;

                case ElementAction.Submit:
                    element.Submit();
                    UpdateSeleniumData(data);
                    break;

                case ElementAction.GetText:
                    if (recursive)
                    {
                        foreach (var elem in elements)
                        {
                            outputs.Add(elem.Text);
                        }
                    }
                    else
                    {
                        outputs.Add(element.Text);
                    }
                    break;

                case ElementAction.GetAttribute:
                    if (recursive)
                    {
                        foreach (var elem in elements)
                        {
                            outputs.Add(elem.GetAttribute(ReplaceValues(input, data)));
                        }
                    }
                    else
                    {
                        outputs.Add(element.GetAttribute(ReplaceValues(input, data)));
                    }
                    break;

                case ElementAction.IsDisplayed:
                    outputs.Add(element.Displayed.ToString());
                    break;

                case ElementAction.IsEnabled:
                    outputs.Add(element.Enabled.ToString());
                    break;

                case ElementAction.IsSelected:
                    outputs.Add(element.Selected.ToString());
                    break;

                case ElementAction.LocationX:
                    outputs.Add(element.Location.X.ToString());
                    break;

                case ElementAction.LocationY:
                    outputs.Add(element.Location.Y.ToString());
                    break;

                case ElementAction.SizeX:
                    outputs.Add(element.Size.Width.ToString());
                    break;

                case ElementAction.SizeY:
                    outputs.Add(element.Size.Height.ToString());
                    break;

                case ElementAction.Screenshot:
                    var image = GetElementScreenShot(data.Driver, element);
                    SaveScreenshot(image, data);
                    break;

                case ElementAction.SwitchToFrame:
                    data.Driver.SwitchTo().Frame(element);
                    break;

                case ElementAction.WaitForElement:
                    var ms  = 0;                                     // Currently waited milliseconds
                    var max = 10000;
                    try { max = int.Parse(input) * 1000; } catch { } // Max ms to wait
                    var found = false;
                    while (ms < max)
                    {
                        try
                        {
                            elements = FindElements(data);
                            element  = elements[0];
                            found    = true;
                            break;
                        }
                        catch { ms += 200; Thread.Sleep(200); }
                    }
                    if (!found)
                    {
                        data.Log(new LogEntry("Timeout while waiting for element", Colors.White));
                    }
                    break;

                case ElementAction.SendKeysHuman:
                    var toSend = ReplaceValues(input, data);
                    var rand   = new Random();
                    foreach (char c in toSend)
                    {
                        element.SendKeys(c.ToString());
                        Thread.Sleep(rand.Next(100, 300));
                    }
                    break;
                }
            }
            catch { data.Log(new LogEntry("Cannot execute action on the element", Colors.White)); }

            data.Log(new LogEntry(string.Format("Executed action {0} on the element with input {1}", action, ReplaceValues(input, data)), Colors.White));

            if (outputs.Count != 0)
            {
                InsertVariables(data, isCapture, recursive, outputs, outputVariable, "", "");
            }
        }
Пример #21
0
        /// <inheritdoc />
        public override void Process(BotData data)
        {
            base.Process(data);

            // If the clearance info is already set and we're not getting it fresh each time, skip
            if (data.UseProxies)
            {
                if (data.Proxy.Clearance != "" && !data.GlobalSettings.Proxies.AlwaysGetClearance)
                {
                    data.Log(new LogEntry("Skipping CF Bypass because there is already a valid cookie", Colors.White));
                    data.Cookies["cf_clearance"] = data.Proxy.Clearance;
                    data.Cookies["__cfduid"]     = data.Proxy.Cfduid;
                    return;
                }
            }

            var localUrl = ReplaceValues(url, data);
            var uri      = new Uri(localUrl);

            // Initialize the captcha provider
            CaptchaService service = Captchas.GetService(data.GlobalSettings.Captchas);

            // Initialize the Cloudflare Solver
            CloudflareSolver cf = new CloudflareSolver(service, ReplaceValues(UserAgent, data))
            {
                ClearanceDelay  = 3000,
                MaxCaptchaTries = 1,
                MaxTries        = 3
            };

            // Create the cookie container
            CookieContainer cookies = new CookieContainer();

            foreach (var cookie in data.Cookies)
            {
                cookies.Add(new Cookie(cookie.Key, cookie.Value, "/", uri.Host));
            }

            // Initialize the http handler
            HttpClientHandler handler = new HttpClientHandler
            {
                AllowAutoRedirect      = AutoRedirect,
                CookieContainer        = cookies,
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
                SslProtocols           = SecurityProtocol.ToSslProtocols()
            };

            // Assign the proxy to the inner handler if necessary
            if (data.UseProxies)
            {
                if (data.Proxy.Type != Extreme.Net.ProxyType.Http)
                {
                    throw new Exception($"The proxy type {data.Proxy.Type} is not supported by this block yet");
                }

                handler.Proxy    = new WebProxy(data.Proxy.Proxy, false);
                handler.UseProxy = true;

                if (!string.IsNullOrEmpty(data.Proxy.Username))
                {
                    handler.Proxy.Credentials = new NetworkCredential(data.Proxy.Username, data.Proxy.Password);
                }
            }

            // Initialize the http client
            HttpClient http = new HttpClient(handler);

            http.Timeout = TimeSpan.FromSeconds(data.GlobalSettings.General.RequestTimeout);
            http.DefaultRequestHeaders.Add("User-Agent", ReplaceValues(UserAgent, data));

            SolveResult result = new SolveResult();

            try
            {
                result = cf.Solve(http, handler, uri, ReplaceValues(UserAgent, data)).Result;
            }
            catch (AggregateException ex)
            {
                // Join all the aggregate exception inner exception messages
                var message = string.Join(Environment.NewLine, ex.InnerExceptions.Select(e => e.Message));

                if (data.ConfigSettings.IgnoreResponseErrors)
                {
                    data.Log(new LogEntry(message, Colors.Tomato));
                    data.ResponseCode = message;
                    return;
                }
                throw new Exception(message);
            }
            catch (Exception ex)
            {
                if (data.ConfigSettings.IgnoreResponseErrors)
                {
                    data.Log(new LogEntry(ex.Message, Colors.Tomato));
                    data.ResponseSource = ex.Message;
                    return;
                }
                throw;
            }

            if (result.Success)
            {
                data.Log(new LogEntry($"[Success] Protection bypassed: {result.DetectResult.Protection}", Colors.GreenYellow));
            }
            else if (result.DetectResult.Protection == CloudflareProtection.Unknown)
            {
                data.Log(new LogEntry($"Unknown protection, skipping the bypass!", Colors.Tomato));
            }
            else
            {
                var message = $"CF Bypass Failed: {result.FailReason}";

                if (data.ConfigSettings.IgnoreResponseErrors)
                {
                    data.Log(new LogEntry(message, Colors.Tomato));
                    data.ResponseSource = message;
                    return;
                }
                throw new Exception(message);
            }

            // Now that we got the cookies, proceed with the normal request
            HttpResponseMessage response = null;

            try
            {
                response = http.GetAsync(uri).Result;
            }
            catch (Exception ex)
            {
                if (data.ConfigSettings.IgnoreResponseErrors)
                {
                    data.ResponseSource = ex.Message;
                    return;
                }
                throw new Exception(ex.Message);
            }
            finally
            {
                handler.Dispose();
                http.Dispose();
            }

            var responseString = response.Content.ReadAsStringAsync().Result;

            // Save the cloudflare cookies
            var clearance = "";
            var cfduid    = "";

            foreach (Cookie cookie in cookies.GetCookies(uri))
            {
                switch (cookie.Name)
                {
                case "cf_clearance":
                    clearance = cookie.Value;
                    break;

                case "__cfduid":
                    cfduid = cookie.Value;
                    break;
                }
            }

            // Save the cookies in the proxy
            if (data.UseProxies)
            {
                data.Proxy.Clearance = clearance;
                data.Proxy.Cfduid    = cfduid;
            }

            if (clearance != "")
            {
                data.Log(new LogEntry("Got Cloudflare clearance!", Colors.GreenYellow));
                data.Log(new LogEntry(clearance + Environment.NewLine + cfduid + Environment.NewLine, Colors.White));
            }

            // Get address
            data.Address = response.RequestMessage.RequestUri.AbsoluteUri;
            if (PrintResponseInfo)
            {
                data.Log(new LogEntry($"Address: {data.Address}", Colors.Cyan));
            }

            // Get code
            data.ResponseCode = ((int)response.StatusCode).ToString();
            if (PrintResponseInfo)
            {
                data.Log(new LogEntry($"Response code: {data.ResponseCode}", Colors.Cyan));
            }

            // Get headers
            if (PrintResponseInfo)
            {
                data.Log(new LogEntry("Received headers:", Colors.DeepPink));
            }
            data.ResponseHeaders.Clear();
            foreach (var header in response.Headers)
            {
                var h = new KeyValuePair <string, string>(header.Key, header.Value.First());
                data.ResponseHeaders.Add(h.Key, h.Value);
                if (PrintResponseInfo)
                {
                    data.Log(new LogEntry($"{h.Key}: {h.Value}", Colors.LightPink));
                }
            }

            // Add the Content-Length header if it was not sent by the server
            if (!data.ResponseHeaders.ContainsKey("Content-Length"))
            {
                if (data.ResponseHeaders.ContainsKey("Content-Encoding") && data.ResponseHeaders["Content-Encoding"].Contains("gzip"))
                {
                    data.ResponseHeaders["Content-Length"] = GZip.Zip(responseString).Length.ToString();
                }
                else
                {
                    data.ResponseHeaders["Content-Length"] = responseString.Length.ToString();
                }

                if (PrintResponseInfo)
                {
                    data.Log(new LogEntry($"Content-Length: {data.ResponseHeaders["Content-Length"]}", Colors.LightPink));
                }
            }

            // Get cookies
            if (PrintResponseInfo)
            {
                data.Log(new LogEntry("Received cookies:", Colors.Goldenrod));
            }
            foreach (Cookie cookie in cookies.GetCookies(uri))
            {
                data.Cookies[cookie.Name] = cookie.Value;
                if (PrintResponseInfo)
                {
                    data.Log(new LogEntry($"{cookie.Name}: {cookie.Value}", Colors.LightGoldenrodYellow));
                }
            }

            // Print source
            data.ResponseSource = responseString;
            if (PrintResponseInfo)
            {
                data.Log(new LogEntry("Response Source:", Colors.Green));
                data.Log(new LogEntry(data.ResponseSource, Colors.GreenYellow));
            }
        }
Пример #22
0
        /// <inheritdoc/>
        public override void Process(BotData data)
        {
            base.Process(data);

            var  ws          = data.GetCustomObject(nameof(WebSocket)) as WebSocket;
            var  receivedMsg = string.Empty;
            bool onMsg       = false;

            switch (Command)
            {
            case WSCommand.Connect:
            {
                var inputs = ReplaceValues(Url, data);
                try { ws?.Close(CloseStatusCode.NoStatus); } catch { }
                ws = new WebSocket(inputs)
                {
                    Compression       = Compression,
                    Origin            = Origin,
                    WaitTime          = WaitTime,
                    EmitOnPing        = EmitOnPing,
                    EnableRedirection = Redirection,
                };
                ws.SslConfiguration.EnabledSslProtocols = SslProtocols;
                //ws.SetProxy()

                if (Credentials)
                {
                    ws.SetCredentials(ReplaceValues(Username, data), ReplaceValues(Password, data), PreAuth);
                }

                // Set cookies
                data.Log(new LogEntry("Sent Cookies:", Colors.MediumTurquoise));

                foreach (var cookie in CustomCookies)         // Add new user-defined custom cookies to the bot's cookie jar
                {
                    ws.SetCookie(new WebSocketSharp.Net.Cookie(ReplaceValues(cookie.Key, data), ReplaceValues(cookie.Value, data), "/"));
                    data.LogBuffer.Add(new LogEntry($"{cookie.Key}: {cookie.Value}", Colors.MediumTurquoise));
                }
                foreach (var cookie in data.Cookies)
                {
                    ws.SetCookie(new WebSocketSharp.Net.Cookie(ReplaceValues(cookie.Key, data), ReplaceValues(cookie.Value, data), "/"));
                    data.LogBuffer.Add(new LogEntry($"{cookie.Key}: {cookie.Value}", Colors.MediumTurquoise));
                }
#if DEBUG
                ws.Log.Level = WebSocketSharp.LogLevel.Trace;
#endif

                ws.OnMessage += new EventHandler <MessageEventArgs>((s, e) =>
                    {
                        onMsg   = true;
                        var msg = string.Empty;
                        if (e.IsText)
                        {
                            receivedMsg += $"{msg = e.Data}\n";
                        }
                        else if (e.IsBinary)
                        {
                            receivedMsg += $"{msg = Encoding.ASCII.GetString(e.RawData)}\n";
                        }

                        data.Log(new LogEntry($"On Message Ev: {msg}", Colors.Yellow));
                    });

                ws.ConnectAsync();

                if (!WaitForConnect(ws))
                {
                    throw new Exception($"Connection Status: {ws.ReadyState}");
                }

                data.CustomObjects[nameof(WebSocket)] = ws;
                data.Log(new LogEntry($"Succesfully connected to url: {inputs}.", Colors.LimeGreen));
                data.Log(new LogEntry($"Connection Status: {ws.ReadyState}", Colors.LimeGreen));
                data.Log(new LogEntry(receivedMsg, Colors.GreenYellow));
            }
            break;

            case WSCommand.Disconnect:
            {
                if (ws == null)
                {
                    throw new Exception("Make a connection first!");
                }
                ws.CloseAsync(CloseStatusCode.Normal);
                ws = null;
                data.Log(new LogEntry($"Succesfully closed", Colors.GreenYellow));
                data.CustomObjects[nameof(WebSocket)] = null;
            }
            break;

            case WSCommand.Send:
            {
                if (ws == null)
                {
                    throw new Exception("Make a connection first!");
                }
                var  msg           = ReplaceValues(Message, data);
                var  bytes         = Encoding.ASCII.GetBytes(msg.Unescape());
                bool?wsSendReplied = null;
                data.Log(new LogEntry($"Sending {msg}", Colors.GreenYellow));
                ws.SendAsync(bytes, (completed) =>
                    {
                        wsSendReplied = completed;
                        if (completed)
                        {
                            data.Log(new LogEntry("Success to send Message", Colors.GreenYellow));
                        }
                        else
                        {
                            data.Log(new LogEntry("Failure to send Message", Colors.Red));
                        }
                    });
                TaskExtensions.WaitUntil(() => wsSendReplied.HasValue, timeout: 100).Wait();
            }
            break;
            }
            try
            {
                TaskExtensions.WaitUntil(() => onMsg,
                                         timeout: 500)
                .Wait();
            }
            catch { }

            if (!string.IsNullOrEmpty(VariableName))
            {
                InsertVariable(data, IsCapture, receivedMsg, VariableName);
            }
        }
Пример #23
0
        /// <inheritdoc />
        public override void Process(BotData data)
        {
            base.Process(data);

            // If bypass balance check, skip this method.
            if (data.GlobalSettings.Captchas.BypassBalanceCheck)
            {
                return;
            }

            // Get balance. If balance is under a certain threshold, don't ask for captcha solve
            Balance = 0; // Reset it or the block will save it for future calls
            data.Log(new LogEntry("Checking balance...", Colors.White));
            var cs = data.GlobalSettings.Captchas;

            switch (cs.CurrentService)
            {
            case CaptchaService.ImageTypers:
                Balance = new ImageTyperz(cs.ImageTypToken, cs.Timeout).GetBalance();
                break;

            case CaptchaService.AntiCaptcha:
                Balance = new AntiCaptcha(cs.AntiCapToken, cs.Timeout).GetBalance();
                break;

            case CaptchaService.DBC:
                Balance = new DeathByCaptcha(cs.DBCUser, cs.DBCPass, cs.Timeout).GetBalance();
                break;

            case CaptchaService.TwoCaptcha:
                Balance = new TwoCaptcha(cs.TwoCapToken, cs.Timeout).GetBalance();
                break;

            case CaptchaService.RuCaptcha:
                Balance = new RuCaptcha(cs.RuCapToken, cs.Timeout).GetBalance();
                break;

            case CaptchaService.DeCaptcher:
                Balance = new DeCaptcher(cs.DCUser, cs.DCPass, cs.Timeout).GetBalance();
                break;

            case CaptchaService.AZCaptcha:
                Balance = new AZCaptcha(cs.AZCapToken, cs.Timeout).GetBalance();
                break;

            case CaptchaService.SolveRecaptcha:
                Balance = new SolveReCaptcha(cs.SRUserId, cs.SRToken, cs.Timeout).GetBalance();
                break;

            case CaptchaService.CaptchasIO:
                Balance = new CaptchasIO(cs.CIOToken, cs.Timeout).GetBalance();
                break;

            case CaptchaService.CustomTwoCaptcha:
                Balance = new CustomTwoCaptcha(cs.CustomTwoCapToken, cs.CustomTwoCapDomain, cs.CustomTwoCapPort, cs.Timeout).GetBalance();
                break;

            default:
                Balance = 999;
                break;
            }

            if (Balance <= 0)
            {
                throw new System.Exception($"[{data.GlobalSettings.Captchas.CurrentService}] Bad token/credentials or zero balance!");
            }

            data.Log(new LogEntry($"[{data.GlobalSettings.Captchas.CurrentService}] Current Balance: ${Balance}", Colors.GreenYellow));
            data.Balance = Balance;
        }
Пример #24
0
        /// <inheritdoc />
        public override void Process(BotData data)
        {
            base.Process(data);

            if (data.Driver == null && action != BrowserAction.Open)
            {
                data.Log(new LogEntry("Open a browser first!", Colors.White));
                throw new Exception("Browser not open");
            }

            var     replacedInput = ReplaceValues(input, data);
            Actions keyActions    = null;

            switch (action)
            {
            case BrowserAction.Open:
                if (!string.IsNullOrEmpty(SUserAgent))
                {
                    data.ConfigSettings.CustomUserAgent = ReplaceValues(SUserAgent, data);
                }
                OpenBrowser(data, replacedInput);
                try { UpdateSeleniumData(data); } catch { }
                break;

            case BrowserAction.Close:
                data.Driver.Close();
                data.BrowserOpen = false;
                break;

            case BrowserAction.Quit:
                data.Driver.Quit();
                data.BrowserOpen = false;
                break;

            case BrowserAction.ClearCookies:
                data.Driver.Manage().Cookies.DeleteAllCookies();
                break;

            case BrowserAction.SendKeys:
                keyActions = new Actions(data.Driver);
                foreach (var s in replacedInput.Split(new string[] { "||" }, StringSplitOptions.None))
                {
                    switch (s)
                    {
                    case "<TAB>":
                        keyActions.SendKeys(Keys.Tab);
                        break;

                    case "<ENTER>":
                        keyActions.SendKeys(Keys.Enter);
                        break;

                    case "<BACKSPACE>":
                        keyActions.SendKeys(Keys.Backspace);
                        break;

                    case "<ESC>":
                        keyActions.SendKeys(Keys.Escape);
                        break;

                    default:
                        // List of available keys https://github.com/SeleniumHQ/selenium/blob/master/dotnet/src/webdriver/Keys.cs
                        var keyFields     = typeof(Keys).GetFields();
                        var matchingField = keyFields.FirstOrDefault(f =>
                                                                     $"<{f.Name}>".Equals(s, StringComparison.InvariantCultureIgnoreCase));

                        if (matchingField != null)
                        {
                            keyActions.SendKeys(matchingField.GetValue(null).ToString());
                        }
                        else
                        {
                            keyActions.SendKeys(s);
                        }
                        break;
                    }
                }
                keyActions.Perform();
                Thread.Sleep(1000);
                if (replacedInput.Contains("<ENTER>") || replacedInput.Contains("<BACKSPACE>"))     // These might lead to a page change
                {
                    UpdateSeleniumData(data);
                }
                break;

            case BrowserAction.Screenshot:
                var image = data.Driver.GetScreenshot();
                Files.SaveScreenshot(image, data);
                break;

            case BrowserAction.OpenNewTab:
                ((IJavaScriptExecutor)data.Driver).ExecuteScript("window.open();");
                data.Driver.SwitchTo().Window(data.Driver.WindowHandles.Last());
                break;

            case BrowserAction.SwitchToTab:
                data.Driver.SwitchTo().Window(data.Driver.WindowHandles[int.Parse(replacedInput)]);
                UpdateSeleniumData(data);
                break;

            case BrowserAction.CloseCurrentTab:
                ((IJavaScriptExecutor)data.Driver).ExecuteScript("window.close();");
                break;

            case BrowserAction.Refresh:
                data.Driver.Navigate().Refresh();
                break;

            case BrowserAction.Back:
                data.Driver.Navigate().Back();
                break;

            case BrowserAction.Forward:
                data.Driver.Navigate().Forward();
                break;

            case BrowserAction.Maximize:
                data.Driver.Manage().Window.Maximize();
                break;

            case BrowserAction.Minimize:
                data.Driver.Manage().Window.Minimize();
                break;

            case BrowserAction.FullScreen:
                data.Driver.Manage().Window.FullScreen();
                break;

            case BrowserAction.SetWidth:
                data.Driver.Manage().Window.Size = new Size(int.Parse(replacedInput), data.Driver.Manage().Window.Size.Height);
                break;

            case BrowserAction.SetHeight:
                data.Driver.Manage().Window.Size = new Size(data.Driver.Manage().Window.Size.Width, int.Parse(replacedInput));
                break;

            case BrowserAction.DOMtoSOURCE:
                data.ResponseSource = data.Driver.FindElement(By.TagName("body")).GetAttribute("innerHTML");
                break;

            case BrowserAction.GetCookies:
                foreach (var cookie in data.Driver.Manage().Cookies.AllCookies)
                {
                    try { data.Cookies.Add(cookie.Name, cookie.Value); } catch { }
                }
                break;

            case BrowserAction.SetCookies:
                var baseURL = Regex.Match(ReplaceValues(input, data), "^(?:https?:\\/\\/)?(?:[^@\\/\n]+@)?([^:\\/?\n]+)").Groups[1].Value;
                foreach (var cookie in data.Cookies)
                {
                    try { data.Driver.Manage().Cookies.AddCookie(new Cookie(cookie.Key, cookie.Value, baseURL, "/", DateTime.MaxValue)); } catch { }
                }
                break;

            case BrowserAction.SwitchToDefault:
                data.Driver.SwitchTo().DefaultContent();
                break;

            case BrowserAction.SwitchToAlert:
                data.Driver.SwitchTo().Alert();
                break;

            case BrowserAction.SwitchToParentFrame:
                data.Driver.SwitchTo().ParentFrame();
                break;
            }

            data.Log(new LogEntry(string.Format("Executed browser action {0} on input {1}", action, ReplaceValues(input, data)), Colors.White));
        }
Пример #25
0
 /// <summary>
 /// Updates the ADDRESS and SOURCE variables basing on the selenium-driven browser's URL bar and page source.
 /// </summary>
 /// <param name="data">The BotData containing the driver and the variables</param>
 public static void UpdateSeleniumData(BotData data)
 {
     data.Address        = data.Driver.Url;
     data.ResponseSource = data.Driver.PageSource;
 }
Пример #26
0
        /// <summary>
        /// Opens a browser of the given type (if not already open) and with the given settings.
        /// </summary>
        /// <param name="data">The BotData where the settings are stored.</param>
        public static void OpenBrowser(BotData data, string url = "")
        {
            if (!data.BrowserOpen)
            {
                data.Log(new LogEntry("Opening browser...", Colors.White));

                switch (data.GlobalSettings.Selenium.Browser)
                {
                case BrowserType.Chrome:
                    try
                    {
                        ChromeOptions       chromeop      = new ChromeOptions();
                        ChromeDriverService chromeservice = ChromeDriverService.CreateDefaultService();
                        chromeservice.SuppressInitialDiagnosticInformation = true;
                        chromeservice.HideCommandPromptWindow = true;
                        chromeservice.EnableVerboseLogging    = false;
                        chromeop.AddArgument("--log-level=3");
                        chromeop.BinaryLocation = data.GlobalSettings.Selenium.ChromeBinaryLocation;

                        if (data.GlobalSettings.Selenium.Headless || data.ConfigSettings.ForceHeadless)
                        {
                            chromeop.AddArgument("--headless");
                        }
                        else if (data.GlobalSettings.Selenium.ChromeExtensions.Count > 0)     // This should only be done when not headless
                        {
                            chromeop.AddExtensions(data.GlobalSettings.Selenium.ChromeExtensions
                                                   .Where(ext => ext.EndsWith(".crx"))
                                                   .Select(ext => Directory.GetCurrentDirectory() +
                                                           "\\ChromeExtensions\\" + ext));
                        }

                        if (data.ConfigSettings.DisableNotifications)
                        {
                            chromeop.AddArgument("--disable-notifications");
                        }
                        if (data.ConfigSettings.DefaultProfileDirectory)
                        {
                            chromeop.AddArgument("--profile-directory=Default");
                        }
                        if (data.ConfigSettings.CustomCMDArgs != string.Empty)
                        {
                            chromeop.AddArgument(data.ConfigSettings.CustomCMDArgs);
                        }
                        if (data.ConfigSettings.RandomUA)
                        {
                            chromeop.AddArgument("--user-agent=" + UserAgent.Random(data.random));
                        }
                        else if (data.ConfigSettings.CustomUserAgent != string.Empty)
                        {
                            chromeop.AddArgument("--user-agent=" + data.ConfigSettings.CustomUserAgent);
                        }

                        if (data.GlobalSettings.Selenium.FastStart)
                        {
                            chromeop.AddArgument("--fast-start");
                        }
                        if (data.GlobalSettings.Selenium.DisableAutomation)
                        {
                            chromeop.AddArgument("--disable-automation");
                            chromeop.AddArgument("--disable-blink-features=AutomationControlled");
                            chromeop.AddAdditionalCapability("useAutomationExtension", false);
                        }
                        if (data.ConfigSettings.DisableImageLoading)
                        {
                            chromeop.AddArgument("--disable-images");
                            chromeop.AddUserProfilePreference("profile.default_content_setting_values.images", 2);
                        }
                        if (data.UseProxies)
                        {
                            chromeop.AddArgument("--proxy-server=" + data.Proxy.Type.ToString().ToLower() + "://" + data.Proxy.Proxy);
                        }

                        data.Driver = new ChromeDriver(chromeservice, chromeop);
                        if (!string.IsNullOrWhiteSpace(url))
                        {
                            data.Driver.Url = url;
                        }
                    }
                    catch (Exception ex) { data.Log(new LogEntry(ex.ToString(), Colors.White)); return; }

                    break;

                case BrowserType.Firefox:
                    try
                    {
                        FirefoxOptions       fireop      = new FirefoxOptions();
                        FirefoxDriverService fireservice = FirefoxDriverService.CreateDefaultService();
                        FirefoxProfile       fireprofile = new FirefoxProfile();

                        fireservice.SuppressInitialDiagnosticInformation = true;
                        fireservice.HideCommandPromptWindow = true;
                        fireop.AddArgument("--log-level=3");
                        fireop.BrowserExecutableLocation = data.GlobalSettings.Selenium.FirefoxBinaryLocation;
                        if (data.GlobalSettings.Selenium.Headless || data.ConfigSettings.ForceHeadless)
                        {
                            fireop.AddArgument("--headless");
                        }
                        if (data.ConfigSettings.DisableNotifications)
                        {
                            fireprofile.SetPreference("dom.webnotifications.enabled", false);
                        }
                        if (data.ConfigSettings.DefaultProfileDirectory)
                        {
                            fireop.AddArgument("--profile-directory=Default");
                        }
                        if (data.ConfigSettings.CustomCMDArgs != string.Empty)
                        {
                            fireop.AddArgument(data.ConfigSettings.CustomCMDArgs);
                        }
                        if (data.ConfigSettings.RandomUA)
                        {
                            fireprofile.SetPreference("general.useragent.override", UserAgent.Random(data.random));
                        }
                        else if (data.ConfigSettings.CustomUserAgent != string.Empty)
                        {
                            fireprofile.SetPreference("general.useragent.override", data.ConfigSettings.CustomUserAgent);
                        }
                        if (data.GlobalSettings.Selenium.FastStart)
                        {
                            fireop.AddArgument("--fast-start");
                        }
                        if (data.GlobalSettings.Selenium.DisableAutomation)
                        {
                            fireop.AddArgument("--disable-automation");
                            fireop.AddArgument("--disable-blink-features=AutomationControlled");
                            fireop.AddAdditionalCapability("useAutomationExtension", false);
                        }
                        if (data.ConfigSettings.DisableImageLoading)
                        {
                            fireop.SetPreference("permissions.default.image", 2);
                        }

                        if (data.UseProxies)
                        {
                            fireprofile.SetPreference("network.proxy.type", 1);
                            if (data.Proxy.Type == Extreme.Net.ProxyType.Http)
                            {
                                fireprofile.SetPreference("network.proxy.http", data.Proxy.Host);
                                fireprofile.SetPreference("network.proxy.http_port", int.Parse(data.Proxy.Port));
                                fireprofile.SetPreference("network.proxy.ssl", data.Proxy.Host);
                                fireprofile.SetPreference("network.proxy.ssl_port", int.Parse(data.Proxy.Port));
                            }
                            else
                            {
                                fireprofile.SetPreference("network.proxy.socks", data.Proxy.Host);
                                fireprofile.SetPreference("network.proxy.socks_port", int.Parse(data.Proxy.Port));
                                if (data.Proxy.Type == Extreme.Net.ProxyType.Socks4)
                                {
                                    fireprofile.SetPreference("network.proxy.socks_version", 4);
                                }
                                else if (data.Proxy.Type == Extreme.Net.ProxyType.Socks5)
                                {
                                    fireprofile.SetPreference("network.proxy.socks_version", 5);
                                }
                            }
                        }

                        fireop.Profile = fireprofile;
                        data.Driver    = new FirefoxDriver(fireservice, fireop, new TimeSpan(0, 1, 0));
                        if (!string.IsNullOrWhiteSpace(url))
                        {
                            data.Driver.Url = url;
                        }
                    }
                    catch (Exception ex) { data.Log(new LogEntry(ex.ToString(), Colors.White)); return; }

                    break;
                }

                data.Driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(data.GlobalSettings.Selenium.PageLoadTimeout);
                data.Log(new LogEntry("Opened!", Colors.White));
                data.BrowserOpen = true;
            }
            else
            {
                try
                {
                    UpdateSeleniumData(data);
                }
                catch { }
            }
        }
Пример #27
0
        /// <summary>
        /// Replaces variables recursively, expanding lists or dictionaries with jolly indices.
        /// </summary>
        /// <param name="input">The string to replace variables into</param>
        /// <param name="data">The BotData needed for variable replacement</param>
        /// <returns>An array of values obtained replacing the original input with each of the possible values of the first List or Dictionary variable found</returns>
        public static List <string> ReplaceValuesRecursive(string input, BotData data)
        {
            var toReplace = new List <string>();

            // Regex parse the syntax <LIST[*]>
            var matches   = Regex.Matches(input, @"<([^\[]*)\[\*\]>");
            var variables = new List <CVar>();

            foreach (Match m in matches)
            {
                var name = m.Groups[1].Value;

                // Retrieve the dict
                var variable = data.Variables.Get(name);
                if (variable == null)
                {
                    variable = data.GlobalVariables.Get(name);
                    if (variable == null)
                    {
                        continue;
                    }
                }

                if (variable.Type == CVar.VarType.List)
                {
                    variables.Add(variable);
                }
            }

            // If there's no corresponding variable, just readd the input string and proceed with normal replacement
            if (variables.Count > 0)
            {
                var max = variables.OrderBy(v => v.Value.Count).Last().Value.Count;
                for (var i = 0; i < max; i++)
                {
                    var replaced = input;
                    foreach (var variable in variables)
                    {
                        var list = (List <string>)variable.Value;
                        if (list.Count > i)
                        {
                            replaced = replaced.Replace($"<{variable.Name}[*]>", list[i]);
                        }
                        else
                        {
                            replaced = replaced.Replace($"<{variable.Name}[*]>", "NULL");
                        }
                    }
                    toReplace.Add(replaced);
                }
                goto END;
            }

            // Regex parse the syntax <DICT(*)> (wildcard key -> returns list of all values)
            var match = Regex.Match(input, @"<([^\(]*)\(\*\)>");

            if (match.Success)
            {
                var full = match.Groups[0].Value;
                var name = match.Groups[1].Value;

                // Retrieve the list
                var dict = data.Variables.GetDictionary(name);
                if (dict == null)
                {
                    dict = data.GlobalVariables.GetDictionary(name);
                }

                // If there's no corresponding variable, just readd the input string and proceed with normal replacement
                if (dict == null)
                {
                    toReplace.Add(input);
                }
                else
                {
                    foreach (var item in dict)
                    {
                        toReplace.Add(input.Replace(full, item.Value));
                    }
                }
                goto END;
            }

            // Regex parse the syntax <DICT{*}> (wildcard value -> returns list of all keys)
            match = Regex.Match(input, @"<([^\{]*)\{\*\}>");

            if (match.Success)
            {
                var full = match.Groups[0].Value;
                var name = match.Groups[1].Value;

                // Retrieve the dict
                var dict = data.Variables.GetDictionary(name);
                if (dict == null)
                {
                    dict = data.GlobalVariables.GetDictionary(name);
                }

                // If there's no corresponding variable, just readd the input string and proceed with normal replacement
                if (dict == null)
                {
                    toReplace.Add(input);
                }
                else
                {
                    foreach (var item in dict)
                    {
                        toReplace.Add(input.Replace(full, item.Key));
                    }
                }
                goto END;
            }

            // If no other match was a success, it means there's no recursive value and we simply add the input to the list
            toReplace.Add(input);

END:
            // Now for each item in the list, do the normal replacement and return the replaced list of strings
            return(toReplace.Select(i => ReplaceValues(i, data)).ToList());
        }
Пример #28
0
        /// <summary>
        /// Replaces variables recursively, expanding lists or dictionaries with jolly indices.
        /// </summary>
        /// <param name="input">The string to replace variables into</param>
        /// <param name="data">The BotData needed for variable replacement</param>
        /// <returns>An array of values obtained replacing the original input with each of the possible values of the first List or Dictionary variable found</returns>
        public static List <string> ReplaceValuesRecursive(string input, BotData data)
        {
            var toReplace = new List <string>();

            // Regex parse the syntax <LIST[*]> (only the first one! This doesn't support multiple arrays because they can have different sizes)
            var match = Regex.Match(input, @"<([^\[]*)\[\*\]>");

            if (match.Success)
            {
                var full = match.Groups[0].Value;
                var name = match.Groups[1].Value;

                // Retrieve the dict
                var list = data.Variables.GetList(name);
                if (list == null)
                {
                    list = data.GlobalVariables.GetList(name);
                }

                // If there's no corresponding variable, just readd the input string and proceed with normal replacement
                if (list == null)
                {
                    toReplace.Add(input);
                }
                else
                {
                    foreach (var item in list)
                    {
                        toReplace.Add(input.Replace(full, item));
                    }
                }
                goto END;
            }

            // Regex parse the syntax <DICT(*)> (wildcard key -> returns list of all values)
            match = Regex.Match(input, @"<([^\(]*)\(\*\)>");

            if (match.Success)
            {
                var full = match.Groups[0].Value;
                var name = match.Groups[1].Value;

                // Retrieve the list
                var dict = data.Variables.GetDictionary(name);
                if (dict == null)
                {
                    dict = data.GlobalVariables.GetDictionary(name);
                }

                // If there's no corresponding variable, just readd the input string and proceed with normal replacement
                if (dict == null)
                {
                    toReplace.Add(input);
                }
                else
                {
                    foreach (var item in dict)
                    {
                        toReplace.Add(input.Replace(full, item.Value));
                    }
                }
                goto END;
            }

            // Regex parse the syntax <DICT{*}> (wildcard value -> returns list of all keys)
            match = Regex.Match(input, @"<([^\{]*)\{\*\}>");

            if (match.Success)
            {
                var full = match.Groups[0].Value;
                var name = match.Groups[1].Value;

                // Retrieve the dict
                var dict = data.Variables.GetDictionary(name);
                if (dict == null)
                {
                    dict = data.GlobalVariables.GetDictionary(name);
                }

                // If there's no corresponding variable, just readd the input string and proceed with normal replacement
                if (dict == null)
                {
                    toReplace.Add(input);
                }
                else
                {
                    foreach (var item in dict)
                    {
                        toReplace.Add(input.Replace(full, item.Key));
                    }
                }
                goto END;
            }

            // If no other match was a success, it means there's no recursive value and we simply add the input to the list
            toReplace.Add(input);

END:
            // Now for each item in the list, do the normal replacement and return the replaced list of strings
            return(toReplace.Select(i => ReplaceValues(i, data)).ToList());
        }