Skip to content

Instantly share code, notes, and snippets.

@MeinLiX
Last active May 18, 2021 19:04
Show Gist options
  • Save MeinLiX/f67ac4cdcd05816d38c37302752bf0fe to your computer and use it in GitHub Desktop.
Save MeinLiX/f67ac4cdcd05816d38c37302752bf0fe to your computer and use it in GitHub Desktop.
Example parse status code by brute force web pages. C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
namespace ParseCorrectPages
{
class Source
{
public List<string> CorrectUrlList { get; private set; } = new();
private readonly string _baseUrl;
private enum Letters
{
start = 48,
end = 57
}
public Source(string baseUrl)
{
if (!Uri.IsWellFormedUriString(baseUrl, UriKind.Absolute))
throw new Exception("Incorrect URL");
_baseUrl = baseUrl;
}
public void PrintValidUrl()
{
Console.WriteLine("\t***\nCorrect urls:");
CorrectUrlList.ForEach(i => Console.WriteLine(i));
Console.WriteLine("\nend\n\t***");
}
public Task BruteForce(int CountOfRandomLetters = 3, char stamph = '~')
{
int replaceIdx = _baseUrl.IndexOf(stamph);
if (replaceIdx == -1 || CountOfRandomLetters < 0 || CountOfRandomLetters > int.MaxValue)
return Task.CompletedTask;
List<char> countOfLetters = Enumerable.Range(1, CountOfRandomLetters).Select(i => (char)Letters.start).ToList();
List<Task> taskList = new();
while (true)
for (int l = 0; l < countOfLetters.Count; l++)
{
if (countOfLetters[l] >= (char)Letters.end)
{
if ((l + 1) >= countOfLetters.Count)
{
Task.WaitAll(taskList.ToArray());
return Task.CompletedTask;
}
countOfLetters[l] = (char)Letters.start;
if (l + 1 < countOfLetters.Count)
countOfLetters[l + 1]++;
}
string currentUrl = _baseUrl[0..replaceIdx] + string.Join("", countOfLetters) + _baseUrl[(replaceIdx + 1)..];
taskList.Add(Task.Factory.StartNew(() =>
{
HttpWebRequest HWReq = WebRequest.Create(currentUrl) as HttpWebRequest;
HWReq.AllowAutoRedirect = false;
HWReq.UserAgent = "secret.agent";
try
{
using HttpWebResponse HWRes = (HttpWebResponse)HWReq.GetResponse();
if (HWRes.StatusCode == HttpStatusCode.OK)
{
CorrectUrlList.Add(currentUrl);
Console.WriteLine($"OK: {currentUrl}");
}
else
{
Console.WriteLine($"Fail: {currentUrl}");
}
}
catch (Exception e) { Console.WriteLine(e.Message); }
}));
countOfLetters[0]++;
}
}
}
class Program
{
static void Main(string[] args)
{
try
{
Source src = new(@"https://store.steampowered.com/app/~");
src.BruteForce(6);
src.PrintValidUrl();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment