contoh kode yang vulnerable:
<?php
$host = "localhost";$user = "admin";$pass = "admin123";$database = "webhack_example";mysql_connect($host, $user, $pass);mysql_select_db($database);
if ( isset($_POST['submit']) ) {
$username = $_POST['username'];
$password = md5($_POST['password']);
if ( empty($_POST['username']) ) {
echo "Username Empty";
}
else {
$query= "SELECT * FROM user WHERE username = '$username' AND password = '$password' LIMIT 1";
$result = mysql_query($query);
if ($result) {
$result2 = mysql_num_rows($result);
if ( $result2 > 0 ) {
echo "LOGIN SUCCESS, You will be redirect to admin page in blah blah blah";
}
else {
echo "LOGIN FAILED! USERNAME PASSWORD DID NOT MATCH!";
}
}
}
}?><form method="POST">
Username : <input type="text" name="username" />
Password : <input type="password" name="password" />
<input type="submit" name="submit" value="Login" />
</form>
Sekarang kita dapat melihat bahwa kode tersebut mengambil variable 'username' dan 'password' dan mengeksekusinya tanpa filterisasi sedikitpun!katakanlah username dan password yang valid adalah admin:admin
Normalnya validasi login akan seperti berikut:
SELECT * FROM user WHERE username = 'admin' AND password = '21232f297a57a5a743894a0e4a801fc3' LIMIT 1
Injeksi Query :Cobalah untuk menggunakan username sebagai berikut :
whatever' or 1=1 -- '
Query MySQL akan berudah menjadi sebagai berikut :
SELECT * FROM user WHERE username = 'whatever' or 1=1 -- '' AND password = 'md5('admin')' LIMIT 1
NB : Query setelah tanda [ -- ] ( Comment SQL ) akan diabaikan sehingga nilai dari query tersebut selalu bernilai true.Sekarang kita tahu apa yang 'salah' pada kode tersebut, yaitu tidak adanya filterisasi pada karakter-karakter berbahaya seperti [ ' ] , [ - ] dan sebagainya.
<?php
$host = "localhost";$user = "admin";$pass = "admin123";$database = "webhack_example";
function clean($val){
$danger = array("*","/","'","="); // add your self here
$clean = '';
for($i = 0; $i <= strlen($val); $i++){
if(!in_array(substr($val,$i,1),$danger)){
$clean .= substr($val,$i,1);
}
}
for($i = 0; $i < count($danger); $i++){
$clean = str_replace($danger[$i],"",$clean);
}
return $clean;
}
@mysql_connect($host, $user, $pass);
@mysql_select_db($database);
if ( isset($_POST['submit']) ) {
$username = clean($_POST['username']);
$password = md5($_POST['password']);
if ( empty($_POST['username']) ) {
echo "Username Empty";
}
else {
$query= "SELECT * FROM user WHERE username = '$username' AND password = '$password' LIMIT 1";
$result = @mysql_query($query);
if ($result) {
$result2 = @mysql_num_rows($result);
if ( $result2 > 0 ) {
echo "LOGIN SUCCESS, You will be redirect to admin page in blah blah blah";
}
else {
echo "LOGIN FAILED! USERNAME PASSWORD DID NOT MATCH!";
}
}
}
}?><form method="POST">
Username : <input type="text" name="username" />
Password : <input type="password" name="password" />
<input type="submit" name="submit" value="Login" />
</form>
that is it, the code is more better now..
0 komentar:
Post a Comment