【问题标题】:RegEx - phishing attempts in HTMLRegEx - HTML 中的网络钓鱼尝试
【发布时间】:2020-01-16 02:30:23
【问题描述】:

我需要你的帮助:(
我想要什么:
如果 url.text AND url.href 都包含不相等的 URL(没有协议和子域),则匹配字符串。

它应该像这样工作:

<a href="http://www.test1.net/dir1/index.html" target="_blank">test1.net/admin</a> <-- NOT MATCH
<a href="https://test2.com">THIS SITE</a> <-- NOT MATCH
<a href="https://subdomain.test3.org">test2.org</a> <-- MATCH
<a href="http://www2.test4.com" target="_blank">https://global.test4.com/index.html</a> <-- NOT MATCH
<a href="http://eu.test5.com">https://evil.com/eu.test5.com/</a> <-- MATCH
<a href="http://eu.site6.com/index.html" target="_blank">https: // eu. evil. com</a> <-- MATCH
<a href="https://site7.com/">http://www.site7.com/123/test</a> <-- NOT MATCH

我开始写类似this 的东西,但是我的代码做相反的事情时遇到了问题。
帮我弄清楚如何制作我想要的东西。

【问题讨论】:

  • Regex is not the best fit to use on HTML。你不能改用 HTML 解析器吗?
  • 我可以帮助处理正则表达式。但是你必须解释这意味着I had a problem with my code doing the opposite。展示一些您要做想要匹配的具体示例以及WHY
  • @Ivar 不,我不能使用除了 RegEx 之外的任何东西 :(
  • @sin 如果 url.text 和 url.href 相等,我共享的代码会标记字符串。我不需要这个。我需要在上面的“代码”部分中标记不相等的东西。
  • @refrigerator - 我实际上要求提供一些确切的示例 with 解释,因为看到您的示例存在矛盾。而且,尽管我们想提供帮助,但没有人愿意浪费时间......

标签: regex url href pcre


【解决方案1】:

您的原始表达设计得非常好,但我会使用一些语句,例如:

(?!.*\1.*)

或:

(?!((?:https?:\/\/)?(?:w{3}\.)?(?:[^"\/]*\.)?(\1)).*)

内,绕过url.text中的同一个域,可能有一些类似于:

(?i)<a\s+href="(?:https?:\/\/)?(?:w{3}\.)?(?:[^"\/]*\.)?([a-z0-9_-]+\.[a-z0-9_-]{2,6})(\/[^"]*)?"[^>]*>(?!.*\1.*)(?:https?:\/\/)?(?:w{3}\.)?(?:[^"\/]*\.)?([a-z0-9_-]+\.[a-z0-9_-]{2,6})(\/[^"]*)?.*?<\/a>

或者更准确地说:

(?i)<a\s+href="(?:https?:\/\/)?(?:w{3}\.)?(?:[^"\/]*\.)?([a-z0-9_-]+\.[a-z0-9_-]{2,6})(\/[^"]*)?"[^>]*>(?!((?:https?:\/\/)?(?:w{3}\.)?(?:[^"\/]*\.)?(\1)).*)(?:https?:\s*\/\/\s*)?(?:\s*w{3}\.\s*)?(?:[^"\/]*\.\s*)?([a-z0-9_-]+\s*\.\s*[a-z0-9_-]{2,6}\s*)(\/[^"]*)?.*?<\/a>

您最有可能想要修改并更改边界。例如,您可以添加\s* 任何您想要允许一些空格的地方,或者可以使用双界量词\s{0,5}

Demo


如果您希望简化/修改/探索表达式,在regex101.com 的右上角面板中已对此进行了说明。如果您愿意,您还可以在this link 中观看它如何与一些示例输入匹配。


【讨论】:

  • 太棒了!但第三场比赛不能正常工作。您正在删除 url.text 中单词之间的空格,但我需要它们。 DEMO
猜你喜欢
  • 2014-12-27
  • 2023-04-10
  • 2012-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
相关资源
最近更新 更多