Loading

jsonp

一、学习链接

https://saucer-man.com/information_security/309.html

二、jsonp简介

JSONP全称是JSON with Padding ,是基于JSON格式的为解决跨域请求资源而产生的解决方案。他实现的基本原理是利用了HTML里script元素标签没有跨域限制。
JSONP原理就是动态插入带有跨域url的script标签,然后调用回调函数,把我们需要的json数据作为参数传入,通过一些逻辑把数据显示在页面上。
比如通过script访问http://www.test.com/index.html?jsonpcallback=callback, 执行完script后,会调用callback函数,参数就是获取到的数据。
原理很简单,在本地复现一下,首先新建callback.php:

<!-- callback.php -->
<?php
    header('Content-type: application/json');
    $callback = $_GET["callback"];
    //json数据
    $json_data = '{"customername1":"user1","password":"12345678"}';
    //输出jsonp格式的数据
    echo $callback . "(" . $json_data . ")";
?>

然后新建test.html:

<!-- test.html -->

<html>
<head>
<title>test</title>
<meta charset="utf-8">
<script type="text/javascript">
function hehehe(obj){
    alert(obj["password"]);
}
</script>
</head>
<body>
<script type="text/javascript" src="http://localhost/callback.php?callback=hehehe"></script>
</body>
</html>

我们访问test.html,页面会执行script,请求http://localhost/callback.php?callback=hehehe,然后将请求的内容作为参数,执行hehehe函数,hehehe函数将请求的内容alert出来。最终的结果如下

这样我们就实现了通过js操作跨域请求到的资源,绕过了同源策略。

三、jsonp劫持

诱惑用户访问点击制作的html,这个html可以利用jsonp来获得用户的敏感数据,并借由html来发送给攻击者。

posted @ 2021-11-28 18:58  Ctrl_C+Ctrl_V  阅读(29)  评论(0)    收藏  举报