【问题标题】:CF-Hash attribute and script mysteriously added to mailto linksCF-Hash 属性和脚本神秘地添加到 mailto 链接
【发布时间】:2015-02-15 06:27:57
【问题描述】:

我有一个开发网站和生产网站:

我在底部有一个ma​​ilto邮箱链接,php源码如下:

           <section>
               <h2>Looking for a LAMP, WordPress or Drupal Developer?</h2>
               <p>Contact me today: <br/>
                <a href='mailto:mail@example.com'>mail@example.com</a>
                   <br/>
                <a href='tel:+13334445555'>333 444 5555</a>
               </p>
           </section>

我的开发站点一切正常,生成的html如下:

                <section>
                    <h2>Looking for a LAMP, WordPress or Drupal Developer?</h2>
                    <p>Contact me today: <br>
                        <a href="mailto:mail@example.com">mail@example.com</a>
                        <br>
                        <a href="tel:+13334445555">333 444 5555</a>
                    </p>
                </section>

然后神秘地在我的生产站点上添加了一些 javascript 我的 mailto 链接(并且只有 mailto 链接,在这种情况下只是一个,但我添加了更多并且脚本也添加到它们)这是 html 输出在生产现场:

<section>
<h2>Looking for a LAMP, WordPress or Drupal Developer?</h2>
<p>Contact me today: <br>
<a href="mailto:mail@example.com">mail@example.com
<script cf-hash="f9e31" type="text/javascript">
/* <![CDATA[ */!function(){try{var t="currentScript"in document?document.currentScript:function()        {for(var t=document.getElementsByTagName("script"),e=t.length;e--;)if(t[e].getAttribute("cf-hash"))return t[e]}();if(t&&t.previousSibling){var e,r,n,i,c=t.previousSibling,a=c.getAttribute("data-cfemail");if(a){for(e="",r=parseInt(a.substr(0,2),16),n=2;a.length-n;n+=2)i=parseInt(a.substr(n,2),16)^r,e+=String.fromCharCode(i);e=document.createTextNode(e),c.parentNode.replaceChild(e,c)}}}catch(u){}}();/* ]]> */
</script>
</a>
<br>
<a href="tel:+13334445555">333 444 5555</a>
</p>
</section>

我检查了我的生产服务器上的代码,这个脚本不存在。

可能发生了什么?

【问题讨论】:

  • 只是猜测,但您使用 ColdFusion 吗?
  • 不,我没有,我检查了我的服务器它没有安装在那里(至少我找不到它)。

标签: javascript php html mailto cloudflare


【解决方案1】:

这与 ColdFusion 无关。在此代码中,cf-hashSCRIPT 标记(纯 HTML)的属性。搜索 'cf-hash="f9e31" 会得到很多类似的代码。发现 this link 指出它可能是 CloudFlare 电子邮件保护脚本。那将是在您的生产服务器上运行的东西,而不是在您的本地开发环境中。

【讨论】:

  • 谢谢阿德里安! Cloudflare 正在混淆电子邮件(为了保护我),我能够添加电子邮件混淆功能并将数据附加到 cf-hash(保留该功能并使其有效 html5)。 support.cloudflare.com/hc/en-us/articles/…
【解决方案2】:

CloudFlare 默认情况下会混淆您的电子邮件地址。如果您想忽略电子邮件的混淆,只需将它们包装在 HTML 注释标签中,就像这样。 CloudFlare 将忽略这些。

<!--email_off-->EMAIL ADDRESS<!--/email_off-->

来源:http://roaringapps.com/blog/cloudflare-email-obfuscation/

【讨论】:

  • 这个答案对我有帮助,但仅供参考,您的语法是错误的。应该是&lt;!--email_off--&gt;email here&lt;!--/email_off--&gt;
【解决方案3】:

可以用不同的语言对电子邮件进行去混淆处理:

Javascript

function cfDecodeEmail(encodedString) {
    var email = "", r = parseInt(encodedString.substr(0, 2), 16), n, i;
    for (n = 2; encodedString.length - n; n += 2){
        i = parseInt(encodedString.substr(n, 2), 16) ^ r;
        email += String.fromCharCode(i);
    }
    return email;
}

console.log(cfDecodeEmail("543931142127353935313e352e7a373b39")); // usage

Python

def cfDecodeEmail(encodedString):
    r = int(encodedString[:2],16)
    email = ''.join([chr(int(encodedString[i:i+2], 16) ^ r) for i in range(2, len(encodedString), 2)])
    return email

print( cfDecodeEmail('543931142127353935313e352e7a373b39') ) # usage

PHP

function cfDecodeEmail($encodedString){
  $k = hexdec(substr($encodedString,0,2));
  for($i=2,$email='';$i<strlen($encodedString)-1;$i+=2){
    $email.=chr(hexdec(substr($encodedString,$i,2))^$k);
  }
  return $email;
}

echo cfDecodeEmail('543931142127353935313e352e7a373b39'); // usage

package main

import (
    "bytes"
    "strconv"
)

func cf(a string) (s string) {
    var e bytes.Buffer
    r, _ := strconv.ParseInt(a[0:2], 16, 0)
    for n := 4; n < len(a)+2; n += 2 {
        i, _ := strconv.ParseInt(a[n-2:n], 16, 0)
        e.WriteString(string(i ^ r))
    }
    return e.String()
}

func main() {
    email := cf("543931142127353935313e352e7a373b39") // usage
    print(email)
    print("\n")
}

C++

#include <iostream>
#include <string>

using namespace std;

string cfDecodeEmail(string encodedString);

int main()
{
    cout << cfDecodeEmail("543931142127353935313e352e7a373b39") << endl;
}

string cfDecodeEmail(string encodedString)
{
    string email;
    char xorKey = stoi( encodedString.substr(0, 2), nullptr, 16);
    for( unsigned i = 2; i < encodedString.length(); i += 2)
        email += stoi( encodedString.substr(i, 2), nullptr, 16) ^ xorKey;

    return email;
}

C

using System;

public class Program
{
    public static string cfDecodeEmail(string encodedString)
    {
        string email = "";
        int r = Convert.ToInt32(encodedString.Substring(0, 2), 16), n, i;
        for (n = 2; encodedString.Length - n > 0; n += 2)
        {
            i = Convert.ToInt32(encodedString.Substring(n, 2), 16) ^ r;
            char character = (char)i;
            email += Convert.ToString(character);
        }

        return email;
    }

    public static void Main(string[] args)
    {
        Console.WriteLine(cfDecodeEmail("543931142127353935313e352e7a373b39")); // usage
    }
}


Source

【讨论】:

    【解决方案4】:

    Cloudflare 隐藏电子邮件地址以防止机器人从网页中抓取它们。

    如果您是普通网络用户而不是机器人,您将运行 JavaScript。 Cloudflare 注入 JavaScript,可解读电子邮件地址。

    某些网页不允许内联 JavaScript 运行,因此最终用户无法看到电子邮件地址。

    考虑改变网站发出的 Content_Security-Policy 元标记,以允许运行内联 JavaScript。

    例如查看“不安全内联”的使用;

    “从 Chrome 46 开始,可以通过在策略中指定源代码的 base64 编码散列来将内联脚本列入白名单。此散列必须以使用的散列算法(sha256、sha384 或 sha512)作为前缀。请参阅散列使用以元素为例。”

    更多有用的信息在这里:https://developer.chrome.com/extensions/contentSecurityPolicy

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 2011-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-07
      相关资源
      最近更新 更多