zoukankan      html  css  js  c++  java
  • 20169210 2016-2017-2《网络攻防实践》第十一周总结

    Collabtive 系统 SQL 注入实验

    环境搭建

    启动mysql:

    sudo mysqld_safe
    

    注意启动后程序不会退出,可以打开新的终端执行后续命令。

    启动Apache:

    sudo service apache2 start
    
        密码:dees
    

    配置DNS:

    sudo vim /etc/hosts
    
        按i进入编辑模式,编辑文件
    
        编辑完成后按Esc退出编辑
    
        完成后使用 :wq 保存并退出
    


    配置网站文件:

    sudo vim /etc/apache2/conf.d/lab.conf
    sudo service apache2 restart  重启服务
    

    关闭php配置策略:

    sudo vim /etc/php5/apache2/php.ini
    
    把magic_quotes_gpc=On 改为 magic_quotes_gpc = Off
    

    lab1 select语句的sql注入

    访问:www.sqllabcollabtive.com;当我们知道用户而不知道到密码的时候,我们可以怎么登陆?

    查看登陆验证文件:

    sudo vim /var/www/SQL/Collabtive/include/class.user.php
    

    设置行号 :set number

    查找 :/keyword

    找到其中第375行

    修改完后重启一下服务器:

    sudo service apache2 restart
    

    我们在$user后面加上) # 这样就会只验证用户名,后面的会被#注释

    绕过密码登录成功

    lab2 update语句的sql注入

    在Collabtive web应用程序中,如果用户想更新他们的个人资料,他们可以去我的帐户,单击编辑链接,然后填写表格以更新资料信息。在用户发送更新请求到服务器,一个UPDATE SQL语句将建造在include/class.user.php。这句话的目的是修改用户表中的当前用户的配置信息。

    有一个在这个SQL语句中的SQL注入漏洞:

    sudo vim /var/www/SQL/Collabtive/include/class.user.php
    

    我们可以找到如下的代码

    我们会发现sql语句为:SELECT ID WHERE name= '$user',并且company的位置是存在注入漏洞,原理同lab1。

    这样我们就可以越权来修改其他用户的信息及密码;我们使用任意用户,如: bob bob 进行登录;

    在编辑用户的位置:user 填 ted 用户;

    Company 处填:

    ', `pass` = '9d4e1e23bd5b727046a9e3b4b7db57bd8d6ee684' WHERE ID = 4 # '
     注:这里的 9d4e1e23bd5b727046a9e3b4b7db57bd8d6ee684 就是pass的md5值;
    

    点击修改,然后我们退出当前用户,使用ted用户登录,这个时候ted用户的密码应该是pass;

    防御策略

    SQL注入漏洞的根本问题是数据与代码的分离失败,因此我们可以针对这个原因进行防御

    防御策略1

    防御转义特殊字符使用,默认开启magic_quotes_gpc,将magic_quotes_gpc值设为On。

    sudo vim /etc/php5/apache2/php.ini
        sudo service apache2 restart
    

    防御策略2--避免使用特殊字符

    MySQL提供一个函数 mysql_real_escape_string(),这个函数可以用来过滤一些特殊字符;如x00, , , , ', " and x1a;

    代码防御示例:

    sudo vim /var/www/SQL/Collabtive/include/class.user.php
    

    修改为如下:

    // This code was provided by the lab's author Wenliang Du, of Syracuse
        // University under the GNU Free Documentation License
    
        function login($user, $pass)
        {
            if (!$user)
                {
                    return false;
                }
    
            // modification fixed
            $user = mysql_real_escape_string($user);
            $pass = mysql_real_escape_string($pass);
            $pass = sha1($pass);
    
            $sel1 = mysql_query("SELECT ID, name, locale, lastlogin, gender
                             FROM user WHERE (name =  '$user' OR
                             email = '$user') AND pass = '$pass'");
            $chk = mysql_fetch_array($sel1);
            if ($chk["ID"] != "")
                {
                    // New user session object and cookie creation code
                    // removed for brevity
                    return true;
                }
            else
            {
                return false;
            }
        }
    

    以及编辑用户代码:

    function edit($id, $name, $realname, $email, $tel1, $tel2, $company,
                  $zip, $gender, $url, $address1, $address2, $state,
                  $country, $tags, $locale, $avatar = "", $rate = 0.0)
        {
        $name = mysql_real_escape_string($name);
        $realname = mysql_real_escape_string($realname);
    
        // modification fixed
        $company = mysql_real_escape_string($company);
        $email = mysql_real_escape_string($email);
    
        // further escaped parameters removed for brevity...
    
        $rate = (float) $rate;
        $id = (int) $id;
    
        if ($avatar != "")
            {
                $upd = mysql_query("UPDATE user SET name='$name', email='$email',
                                    tel1='$tel1', tel2='$tel2', company='$company',
                                    zip='$zip', gender='$gender', url='$url',
                                    adress='$address1', adress2='$address2',
                                    state='$state', country='$country',
                                    tags='$tags', locale='$locale',
                                    avatar='$avatar', rate='$rate' WHERE ID = $id");
            }
        else
            {
                // same query as above minus setting avatar; removed for
                // brevity
            }
        if ($upd)
            {
                $this->mylog->add($name, 'user', 2, 0);
                return true;
            }
        else
            {
                return false;
            }
        }
    

    防御策略3--数据与sql语句的分离

    通过SQL逻辑分离来告诉数据库到底是哪部分是数据部分,哪一部分是SQL语句部分;

    提供以新的new mysqli()函数, 将这个函数写入config/standary/config.php文件:
    sudo vim /var/www/SQL/Collabtive/include/class.user.php

    修改代码如下:

        // This code was provided by the lab's author Wenliang Du, of Syracuse
        // University under the GNU Free Documentation License
    
        function login($user, $pass)
        {
        if (!$user)
            {
                return false;
            }
    
        // using prepared statements
    
        // note that $conn is instantiated in the datenbank class found in
        // ./class.datenbank.php. this may need to be passed in, but we
        // will assume we have access to it for the sake of brevity
    
        $stmt = $conn->prepare("SELECT ID,name,locale,lastlogin,gender FROM user
                                WHERE (name=? OR email=?) AND pass=?");
        $stmt->bind_param("sss", $user, $user, sha1($pass));
        $stmt->execute();
        $stmt->bind_result($bind_ID, $bind_name, $bind_locale, $bind_lastlogin,
                           $bind_gender);
        $chk = $stmt->fetch();
        if ($bind_ID != "")
            {
                // New user session object and cookie creation code
                // removed for brevity
                return true;
            }
        else
            {
                return false;
            }
        }
    

    以及编辑用户处的代码:

        // This code was provided by the lab's author Wenliang Du, of Syracuse
        // University under the GNU Free Documentation License
    
        function edit($id, $name, $realname, $email, $tel1, $tel2, $company, $zip,
                  $gender, $url, $address1, $address2, $state, $country, $tags,
                  $locale, $avatar = "", $rate = 0.0)
        {
        // the bind_param() function wants a double, not float, though
        // they are the same internally
        $rate = (double) $rate;
        $id = (int) $id;
    
        if ($avatar != "")
            {
                // again, $conn is instantiated in the datenbank class, and
                // may need to be passed, but we are assuming we have
                // access to it for the sake of brevity
    
                // note that the app uses zip as a string, does not use
                // realname although it is passed, and the columns adress
                // and adress2 are misspelled
    
                $stmt = $conn->prepare("UPDATE user SET name=?, email=?, tel1=?,
                                        tel2=?, company=?, zip=?, gender=?, url=?,
                                        adress=?, adress2=?, state=?, country=?,
                                        tags=?, locale=?, avatar=? rate=?
                                        WHERE ID = ?");
                $stmt->bind_param("sssssssssssssssdi", $name, $email, $tel1, $tel2,
                                   $company, $zip, $gender, $url, $address1,
                                   $address2, $state, $country, $tags, $locale,
                                   $avatar, $rate, $id);
                $upd = $stmt->execute();
            }
        else
            {
                $stmt = $conn->prepare("UPDATE user SET name=?, email=?, tel1=?,
                                        tel2=?, company=?, zip=?, gender=?, url=?,
                                        adress=?, adress2=?, state=?, country=?,
                                        tags=?, locale=?, rate=? WHERE ID = ?");
                $stmt->bind_param("ssssssssssssssdi", $name, $email, $tel1, $tel2,
                                   $company, $zip, $gender, $url, $address1,
                                   $address2, $state, $country, $tags, $locale,
                                   $rate, $id);
                $upd = $stmt->execute();
            }
        if ($upd)
            {
                $this->mylog->add($name, 'user', 2, 0);
                return true;
            }
        else
            {
                return false;
            }
        }
    

    调试

    下面的代码可以让服务器端php程序变量输出到一个文件中,以此来对php代码进行调试

        $myFile = "/tmp/mylog.txt";
        $fh = fopen($myFile, ’a’) or die("can’t open file");
        $Data = "a string";
        fwrite($fh, $Data . "
    ");
        fclose($fh);
    

    TCP_IP网络协议攻击实验

    ARP缓存欺骗

    RP 缓存是 ARP 协议的重要组成部分。ARP 协议运行的目标就是建立 MAC 地址和 IP 地址的映射,然后把这一映射关系保存在 ARP 缓存中,使得不必重复运行 ARP 协议。因为 ARP 缓存中的映射表并不是一直不变的,主机会定期发送 ARP 请求来更新它的 ARP 映射表,利用这个机制,攻击者可以伪造 ARP 应答帧使得主机错误的更新自己的 ARP 映射表,这个过程就是 ARP 缓存中毒。
    这样的后果即是要么使主机发送 MAC 帧到错误的 MAC 地址,导致数据被窃听;要么由于 MAC 地址不存在,导致数据发送不成功。
    首先查看靶机的ip地址

    用命令arp -a查看的arp缓存信息
    在攻击机上执行netwox 命令:实行arp欺骗,使靶机的MAC地址变为广播地址

    看到缓存的arp信息已经改变,攻击成功

  • 相关阅读:
    ubuntu 14.04+apache 反向代理设置
    ubuntu 14.04 使用apt-get出现如下问题解决办法
    ubuntu 出现 Unable to locate package update 解决办法
    在ubuntu 12.04 apache 限制IP访问的方法
    (原创)在ubuntu 14.04 中安装Apache2+modsecurity+awstats (新手教程)
    windows 10 的安装说明
    前端三大主流框架中文文档
    关于移动端影像配置了https之后拍出来的照片在android手机无法显示的问题
    ES5常用api
    promise循环调用异步函数(以图片上传为例)
  • 原文地址:https://www.cnblogs.com/crisgy/p/6852872.html
Copyright © 2011-2022 走看看