Jump to content
Sign in to follow this  
sunjester

[c#] Working with Regular Expressions

3 posts in this topic

Offline

Regular expressions can be a very useful tool in your arsenal when creating hacking tools. You should use regular expressions when split just won't cut it. You would normally use String.Split when there is only one occurrence of a string in the block your searching. Regular expressions is great for finding specific strings in a block of text, and it can be MUCH faster.

You can use Regex101 to help create your regular expression, it's what I normally use. I will go over a short intro to help get you started when creating a regular expression.

Match a single digit
\d

Match multiple digits
\d+

Match X amount of digits (if you want 3 digits; {3})
\d{x}

Match a single character
\w

Match any character or digit

.

Match any digit or character multiple times
.+

Match zero or more
?


So as an example, let's open the cracked.to website and view the source. We are going to use them as an example on how to scrape stuff. Let's scrape usernames and ID's.

Around line 2955 you will start to see the usernames

49BbH4T.png

So copy that and paste it into the bottom textbox on regex101. The top box is where we will begin to write our regex. Each entry is an anchor link which has the username and their ID.

<a href="https://cracked.to/member.php?action=profile&uid=103683" title="17MOD1997">17MOD1997</a>

In the top box put the following expression


member[.]php[?]action=profile&uid=(\d+)"" title=""[a-zA-Z0-9&_\.-]+?"">([a-zA-Z0-9&_\.-]+?)<\/a>


Which should match that user (if they are logged in)

SBX9fpy.png

On the right in the image above you can see that we matched it. However, we only matched that one entry and we want them all. So now, let's replace the name and id with a regular expression.

member[.]php[?]action=profile&uid=(\d+)" title=".+?">(.+?)<\/a>

So now let's turn this into something we can use in C#. You will need to include the RegularExpression namespace (https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions?view=netframework-4.7.2). You can do that by adding the following line to the head of your code

using System.Text.RegularExpressions;

We are going to read a text file that has the source of the main cracked.to website, so we don't leave a footprint on their site while working with the regular expression demo. You can download the source I am using here (https://pastr.io/raw/NehxTX). We can read the file using the File.ReadAllLines() method. We will need to add the System.IO namespace for this.

string src = File.ReadAllLines("testsrc");

So now let's apply our expression to the file we read above. Since the file we are using as a test is much different than how the source looks in your browser (in my case, Chrome) we are going to have to change our expression. Below is the image of the block of text on which we are going to use the expression.

VzI1mcp.png

Create a new Regex Object

Regex r = new Regex(@"https:\/\/cracked[.]to\/member[.]php[?]action=profile&amp;uid=(\d+)&quot; title=&quot;([a-zA-Z0-9&_\.-]+)&quot;&gt;&lt;span");

Get the matches with MatchCollection

MatchCollection mc = r.Matches(src);


Now we can loop through the matches in the MatchCollection, showing the capture groups. The capture groups is the data in parenthesis in the regular expression. 


foreach(Match m in mc)
{
        Console.WriteLine("ID: " + m.Groups[1].Value + " \t Name: " + m.Groups[2].Value);
}

vhrK4py.png

  • Like 1

Share this post


Link to post
Share on other sites
Offline

Your a fucking genius am gonna go back to grinding on coding C# 24/7 can't let up again.

Share this post


Link to post
Share on other sites
Offline
  1. 
    
            public static List<string> ReplaceValuesRecursive(string input, BotData data)
            {
                var toReplace = new List<string>();
    
                var match = Regex.Match(input, @"<([^\[]*)\[\*\]>");
    
                if (match.Success)
                {
                    var full = match.Groups[0].Value;
                    var name = match.Groups[1].Value;
    
                    var list = data.Variables.GetList(name);
                    if (list == null) list = data.GlobalVariables.GetList(name);
    
                    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;
    
                    var dict = data.Variables.GetDictionary(name);
                    if (dict == null) dict = data.GlobalVariables.GetDictionary(name);
    
                    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;
    
                    var dict = data.Variables.GetDictionary(name);
                    if (dict == null) dict = data.GlobalVariables.GetDictionary(name);
    
                    if (dict == null) toReplace.Add(input);
                    else
                    {
                        foreach (var item in dict)
                            toReplace.Add(input.Replace(full, item.Key));
                    }
                    goto END;
                }
    
                toReplace.Add(input);
    
                END:
    
    
    

     

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

About us

3rdWorld.cc is a cracking forum community that suits the purpose of everyone. We offer paid program for free, cracking tools, combolists, marketplace and many more. You can also expand on knowledge and participate in active discussions.

×