Autor Zpráva
Anonymní
Profil *
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
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
ale jak řešit text
http://cz.php.net/manual/cs/function.mysql-real-escape-string.php
nightfish
Profil
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
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
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
1ight
Co ti vadí na mezerách?
1ight
Profil
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
V případě čísla použij funkci intval.
1ight
Profil
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
texty nevim jak ošetřit
http://cz.php.net/manual/cs/function.mysql-real-escape-string.php
pomocítřídy
Profil *
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 *
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 *
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 *
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 *
může to admin umazat, nezobrazilo se mi to hned, tak jsem to zkoušel ještě dvakrát, s úpravou.

Vaše odpověď

Mohlo by se hodit


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0