[ 환경 ]
DVWA | v1.9 |
Burp Suite | Community Edition v2024.11.2 |
Impossible level은 보안조치가 된 소스코드로 이 코드는 사용자가 입력한 IP 주소로 ping 명령을 실행하는 기능을 제공한다. 추가적으로, CSRF(Cross-Site Request Forgery) 방어를 위해 Anti-CSRF 토큰을 검증하며, IP 주소 형식을 직접 확인한다.
checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
$target = $_REQUEST[ 'ip' ];
$target = stripslashes( $target );
$octet = explode( ".", $target );
if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {
$target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];
if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
$cmd = shell_exec( 'ping ' . $target );
} else {
$cmd = shell_exec( 'ping -c 4 ' . $target );
}
echo "<pre>{$cmd}</pre>";
else {
echo '<pre>ERROR: You have entered an invalid IP.</pre>';
}
generateSessionToken();
다음은 앞서 살펴본 코드에서 보안성과 기능성을 개선한 코드의 예제로 명령어 삽입 방지, 정확한 IP 검증, 출력 안전화와 같은 보안 강화를 적용했다.
<?php
// Anti-CSRF 토큰 생성
session_start();
if (!isset($_SESSION['session_token'])) {
$_SESSION['session_token'] = bin2hex(random_bytes(32));
}
function checkToken($userToken, $sessionToken, $redirect)
{
if (!hash_equals($sessionToken, $userToken)) {
header("Location: $redirect");
exit("CSRF token validation failed.");
}
}
if (isset($_POST['Submit'])) {
// Check Anti-CSRF token
checkToken($_POST['user_token'], $_SESSION['session_token'], 'index.php');
// Get and validate input
$target = $_POST['ip'];
// Validate IP address
if (!filter_var($target, FILTER_VALIDATE_IP)) {
die('<pre>ERROR: You have entered an invalid IP.</pre>');
}
// Determine OS and execute the ping command
if (stristr(php_uname('s'), 'Windows NT')) {
$cmd = shell_exec('ping ' . escapeshellarg($target));
} else {
$cmd = shell_exec('ping -c 4 ' . escapeshellarg($target));
}
// Feedback for the end user
echo "<pre>" . htmlspecialchars($cmd, ENT_QUOTES, 'UTF-8') . "</pre>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ping Utility</title>
</head>
<body>
<form method="post">
<label for="ip">Enter IP Address:</label>
<input type="text" name="ip" id="ip" required>
<input type="hidden" name="user_token" value="<?php echo $_SESSION['session_token']; ?>">
<button type="submit" name="Submit">Ping</button>
</form>
</body>
</html>
if (!isset($_SESSION['session_token'])) {
$_SESSION['session_token'] = bin2hex(random_bytes(32));
}
if (!hash_equals($sessionToken, $userToken)) {
header("Location: $redirect");
exit("CSRF token validation failed.");
}
if (!filter_var($target, FILTER_VALIDATE_IP)) {
die('<pre>ERROR: You have entered an invalid IP.</pre>');
}
if (stristr(php_uname('s'), 'Windows NT')) {
$cmd = shell_exec('ping ' . escapeshellarg($target));
} else {
$cmd = shell_exec('ping -c 4 ' . escapeshellarg($target));
}
echo "<pre>" . htmlspecialchars($cmd, ENT_QUOTES, 'UTF-8') . "</pre>";
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ping Utility</title>
</head>
<body>
<form method="post">
<label for="ip">Enter IP Address:</label>
<input type="text" name="ip" id="ip" required>
<input type="hidden" name="user_token" value="<?php echo $_SESSION['session_token']; ?>">
<button type="submit" name="Submit">Ping</button>
</form>
</body>
</html>
DVWA : CSRF - Medium level (0) | 2025.01.01 |
---|---|
DVWA : CSRF - Low level (0) | 2024.12.30 |
DVWA : Command Injection - High level (0) | 2024.12.29 |
DVWA : Command Injection - Medium level (0) | 2024.12.29 |
DVWA : Command Injection - Low level (0) | 2024.12.29 |