DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Security by Design: Building Full-Stack Applications With DevSecOps
  • How GitHub Copilot Helps You Write More Secure Code
  • Proactive Security in Distributed Systems: A Developer’s Approach
  • Analyzing Techniques to Provision Access via IDAM Models During Emergency and Disaster Response

Trending

  • Caching 101: Theory, Algorithms, Tools, and Best Practices
  • Understanding the Shift: Why Companies Are Migrating From MongoDB to Aerospike Database?
  • Modern Test Automation With AI (LLM) and Playwright MCP
  • The Perfection Trap: Rethinking Parkinson's Law for Modern Engineering Teams
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. Regular Expressions: A Quick Intro for Security Professionals

Regular Expressions: A Quick Intro for Security Professionals

RegEx is a powerful tool for creating string-based search queries based on a combination of constants and operators. Check out this introduction here.

By 
Vickie Li user avatar
Vickie Li
·
Jul. 23, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
10.4K Views

Join the DZone community and get the full member experience.

Join For Free

As someone who works in security, you often have to deal with large quantities of information (in the form of log files, internet packets, etc.). You also likely have to design firewall rules, analyze internet traffic, or set up malware scanners.

Regex is the most underrated security skill that can help you accomplish these tasks. This article is going to break down the most useful RegEx tips and show you how to use them to automate complex tasks.

Regex Syntax Overview

A regular expression, or “RegEx”, is a special string that describes a search pattern.

You can think of RegEx as consisting of two different parts: constants and operators. Constants are sets of strings, while operators are symbols that denote operations over these strings. These two elements together make RegEx a powerful tool of pattern matching.

For example, the regular expression below matches every IP address from subnet 192.168.0.0/24. The highlighted portions are constants, meaning that the RegEx will match the highlighted strings literally. The rest of the RegEx are operators: they have special meanings and add flexibility to the pattern matching.

Shell
 




xxxxxxxxxx
1


 
1
192\.168\.0\.\d{1,3}


Different Regex Flavors

Before we dive into the basics of RegEx syntax, please note that RegEx has many different versions. Different software and RegEx engines will often have their own specificities, and it's best to check the official documentation pages for a full reference of the RegEx version that you are using. However, all RegEx tends to build upon the same set of generic rules.

Regex Operators

First off, these RegEx operators match with single characters:

  • \d: matches any digit
  • \w: matches any character
  • \s: matches any whitespace
  • .: matches with any single character
  • \: escapes a special character

We also have a number of operators that specify the number of characters we are matching:

  • *: zero or more
  • +: one or more
  • {3}: exactly three times
  • {1, 3}: one to three times
  • {1, }: one or more times
  • [abc]: one of the characters within the brackets
  • [a-z]: one of the characters within the range of a - z
  • (a|b|c): either a or b or c

There are a lot more advanced RegEx features that you can use to perform more sophisticated matching. However, the simple set of operators above serves well for most security purposes.

For a complete guide to RegEx syntax, read RexEgg's cheat sheet.

Harness the Power of Regex

So what can we do with RegEx? Here are just a few of the many use cases of RegEx in your day-to-day tasks!

Logfile Analysis with Regex

One of the ways you can use RegEx is to perform complex text searches. This means RegEx is very useful during the analysis of log files: instead of searching for simple terms, you can use RegEx to quickly find more accurate results.

For example, let's say that your logfile entries are in this format:

Plain Text
 




x


 
1
Mar 2 12:55:09 HOST_MACHINE PROCESS LOG_MESSEGE


With RegEx, we can quickly find all the processes that ran during a specific time frame. This RegEx will match with all log entries that have the timestamp between 12 and 2 PM on March 2nd.

Plain Text
 




xxxxxxxxxx
1


 
1
Mar 2 1[2-4]:[0-9]{2}:[0-9]{2}


You can also use RegEx to find all the IP addresses that show up in access logs. You can do something like this, which will match with all IP addresses in the log file.

Plain Text
 




xxxxxxxxxx
1


 
1
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}


Combine a couple of different metrics (IP ranges, timestamp, hostnames, and usernames) and you'll have an extremely powerful log analysis utility that you can fully customize!

Firewall Rules and Input Validation with Regex

Many people use RegEx to specify firewall rules. For example, you can use RegEx to create rules to block requests to certain file types. This RegEx will match with any request that contains the terms "json", "exe", "tar" and "rar".

Shell
 




xxxxxxxxxx
1


 
1
(json|exe|tar|rar)


A sound firewall rule will use a RegEx pattern like the above but with a wide range of file types, while also accounting for possible bypasses such as case changes and the inclusion of non-ASCII characters.

If you are a developer, you will also often need RegEx to deal with input validation in your programs. For example, the code below will reject any user input that contains non-alphanumeric characters and is longer than 50 characters.

Closure Stylesheets (GSS)
 




xxxxxxxxxx
1


 
1
check_username:
2
 if RegEx_match("[a-zA-Z0-9]{1,50}", user_input):
3
   // Continue processing if the input is valid
4
 else:
5
   // Stop processing and reject the request


Proxy Rules with Regex

Regex can also be useful when you debug or test your applications. For example, using effective RegEx to filter traffic on debugging proxies can make your work a lot more efficient. Instead of churning through endless requests flowing through your proxy windows (which is a gigantic time-suck), you can isolate the requests going to a specific subdomain of your site like this:

Shell
 




xxxxxxxxxx
1


 
1
[A-Za-z0–9]*\.dev\.site\.com


Malware Scanning with Regex

Finally, RegEx is also one of the most powerful tools used for identifying malware.

For example, YARA is a tool that identifies malware by creating descriptions that look for certain characteristics. It uses RegEx patterns to detect specific text or binary patterns in files that might indicate that the file is malicious.

Shell
 




xxxxxxxxxx
1
12


 
1
rule example_malware : example
2
{
3
  meta:
4
    description = "This is just an example."
5
    threat_level = 3
6
    in_the_wild = true
7
  strings:
8
    $a = /evil software version: [0-9a-zA-Z]{32}/
9
    $b = "Malware Inc"
10
  condition:
11
    $a and $b
12
}


This example rule states that any file that contains the strings "Malware Inc" and "evil software version: [0–9a-zA-Z]{32}" is suspected to be a piece of malware.

To learn more about how YARA detects malware, read my Intro to Malware Detection Using YARA.

Useful Regex Resources

Here are a few resources to help you build your RegEx skills!

To build solid RegEx skills, follow these amazing RegEx tutorials.

For some practice writing regular expressions, play the RegexOne game.

Sometimes, you can't be sure if your regular expression matches exactly what you are looking for. So to test your RegEx strings, use the Regex101 RegEx tester. And here's a great RegEx cheat sheet if you ever forget what a particular operator means.

Finally, don't forget to check out the documentation of your particular RegEx dialect before you dive into constructing RegEx strings!

security

Opinions expressed by DZone contributors are their own.

Related

  • Security by Design: Building Full-Stack Applications With DevSecOps
  • How GitHub Copilot Helps You Write More Secure Code
  • Proactive Security in Distributed Systems: A Developer’s Approach
  • Analyzing Techniques to Provision Access via IDAM Models During Emergency and Disaster Response

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!