Last active
May 18, 2021 19:04
-
-
Save MeinLiX/f67ac4cdcd05816d38c37302752bf0fe to your computer and use it in GitHub Desktop.
Example parse status code by brute force web pages. C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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