• web安全知识


    参考文章 :  https://www.mudoom.com/php%E5%AE%89%E5%85%A8%E7%BC%96%E7%A0%81/ 

    SQL注入

    造成sql注入的原因是因为程序没有过滤用户输入的内容,  本质上是在执行sql时数据和语句混淆,下面举一个例子: 

    万能密码和万能用户名

    正常写法 : select * from table_name where username = 'admin' and password = 'admin'

    万能密码 : 利用输入表单构造sql,   select * from table_name where username = 'admin' and password = 'admin' or 1 = 1,     要防止非常简单, 对密码进行md5加密比对即可

    万能用户名  : select * from table_name where username = 'admin' or 1 = 1 and password = 'admin' , 绝对不要相信用户输入的任何数据。 利用addslashes()转义, 对单引号, 双引号, NULL, 反斜杠()进行转义.

    预防数据库攻击正确做法:  关闭所有错误提示!!!

    1. 不能使用mysqli或PDO时, 可以临时使用addslashes() 转义, 前提是数据库为utf-8编码, 并且参数位于''

    2. 使用mysqli或pdo预编译处理sql, 原理是命令与参数分两次发送到MYSQL, 这样MYSQL就能识别参数与命令

    $username = 'root';
    $password = '1234abcd';
    $driver_options = array(
        PDO::MYSQL_ATTR_INIT_COMMAND    => 'SET NAMES UTF8',
        );
    $pdo = new PDO($dsn, $username, $password, $driver_options);
    //1.编译统一的结构
    $sql = "insert into team values (null, :team_name)";
    $stmt = $pdo->prepare($sql);
    $data_list = array(
        array('name'=>'国安'),
        array('name'=>'绿地'),
        array('name'=>'恒大'),
        array('name'=>'建业'),
        array('name'=>'鲁能'),
        array('name'=>'申花'),
        );
    foreach($data_list as $row) {
        //2.绑定数据到中间编译结果
        $stmt->bindValue(':team_name', $row['name']);
    
        //3.执行
        $result = $stmt->execute();
        var_dump($result);
    }
    View Code

    3.自己写一个prepared(不推荐)

    function prepare( $query, $args ) {
        if ( is_null( $query ) )
             return;
    
        // This is not meant to be foolproof -- 
               but it will catch obviously incorrect usage.
        if ( strpos( $query, '%' ) === false ) {
             _doing_it_wrong( 'wpdb::prepare' , 
             sprintf ( __( 'The query argument of %s
                     must have a placeholder.' ), 'wpdb::prepare()' ), '3.9' );
       }
    
        $args = func_get_args();
        array_shift( $args );
        // If args were passed as an array (as in vsprintf), move them up
        if ( isset( $args[ 0] ) && is_array( $args[0]) )
             $args = $args [0];
        $query = str_replace( "'%s'", '%s' , $query ); 
            // in case someone mistakenly already singlequoted it
        $query = str_replace( '"%s"', '%s' , $query ); 
            // doublequote unquoting
        $query = preg_replace( '|(?<!%)%f|' , '%F' , $query ); 
            // Force floats to be locale unaware
        $query = preg_replace( '|(?<!%)%s|', "'%s'" , $query ); 
            // quote the strings, avoiding escaped strings like %%s
        array_walk( $args, array( $this, 'escape_by_ref' ) );
        return @ vsprintf( $query, $args );
    }
    View Code

    XSS攻击

    https://segmentfault.com/a/1190000005032978

    反射型xss 和 存储型xss

    主要目的:

    1. 盗取cookie, 获取敏感信息
    2. 冒充他人进行操作, 如加好友, 购物, 发消息...
    3. 在访问量极大的页面的xss可以攻击一些小网站,达到DDos的效果
    4. ...

    防御

    坚决不相信任何用户的输入。

    1. 在输出时使用htmlspecialchars()过滤
    2. 针对链接类型(如图片、超链等)的输出也可使用htmlspecialchars进行安全过滤,但如果变量是整个URL则应检查这个变量是否以http或https开头,如果不是则自动补齐避免出现伪协议类的XSS攻击。(例子:<a href=”data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTs8L3NjcmlwdD4=”>xss</a>)
    3. 富文本编辑器采用白名单, 再针对可能出现xss的标签进行安全过滤

    CSRF

    https://www.ibm.com/developerworks/cn/web/1102_niugang_csrf/

    攻击者伪造请求,  举个例子:

    a.com 有一个标签<img src=”http://b.com?action=del&id=1″>,  用户请求a.com, 将会产生一个请求 :http://b.com?action=del&id=1,, 浏览器会以当前用户在b.com上的身份(cookie信息)发起删除操作

    防御:

    1. 对于敏感操作使用随机token验证。 token必须足够随机,验证后立即删除,尽量以post提交防止泄露, 原理: 可以在服务端的session里面存一个token, 然后在用户提交的表单中也带一个token, 这个token不存在cookie中, 攻击者无法伪造此token, 也就攻击失败了。 通常做法是在页面加载时遍历整个dom树,在a和form标签后面加上token.

    2.验证HTTP Referer字段, 该字段记录了http请求来源地址, 但是不能确保万无一失,依赖于第三方浏览器。

    3.在HTTP头中自定义属性并验证

    文件上传漏洞

     白名单上传, 结合MIME和后缀检查文件类型

     使用随机文件名保存上传的文件,避免因终止符造成的文件名中断

    跨域

     浏览器同源策略限制, 不能访问其他域上的资源, 最主流的解决办法是jsonp, 原理 : 

    客户端

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script type="text/javascript">
            function get(data){
                alert(data);
            }
        </script>
        <script src="http://about.tw1996.com/jsonp.php?callback=get">
        </script>
    </head>
    <body>
    
    </body>
    </html>

    服务端

    <?php
    
    $callback = $_GET['callback'];
    
    echo $callback."(111)";
  • 相关阅读:
    计蒜客
    CodeForces -1216B.Shooting
    关于gets读入因为缓冲区出现的问题
    CodeForces
    Codeforces Round #603 (Div. 2) C.Everyone is A Winner!
    Codeforces Round #603 (Div. 2) A.Sweet Problem
    Codeforces Round #603 (Div. 2)B. PIN Codes
    Codeforces 524C.The Art of Dealing with ATM(暴力)
    Codeforces Round #600 (Div. 2) C. Sweets Eating
    数组的定义和初始化
  • 原文地址:https://www.cnblogs.com/tanxing/p/5689587.html
Copyright © 2020-2023  润新知