PHP& Javascript:如何保护Web黑客的发布和获取方法? [英] PHP & Javascript : How to protect web hacker for post and get method?

查看:80
本文介绍了PHP& Javascript:如何保护Web黑客的发布和获取方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的sample.html文件,位于 http://www.aaa.com/sample.html中

Here is my sample.html file located in http://www.aaa.com/sample.html

<html>
<script>
   $(document).ready(function(){
     $.post('http://www.aaa.com/api/_file.php?act=add', {val : '1234'});
   });
</script>
<body>

</body>
</html>

这是我的PHP文件,可接收来自sample.html文件的请求

and this is my PHP file that recieve request from sample.html file

<?php
  switch($_GET['act']){
    case 'add' :
    doFunction();
    break;
  }

  function doFunction(){
    echo $_POST['val'];
  }

?>

如果我在另一个网站上有另一个类似hack.html的html页面,例如 http://www .bbb.com/hack.html

if I have another html page like hack.html that located on another website, example http://www.bbb.com/hack.html

<html>
<body>
  <a href="http://www.aaa.com/api.php_file.php?act=add">Hack them!!!</a>
</body>
</html>

我可以通过单击某些链接来使用bbb.com网站访问aaa.com中的数据吗?如果是,我该如何保护它?

Can I use bbb.com website to access data in aaa.com by cliking some link? If yes, how do I protect it?

有什么主意吗?还是更好的方法?

Any idea? or better way?

保卫. (^^)

推荐答案

这里有两个可能的问题.

There are two possible problems here.

  1. 跨站点脚本(XSS)
  2. 跨站点请求伪造
  1. Cross site scripting (XSS)
  2. Cross site request forgery

先反对后卫,

  • 不允许用户将HTML添加到网站(对于用户或任何其他用户).对所有数据运行 htmlspecialchars ,然后再将其输出到站点.如果要设置属性值而不是标签之间显示的数据,则需要采取其他步骤(例如,禁止data:javascript:方案URI).
  • 解析所有HTML输入并在输出之前通过白名单运行它.
  • Not allowing HTML to be added to the site (for the user or any other user) by users. Run htmlspecialchars over all data before outputting it to the site. If you are setting attribute values instead of data that appears between tags, then you need to take additional steps (e.g. forbidding data: or javascript: scheme URIs).
  • Parsing all HTML input and running it through a white list before outputting it.

通过第二个来捍卫第二个:

Defend against the second by:

  1. 启动会话,并在用户首次访问非编辑页面时将随机值存储在数据中. (您可能需要定期生成一个新令牌).
  2. 以您使用的任何可以更改的形式包含该值(或者,如果使用链接,则包含在查询字符串中,但您不应该这样做,应该认为GET请求是安全的)
  3. 拒绝表单数据中没有与会话中的值匹配的任何请求. (第三方站点无法从会话中读取它,因此不知道要使用什么值).

这篇关于PHP&amp; Javascript:如何保护Web黑客的发布和获取方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