Autor | Zpráva | ||
---|---|---|---|
Anonymní Profil * |
#1 · Zasláno: 13. 2. 2008, 13:12:29
Dobrý dech chtel bych se Vás zeptat
včera jsem se snažil řešit ochranu podle informací které jsem našel na internetu (6 hodin řešil aby jste mi tu nevytkly malo studuje :) a nejak mi to nefugnuje mužete poradit proč je muj přistup špatný? PS: dodatek bylo mi poraděno abcyh to posílal postem ale v případě formulářu co jsem četl nekde nainternetu se da taky zneužít sql dotaz proto bych chtel rozchodit get <?php $link = mysql_connect.......... mysql_select_db...... if ( $_REQUEST['id'] == "" ) { $_REQUEST['id'] = "10_inzert"; } echo '<a href="x.php?id=' . $_REQUEST['id'] . '">'; $result = mysql_query("SELECT * FROM '" . addslashes($_GET["id"]) . "' ", $link); $num_rows = mysql_num_rows($result) or die ( mysql_error()) ; echo "$num_rows Rows\n"; ?> když pak zapíšu cokoliv v url např známý 1=1 tak to hází ============================================ Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/localhost/htdocs/x.php on line 41 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''10_inzert 1=1'' at line 1 jedinný který jsem rozběhal bylo to když jsem tam místo addslashes zmenil na intval to pak nepropustí žádný text.. ale jak řešit text? |
||
1ight Profil |
#2 · Zasláno: 13. 2. 2008, 13:17:43
URL: pro sql error vypada takhle: ´
======================================== před: http://10.0.0.254/x.php?id=10_inzert 1=1 po enteru: http://10.0.0.254/x.php?id=10_inzert%201=1 |
||
djlj Profil |
#3 · Zasláno: 13. 2. 2008, 13:21:27
ale jak řešit text
http://cz.php.net/manual/cs/function.mysql-real-escape-string.php |
||
nightfish Profil |
#4 · Zasláno: 13. 2. 2008, 13:55:44
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/localhost/htdocs/x.php on line 41
viz http://diskuse.jakpsatweb.cz/index.php?action=vthread&forum=28&topic=3 7836 |
||
1ight Profil |
#5 · Zasláno: 13. 2. 2008, 14:17:54
Já vám moc děkuju za tipy, až budu zkušenější pokusím se Vám to určitě taky nějak vrátit. Fakt díky moc za cesty
|
||
1ight Profil |
#6 · Zasláno: 13. 2. 2008, 18:00:56
Poradíte mi ještě prosím někdo?
příklad z http://cz.php.net/manual/cs/function.mysql-real-escape-string.php $item = "Zak's and Derick's Laptop"; $escaped_item = mysql_real_escape_string($item); printf ("Escaped string: %s\n", $escaped_item); v tomlhe příkladu to mezery vubec neřeší :-( řeší to jen (uvozovky kdy před to vloží / viz např href:.....?id=1 (tady si mužu psat co chci a sql vesele vypisuje všechno co budu chtít jak na vánoce) a tenhle příklad mi vubec nefunguje se zdvojenýma ' " . ." ' píše to // ošetření vstupních dat od z: http://php.vrana.cz/obrana-proti-sql-injection.php mysql_query("SELECT * FROM tabulka WHERE nazev = '" . addslashes($_GET["nazev"]) . "' OR id = " . intval($_GET["id"])); ____________ když to převedu na praxi tak to vypisuje Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/localhost/htdocs/x.php on line 24 to je řádka While atd... muj příklad z praxe je $username = $_REQUEST['id']; echo addslashes($username); $result = mysql_query("SELECT * FROM '" . addslashes($username) . "' ", $link); while ( $sql03 = mysql_fetch_array ( $result ) ) { $yy_01 = $sql03['yy']; } echo "<br>". $yy_01; |
||
djlj Profil |
#7 · Zasláno: 14. 2. 2008, 02:09:46
1ight
Co ti vadí na mezerách? |
||
1ight Profil |
#8 · Zasláno: 14. 2. 2008, 02:31:27
to je jednoduchý když zapíšu do url
http://10.0.0.254/x.php?id=10_inzert tak to vypíše Resource id #3 21 // to je správný mam tam 21 tabulek a když to upravím na http://10.0.0.254/x.php?id=10_inzert WHERE id_00 = 1 > tohle schrousta a vyhodí >http://10.0.0.254/x.php?id=10_inzert%20WHERE%20id_00%20=%201 tak to vypíše Resource id #3 1 // 1 tabulka takže komunikuju se sql :-(nightfish jak psal nightfish odkaz tak mě to až napadlo před chvílí že bych mohl mít problém s Db a kodováním ale tak snažil jsem se to donastavit přes mysql_query (" SET NAMES 'cp1250'" ); ale nevím jak se to má chovat přesně a nevím jestli jsem to dobře nastavil nebo protože jak jsem psal tenhle příkaz mi to nevezme '" . addslashes($_GET["nazev"]) . "' musím ho skárti o jedny apostrofy nebo co je to. nemůžu tam mít dvoje :-( tak nevim |
||
djlj Profil |
#9 · Zasláno: 14. 2. 2008, 11:02:05
V případě čísla použij funkci intval.
|
||
Časová prodleva: 5 dní
|
|||
1ight Profil |
#10 · Zasláno: 19. 2. 2008, 12:13:11
djlj to ju máš pravdu ale texty nevim jak ošetřit ... jedinný na co jsem přišel a co funguje asi na 100% aby mi nikdy nešahal do db ani nepokoušel se nejak neco delas s sql dotazem tak to řešit těma regulárníma vyrazama nebo jak se to píše ale je to zase problém na víc :-( Děkuju moc
|
||
djlj Profil |
#11 · Zasláno: 19. 2. 2008, 12:28:10
texty nevim jak ošetřit
http://cz.php.net/manual/cs/function.mysql-real-escape-string.php |
||
Časová prodleva: 8 dní
|
|||
pomocítřídy Profil * |
#12 · Zasláno: 27. 2. 2008, 17:38:01
class pripojeni { protected $mysqli; protected $showerror = TRUE; // nastavte FALSE, pokud nechcete zobrazovat chybová hlášení protected $showsql = false; // nastavte TRUE, pokud chcete kvůli ladění zobrazovat všechny dotazy SQL protected $showtags = TRUE; // konstruktor function __construct() { require('password.php'); $this->mysqli = @new mysqli($mysqlhost, $mysqluser, $mysqlpasswd, $mysqldb); // je připojení v pořádku? if(mysqli_connect_errno()) { echo '<span style="color:green;font:bold 12px Arial, Tahoma">Chyba ve spojení MySQL!</span>'; $this->printerror("Nemám spojení na MySQL! (" . mysqli_connect_error() . ")"); $this->mysqli = FALSE; exit(); } $this->getMysqli("SET NAMES 'cp1250'"); $this->starttime = $this->microtime_float(); } // destruktor function __destruct() { $this->close(); } // dodatečné uzavření function close() { if($this->mysqli) $this->mysqli->close(); $this->mysqli = FALSE; } function getMysqli() { return $this->mysqli; } // vykonání dotazu SELECT, vrací se pole function queryObjectArray($sql, $file, $line, $tags=0, $and=0, $ins=0, $val=0, $or=0, $del=0, $trun=0) { $file = shortFile($file); $sql = strtolower($sql); if($tags!=0) { if((substr_count($sql,"'")) !=$tags) {$this->printtags("Number of TAGs is not validated from entry!", $file, $line); return false; } } else { if(stripos($sql, "'")) {$this->printtags("The TAGs has not been exists!", $file, $line); return false; } } if($and!=0 ) { if((substr_count($sql, "and"))!=$and ) {$this->printtags("Number of ANDs is not validated from entry!", $file, $line); return false; } } else { if(stripos($sql, " and ")) {$this->printtags("The ANDs has not been exists!", $file, $line); return false; } } if($val!=0 ) { if((substr_count($sql, "values"))!=$val ) {$this->printtags("Number of VALUESs is not validated from entry!", $file, $line); return false; } } else { if(stripos($sql, " values ")) {$this->printtags("The VALUESs has not been exists!", $file, $line); return false; } } if($or!=0 ) { if((substr_count($sql, "or"))!=$or ) {$this->printtags("Number of ORs is not validated from entry!", $file, $line); return false; } } else { if(stripos($sql, " or ")) {$this->printtags("The ORs has not been exists!", $file, $line); return false; } } if($del!=0 ) { if((substr_count($sql, "delete"))!=$del ) {$this->printtags("Number of DELETEs is not validated from entry!", $file, $line); return false; } } else { if(stripos($sql, " delete ")) {$this->printtags("The DELETEs has not been exists!", $file, $line); return false; } } if($ins!=0 ) { if((substr_count($sql, "insert"))!=$ins ) {$this->printtags("Number of INSERTs is not validated from entry!", $file, $line); return false; } } else { if(stripos($sql, " insert ")) {$this->printtags("The INSERTs has not been exists!", $file, $line); return false; } } if($trun!=0) { if((substr_count($sql, "truncate"))!=$trun) {$this->printtags("Number of TRUNCATEs is not validated from entry!", $file, $line); return false; } } else { if(stripos($sql, " truncate ")){$this->printtags("The TRUNCATEs has not been exists!", $file, $line); return false; } } $this->printsql($sql); $result = $this->mysqli->query($sql); if($result) { if($result->num_rows) { while($row = $result->fetch_object()) $result_array[] = $row; return $result_array; } else return FALSE; } else { $this->printerror($this->mysqli->error, $file, $line); return FALSE; } } // vykonání dotazu SELECT, vrací se pole function queryArray($sql, $file, $line, $tags=0, $and=0, $ins=0, $val=0, $or=0, $del=0, $trun=0) { $file = shortFile($file); $sql = strtolower($sql); if($tags!=0) { if((substr_count($sql,"'")) !=$tags) {$this->printtags("Number of TAGs is not validated from entry!", $file, $line); return false; } } else { if(stripos($sql, "'")) {$this->printtags("The TAGs has not been exists!", $file, $line); return false; } } if($and!=0 ) { if((substr_count($sql, " and "))!=$and ) {$this->printtags("Number of ANDs is not validated from entry!", $file, $line); return false; } } else { if(stripos($sql, " and ")) {$this->printtags("The ANDs has not been exists!", $file, $line); return false; } } if($val!=0 ) { if((substr_count($sql, " values "))!=$val ) {$this->printtags("Number of VALUESs is not validated from entry!", $file, $line); return false; } } else { if(stripos($sql, " values ")) {$this->printtags("The VALUESs has not been exists!", $file, $line); return false; } } if($or!=0 ) { if((substr_count($sql, " or "))!=$or ) {$this->printtags("Number of ORs is not validated from entry!", $file, $line); return false; } } else { if(stripos($sql, " or ")) {$this->printtags("The ORs has not been exists!", $file, $line); return false; } } if($del!=0 ) { if((substr_count($sql, " delete "))!=$del ) {$this->printtags("Number of DELETEs is not validated from entry!", $file, $line); return false; } } else { if(stripos($sql, " delete ")) {$this->printtags("The DELETEs has not been exists!", $file, $line); return false; } } if($ins!=0 ) { if((substr_count($sql, " insert "))!=$ins ) {$this->printtags("Number of INSERTs is not validated from entry!", $file, $line); return false; } } else { if(stripos($sql, " insert ")) {$this->printtags("The INSERTs has not been exists!", $file, $line); return false; } } if($trun!=0) { if((substr_count($sql, " truncate "))!=$trun) {$this->printtags("Number of TRUNCATEs is not validated from entry!", $file, $line); return false; } } else { if(stripos($sql, " truncate ")){$this->printtags("The TRUNCATEs has not been exists!", $file, $line); return false; } } $this->printsql($sql); $result = $this->mysqli->query($sql); if($result) { if($result->num_rows) { while($row = $result->fetch_array()) $result_array[] = $row; return $result_array; } else return FALSE; } else { $this->printerror($this->mysqli->error, $file, $line); return FALSE; } } // použití insert_id za příkazem INSERT function insertId() { return $this->mysqli->insert_id; } // vložení \ před ', " atd. function escape($txt) { return trim($this->mysqli->escape_string($txt)); } // vrací hodnotu 'NULL' nebo '<quoted string>' function sql_string($txt) { if(!$txt || trim($txt)=="") return 'NULL'; else return "'" . $this->escape(trim(addslashes($txt))) . "'"; } function error() { return $this->mysqli->error; } private function printsql($sql) { if($this->showsql) printf("<p><font color=\"#0000ff\">%s</font></p>\n", htmlentities($sql)); } private function printtags($txt, $file, $line) { if($this->showtags) { printf("<p><font color=\"#ff9900\">%s</font></p>\n", htmlentities($txt)); printf("<p><font color=\"#00cc00\">%s</font></p>\n", htmlentities($file.":::".$line)); } } private function printerror($txt, $file, $line) { if($this->showerror) { printf("<p><font color=\"#ff0000\">%s</font></p>\n", htmlentities($txt)); printf("<p><font color=\"#00cc00\">%s</font></p>\n", htmlentities($file.":::".$line)); } } } Potom ve scriptu se používá takto: if ($db->query |
||
pomocítřídy Profil * |
#13 · Zasláno: 27. 2. 2008, 17:39:05
Potom ve scriptu se používá takto:
if ($db->queryArray("SELECT neco FROM tabulka WHERE DATE(sloupecsacaem) = CURDATE() AND username=".$db->sql_string($_POST['username']).", __FILE__, __LINE__, 2, 1)) { echo výpis polí; } tučně je důležité, dvojka znamená počet tagů ' jednička počet AND tím se vyhnete vložení něčeho co nechcete |
||
pomocítřídy Profil * |
#14 · Zasláno: 27. 2. 2008, 17:39:58
Potom ve scriptu se používá takto:
if ($db->queryArray("SELECT neco FROM tabulka WHERE DATE(sloupecsacaem) = CURDATE() AND username=".$db->sql_string($_POST['username']).", __FILE__, __LINE__, 2, 1)) { echo výpis polí; } tučně je důležité, dvojka znamená počet tagů " ' " jednička počet AND tím se vyhnete vložení něčeho co nechcete |
||
pomocítřídy Profil * |
#15 · Zasláno: 27. 2. 2008, 17:41:07
Potom ve scriptu se používá takto:
if ($db->dotazSQL, __FILE__, __LINE__, počet tagů ' ', počet ANDů, atd...)) { echo výpis polí; } tučně je důležité, dvojka znamená počet tagů ' jednička počet AND tím se vyhnete vložení něčeho co nechcete |
||
pomocítřídy Profil * |
#16 · Zasláno: 27. 2. 2008, 17:41:57
může to admin umazat, nezobrazilo se mi to hned, tak jsem to zkoušel ještě dvakrát, s úpravou.
|
||
Časová prodleva: 16 let
|
0