Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Tutorial Working with Regular Expressions filter_list
Author
Message
Working with Regular Expressions #1
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

[Image: 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.

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

In the top box put the following expression

Code:
member[.]php[?]action=profile&uid=(103683)" title="17MOD1997">(17MOD1997)<\/a>

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

[Image: 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.

Code:
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/...work-4.7.2). You can do that by adding the following line to the head of your code

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.

Code:
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.

[Image: VzI1mcp.png]

Create a new Regex Object
Code:
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
Code:
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. 


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

[Image: vhrK4py.png]

[+] 1 user Likes sunjester's post
Reply

RE: Working with Regular Expressions #2
nice tutorial Smile! There are some websites to test regular expressions, I find them very useful when working with reg expr

Reply

RE: Working with Regular Expressions #3
Thanks for the tutorial,
I would like to share this site: https://www.rexegg.com/regex-quickstart.html
I use that site when writing regular expressions, it's very helpful

Reply

RE: Working with Regular Expressions #4
The only thing VB has going for it is the basic syntax if you grew on it so it has a sorta nostalgic value ("muh goto").

Reply







Users browsing this thread: 2 Guest(s)