PHP利用白名单防御相关函数

接着采用对DVWA代码执行漏洞模块进行代码审计

<?php

if( 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 );

    // 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.
        $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
        echo "<pre>{$cmd}</pre>";
    }
    else {
        // Ops. Let the user name theres a mistake
        echo '<pre>ERROR: You have entered an invalid IP.</pre>';
    }
}

// Generate Anti-CSRF token
generateSessionToken();

?>

checkToken()

主要作用:用于验证一个给定的令牌(token)具体来说,这个函数可能用于检查令牌是否已经过期或是否已被篡改。

举例:

<?php
function checkToken($token) {
    // 验证令牌是否为空
    if (empty($token)) {
        throw new Exception('令牌不能为空');
    }

    // 解析令牌并提取相关信息
    $tokenData = parseToken($token);

    // 验证令牌中的数据是否有效
    if (!validateTokenData($tokenData)) {
        throw new Exception('令牌数据无效');
    }
}
?>

stripslashes()

主要作用:stripslashes()它的作用是从字符串中移除反斜杠字符 (\)

举例:


<?php
$str = "It's a \"test\" string.";
echo stripslashes($str);
// 输出:It's a "test" string.
?>

explode()

主要作用:explode()用于将字符串按照指定的分隔符拆分为数组

基本语法:array explode ( string $delimiter , string $string [, int $limit ] )

  • $delimiter 是要用来分割字符串的字符或字符串;
  • $string 是要被分割的字符串;
  • [int $limit] 是可选参数,表示最多保留多少个元素,如果设置为负数,则从最后开始计数。

举例:

<?php
$string = "apple,banana,cherry,dog,elephant";
$fruits = explode(",",$string);
print_r($fruits);
?>

        在这个例子中,我们将一个包含多个水果名称的字符串按照逗号进行拆分,得到一个数组。运行上述代码后,将会输出:

Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
    [3] => dog
    [4] => elephant
)

is_numeric()

主要作用:is_numeric()用于检查变量是否包含一个数字或者可转换为数字(如浮点数或十六进制数)的字符串。

该函数会返回布尔值,即 true 或 false。

举例:

<?php

$var = "123";

if (is_numeric($var)) {
    echo "$var is numeric";
} else {
    echo "$var is not numeric";
}

?>

sizeof()

主要作用:sizeof()实际上是PHP中的一个别名函数,它的功能与count()完全相同。sizeof()count()都可以用来计算数组或对象中的元素数量。

举例:

<?php
$fruits = array("apple", "banana", "orange");

echo sizeof($fruits); // 输出: 3
echo count($fruits); // 输出: 3

?>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值