关于联通(网通)DNS劫持广告的屏蔽办法.

最近接到投诉,说我的网站老弹出广告,而且是死对头家的广告. 经过仔细研究,终于发现了猫腻.

 

关于dns劫持我说明一下,过程是这样的, 首先用户发送请求到你的网站服务器,网站服务器响应请求,响应结果需要返回给用户,

在这个时候,dns服务器就开始搞鬼了,不定期植入一些你网页代码中包含的关键字的广告,这就是为什么我页面中会弹出对家的广告的原因了.

因为我们做的都是相同的服务产品.

 

下面是联通植入到我网站页面内的js代码:

 

/* $Rev: 145 $ $Date: 2010-04-29 14:20:48 -0700 (Thu, 29 Apr 2010) $ */ (function() { try { var d = document, h = window, x, z = (navigator.userAgent.indexOf("Opera") >= 0) && parseFloat(navigator.appVersion), p = ((d.all) && (!z)) && parseFloat(navigator.appVersion.split("MSIE ")[1].split(";")[0]), A = (!p ? 1024: (d.documentMode ? d.documentMode: (d.compatMode && d.compatMode === "CSS1Compat" ? p: (d.compatMode && d.compatMode !== "CSS1Compat" ? 5: p)))), t = 300, k = 2147483647, g = "", l = '', s = ""; if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(D, C) { C = C || 0; var e = this.length, B; if (C < 0) { C += e } for (B = C; B < e; B++) { if (this[B] === D) { return B } } return - 1 } } function m(D, G, B, e, F) { F = F || d; var E = F.createElement(D), C, H; if (B) { H = F.createTextNode(B); E.appendChild(H) } if (e) { for (C in e) { if (e.hasOwnProperty(C)) { E.setAttribute(C, e[C]) } } } G.appendChild(E); return E } function j(C, B) { B = B || d; if (!B.createStyleSheet) { m("style", B.getElementsByTagName("head")[0], C) } else { var e = B.createStyleSheet(""); e.cssText = C } } function c() { return new Date().getTime() } function b(e) { if (d && d.body && (typeof d.readyState === "undefined" || d.readyState === "complete" || d.readyState === 4)) { e() } else { x = setInterval(function() { if (d && d.body && (typeof d.readyState === "undefined" || d.readyState === "complete" || d.readyState === 4)) { clearInterval(x); e() } else { if (--t < 0) { clearInterval(x) } } }, 100) } } function f(B) { B = B || d; var e = "f", C; do { C = (e + Math.random()).replace(".", "_") } while (B.getElementById(C)); return C } function a(F, I, C, G) { var e, H, D = G.createElement("iframe"), E = location; if (p && E.hostname != document.domain) { D.src = E.protocol + "//" + E.host } function B() { if (B.gone) { return } B.gone = true; clearInterval(e); D.onload = null; D.onreadystatechange = null; C(D.contentWindow) } for (H in I) { if (I.hasOwnProperty(H)) { D.setAttribute(H, I[H]) } } if (A === 7 && I.height === "100%") { F.attachEvent("onresize", function() { D.style.height = F.offsetHeight + "px" }) } if (C) { D.onload = B; D.onreadystatechange = function() { if (D.readyState === "complete") { B() } }; if (!p) { e = setInterval(function() { if (D.contentWindow) { B() } }, 200) } } F.appendChild(D); return D } function r() { if (r.settings) { return r.settings } var D = "/floating-frame.", e = d.getElementsByTagName("script"), C, B; for (C = 0; C < e.length; C++) { if (e[C].src && e[C].src.indexOf(D) !== -1) { B = e[C].src; break } } if (!B) { return null } D = B.lastIndexOf("?"); if (D === -1) { return null } D = B.substring(D + 1); if (D && h[D] && h[D].sprite_img) { C = r.settings = h[D]; return C } return null } function v() { var e, D = ["border_top_width", "border_right_width", "border_bottom_width", "border_left_width", "message_html", "content_url", "allow_content_transparency", "allow_content_scroll", "animate_out_duration", "animate_in_duration", "visible_duration", "delay_duration", "ok_to_show", "on_ready_to_show", "top_offset_right", "top_offset_left", "bottom_offset_right", "bottom_offset_left", "top_offset_vertical", "bottom_offset_vertical", "opaque_outer_frame"], C = ["animation_properties"], B = r(); if (B === null) { return null } B.fp = B._accounting; if (!B.fp || !B.fp.stopTime || isNaN(B.fp.stopTime) || c() > B.fp.stopTime || !B.fp.sendAccounting) { return null } for (e = 0; e < D.length; e++) { if (!B[D[e]]) { B[D[e]] = 0 } } for (e = 0; e < C.length; e++) { if (typeof B[C[e]] !== "object" || !B[C[e]].join) { B[C[e]] = [] } } if (typeof B.close_horizontal_padding !== "number") { B.close_horizontal_padding = B["border_" + B.close_align + "_width"] } if (B.outer_width === -100) { B.anchor_corner = B.anchor_corner.charAt(0) + "c" } if (B.outer_height === -100) { B.anchor_corner = "c" + B.anchor_corner.charAt(1) } return B } function y(D, B, J, M) { var e, K, N, O, F, E, L, C, I = [], H, G = 31; if (!p || p > 6) { L = B.style; L.position = "relative"; L.zIndex = k - 1; while (L.zIndex != k - 1 && G > 0) { k = Math.pow(2, --G) - 1; L.zIndex = k - 1 } L.position = "static" } H = ["nw", "sw", "se", "ne"]; for (G = 0; G < H.length; G++) { e = H[G]; if (D[e + "_corner_position"]) { O = ["position:absolute;overflow:hidden"]; if (e.charAt(0) === "n") { O.push("top:0;z-index:" + (k - 4)); C = "border_top_width" } else { O.push("bottom:0;z-index:" + (k - 5)); C = "border_bottom_width" } if (e.charAt(1) === "e") { O.push("right:0"); F = "border_right_width" } else { O.push("left:0"); F = "border_left_width" } N = e + "_corner_width"; if (D[N]) { F = D[N] } else { D[N] = F = D[F] } O.push("background-image:url(" + D.sprite_img + ");background-position:" + D[e + "_corner_position"] + ";width:" + F + "px;height:" + D[C] + "px"); I.push("#fpid ." + e + "{" + O.join(";") + "}"); E = m("div", B, null, null, M); E.className = e } else { D[e + "_corner_width"] = 0 } } H = ["border_top_", "border_right_", "border_bottom_", "border_left_"]; for (G = 0; G < H.length; G++) { e = H[G]; O = []; C = false; K = e + "width"; if (D[K]) { F = D[K]; K = e + "color"; if (D[K]) { O.push("background-color:" + D[K]) } K = e + "position"; if (D[K]) { O.push("background-position:" + D[K]); O.push("background-image:url(" + D.sprite_img + ")"); K = e + "repeat"; if (D[K]) { O.push("background-repeat:" + D[K]) } } N = false; K = e.split("_")[1]; if (K === "top") { C = true; N = "n"; O.push("z-index:2") } else { if (K === "bottom") { C = true; N = "s" } else { if (K === "right" || K === "left") { N = true } } } if (N) { if (C) { N = K + ":0;left:" + D[N + "w_corner_width"] + "px;" + (D.outer_width === -100 ? "right:" + D[N + "e_corner_width"] : "width:" + (D.outer_width - D[N + "w_corner_width"] - D[N + "e_corner_width"])) + "px;" } else { N = K + ":0;top:" + D.border_top_width + "px;" + (D.outer_height === -100 ? "bottom:" + D.border_bottom_width: "height:" + (D.outer_height - D.border_top_width - D.border_bottom_width)) + "px;" } } O.push("z-index:" + (k - (C ? (K === "top" ? 5: 6) : 2))); if (N) { I.push("#fpid ." + K + "{position:absolute;" + (C ? "height": "width") + ":" + F + "px;" + N + O.join(";") + "}"); E = m("div", B, null, null, M); E.className = K } } } if (D.border_top_width && D.close_position) { N = Math.floor((D.border_top_width - D.close_height) / 2); if (D.close_vertical_offset && typeof D.close_vertical_offset === "number") { N += D.close_vertical_offset } E = m("a", B, null, { href: "#" }, M); E.onclick = function() { this.blur(); this.onclick = null; J(); return false }; E.className = "close"; I.push("#fpid .close{overflow:hidden;position:absolute;top:" + N + "px;" + D.close_align + ":" + D.close_horizontal_padding + "px;width:" + D.close_width + "px;height:" + D.close_height + "px;background-image:url(" + D.sprite_img + ");background-position:" + D.close_position + ";z-index:" + (k - 1) + ";}"); if (D.close_hover_position) { I.push("#fpid .close:hover, #fpid .close:active{background-position:" + D.close_hover_position + ";}") } if (D.close_alt_text) { E.title = D.close_alt_text } } H = ["top", "bottom"]; for (G = 0; G < H.length; G++) { K = H[G]; if (D["border_" + K + "_width"]) { if (D[K + "_text"]) { E = m("div", B, D[K + "_text"], null, M); E.className = K + "Text"; O = [K + ":" + D[K + "_offset_vertical"] + "px;z-index:" + (k - 3)]; L = { color: "color", align: "text-align", direction: "text-direction", decoration: "text-decoration", font_size: "font-size", font_weight: "font-weight" }; for (e in L) { if (L.hasOwnProperty(e)) { N = K + "_" + e; if (D[N]) { O.push(L[e] + ":" + D[N]) } } } L = { letter_spacing: "letter-spacing", word_spacing: "word-spacing", offset_right: "right", offset_left: "left" }; for (e in L) { if (L.hasOwnProperty(e)) { N = K + "_" + e; if (typeof D[N] === "number") { O.push(L[e] + ":" + D[N] + "px") } } } N = 0; if (K === "top" && D.top_align === D.close_align) { N = D.close_horizontal_padding + D.close_width + 2; if (A > 6) { O.push("padding-" + D.close_align + ":" + N + "px") } else { if (D.top_align === "left") { O.push("left:" + (N + D.top_offset_left) + "px") } } } if (A < 7) { O.push("width:" + (D.outer_width - D[K + "_offset_right"] - D[K + "_offset_left"] - N) + "px") } I.push("#fpid ." + K + "Text{position:absolute;" + O.join(";") + "}") } else { if (p < 7) { I.push("#fpid ." + K + "{font-size: 0;}") } } } } I.push("#iwrap{position:absolute;top:" + D.border_top_width + "px;right:" + D.border_right_width + "px;bottom:" + D.border_bottom_width + "px;left:" + D.border_left_width + "px;}#fpid iframe{overflow:hidden;border:none;position:relative;overflow-x:" + (D.allow_content_scroll ? "auto": "hidden") + ";overflow-y:" + (D.allow_content_scroll ? "auto": "hidden") + ";z-index:" + k + ";width:100%;height:100%;}"); return I } function o(E, C) { var J, D, e = false, N = false, M = C.anchor_corner, F = false, P = false, L = A >= 6 ? d.documentElement: d.body, O, H, B, K, G, I = this; this.place = function() { E.style.top = J + (e ? L.clientHeight: 0) + L.scrollTop; if (P) { E.style.left = L.clientWidth / 2 + D } else { E.style.left = D + (N ? L.clientWidth: 0) + L.scrollLeft } if (F) { E.style.height = L.clientHeight + "px" } }; this.addListeners = function() { attachEvent("onscroll", this.place); attachEvent("onresize", O) }; this.removeListeners = function() { detachEvent("onscroll", this.place); detachEvent("onresize", O) }; this.init = function() { E.style.position = "absolute"; if (C.outer_width === -100) { M = M.charAt(0) + "w"; B = []; B.push(E); B.push(0); B.push(E.firstChild.contentWindow.document.getElementsByTagName("iframe")[0]); B.push(C.border_left_width + C.border_right_width); K = E.firstChild.contentWindow.document.getElementsByTagName("div"); for (G = 0; G < K.length; G++) { if (K[G].className === "top") { B.push(K[G]); B.push(C.ne_corner_width + C.nw_corner_width) } else { if (K[G].className === "bottom") { B.push(K[G]); B.push(C.se_corner_width + C.sw_corner_width) } } } K = null } else { E.firstChild.contentWindow.document.getElementsByTagName("iframe")[0].style.width = C.outer_width - C.border_left_width - C.border_right_width } if (C.outer_height === -100) { M = "n" + M.charAt(1); H = []; H.push(E); H.push(0); H.push(E.firstChild.contentWindow.document.getElementsByTagName("iframe")[0]); H.push(C.border_top_width + C.border_bottom_width); K = E.firstChild.contentWindow.document.getElementsByTagName("div"); for (G = 0; G < K.length; G++) { if (K[G].className === "right" || K[G].className === "left") { H.push(K[G]); H.push(C.border_top_width + C.border_bottom_width) } } K = null } else { E.firstChild.contentWindow.document.body.firstChild.getElementsByTagName("iframe")[0].style.height = C.outer_height - C.border_top_width - C.border_bottom_width } if (B || H) { O = function(R) { var Q, S; if (B) { S = L.clientWidth - (2 * C.horizontal_offset); for (Q = 0; Q < B.length; Q += 2) { B[Q].style.width = S - B[Q + 1] } } if (H) { S = L.clientHeight - (2 * C.vertical_offset); for (Q = 0; Q < H.length; Q += 2) { H[Q].style.height = S - H[Q + 1] } } R !== "noplace" && I.place() }; O("noplace") } else { O = I.place } switch (M.charAt(0)) { case "s": J = ( - 1 * C.vertical_offset) - C.outer_height; e = true; break; case "n": J = C.vertical_offset; break; case "c": J = 0; E.style.height = "100%"; E.firstChild.style.position = "relative"; E.firstChild.style.top = "50%"; E.firstChild.style.marginTop = ( - 1 * Math.floor(C.outer_height / 2)) + "px"; if (A < 7) { F = true } break } switch (M.charAt(1)) { case "e": D = ( - 1 * C.horizontal_offset) - C.outer_width; N = true; break; case "w": D = C.horizontal_offset; break; case "c": P = true; D = -1 * C.outer_width / 2; break } } } function i(G, M, F, L) { if (M.attribute) { M = [M] } function D() { G.style.filter = ""; if (typeof L === "function") { L() } } function e(N) { return ( - Math.cos(N * Math.PI) / 2) + 0.5 } var H, E, C, B = +new Date, K = B + F, J = e, I = M.length; for (H = 0; H < I; H++) { E = M[H]; if (p && E.attribute === "opacity") { E.ie_opacity = true; E.stop *= 100; E.start *= 100 } else { E.ie_opacity = false } E.delta = E.stop - E.start; E.units = E.units || "" } C = setInterval(function() { var O = +new Date, P = O > K ? 1: (O - B) / F, N; for (H = 0; H < I; H++) { E = M[H]; N = (E.start + E.delta * J(P)).toFixed(3) + E.units; if (E.ie_opacity) { G.style.filter = "alpha(opacity=" + N + ")" } else { G.style[E.attribute] = N } } if (O > K) { clearInterval(C); D() } }, 13); return C } function n() { var e = r(); if (!e || !e.sprite_img) { return } n.ran = true; n.loaded = false; n.onload = null; if (!n.sprite) { n.sprite = new Image(); n.sprite.onload = function() { n.loaded = true; if (typeof n.onload === "function") { n.onload() } }; n.sprite.src = e.sprite_img } if (!e.delay_duration || e.delay_duration < 0) { n.ok_to_show = true } else { n.ok_to_show = false; setTimeout(function() { n.ok_to_show = true; if (n.on_ok_to_show) { n.on_ok_to_show() } }, 1000 * e.delay_duration) } } function u(B, C) { if (u.ran) { return } u.ran = true; function e() { B.style.display = "none"; if (C.fixIE) { C.fixIE.removeListeners() } try { var E = B.parentNode.removeChild(B); E = null } catch(D) {} } if (C.animation) { clearInterval(C.animation) } if (C.animate_out_duration) { B.firstChild.style.position = "relative"; i(B.firstChild, C.animation_out, C.animate_out_duration * 1000, e) } else { e() } } function q(I, G) { if (q.ran) { return } q.ran = true; var M = { zIndex: k }, B, E = null, L = null, D, C, K, J, F = [], N = G.anchor_corner; if (h.addEventListener && I.firstChild && I.firstChild.contentWindow) { I.firstChild.contentWindow.addEventListener("unload", function() {}, false) } if (G.outer_width === -100) { M.left = M.right = G.horizontal_offset + "px" } else { M.width = G.outer_width + "px" } if (G.outer_height === -100) { M.top = M.bottom = G.vertical_offset + "px" } else { M.height = G.outer_height + "px" } if (A > 6) { M.position = "fixed"; if (G.outer_height === -100) { M.top = M.bottom = G.vertical_offset + "px" } else { if (N.charAt(0) === "c") { M.top = "50%"; M.marginTop = ( - 1 * Math.floor(G.outer_height / 2)) + "px" } else { M[N.charAt(0) === "n" ? "top": "bottom"] = G.vertical_offset + "px" } } if (G.outer_width === -100) { M.right = M.left = G.horizontal_offset + "px" } else { if (N.charAt(1) === "c") { M.left = "50%"; M.marginLeft = ( - 1 * Math.floor(G.outer_width / 2)) + "px" } else { M[N.charAt(1) === "e" ? "right": "left"] = G.horizontal_offset + "px" } } } else { G.fixIE = L = new o(I, G); L.init(); L.addListeners() } for (B in M) { if (M.hasOwnProperty(B)) { I.style[B] = M[B] } } if (L) { L.place() } if (G.visible_duration > 0) { E = function() { setTimeout(function() { u(I, G) }, G.visible_duration * 1000) } } if (G.animate_in_duration || G.animate_out_duration) { D = I.firstChild; D.style.position = "relative"; for (K = 0; K < G.animation_properties.length; K++) { B = G.animation_properties[K]; if (B === "position") { if (N === "cc" && A < 7) { if (G.outer_height === -100) { F.push({ attribute: "top", units: "px", start: -1 * (d.documentElement.clientHeight || d.body.clientHeight), stop: 0 }); D.style.top = -1 * (d.documentElement.clientHeight || d.body.clientHeight) + "px" } else { F.push({ attribute: "top", units: "px", start: -1 * G.outer_height / 2, stop: (d.documentElement.clientHeight || d.body.clientHeight) / 2 }); if (G.animate_in_duration) { D.style.top = ( - 1 * G.outer_height) + "px" } } } else { if (N === "cc") { C = h.innerHeight || d.documentElement.clientHeight; if (G.outer_height !== -100) { C = (C + G.outer_height) / 2 } B = "bottom" } else { if (N.charAt(0) === "c") { C = G.outer_width + G.horizontal_offset; B = N.charAt(1) === "w" ? "right": "left" } else { C = (N.charAt(0) === "n" ? -1: 1) * (G.outer_height + G.vertical_offset); B = "top" } } if (G.animate_in_duration) { D.style[B] = C + "px" } F.push({ attribute: B, units: "px", start: C, stop: 0 }) } } else { if (B === "opacity") { if (p) { D.zoom = 1; if (G.animate_in_duration) { D.style.filter = "alpha(opacity=0)" } } else { if (G.animate_in_duration) { D.style.opacity = 0 } } F.push({ attribute: "opacity", units: "", start: 0, stop: 1 }) } } } G.animation_out = []; for (K = 0; K < F.length; K++) { C = {}; for (B in F[K]) { if (F[K].hasOwnProperty(B)) { C[B] = F[K][B] } } J = C.start; C.start = C.stop; C.stop = J; G.animation_out.push(C) } if (A < 7 && N === "ce") { I.style.overflow = "hidden" } } function e() { var O = h.innerHeight, Q, P; e.t = c(); if (e.h == O) { return } e.h = O; O -= 2 * G.vertical_offset; if (O == I.offsetHeight) { return } I.firstChild.contentDocument.getElementById("fpid").style.height = I.style.height = O + "px"; O -= G.border_top_width + G.border_bottom_width; Q = I.firstChild.contentDocument.querySelectorAll(".right,.left,#iwrap"); for (P = 0; P < Q.length; P++) { Q[P].style.height = O } } e.t = 0; e.h = h.innerHeight; function H() { I.style.display = "block"; if (h.opera && G.outer_height === -100 && d.querySelectorAll) { h.addEventListener("resize", function() { if (e.t - c() < -100) { e() } }, false); J = I.firstChild.contentDocument.getElementById("fpid").style; setTimeout(function() { J.position = "absolute"; J.width = "100%"; J.height = "100%" }, 50) } if (G.animate_in_duration <= 0 || F.length === 0) { if (E) { E() } } else { G.animation = i(D, F, G.animate_in_duration * 1000, E) } } if (n.ok_to_show) { if (n.loaded) { setTimeout(H, 13) } else { n.onload = H } } else { n.on_ok_to_show = function() { if (n.loaded) { H() } else { n.onload = H } } } } n(); b(function() { var D, B, e, F = v(), C; function G(H) { if (c() > F.fp.stopTime) { return } clearTimeout(e); if (!F.content_url && F.message_html) { H.document.write(g + l + F.message_html); H.document.close() } F.fp.accounting = F.fp.sendAccounting(1); q(B, F) } function E(K) { if (c() > F.fp.stopTime) { return } var J, N, I, M, H; try { M = K.document } catch(L) { return } M.write(g + "" + l + s + ""); M.close(); if (parseInt(p, 10) === 6) { try { M.execCommand("BackgroundImageCache", false, true) } catch(L) {} } N = m("div", M.body, null, { id: "fpid" }, M); H = m("div", N, null, { id: "iwrap" }, M); I = y(F, N, function() { u(B, F) }, M); j(I.join(""), M); C = { frameBorder: "0", scrolling: (F.allow_content_scroll ? "auto": "no") }; if (p && F.allow_content_transparency) { C.allowTransparency = !!F.allow_content_transparency } if (F.content_url) { C.src = F.content_url } C.height = "100%"; J = a(H, C, G, M); if (!F.allow_content_transparency) { J.style.backgroundColor = "#fff" } e = setTimeout(function() { try { J.src = "about:blank" } catch(O) {} try { H.removeChild(J) } catch(O) {} }, F.fp.stopTime - c()) } if (!n.ran) { n() } if (F === null || c() > F.fp.stopTime) { return } B = m("div", d.body, null, { style: "display:none" }); B.style.display = "none"; C = { scrolling: "no", width: F.outer_width === -100 ? "100%": F.outer_width, height: F.outer_height === -100 ? "100%": F.outer_height, frameBorder: 0, id: f() }; if (p) { C.allowTransparency = !F.opaque_outer_frame } j("@media print{#" + C.id + "{display:none}}"); D = a(B, C, E, d) }) } catch(w) {} })();

 

 

