您的位置:首页 > Web前端 > JavaScript

js注入

2015-07-30 23:24 676 查看
1、JavaScript注入就是在浏览器地址栏中输入一段js代码,用来改变页面js变量、页面标签的内容。

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

Js代码

javascript:alert(#command#)


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

Js代码

javascript:alert("Hello World")


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

Js代码

<img name="ww" src="xxx.gif">


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

js代码

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


你将会看到弹出"http://www.my.com/t1.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="someone@somewhere.com">
<input type="text" name="name">
<input type="submit" value="submit"></form>


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

Js代码

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)。

但是,页面中的大部分标签都没有名字,比如:

Js代码

<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>


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

Js代码

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


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

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

Js代码

javascript:alert(document.forms[1].mail.value="me@hacker.com")


这样你就可以更改没有名字的图片或链接了,你可以把“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")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: