I am a 3rd-year computer science undergraduate. One of my university lecturers has developed his own page for students to submit work. It came up that one student was accused of hacking (sic) by the system. The problem was the characters used in the comment field.
The lecturer claimed that he needed to filter out certain characters in order to stop attacks, and that it was a known problem with PHP. (His system had been broken by students before). I am suspecting poor programming, as he claims that the strings are being executed by PHP.
He told me to try writing a PHP program with a form text field, then input PHP code into the text form. He expects that there to be a vulnerability without filtering the input for bad chars.
<?php
if(isset($_POST['data']) ){
if( file_exists("../post.sqlite") ){
$db = new SQLite3("../post.sqlite");
if( isset($_POST['password']) && $_POST['password'] === "correct horse battery staple" ){
$statement = $db->prepare("INSERT INTO posts( id, post ) VALUES ( NULL, ? )");
$statement->bindValue(1, $_POST['data'], SQLITE3_TEXT);
$statement->execute();
}
}
else {
$db = new SQLite3("../post.sqlite");
$db->exec("CREATE TABLE posts( id INTEGER PRIMARY KEY, post TEXT )");
}
}
function posts(){
if( file_exists("../post.sqlite") ){
$db = new SQLite3("../post.sqlite");
$query = $db->query("SELECT post from posts ORDER BY id DESC");
while( $row = $query->fetchArray(SQLITE3_ASSOC)){
print( "<pre>" );
print( htmlentities( $row['post'] ) );
print( "</pre><hr />" );
}
}
}
?>
<!DOCTYPE html>
<html>
<head><title>Try This</title></head>
<body>
<form action="post.php" method="post">
<textarea rows="10" cols="80" name="data"></textarea><br />
<input type="submit" value="Post" /><br />
Secret key: <input type="text" name="password" />
</form>
<hr />
<div id="posts">
<?php posts(); ?>
</div>
</body>
</html>
Without the htmlentities
function, I know it would have client side vulnerabilities (running malicious scripts). SQL injection is covered (prepared statements). I don't see how the program can be exploited server side. What (if anything) am I missing?
$_POST['data']
though that is the main concern here. If you have PHP version >= 5.2 check outfilter_input()
\$\endgroup\$