内容是以上这些,得到的原始代码是压缩代码.现在大家看到的是解压以后的可以直观展现出来的代码.

 

其实上面这段代码不是直接植入网站页面的.

 

而是在你网页的head标签中植入一条js引用:

 

 

.js?fp681  这个就是广告了, 根据后面的fp数字不同 得到的似乎是不同的广告.

 

反正我这里几次截取的.js?后面的内容都不一样.

 

下面说一下问题的解决过程:

 

一开始 想着 干掉js生成的iframe 但是后来考虑,要从根源抓起.

 

于是 动手解决head标签中植入的js引用.

 

发现一个重复点: 广告的域名根为 clicklifter.com

 

于是写了下面的方法, 放到了页面body标签的最后.

 

 

经过测试 ok 轻松解决. 但是唯一的遗憾就是 偶尔访问页面的时候被植入广告了 页面会重新刷新一遍.

 

虽然能解决广告弹出 但是还是不够"优雅"

 

js代码的前2行是为了跳出iframe(防止别人使用iframe把你的页面嵌入进去)

 

剩下的就是判断页面中clicklifter.com的数量, 除了自己原始代码中有一个以外,如果dns服务器植入js的话就会超过1个.

所以在下面判断 如果统计超过了1个 就刷新本页面.

 

 

好了,说完了. 希望大家遇到同样问题了可以借鉴一下此方法. 还是比较有效的.

 

如果哪位大侠能有更好办法, 请分享一下.

 

 

你可能感兴趣的:(个人原创)