日期:2014-05-16  浏览次数:20323 次

javascript注入

1、JavaScript注入就是在浏览器地址栏中输入一段js代码,用来改变页面js变量、页面标签的内容。

使用Javascript注入,用户不需要关闭或保存网页就可以改变其内容,这是在浏览器的地址栏上完成的。命令的语法如下:

javascript:alert(#command#) 

例如,如果你想在http://www.example.com站点上看到一个alert警告框,那么首先在地址栏上输入URL并等待页面加载完成,然后删掉URL并输入:

javascript:alert("Hello World")

?作为新的URL。这将弹出一个“Hello World”警告框,使用这一技术几乎可以改变网页的任何内容,例如一张图片。假设有一张网站logo图片,我们通过查看页面源文件找到其中一段HTML代码:

<IMG Name="hi" SRC="hello.gif">

?图片被命名为“hi”,源文件是“hello.gif”,我们想要把它改成存储在我们站点(http://www.mysite.com)上的 “bye.jpeg”文件,因此图片完整的URL地址是http://www.mysite.com/bye.jpeg,使用Javascript注入, 我们只需要在地址栏上输入:

javascript:alert(document.hi.src="http://www.mysite.com/bye.jpeg")

?你将会看到弹出“http://www.mysite.com/bye.jpeg”alert警告,然后图片就被更改了。需要注意的是,这些更改只是暂时的!如果你刷新页面或者重新进入,你的更改将会消失,因为你只是在你的PC作了这些更改,而不是在网页服务器上。

使用同样的方法我们可以查看或更改变量的值,例如我们在网页上找到一段这样的代码:

<SCRIPT LANGUAGE="JavaScript">
var a="test"
</SCRIPT> 

?意思是变量a的值为“test”,现在我们输入:

javascript:alert(a)

?然后我们将其值改为“hello”:

javascript:alert(a="hello")

?Javascript注入通常被用来更改表单属性,假设有一段这样的代码:

<form name="format" action="send.php" method="post">
<input type="hidden" name="mail" value="someone@somewhere.com">
<input type="text" name="name">
<input type="submit" value="submit"></form>

?我们想让表单发送到我们的邮箱,而不是someone@somewhere.com。可以使用如下命令:

javascript:alert(document.format.mail.value="me@hacker.com")
  • 也许你已经注意到了这些命令的层次关系:
  • 我们按照从左到右的顺序依次说明:
  • 1)最左边是document
  • 2)然后是我们想要更改的对象名(比如document.hi.src)或其包含的对象(比如document.format.mail.value)
  • 3)最后是我们想要更改的属性(比如源路径:document.hi.src,或变量值:document.format.mail.value)
  • 4)使用“.”号分隔
  • 5)当我们想要更改属性值的时候,我们使用“=”号和新的属性值
  • *注释:当新的属性值为字符串时(比如:document.format.mail.value="me@hacker.com")需要用双引号把它括起来。
  • 如果我们想要把它作为一个变量的值,则不需要使用双引号""。例如我们想要将变量b的值赋予变量a,我们可以输入javascript:alert(a=b)。
  • 但是,页面中的大部分标签都没有名字,比如:
<form action="send.php" method="post">
<input type="hidden" name="mail" value="someone@somewhere.com">
<input type="text" name="name">
<input type="submit" value="submit"></form>

?在这段代码中没有表单名,综合上面这些信息,可以使用此命令:

javascript:alert(document. .mail.value="me@hacker.com")

?在这种情况下我们必须统计并找出表单序号,下面是一个例子:

<form action="send.php" method="post">
<input type="text" name="name">
<input type="submit" value="submit">
</form>

<form action="send.php" method="post">
<input type="hidden" name="mail" value="someone@somewhere.com">
<input type="text" name="name">
<input type="submit" value="submit">
</form>

<form action="send.php" method="post">
<input type="text" name="name">
<input type="submit" value="submit">
</form> 
  • 在以上代码中我们看见了3个表单,但我们只对第二个感兴趣,因此我们想要的表单序号就是2。不要忘记我们是从1开始计算的,比如1,2,3,4...而javascript却从0开始计算,比如0,1,2,3...所以真正的表单序号是1,不是2,通常我们要把找到的表单序号减一。我们将用这个序号来补全我们的命令:
javascript:alert(document.forms[1].mail.value="me@hacker.com") 
  • 这样你就可以更改没有名字的图片或链接了,你可以把“forms”换成任何你想要的标签类型。对于图片就是
javascript:alert(document.images[3].src="#the url of the picture you want#")

?对于链接就是

javascript:alert(document.links[0].href="#the url you want#")
  • ?最后,我们可以用这个技巧来编辑cookies。下面的命令由triviasecurity.net的Dr_aMado编写,我只修改了一点点,让它在用户编辑之前显示出来。你只要把它们复制到地址栏就可以了:
javascript:alert(window.c=function a(n,v,nv){c=document.cookie;c=c.substring(c.indexOf(n)+n.length,c.length);
c=c.substring