javascript 注入

作者: secflag 分类: 渗透测试 发布时间: 2018-03-06 09:54

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

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

Js代码

javascript:alert(#command#) 

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

Js代码

javascript:alert("Hello World")

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

Js代码

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

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

Java代码

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

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

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

Js代码

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

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

Js代码

javascript:alert(a)

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

Js代码

javascript:alert(a="hello")

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

Js代码

<form name="format" action="send.php" method="post">
<input type="hidden" name="mail" value="[email protected]">
<input type="text" name="name">
<input type="submit" value="submit"></form>

我们想让表单发送到我们的邮箱,而不是[email protected]。可以使用如下命令:

Js代码

javascript:alert(document.format.mail.value="[email protected]")

 

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

Js代码

<form action="send.php" method="post">
<input type="hidden" name="mail" value="[email protected]">
<input type="text" name="name">
<input type="submit" value="submit"></form>

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

Js代码

javascript:alert(document. .mail.value="[email protected]")

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

Js代码

<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="[email protected]">
<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,通常我们要把找到的表单序号减一。我们将用这个序号来补全我们的命令:

Js代码

javascript:alert(document.forms[1].mail.value="[email protected]") 

这样你就可以更改没有名字的图片或链接了,你可以把“forms”换成任何你想要的标签类型。对于图片就是

Js代码

javascript:alert(document.images[3].src="#the url of the picture you want#")

对于链接就是

Js代码

javascript:alert(document.links[0].href="#the url you want#")

最后,我们可以用这个技巧来编辑cookies。下面的命令由triviasecurity.net的Dr_aMado编写,我只修改了一点点,让它在用户编辑之前显示出来。你只要把它们复制到地址栏就可以了:

Js代码

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(1,( (c.indexOf(";")>-1) ? c.indexOf(";") : c.length));nc=unescape(c).replace(v,nv);
document.cookie=n+"="+escape(nc);return unescape(document.cookie);});
alert('The cookie is: "'+document.cookie+'"');alert(c(prompt("The name of the cookie:",""),
prompt("Change this value:",""),prompt("with this:","")))

Js代码

//如果你想要手动更改你的cookie,可以使用下面这条命令:
javascript:alert(document.cookie)

这将显示你的当前cookie,假设是“userid=1”,如果你想把它改成“userid=2”,可以使用下列命令:

Js代码

javascript:alert(document.cookie="userid=2")

最后我必须强调的是,所有的更改都只是在客户端!就像是把网页保存在你的PC上然后修改它。尽管如此,使用这一技巧你仍然可以欺骗页面(例如cookies)或绕过安全验证。例如一些网页会检测用户发送数据的位置,如果从http://www.test.com/form.php 发送数据到http://www.test.com/check.php,check.php可能会检测数据是否来自http: //www.test.com/form.php上的表单。除此之外,如果你打算在页面中输入你自己的JavaScript代码,通过使用一些这样的技巧,你将能够更改图片并保持不变!