Pereiti prie turinio

Binladens

Nariai
  • Pranešimai

    21
  • Užsiregistravo

  • Lankėsi

  • Atsiliepimai

    0%

Reputacijos išklotinė

  1. Patinka
    Binladens sureagavo į DeSigneR php, array if   
    http://php.net/manual/en/function.in-array.php
     

    if (!in_array($kazkas, $haha)){ // vykdoma, jei $kazkas reiksmes nera $haha masyve. }
     
     
  2. Patinka
    Binladens sureagavo į Danielius5 PDO register/login + xss apsauga + xss atakos fixeris   
    Sveiki taigi pagalvojau parašyti PDO registracija/prisijungima taigi ko mums reikės -> index.php failo, .htaccess failo, funkcijos.php failo (nors funkcios tik dvi, bet manau kad jei bus padaryta atskirame faile suprasite daugiau :) mysql duom bazes ir users table. tai pradėkime :)
    visu pirma, kodėl PDO? nes PDO tai yra naujausias ir geriausias būdas daryti užklausas duom bazei, teisingai naudojamas PDO panaikina mysql injekcijos galimybę taip padarydamas puslapį saugesnį :)
    užeiname į mysql ir susikuriam table users ten sukuriame columns `id` su auto_incrementu `n`, `p`, `d`,`ip`
    tada sukuriame kita table log kur sukuriame columns `id` su auto_increment,`ip`,`d`,`v`,`b`
    atsirdarome funkcijos.php faila ir įrašome connect funkcija:

    function conn() { GLOBAL $dbh, $aplankas; $username = 'tavoduombazesuser'; $password = 'tavoduombazespwd'; try { $dbh = new PDO("mysql:host=localhost;dbname=tavoduombaze", $username, $password); echo ''; } catch(PDOException $e) { echo $e->getMessage(); } }
    Na tai čia yra mysql connect funkcija su PDO. Pakeiskit viska kas prasideda "tavo.." į savo duomenis.
    galime tęsti darbą. Padarykime dvi formas registracijos ir prisijungimo index.php faile (P.S. ŠRIFTUOTA TURI BŪTI Į UFT-8 WITHOUT BOM!) nes jei bus uft-8 mėtys warnings ir neveiks sesijos mėtys kažką tokio (Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by)

    <?php session_start(); include ("funkcijos.php"); conn (); ?> <html> <HEAD> <meta charset="utf-8" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </HEAD> <body> <?php if(!isset($_SESSION['login'])) { //patikrinam ar nėra sesijos login, sesija login bus sukurta kai prisijungsim if(!isset($_GET['action'])) { //patikrinam ar nėra get metode jokio action ?> <center><h1>Prisijungimas</h1></center><br/> <form action='index.php?action=login'method='post'> Vartotojo vardas:<br/><input name='n' placeholder=' Vartotojo vardas'><br/> Slaptažodis:<br/><input name='p' type='password' placeholder=' Slaptažodis'><br/> <input type='submit' value='Prisijungti'></form><br/><br/> <center><h1>Registracija</h1></center><br/> <form action='index.php?action=reg 'method='post'> Vartotojo vardas:<br/><input name='n' placeholder=' Vartotojo vardas'><br/> Slaptažodis:<br/><input name='p' type='password' placeholder=' Slaptažodis'><br/> <input type='submit' value='Registruotis'></form> <?php } // uždarom if salygą ?></body></html>
    Na štai turime reg ir login Formas. na o dabar grįžkime į funkcijų failą ir sukurkime fixerio xss atakų funkcija.

    function safe ($i,$input) { $username = 'tavodbuser'; $password = 'tavodbpw'; try { $dbh = new PDO("mysql:host=localhost;dbname=tavodb", $username, $password); echo ''; } catch(PDOException $e) { echo $e->getMessage(); } // nezinau kodėl bet kažkodėl reikia junktis iš naujo, nes kitaip neveikia if(strpos($i,"<script>")!== FALSE OR strpos($i,"</script>")!== FALSE) { /* xss ataka daroma su tagu "<script>" todel neleide jo įrašyt apsaugosime nuo atakos ir fixinsime bandymus */ $i = htmlspecialchars($i); // paverčiame visus < > ir kt ko nereikia spec simboliais $date = date("Y-m-d H:i"); // išgaunam data metai-menuo-diena valanda:minutes $ip = $_SERVER['REMOTE_ADDR']; // išgaunam ip adresa $get = $dbh->prepare("SELECT * FROM `log` WHERE `ip`=? AND `v`=?"); // su PDO patikrinam ar jau yra bandyta laužtis su šiuo ip per šitą vietą $get -> execute (array($ip,$input)); $get = $get->fetch(); if($get) { // jeigu yra bandyta tiesiog updatinsim data $info = $dbh->prepare("UPDATE `log` SET `d`=?,`b`=? WHERE `ip`=?"); // updatinam $info -> execute (array($date,$i,$ip)); // b kintamasis yra pravalytas ivestas tekstas $op = fopen(".htaccess","c+"); // atidarome htaccess faila fwrite($op, "deny from ".$ip); //įrašome ip draudimo funkcija įrašius ja žmogus nurodytu ip adresu negalės pasiekti tinklapio fclose($op); // uždarome faila } else { // o jeigu iš šio ip per šia vietą nebuvo bandoma xss tada iterpsim nauja bandyma į duom baze $info = $dbh->prepare("INSERT INTO `log` (`ip`,`d`,`v`,`b`) VALUES (?,?,?,?)"); // iterpiam bandyma $info -> execute (array($ip,$date,$input,$i)); $op = fopen(".htaccess","c+"); // įrašom į .htaccess kad nebeleistu įeiti fwrite($op, "deny from ".$ip); // įrašom į .htaccess kad nebeleistu įeiti fclose($op); //uždarome } Die(" <span style='color:red;'>Patikėk mūsų sistema parašyta gerai ir tokie kaip tu jokiu būdu nesugebės pakenkti, o tavo kvaili mėginimai baigsis blogai tik tau pačiam ;) </span> "); // išvedame die klaida (die klaida išveda į ekrana teksta ir nutraukia visa koda esanti po ja } else { return $i;} }
    Fsio fixerį jau padarėm dabar tęskim darbus :) einam į index.php ir po

    <?php } // uždarom if salygą
    sukuriame register sistemą ;)

    if(isset($_GET['action'] && $_GET['action'] == "reg") { // jeigu get metodu siunčiama "action" ir action = reg tada vykdysime koda tarp lauštinių skliaustų if(isset($_SESSION['login'])) { echo "<meta http-equiv="refresh" content="0;url=http://errqraz.us.lt/darbas/disabled.php" />"; } // jeigu bando registruotis prisijunges mes į pagrindini puslapi else { if(isset($_POST['n']) && isset($_POST['p']) && !empty($_POST['n']) && !empty($_POST['p']) { // patikrinam ar siunčiami n ir p ir ar jie ne tušti $n = safe($_POST['n'],"register"); //prafiltuojam gaunamus duomenis su musu fixeriu $p = safe($_POST['p'],"register"); //prafiltuojam gaunamus duomenis su musu fixeriu $n = htmlspecialchars($n); // del visa ko praeinam pro xss filtra $p = htmlspecialchars($p); // del visa ko praeinam pro xss filtra $n1 = strlen($n); // patikrinam ilguma, strlen skaiciuoja simboliu kieki $p1 = strlen($p); if($n1 >30 OR $n1 <6 OR $p1 >30 OR $p1 < 6) { echo "nickas ir passwordas turi būti 6-30 simbolių ilgumo"; } else {$info = $dbh->prepare("SELECT * FROM `users` WHERE n=?"); // patikrinam ar toks nick neregistruotas $info -> execute (array($n)); $info = $info->fetch(); if($info) { // jeigu registruotas echo "toks nick registruotas"; } else { if($n == $p) { echo "Vesk slaptažodį kitokį nei vartotojo vardą."; } else { // na va pagrindinius dalykus apėjome dabar kiškime į duom bazę $date = date("Y-m-d H:i"); $ip = $_SERVER['REMOTE_ADDR']; require("password.php"); // includinam biblioteką, nes ne visos php versijos palaikys šia funkcija $hash = password_hash($p, PASSWORD_DEFAULT); /*užkoduojam slaptažodį, tai pats saugiausias būdas užkodavimo todėl kad viena žodį hashinant 10 kartų bus 10 skirtingų hash, todėl iškoduoti labai sunku (rodos neįmanoma net)*/ $reg = $dbh->prepare("INSERT INTO `users`(n,p,d,ip,) VALUES (?,?,?,?)"); $reg -> execute (array($n,$hash,$date,$ip)); echo "Sėkmingai užregistruotas!"; //registracija baigta } }} } else { // jeigu tušti echo "Kažko neįvedei"; }
    Na liko login ;D
     

    if(isset($_GET['action']) && $_GET['action']=="login") { if(isset($_POST['n']) && isset($_POST['p']) && !empty($_POST['n']) && !empty($_POST['p'])) { $n = safe($_POST['n'],"login"); // jei bus isilauzimo bandymas rodys kad lauztasi per login $p = safe($_POST['p'],"login"); $n = htmlspecialchars($n)); $p = htmlspecialchars($p)); $info = $dbh->prepare("SELECT * FROM `users` WHERE n=?"); $info -> execute (array($n)); $info = $info->fetch(); $ip = $_SERVER['REMOTE_ADDR']; if($info) { // jeigu yra toks vartotojas require("password.php"); //vel includinam biblioteka if(password_verify($p, $info['p']) ) { // čia funkcija kuri sulygina ar gali but tam passwordui toks hash echo "Sėkmingai prisijungei"; $_SESSION['login']=true; $_SESSION['n']=$n; } else { echo "slaptažodis neteisingas!"; } } else { echo"nėra tokio vartotojo!";}
    na ka teliko sukurti password.php failą sukuriam ir ten įrašom ši kodą, ir uždarom daugiau mum jo nereikės :) nes čia tiesiog biblioteka ;)
     

    <?php /** * A Compatibility library with PHP 5.5's simplified password hashing API. * * @author Anthony Ferrara <[email protected]> * @license http://www.opensource.org/licenses/mit-license.html MIT License * @copyright 2012 The Authors */ namespace { if (!defined('PASSWORD_DEFAULT')) { define('PASSWORD_BCRYPT', 1); define('PASSWORD_DEFAULT', PASSWORD_BCRYPT); /** * Hash the password using the specified algorithm * * @param string $password The password to hash * @param int $algo The algorithm to use (Defined by PASSWORD_* constants) * @param array $options The options for the algorithm to use * * @return string|false The hashed password, or false on error. */ function password_hash($password, $algo, array $options = array()) { if (!function_exists('crypt')) { trigger_error("Crypt must be loaded for password_hash to function", E_USER_WARNING); return null; } if (!is_string($password)) { trigger_error("password_hash(): Password must be a string", E_USER_WARNING); return null; } if (!is_int($algo)) { trigger_error("password_hash() expects parameter 2 to be long, " . gettype($algo) . " given", E_USER_WARNING); return null; } $resultLength = 0; switch ($algo) { case PASSWORD_BCRYPT: // Note that this is a C constant, but not exposed to PHP, so we don't define it here. $cost = 10; if (isset($options['cost'])) { $cost = $options['cost']; if ($cost < 4 || $cost > 31) { trigger_error(sprintf("password_hash(): Invalid bcrypt cost parameter specified: %d", $cost), E_USER_WARNING); return null; } } // The length of salt to generate $raw_salt_len = 16; // The length required in the final serialization $required_salt_len = 22; $hash_format = sprintf("$2y$%02d$", $cost); // The expected length of the final crypt() output $resultLength = 60; break; default: trigger_error(sprintf("password_hash(): Unknown password hashing algorithm: %s", $algo), E_USER_WARNING); return null; } $salt_requires_encoding = false; if (isset($options['salt'])) { switch (gettype($options['salt'])) { case 'NULL': case 'boolean': case 'integer': case 'double': case 'string': $salt = (string) $options['salt']; break; case 'object': if (method_exists($options['salt'], '__tostring')) { $salt = (string) $options['salt']; break; } case 'array': case 'resource': default: trigger_error('password_hash(): Non-string salt parameter supplied', E_USER_WARNING); return null; } if (PasswordCompat\binary\_strlen($salt) < $required_salt_len) { trigger_error(sprintf("password_hash(): Provided salt is too short: %d expecting %d", PasswordCompat\binary\_strlen($salt), $required_salt_len), E_USER_WARNING); return null; } elseif (0 == preg_match('#^[a-zA-Z0-9./]+$#D', $salt)) { $salt_requires_encoding = true; } } else { $buffer = ''; $buffer_valid = false; if (function_exists('mcrypt_create_iv') && !defined('PHALANGER')) { $buffer = mcrypt_create_iv($raw_salt_len, MCRYPT_DEV_URANDOM); if ($buffer) { $buffer_valid = true; } } if (!$buffer_valid && function_exists('openssl_random_pseudo_bytes')) { $buffer = openssl_random_pseudo_bytes($raw_salt_len); if ($buffer) { $buffer_valid = true; } } if (!$buffer_valid && @is_readable('/dev/urandom')) { $f = fopen('/dev/urandom', 'r'); $read = PasswordCompat\binary\_strlen($buffer); while ($read < $raw_salt_len) { $buffer .= fread($f, $raw_salt_len - $read); $read = PasswordCompat\binary\_strlen($buffer); } fclose($f); if ($read >= $raw_salt_len) { $buffer_valid = true; } } if (!$buffer_valid || PasswordCompat\binary\_strlen($buffer) < $raw_salt_len) { $bl = PasswordCompat\binary\_strlen($buffer); for ($i = 0; $i < $raw_salt_len; $i++) { if ($i < $bl) { $buffer[$i] = $buffer[$i] ^ chr(mt_rand(0, 255)); } else { $buffer .= chr(mt_rand(0, 255)); } } } $salt = $buffer; $salt_requires_encoding = true; } if ($salt_requires_encoding) { // encode string with the Base64 variant used by crypt $base64_digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; $bcrypt64_digits = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; $base64_string = base64_encode($salt); $salt = strtr(rtrim($base64_string, '='), $base64_digits, $bcrypt64_digits); } $salt = PasswordCompat\binary\_substr($salt, 0, $required_salt_len); $hash = $hash_format . $salt; $ret = crypt($password, $hash); if (!is_string($ret) || PasswordCompat\binary\_strlen($ret) != $resultLength) { return false; } return $ret; } /** * Get information about the password hash. Returns an array of the information * that was used to generate the password hash. * * array( * 'algo' => 1, * 'algoName' => 'bcrypt', * 'options' => array( * 'cost' => 10, * ), * ) * * @param string $hash The password hash to extract info from * * @return array The array of information about the hash. */ function password_get_info($hash) { $return = array( 'algo' => 0, 'algoName' => 'unknown', 'options' => array(), ); if (PasswordCompat\binary\_substr($hash, 0, 4) == '$2y$' && PasswordCompat\binary\_strlen($hash) == 60) { $return['algo'] = PASSWORD_BCRYPT; $return['algoName'] = 'bcrypt'; list($cost) = sscanf($hash, "$2y$%d$"); $return['options']['cost'] = $cost; } return $return; } /** * Determine if the password hash needs to be rehashed according to the options provided * * If the answer is true, after validating the password using password_verify, rehash it. * * @param string $hash The hash to test * @param int $algo The algorithm used for new password hashes * @param array $options The options array passed to password_hash * * @return boolean True if the password needs to be rehashed. */ function password_needs_rehash($hash, $algo, array $options = array()) { $info = password_get_info($hash); if ($info['algo'] != $algo) { return true; } switch ($algo) { case PASSWORD_BCRYPT: $cost = isset($options['cost']) ? $options['cost'] : 10; if ($cost != $info['options']['cost']) { return true; } break; } return false; } /** * Verify a password against a hash using a timing attack resistant approach * * @param string $password The password to verify * @param string $hash The hash to verify against * * @return boolean If the password matches the hash */ function password_verify($password, $hash) { if (!function_exists('crypt')) { trigger_error("Crypt must be loaded for password_verify to function", E_USER_WARNING); return false; } $ret = crypt($password, $hash); if (!is_string($ret) || PasswordCompat\binary\_strlen($ret) != PasswordCompat\binary\_strlen($hash) || PasswordCompat\binary\_strlen($ret) <= 13) { return false; } $status = 0; for ($i = 0; $i < PasswordCompat\binary\_strlen($ret); $i++) { $status |= (ord($ret[$i]) ^ ord($hash[$i])); } return $status === 0; } } } namespace PasswordCompat\binary { /** * Count the number of bytes in a string * * We cannot simply use strlen() for this, because it might be overwritten by the mbstring extension. * In this case, strlen() will count the number of *characters* based on the internal encoding. A * sequence of bytes might be regarded as a single multibyte character. * * @param string $binary_string The input string * * @internal * @return int The number of bytes */ function _strlen($binary_string) { if (function_exists('mb_strlen')) { return mb_strlen($binary_string, '8bit'); } return strlen($binary_string); } /** * Get a substring based on byte limits * * @see _strlen() * * @param string $binary_string The input string * @param int $start * @param int $length * * @internal * @return string The substring */ function _substr($binary_string, $start, $length) { if (function_exists('mb_substr')) { return mb_substr($binary_string, $start, $length, '8bit'); } return substr($binary_string, $start, $length); } }
     
    Na va dabar turime manau gerai apsaugota reg/login sistemą su password hashinimu, visgi rašiau iš galvos tai jei pamatysit klaidų sakykit ištaisysiu nors manau kad gerai viskas turi būti ;) Sėkmės :) Laukiu komentarų ;) PAMOKA KURTA TIK MANO IR BE MANO SUTIKMO NELEIDŽIU NIEKAM JĄ DALINTIS!
×
×
  • Pasirinkite naujai kuriamo turinio tipą...