$maxlifetime,
'path' => '/',
'domain' => $domain,
'secure' => $secure,
'httponly' => $httponly,
'samesite' => $samesite
]);
session_start();
// This is the call that will force a new Set-Cookie header with the right flags
session_regenerate_id();
}
if (array_key_exists ("Login", $_POST) && $_POST['Login'] == "Login") {
dvwa_start_session();
} else {
if (!session_id()) {
session_start();
}
}
if (!array_key_exists ("default_locale", $_DVWA)) {
$_DVWA[ 'default_locale' ] = "en";
}
dvwaLocaleSet( $_DVWA[ 'default_locale' ] );
// Start session functions --
function &dvwaSessionGrab() {
if( !isset( $_SESSION[ 'dvwa' ] ) ) {
$_SESSION[ 'dvwa' ] = array();
}
return $_SESSION[ 'dvwa' ];
}
function dvwaPageStartup( $pActions ) {
if (in_array('authenticated', $pActions)) {
if( !dvwaIsLoggedIn()) {
dvwaRedirect( DVWA_WEB_PAGE_TO_ROOT . 'login.php' );
}
}
}
function dvwaLogin( $pUsername ) {
$dvwaSession =& dvwaSessionGrab();
$dvwaSession[ 'username' ] = $pUsername;
}
function dvwaIsLoggedIn() {
global $_DVWA;
if (in_array("disable_authentication", $_DVWA) && $_DVWA['disable_authentication']) {
return true;
}
$dvwaSession =& dvwaSessionGrab();
return isset( $dvwaSession[ 'username' ] );
}
function dvwaLogout() {
$dvwaSession =& dvwaSessionGrab();
unset( $dvwaSession[ 'username' ] );
}
function dvwaPageReload() {
if ( array_key_exists( 'HTTP_X_FORWARDED_PREFIX' , $_SERVER )) {
dvwaRedirect( $_SERVER[ 'HTTP_X_FORWARDED_PREFIX' ] . $_SERVER[ 'PHP_SELF' ] );
}
else {
dvwaRedirect( $_SERVER[ 'PHP_SELF' ] );
}
}
function dvwaCurrentUser() {
$dvwaSession =& dvwaSessionGrab();
return ( isset( $dvwaSession[ 'username' ]) ? $dvwaSession[ 'username' ] : 'Unknown') ;
}
// -- END (Session functions)
function &dvwaPageNewGrab() {
$returnArray = array(
'title' => 'Damn Vulnerable Web Application (DVWA)',
'title_separator' => ' :: ',
'body' => '',
'page_id' => '',
'help_button' => '',
'source_button' => '',
);
return $returnArray;
}
function dvwaSecurityLevelGet() {
global $_DVWA;
// If there is a security cookie, that takes priority.
if (isset($_COOKIE['security'])) {
return $_COOKIE[ 'security' ];
}
// If not, check to see if authentication is disabled, if it is, use
// the default security level.
if (in_array("disable_authentication", $_DVWA) && $_DVWA['disable_authentication']) {
return $_DVWA[ 'default_security_level' ];
}
// Worse case, set the level to impossible.
return 'impossible';
}
function dvwaSecurityLevelSet( $pSecurityLevel ) {
if( $pSecurityLevel == 'impossible' ) {
$httponly = true;
}
else {
$httponly = false;
}
setcookie( 'security', $pSecurityLevel, 0, "/", "", false, $httponly );
$_COOKIE['security'] = $pSecurityLevel;
}
function dvwaLocaleGet() {
$dvwaSession =& dvwaSessionGrab();
return $dvwaSession[ 'locale' ];
}
function dvwaSQLiDBGet() {
global $_DVWA;
return $_DVWA['SQLI_DB'];
}
function dvwaLocaleSet( $pLocale ) {
$dvwaSession =& dvwaSessionGrab();
$locales = array('en', 'zh');
if( in_array( $pLocale, $locales) ) {
$dvwaSession[ 'locale' ] = $pLocale;
} else {
$dvwaSession[ 'locale' ] = 'en';
}
}
// Start message functions --
function dvwaMessagePush( $pMessage ) {
$dvwaSession =& dvwaSessionGrab();
if( !isset( $dvwaSession[ 'messages' ] ) ) {
$dvwaSession[ 'messages' ] = array();
}
$dvwaSession[ 'messages' ][] = $pMessage;
}
function dvwaMessagePop() {
$dvwaSession =& dvwaSessionGrab();
if( !isset( $dvwaSession[ 'messages' ] ) || count( $dvwaSession[ 'messages' ] ) == 0 ) {
return false;
}
return array_shift( $dvwaSession[ 'messages' ] );
}
function messagesPopAllToHtml() {
$messagesHtml = '';
while( $message = dvwaMessagePop() ) { // TODO- sharpen!
$messagesHtml .= "
{$message}
";
}
return $messagesHtml;
}
// --END (message functions)
function dvwaHtmlEcho( $pPage ) {
$menuBlocks = array();
$menuBlocks[ 'home' ] = array();
if( dvwaIsLoggedIn() ) {
$menuBlocks[ 'home' ][] = array( 'id' => 'home', 'name' => 'Home', 'url' => '.' );
$menuBlocks[ 'home' ][] = array( 'id' => 'instructions', 'name' => 'Instructions', 'url' => 'instructions.php' );
$menuBlocks[ 'home' ][] = array( 'id' => 'setup', 'name' => 'Setup / Reset DB', 'url' => 'setup.php' );
}
else {
$menuBlocks[ 'home' ][] = array( 'id' => 'setup', 'name' => 'Setup DVWA', 'url' => 'setup.php' );
$menuBlocks[ 'home' ][] = array( 'id' => 'instructions', 'name' => 'Instructions', 'url' => 'instructions.php' );
}
if( dvwaIsLoggedIn() ) {
$menuBlocks[ 'vulnerabilities' ] = array();
$menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'brute', 'name' => 'Brute Force', 'url' => 'vulnerabilities/brute/' );
$menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'exec', 'name' => 'Command Injection', 'url' => 'vulnerabilities/exec/' );
$menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'csrf', 'name' => 'CSRF', 'url' => 'vulnerabilities/csrf/' );
$menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'fi', 'name' => 'File Inclusion', 'url' => 'vulnerabilities/fi/.?page=include.php' );
$menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'upload', 'name' => 'File Upload', 'url' => 'vulnerabilities/upload/' );
$menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'captcha', 'name' => 'Insecure CAPTCHA', 'url' => 'vulnerabilities/captcha/' );
$menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'sqli', 'name' => 'SQL Injection', 'url' => 'vulnerabilities/sqli/' );
$menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'sqli_blind', 'name' => 'SQL Injection (Blind)', 'url' => 'vulnerabilities/sqli_blind/' );
$menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'weak_id', 'name' => 'Weak Session IDs', 'url' => 'vulnerabilities/weak_id/' );
$menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'xss_d', 'name' => 'XSS (DOM)', 'url' => 'vulnerabilities/xss_d/' );
$menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'xss_r', 'name' => 'XSS (Reflected)', 'url' => 'vulnerabilities/xss_r/' );
$menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'xss_s', 'name' => 'XSS (Stored)', 'url' => 'vulnerabilities/xss_s/' );
$menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'csp', 'name' => 'CSP Bypass', 'url' => 'vulnerabilities/csp/' );
$menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'javascript', 'name' => 'JavaScript', 'url' => 'vulnerabilities/javascript/' );
if (dvwaCurrentUser() == "admin") {
$menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'authbypass', 'name' => 'Authorisation Bypass', 'url' => 'vulnerabilities/authbypass/' );
}
$menuBlocks[ 'vulnerabilities' ][] = array( 'id' => 'open_redirect', 'name' => 'Open HTTP Redirect', 'url' => 'vulnerabilities/open_redirect/' );
}
$menuBlocks[ 'meta' ] = array();
if( dvwaIsLoggedIn() ) {
$menuBlocks[ 'meta' ][] = array( 'id' => 'security', 'name' => 'DVWA Security', 'url' => 'security.php' );
$menuBlocks[ 'meta' ][] = array( 'id' => 'phpinfo', 'name' => 'PHP Info', 'url' => 'phpinfo.php' );
}
$menuBlocks[ 'meta' ][] = array( 'id' => 'about', 'name' => 'About', 'url' => 'about.php' );
if( dvwaIsLoggedIn() ) {
$menuBlocks[ 'logout' ] = array();
$menuBlocks[ 'logout' ][] = array( 'id' => 'logout', 'name' => 'Logout', 'url' => 'logout.php' );
}
$menuHtml = '';
foreach( $menuBlocks as $menuBlock ) {
$menuBlockHtml = '';
foreach( $menuBlock as $menuItem ) {
$selectedClass = ( $menuItem[ 'id' ] == $pPage[ 'page_id' ] ) ? 'selected' : '';
$fixedUrl = DVWA_WEB_PAGE_TO_ROOT.$menuItem[ 'url' ];
$menuBlockHtml .= "{$menuItem[ 'name' ]}\n";
}
$menuHtml .= "";
}
// Get security cookie --
$securityLevelHtml = '';
switch( dvwaSecurityLevelGet() ) {
case 'low':
$securityLevelHtml = 'low';
break;
case 'medium':
$securityLevelHtml = 'medium';
break;
case 'high':
$securityLevelHtml = 'high';
break;
default:
$securityLevelHtml = 'impossible';
break;
}
// -- END (security cookie)
$userInfoHtml = 'Username: ' . ( dvwaCurrentUser() );
$securityLevelHtml = "Security Level: {$securityLevelHtml}";
$localeHtml = 'Locale: ' . ( dvwaLocaleGet() );
$sqliDbHtml = 'SQLi DB: ' . ( dvwaSQLiDBGet() );
$messagesHtml = messagesPopAllToHtml();
if( $messagesHtml ) {
$messagesHtml = "{$messagesHtml}
";
}
$systemInfoHtml = "";
if( dvwaIsLoggedIn() )
$systemInfoHtml = "{$userInfoHtml}
{$securityLevelHtml}
{$localeHtml}
{$sqliDbHtml}
";
if( $pPage[ 'source_button' ] ) {
$systemInfoHtml = dvwaButtonSourceHtmlGet( $pPage[ 'source_button' ] ) . " $systemInfoHtml";
}
if( $pPage[ 'help_button' ] ) {
$systemInfoHtml = dvwaButtonHelpHtmlGet( $pPage[ 'help_button' ] ) . " $systemInfoHtml";
}
// Send Headers + main HTML code
Header( 'Cache-Control: no-cache, must-revalidate'); // HTTP/1.1
Header( 'Content-Type: text/html;charset=utf-8' ); // TODO- proper XHTML headers...
Header( 'Expires: Tue, 23 Jun 2009 12:00:00 GMT' ); // Date in the past
echo "
{$pPage[ 'title' ]}
{$pPage[ 'body' ]}
{$messagesHtml}
{$systemInfoHtml}
";
}
function dvwaHelpHtmlEcho( $pPage ) {
// Send Headers
Header( 'Cache-Control: no-cache, must-revalidate'); // HTTP/1.1
Header( 'Content-Type: text/html;charset=utf-8' ); // TODO- proper XHTML headers...
Header( 'Expires: Tue, 23 Jun 2009 12:00:00 GMT' ); // Date in the past
echo "
{$pPage[ 'title' ]}
{$pPage[ 'body' ]}
";
}
function dvwaSourceHtmlEcho( $pPage ) {
// Send Headers
Header( 'Cache-Control: no-cache, must-revalidate'); // HTTP/1.1
Header( 'Content-Type: text/html;charset=utf-8' ); // TODO- proper XHTML headers...
Header( 'Expires: Tue, 23 Jun 2009 12:00:00 GMT' ); // Date in the past
echo "
{$pPage[ 'title' ]}
{$pPage[ 'body' ]}
";
}
// To be used on all external links --
function dvwaExternalLinkUrlGet( $pLink,$text=null ) {
if(is_null( $text )) {
return '' . $pLink . '';
}
else {
return '' . $text . '';
}
}
// -- END ( external links)
function dvwaButtonHelpHtmlGet( $pId ) {
$security = dvwaSecurityLevelGet();
$locale = dvwaLocaleGet();
return "";
}
function dvwaButtonSourceHtmlGet( $pId ) {
$security = dvwaSecurityLevelGet();
return "";
}
// Database Management --
if( $DBMS == 'MySQL' ) {
$DBMS = htmlspecialchars(strip_tags( $DBMS ));
}
elseif( $DBMS == 'PGSQL' ) {
$DBMS = htmlspecialchars(strip_tags( $DBMS ));
}
else {
$DBMS = "No DBMS selected.";
}
function dvwaDatabaseConnect() {
global $_DVWA;
global $DBMS;
//global $DBMS_connError;
global $db;
global $sqlite_db_connection;
if( $DBMS == 'MySQL' ) {
if( !@($GLOBALS["___mysqli_ston"] = mysqli_connect( $_DVWA[ 'db_server' ], $_DVWA[ 'db_user' ], $_DVWA[ 'db_password' ], "", $_DVWA[ 'db_port' ] ))
|| !@((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE " . $_DVWA[ 'db_database' ])) ) {
//die( $DBMS_connError );
dvwaLogout();
dvwaMessagePush( 'Unable to connect to the database.
' . mysqli_error($GLOBALS["___mysqli_ston"]));
dvwaRedirect( DVWA_WEB_PAGE_TO_ROOT . 'setup.php' );
}
// MySQL PDO Prepared Statements (for impossible levels)
$db = new PDO('mysql:host=' . $_DVWA[ 'db_server' ].';dbname=' . $_DVWA[ 'db_database' ].';port=' . $_DVWA['db_port'] . ';charset=utf8', $_DVWA[ 'db_user' ], $_DVWA[ 'db_password' ]);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
elseif( $DBMS == 'PGSQL' ) {
//$dbconn = pg_connect("host={$_DVWA[ 'db_server' ]} dbname={$_DVWA[ 'db_database' ]} user={$_DVWA[ 'db_user' ]} password={$_DVWA[ 'db_password' ])}"
//or die( $DBMS_connError );
dvwaMessagePush( 'PostgreSQL is not currently supported.' );
dvwaPageReload();
}
else {
die ( "Unknown {$DBMS} selected." );
}
if ($_DVWA['SQLI_DB'] == SQLITE) {
$location = DVWA_WEB_PAGE_TO_ROOT . "database/" . $_DVWA['SQLITE_DB'];
$sqlite_db_connection = new SQLite3($location);
$sqlite_db_connection->enableExceptions(true);
# print "sqlite db setup";
}
}
// -- END (Database Management)
function dvwaRedirect( $pLocation ) {
session_commit();
header( "Location: {$pLocation}" );
exit;
}
// XSS Stored guestbook function --
function dvwaGuestbook() {
$query = "SELECT name, comment FROM guestbook";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query );
$guestbook = '';
while( $row = mysqli_fetch_row( $result ) ) {
if( dvwaSecurityLevelGet() == 'impossible' ) {
$name = htmlspecialchars( $row[0] );
$comment = htmlspecialchars( $row[1] );
}
else {
$name = $row[0];
$comment = $row[1];
}
$guestbook .= "\n";
}
return $guestbook;
}
// -- END (XSS Stored guestbook)
// Token functions --
function checkToken( $user_token, $session_token, $returnURL ) { # Validate the given (CSRF) token
global $_DVWA;
if (in_array("disable_authentication", $_DVWA) && $_DVWA['disable_authentication']) {
return true;
}
if( $user_token !== $session_token || !isset( $session_token ) ) {
dvwaMessagePush( 'CSRF token is incorrect' );
dvwaRedirect( $returnURL );
}
}
function generateSessionToken() { # Generate a brand new (CSRF) token
if( isset( $_SESSION[ 'session_token' ] ) ) {
destroySessionToken();
}
$_SESSION[ 'session_token' ] = md5( uniqid() );
}
function destroySessionToken() { # Destroy any session with the name 'session_token'
unset( $_SESSION[ 'session_token' ] );
}
function tokenField() { # Return a field for the (CSRF) token
return "";
}
// -- END (Token functions)
// Setup Functions --
$PHPUploadPath = realpath( getcwd() . DIRECTORY_SEPARATOR . DVWA_WEB_PAGE_TO_ROOT . "hackable" . DIRECTORY_SEPARATOR . "uploads" ) . DIRECTORY_SEPARATOR;
$PHPCONFIGPath = realpath( getcwd() . DIRECTORY_SEPARATOR . DVWA_WEB_PAGE_TO_ROOT . "config");
$phpDisplayErrors = 'PHP function display_errors: Enabled' : 'failure">Disabled' ) . ''; // Verbose error messages (e.g. full path disclosure)
$phpDisplayStartupErrors = 'PHP function display_startup_errors: Enabled' : 'failure">Disabled' ) . ''; // Verbose error messages (e.g. full path disclosure)
$phpDisplayErrors = 'PHP function display_errors: Enabled' : 'failure">Disabled' ) . ''; // Verbose error messages (e.g. full path disclosure)
$phpURLInclude = 'PHP function allow_url_include: Enabled' : 'failure">Disabled' ) . ''; // RFI
$phpURLFopen = 'PHP function allow_url_fopen: Enabled' : 'failure">Disabled' ) . ''; // RFI
$phpGD = 'PHP module gd: Installed' : 'failure">Missing - Only an issue if you want to play with captchas' ) . ''; // File Upload
$phpMySQL = 'PHP module mysql: Installed' : 'failure">Missing' ) . ''; // Core DVWA
$phpPDO = 'PHP module pdo_mysql: Installed' : 'failure">Missing' ) . ''; // SQLi
$DVWARecaptcha = 'reCAPTCHA key: ' . $_DVWA[ 'recaptcha_public_key' ] : 'failure">Missing' ) . '';
$DVWAUploadsWrite = 'Writable folder ' . $PHPUploadPath . ': Yes' : 'failure">No' ) . ''; // File Upload
$bakWritable = 'Writable folder ' . $PHPCONFIGPath . ': Yes' : 'failure">No' ) . ''; // config.php.bak check // File Upload
$DVWAOS = 'Operating system: ' . ( strtoupper( substr (PHP_OS, 0, 3)) === 'WIN' ? 'Windows' : '*nix' ) . '';
$SERVER_NAME = 'Web Server SERVER_NAME: ' . $_SERVER[ 'SERVER_NAME' ] . ''; // CSRF
$MYSQL_USER = 'Database username: ' . $_DVWA[ 'db_user' ] . '';
$MYSQL_PASS = 'Database password: ' . ( ($_DVWA[ 'db_password' ] != "" ) ? '******' : '*blank*' ) . '';
$MYSQL_DB = 'Database database: ' . $_DVWA[ 'db_database' ] . '';
$MYSQL_SERVER = 'Database host: ' . $_DVWA[ 'db_server' ] . '';
$MYSQL_PORT = 'Database port: ' . $_DVWA[ 'db_port' ] . '';
// -- END (Setup Functions)
?>