public void TestMethod_Crypto() { //要加密的原始数据 byte[] data = new byte[1024 * 1024 + 7];//假设有一个1M大的文件 for (int i = 0; i < data.Length; i++) { data[i] = (byte)(i % 256); } //使用xuexue这个文本来随便生成一个密钥 MemoryStream msKey = new MemoryStream(); StreamWriter sw = new StreamWriter(msKey); while (msKey.Length < 16) { sw.Write("xuexue"); sw.Flush(); } msKey.SetLength(16); byte[] key = msKey.ToArray();//生成一个16字节的密钥 MemoryStream msIn = new MemoryStream(data); //使用原始数据生成一个流 MemoryStream msAES = new MemoryStream(); //加密后的数据 int elen = Crypto.AESEncrypt(msIn, data.Length, msAES, key); msAES.Position = 0; //把加密后的数据流置回头 MemoryStream msEDResult = new MemoryStream(); //解密的结果流 int dlen = Crypto.AESDecrypt(msAES, msEDResult, key); byte[] dataEDResult = msEDResult.ToArray(); //确认结果和原先是否一致 Assert.IsTrue(data.Length == dataEDResult.Length); for (int i = 0; i < data.Length; i++) { Assert.IsTrue(data[i] == dataEDResult[i]); } }
/// <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.RSAPKCS1PAD2: outputString = Crypto.RSAPkcs1Pad2( localInputString, ReplaceValues(RsaN, data), ReplaceValues(RsaE, data) ); 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); }
/// <inheritdoc /> public override async Task Process(LSGlobals ls) { var data = ls.BotData; await base.Process(ls); var localInputStrings = ReplaceValuesRecursive(InputString, ls); var outputs = new List <string>(); for (var i = 0; i < localInputStrings.Count; i++) { var localInputString = localInputStrings[i]; var outputString = ""; switch (FunctionType) { case Function.Constant: outputString = localInputString; break; case Function.Base64Encode: outputString = Base64Converter.ToBase64String(Encoding.UTF8.GetBytes(localInputString)); break; case Function.Base64Decode: outputString = Encoding.UTF8.GetString(Base64Converter.ToByteArray(localInputString)); break; case Function.HTMLEntityEncode: outputString = WebUtility.HtmlEncode(localInputString); break; case Function.HTMLEntityDecode: outputString = WebUtility.HtmlDecode(localInputString); break; case Function.Hash: outputString = GetHash(localInputString, HashType, InputBase64).ToLower(); break; case Function.HMAC: outputString = Hmac(localInputString, HashType, ReplaceValues(HmacKey, ls), InputBase64, KeyBase64, 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).ToUnixTime().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: outputString = UseRegex ? Regex.Replace(localInputString, ReplaceValues(ReplaceWhat, ls), ReplaceValues(ReplaceWith, ls)) : localInputString.Replace(ReplaceValues(ReplaceWhat, ls), ReplaceValues(ReplaceWith, ls)); break; case Function.RegexMatch: outputString = Regex.Match(localInputString, ReplaceValues(RegexMatch, ls)).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 = long.Parse(localInputString).ToDateTimeUtc().ToString(DateFormat); break; case Function.CurrentUnixTime: outputString = DateTime.UtcNow.ToUnixTime().ToString(); break; case Function.UnixTimeToISO8601: outputString = long.Parse(localInputString).ToDateTimeUtc().ToISO8601(); break; case Function.RandomNum: var min = int.Parse(ReplaceValues(RandomMin, ls)); var max = int.Parse(ReplaceValues(RandomMax, ls)); 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, @"\?n", m => _ldChars[data.Random.Next(_ldChars.Length)].ToString()); outputString = Regex.Replace(outputString, @"\?i", m => _ludChars[data.Random.Next(_ludChars.Length)].ToString()); outputString = Regex.Replace(outputString, @"\?f", m => _upperlwr[data.Random.Next(_upperlwr.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, ls), ReplaceValues(RsaE, ls), RsaOAEP ); break; /* * case Function.RSADecrypt: * outputString = Crypto.RSADecrypt( * localInputString, * ReplaceValues(RsaN, data), * ReplaceValues(RsaD, data), * RsaOAEP * ); * break; */ case Function.RSAPKCS1PAD2: outputString = Crypto.RSAPkcs1Pad2( localInputString, ReplaceValues(RsaN, ls), ReplaceValues(RsaE, ls) ); break; case Function.Delay: try { Thread.Sleep(int.Parse(localInputString)); } catch { } break; case Function.CharAt: outputString = localInputString.ToCharArray()[int.Parse(ReplaceValues(CharIndex, ls))].ToString(); break; case Function.Substring: outputString = localInputString.Substring(int.Parse(ReplaceValues(SubstringIndex, ls)), int.Parse(ReplaceValues(SubstringLength, ls))); break; case Function.ReverseString: var charArray = localInputString.ToCharArray(); Array.Reverse(charArray); outputString = new string(charArray); break; case Function.Trim: outputString = localInputString.Trim(); break; case Function.GetRandomUA: outputString = data.Providers.RandomUA.Generate(); break; case Function.AESEncrypt: outputString = Crypto.AESEncrypt(localInputString, ReplaceValues(AesKey, ls), ReplaceValues(AesIV, ls), AesMode, AesPadding); break; case Function.AESDecrypt: outputString = Crypto.AESDecrypt(localInputString, ReplaceValues(AesKey, ls), ReplaceValues(AesIV, ls), AesMode, AesPadding); break; case Function.PBKDF2PKCS5: outputString = Crypto.PBKDF2PKCS5(localInputString, ReplaceValues(KdfSalt, ls), KdfSaltSize, KdfIterations, KdfKeySize, KdfAlgorithm); break; } data.Logger.Log($"Executed function {FunctionType} on input {localInputString} with outcome {outputString}", LogColors.GreenYellow); // Add to the outputs outputs.Add(outputString); } var isList = outputs.Count > 1 || InputString.Contains("[*]") || InputString.Contains("(*)") || InputString.Contains("{*}"); InsertVariable(ls, IsCapture, isList, outputs, VariableName, "", "", false, true); }
/// <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); }