0

I am trying to run a regex command to parse through incoming emails to identify any hyperlinks that have been sent by phony/phishing sites attempting to use a variation of our company name. Our company is abcdomain. The criteria is:

  1. Find all hyperlinks (hostname portion) that contain our company name 'abcdomain'
  2. Exclude all domains that we have registered ourselves, i.e. myabcdomain.com,site.abcdomain.com,abcdomain.net, etc.)

My regex are not formed well and do not work under some variations. The trouble is probably related to the way I exclude. At first, I was really trying to exclude 'abcdomain.com' but regex did not seem to work that way.

  • Query: (http[s]?|ftp)\S*?(abcdomain|myabcdomain)(?!\.com)
  • String: http://www.abcdomain.com/logo/email/abcdomain-email-logo.png
  • Result: this should be excluded but the query hits on the second abcdomain


  • Query: (http[s]?|ftp):\/\/([\dA-Za-z\.]*)(abcdomain|myabcdomain)(?!.com|..net)

  • String: http://www.fakeabcdomain.com
  • Result: this should be included but my exclusion only looks after the hit result)
2
  • Just wanted to show examples of what should hit and my own progress. regexr.com/3trtg Aug 13, 2018 at 12:50
  • It seems like there could be an infinite number of ways to bypass a regex search, no matter how carefully the regex is crafted. Are there other options available to you? You may be able to filter by geographic ip location of senders, possibly trace the hyperlink and see if it connects to an internal (whitelisted) ip, trace the hyperlink and see if it generates a packet request within your server/domain controller (as sort of a handshake to verify the link), or some other form of check which may be more secure. Is regex all you have the option to use?
    – Trenly
    Aug 14, 2018 at 13:37

1 Answer 1

0

This one works for your test cases:

(?:ht|f)tps?://(?=[^/]*(?:myabcdomain|abcabcdomain|abcdomain(?:\.zendesk)?))(?!(?:\w+\.)?(?:myabcdomain|abcabcdomain|abcdomain(?:\.zendesk)?)\.com)\S+

Explanation:

(?:ht|f)tps?://         : protocol
(?=                     : positive lookahead, make sure we have after
  [^/]*                 : 0 or more non slash
  (?:                   : start non capture group
    myabcdomain         : literally
  |                     : OR
    abcabcdomain        : literally
  |                     : OR
    abcdomain           : literally
    (?:\.zendesk)?      : followed with optional
  )                     : end group
)                       : end lookahead
(?!                     : negative lookahead, make sure we don't a=have after
  (?:\w+\.)?            : optional, 1 or more word character and a dot
  (?:                   : start non capture group
    myabcdomain         : literally
  |                     : OR
    abcabcdomain        : literally
  |                     : OR
    abcdomain           : literally
    (?:\.zendesk)?      : followed with optional
  )                     : end group
  \.com                 : literally
)                       : end lookahead
\S+                     : 1 or more any character that is not a space

It matches:

<a href="http://abcdomain.products.com.vbs">
<a href="https://abcdomainproducts.com">
<a href="http://products.abcdomain.products.net">
<a href="https://products.abcdomainproducts.com/test">
<a href="http://fakeabcdomain.products.com.vbs">
<a href="http://myabcdomain.products.com.vbs">
<a href="http://fakeabcdomain.com">

And doesn't match:

<a href="http://products.myabcdomain.com/help">
<a href="http://abcdomain.zendesk.com/help">
<a href="http://myabcdomain.com/help">
<a href="http://abcdomain.com/help">
<a href="http://products.abcabcdomain.com">
5
  • Sorry, this gives a hit on myabcdomain. Here are examples that I am using: regexr.com/3trsr Aug 13, 2018 at 12:30
  • @DavidRubino: It is confusing why http://products.abcdomain.products.net should match but not http://abcdomain.zendesk.com/help
    – Toto
    Aug 13, 2018 at 12:49
  • sorry, that was another link that I was testing. After I posted my comment, I saw that a group had set up a zendesk link that I also had to exclude. Aug 13, 2018 at 12:54
  • 1
    @DavidRubino: I can't find a simple way to do that with a single regex. I suggest you split the string before the first slash and check for every element.
    – Toto
    Aug 13, 2018 at 13:17
  • @DavidRubino: See my edit, is it OK now?
    – Toto
    Aug 14, 2018 at 13:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .