javascript函数:浅谈Javascript函数劫持

来源:  Xfocus Team

、概述

Javascript劫持也就是老外提到javascript hijackingstyle="margin-left: 300px;" />



/*
FileName:        js_inline_debugger.js
Author:            luoluo
Contact:        luoluonet_at_yahoo.cn
Date:            2007-6-27
Version:        0.1
Usage:   
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">






Instruction:   
It is a simple javascript inline debugger. You must

add xhtml1-transitional dtd to your
html document you wanna to use the script.
*/

//--------------------------------------------------------------------------//
// 公用
//--------------------------------------------------------------------------//
// 判断是否是IE
function isIE {
document.all && window.external;
}

// 去除串两边空格
String.prototype.trim = function {
var re = /(^\s*)|(\s*)$/g;
this.replace(re, "");
}

// 删除中某个元素
Array.prototype.remove = function(i) {
var o = this[i];
for (var j = i; j < this.length - 1; j ) {
this[j] = this[j + 1];
}
-- this.length;
o;
}

// 判断中是否存在相同元素
Array.prototype.search = function(o) {
for (var i = 0; i < this.length; i ) {
(this[i] o) {
i;
}
}

-1;
}

// html编码
function htmlEncode(s) {
s = s.replace(/&/g, "&");
s = s.replace(/s = s.replace(/>/g, ">");
s = s.replace(/\"/g, """);
s = s.replace(/\'/g, """);

s;
}

// js编码
function jsEncode(s) {
s = s.replace(/\\/g, "\\\\");
s = s.replace(/\n/g, "\\n");
s = s.replace(/\"/g, "\\\"");
s = s.replace(/\'/g, "\\\'");
s;
}

//--------------------------------------------------------------//
// 命令行窗口工具
//--------------------------------------------------------------//
function Console(parentNode, processInputProc) {
// 窗口
var panel = document.createElement("div");
panel.style.width = "250px";
panel.style.height = "250px";
panel.style.borderColor = "#666666";
panel.style.borderWidth = "1px";
panel.style.backgroundColor = "#ffffff";
panel.style.borderStyle = "solid";
panel.style.position = "absolute";
panel.style.zIndex = 100;
parentNode.appendChild(panel);

// 标题栏
var title = document.createElement("div");
title.style.width = "250px";
title.style.height = "15px";
title.style.backgroundColor = "#dddddd";
title.style.fontSize = "12px";
title.style.fontFamily = "verdana,tahoma";
panel.appendChild(title);

// 标题栏拖动窗口功能
var isDragging = false;
var startPos = Position(panel.offLeft, panel.offTop);
var startMousePos = Position(0, 0);

title.onmouseover = function(evt) {
this.style.cursor = "poer";
}

title.onmousedown = function(evt) {
(isDragging true) {
;
}

var event = evt || window.event;
startMousePos.x = event.clientX;
startMousePos.y = event.clientY;
isDragging = true;
}

title.onmousemove = function(evt) {
(isDragging false) {
;
}
activateWindow;
var event = evt || window.event;
panel.style.left = (event.clientX - startMousePos.x) + startPos.x + "px";
panel.style.top = (event.clientY - startMousePos.y) + startPos.y + "px";
}

title.onmouseup = function(evt) {
(isDragging false) {
;
}

var event = evt || window.event;
startPos.x =  (event.clientX - startMousePos.x) + startPos.x;
panel.style.left = startPos.x + "px";
startPos.y = (event.clientY - startMousePos.y) + startPos.y;
panel.style.top = startPos.y + "px";
isDragging = false;
}

// 关闭窗口功能
var closeButton = document.createElement("div");
closeButton.style.width = "13px";
closeButton.style.height = "13px";
closeButton.style.backgroundColor = "#ffffff";
closeButton.style.styleFloat = closeButton.style.cssFloat = "left";
closeButton.style.fontSize = "12px";
closeButton.style.borderWidth = "1px";
closeButton.style.borderColor = "#999999";
closeButton.style.borderStyle = "solid";
closeButton.style.paddingButton = "2px";
closeButton.innerHTML = "title.appendChild(closeButton);

var isVisible = true;

// 窗口关闭
closeButton.onclick = function(evt) {
isVisible = false;
panel.style.display = "none";
}

// 标题栏文字
var titleLabel = document.createElement("div");
titleLabel.style.height = "14px";
titleLabel.style.width = "200px";
titleLabel.style.fontSize = "11px";
titleLabel.style.color = "#ffffff";
titleLabel.style.styleFloat = titleLabel.style.cssFloat = "left";
titleLabel.style.fontFamily = "verdana,tahoma";
titleLabel.innerHTML = "Javascript Inline Debugger";
//titleLabel.style.marginTop = isIE ? -14 : "-14px";
titleLabel.style.marginLeft = isIE ? 18 : "18px";
title.appendChild(titleLabel);

// 中间显示部分
var showPanel = document.createElement("div");
showPanel.style.width = "250px";
showPanel.style.height = isIE ? "221px" : "219px";
showPanel.style.fontSize = "11px";
showPanel.style.padding = "0";
showPanel.style.margin = "0";
showPanel.style.overflow = "auto";
showPanel.style.marginTop = isIE ? -1 : "0";
panel.appendChild(showPanel);

// 命令输入框
var cmdArea = document.createElement("div");
panel.appendChild(cmdArea);

var cmdTextbox = document.createElement("input");
cmdTextbox.type = "text";
cmdTextbox.style.width = "250px";
cmdTextbox.style.height = "12px";
cmdTextbox.style.fontSize = "12px";
cmdTextbox.style.padding = "0";
cmdTextbox.style.margin = "0";
cmdTextbox.style.marginTop = isIE ? -2 : "0";
cmdTextbox.style.borderWidth = "0";
cmdTextbox.style.borderTopWidth = "1px";
cmdTextbox.style.paddingTop = "2px";
cmdArea.appendChild(cmdTextbox);

// 窗口激活或者不激活
var isActive = false;

// 激活窗口
var activateWindow = function {
(! isVisible) {
;
}

(isActive) {
;
}

cmdTextbox.focus;
title.style.backgroundColor = "#015DF4";
isActive = true;
}

panel.onclick = activateWindow;

// 不激活窗口
var deactivateWindow = function {
(! isVisible) {
;
}

(! isActive) {
;
}

title.style.backgroundColor = "#cccccc";
isActive = false;
}

cmdTextbox.onfocus = activateWindow;
cmdTextbox.onblur = deactivateWindow;

// 输出
var dbgPr = function(s) {
showPanel.innerHTML htmlEncode(s).replace(/\n|(\r\n)/g, "
");
(parseInt(showPanel.scrollHeight) > parseInt(showPanel.style.height)) {
showPanel.scrollTop parseInt(showPanel.scrollHeight) -

parseInt(showPanel.style.height);
}
}

// 输入处理回调
cmdTextbox.onkeydown = function(evt) {
var event = evt || window.event;

(event.keyCode 0x0d) {
processInputProc(this.value, dbgPr);
this.value = "";
}
}
}

// 坐标类
function Position(x, y) {
this.x = x;
this.y = y;
}

//--------------------------------------------------------------------------//
// 内联调试器类
//--------------------------------------------------------------------------//
function InlineDebugger {
var bpList = Array;
var id_eval;

// 设置断点
var bp = function(funcName) {
// 检查eval是否被hook
(( String(eval)).indexOf("[native code]") < 0) {
"error: eval function was hooked by other codes in the front.\n";   
}

// 保存未被hookedeval
id_eval = eval;

var re = /^[a-zA-Z0-9_\.]+$/i;
(! re.test(funcName)) {
"error: bad argument of command bp \"" + funcName + "\".\n";
}

try {
id_eval(" (typeof(" + funcName + ") \"object\" ||

typeof(" + funcName + ") \"function\") {var obj = " + funcName + ";}");
} catch (e) {
"error: " + e + ", invalid function name \"" + funcName + "\".\n";
}

(obj und) {
"error: the argument of command bp \"" + funcName +

"\" is not a function object.\n";
}

(( String(obj)).indexOf("function") < 0) {
"error: the argument of command bp \"" + funcName +

"\" is a property, a function is required.\n";
}

(bpList.search(funcName) >= 0) {
"error: there is a po _disibledevent=>funcName + " = " +
"function {\n" +
"    var args = \"\";\n" +
"    for (var i = 0; i < arguments.length; i ) {\n" +
"        args arguments[i] + (i (arguments.length - 1) ?

\"\" : \",\");\n" +
"    }\n" +
"    (confirm(\"function \\\"" + funcName +

"\\\" was called, execute it?\\n\\narguments:\\n\" + args +

\"\\n\\ncaller:\\n\" + " + funcName + ".caller)) {\n" +
"        id_eval(\"" + funcName.replace(/\./g, "_") +

"_bak(\\\"\" + jsEncode(args) + \"\\\")\");\n" +
"    }\n" +
"};" +
"\n";
id_eval(sc);
bpList.push(funcName);
"* po _disibledevent=> (bpList.length 0) {
"* no po.\n";
}

var bps = "* " + bpList.length + " pos: \n";

for (var i = 0; i < bpList.length; i ) {
bps i + " - " + bpList[i] + "\n";
}

bps;
}

// 清除某个断点
var bc = function(n) {
try {
n = parseInt(n);
} catch (e) {
"error: bc command requires a numeric argument.\n";
}

(bpList.length 0) {
"error: no po.\n";
}

var funcName = bpList.remove(n);
try {
eval(funcName + " = window." + funcName.replace(/\./g, "_") + "_bak;");
"* po _disibledevent=>var s = "debug commands:\n\n" +
"bp - a po _disibledevent=>cmd = cmd.trim;
var cmdParts = cmd.split(/\s+/g);
var cmdName;
var cmdArg;

(cmdParts.length 1) {
cmdName = cmd;
} {
cmdName = cmdParts[0];
cmdArg = cmdParts[1];
}

switch (cmdName) {
"bp":
(cmdArg und) {
"error: bp command requires an argument.\n";
} {
bp(cmdArg);
}
;

"bl":
bl;
;

"bc":
(cmdArg und) {
"error: bc command requires an argument \"number of po\".\n";
} {
bc(cmdArg);
}
;

"help":
help;
;

default: "error: command \"" + cmdName + "\" not found,

you can get information by \"help\" command.\n";
;
}
}
}

//-----------------------------------------------------------------------------//
// 主过程
//-----------------------------------------------------------------------------//
/*try {
debugger;
} catch (e) {}*/
var id = InlineDebugger;
var console = Console(document.body, function(s, prProc){prProc(id.exeCmd(s));});












Tags:  javascript数组函数 javascript函数详解 javascript函数大全 javascript函数

延伸阅读

最新评论

发表评论