BeQuietAndDrive
BeQuietAndDrive

Reputation: 1

YARA Rule - Regex - String with at least one digit

I'm new to YARA rules and I wanted to build something really simple, a regex to match a hostname naming convention in my company.
Something like: /AX[BCD][EFG](?=.*\d)[A-Z0-9]{5}/ where the last five characters HAVE TO have at least one digit.
Is there a way to "translate" this to YARA? Keeping in mind that only basic constructs are supported:

Thanks!

Upvotes: 0

Views: 361

Answers (2)

The fourth bird
The fourth bird

Reputation: 163577

You can write the pattern with a grouping and alternation matching 5 characters checking for a digit on every position.

AX[BCD][EFG](\d[A-Z\d]{4}|[A-Z\d]\d[A-Z\d]{3}|[A-Z\d]{2}\d[A-Z\d]{2}|[A-Z\d]{3}\d[A-Z\d]|[A-Z\d]{4}\d)

If you don't want a partial match but match 9 characters in total, you can append anchors around the pattern:

^AX[BCD][EFG](\d[A-Z\d]{4}|[A-Z\d]\d[A-Z\d]{3}|[A-Z\d]{2}\d[A-Z\d]{2}|[A-Z\d]{3}\d[A-Z\d]|[A-Z\d]{4}\d)$

Regex demo

Upvotes: 1

Bohemian
Bohemian

Reputation: 425278

If length is known to be correct (that is the regex doesn't need to assert length):

/AX[BCD][EFG][A-Z\d]*\d[A-Z\d]* 

Upvotes: 0

Related Questions