Results 1 to 15 of 15

Thread: Secured PHP Query

  1. #1

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Secured PHP Query

    Does this query the secured one?
    Code:
                    database_connect()
                    $query="SELECT id
    				FROM users
    				WHERE username='$un';";
    		$result =mysql_query($query) or
    		dir(mysql_error($dbconn));
    		if(mysql_num_rows($result) < 1)
    		{
    			die("No such users, Cant create directory");
    		}
    		$userdata=mysql_fetch_array($result,MYSQL_ASSOC);
    		$userid = $userdata['id'];
    		mkdir("images/avatar/".$userid,0777) or
    		dir("Cant create directory");

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Secured PHP Query

    no. the whole point of a "secure query" is to sanitise a user's input. here, you're just taking the user's input (presumably $un) and inserting it into your query. you must sanitise input before using it if you're looking to make something secure. in this case, you would be looking to use mysql_real_escape_string():
    PHP Code:
      $un mysql_real_escape_string($_POST['username']);
      
    $sql "SELECT id FROM users WHERE username='{$un}';"
    also, you're calling the dir() function a bunch of times, probably rather than die(). dir() creates an instance of the Dir class -- die() will kill execution of your script.

  3. #3

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: Secured PHP Query

    sorry i mistyped dir() instead of die().
    Ya i am already using mysql_real_escape_string in the beginning page, but i din't show here.
    I came to hear of the SQL injection. using this alone make the query secured? or Do i need to use paramaterized query?
    If so what is really happening when using parameters.

  4. #4
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: Secured PHP Query

    1) Nobody can really tell if your code is secure if you purposely miss bits out.
    2) If you use mysql_real_escape_string on each of your user inputs then you should be fine.

  5. #5

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: Secured PHP Query

    Ya that's right, but here in this case, i am querying id for the corresponding username, i am using the same username for many cases and so i once used that function at the beginning. But i must shown that here and i missed.
    Thankyou

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Secured PHP Query

    Use mysqli or PDO, and change your code to use prepared statements with parameters. This avoids any risk of SQL injection.

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Secured PHP Query

    Quote Originally Posted by bharanidharanit View Post
    Does this query the secured one?
    Code:
                    database_connect()
                    $query="SELECT id
    				FROM users
    				WHERE username='$un';";
    		$result =mysql_query($query) or
    		dir(mysql_error($dbconn));
    		if(mysql_num_rows($result) < 1)
    		{
    			die("No such users, Cant create directory");
    		}
    		$userdata=mysql_fetch_array($result,MYSQL_ASSOC);
    		$userid = $userdata['id'];
    		mkdir("images/avatar/".$userid,0777) or
    		dir("Cant create directory");
    You tell us!! Its a snippet poorly written code, so I can only assume that it has come from a poorly written application which is insecure.

    On its own and from the looks of things you are using auto globals and creating directories with global write permissions which makes it very insecure.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  8. #8

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: Secured PHP Query

    Quote Originally Posted by visualAd View Post
    On its own and from the looks of things you are using auto globals and creating directories with global write permissions which makes it very insecure.
    How to make it secured?

  9. #9

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: Secured PHP Query

    Hi i searched for PDO coding, and i get this, but i am running with errors. Whats error here?
    Code:
    try{
    $dbh=new
    PDO('mysql:host=localhost;dbname=alejandro','user','password');
    $dbh->prepare('SELECT * FROM users WHERE name=? AND email=?');
    $dbh->execute(array('Alejandro','[email protected]'));
    $result=$dbh->fetchAll();
    // displays data for 'Alejandro'
    print_r($result);
    $dbh->execute(array('John','[email protected]'));
    // display data for 'John'
    print_r($result);
    }
    catch(PDOException $e) {
    echo 'Error : '.$e->getMessage();
    exit();
    }
    As shown above,
    Also for passing one value, why are they using array?

  10. #10

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: Secured PHP Query

    Hi, i debugged the error and also found the use of array.
    Code:
    <?php
    
    try{
    $dbh=new
    PDO('mysql:host=localhost;dbname=mvb22','root','admin');
    $sth=$dbh->prepare('SELECT id,username,userpwd FROM users WHERE username=?');
    $sth->execute(array('bharani'));
    $result=$sth->fetchAll();
    print_r($result[0][0]);
    }
    catch(PDOException $e) {
    echo 'Error : '.$e->getMessage();
    exit();
    }
    
    ?>

  11. #11
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Secured PHP Query

    Replacing your code with that won't work, because the code above does something different from the code you posted. If you wish to use PDO you need to modify your code; you also need to ensure the PDO_Mysql driver is installed and enabled (this can be checked with a call to phpinfo()).

    In response to your previous question, I would suggest the following at minimum:

    • If you are not already, get the user submitted variable from the $_POST or $_GET super-globals and go to your PHP.ini and turn register_globals off. If your host will not let you, find another host.

    • If you are inserting any variables into your script, you need to use mysql_real_escape_string on them before hand. If you do not, they can be populated with SQL and be used to do all kinds of nasty stuff.

      Ideally, you should use either mysqli or PDO. These extensions support parametized queries which will automatically escape the variables for you.

    • Remove access to the images directory by either moving it above the root pages of the website, or by modifying the permissions so that it is not globally writeable (this may not be possible with some setups). At the very least, if you do not want users to have direct access to the directory, you should put an .htaccess file into it with the following lines:

      Code:
      Order Allow, Deny
      
      Deny From All
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  12. #12

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: Secured PHP Query

    Replacing your code with that won't work, because the code above does something different from the code you posted.
    Ya may be the codes are different, but the first one throwed me an error, whereas the second does not. The second one gives me output what i needed.
    Also the second one i referred from PHP Manual, and i think i can use that. ???
    If you are not already, get the user submitted variable from the $_POST or $_GET super-globals
    I am using $_POST already,
    go to your PHP.ini and turn register_globals off
    Why to do this? I am not sure; only if it is on,i can able to get variables from $_POST or $_GET.
    Remove access to the images directory by either moving it above the root pages of the website, or by modifying the permissions so that it is not globally writeable (this may not be possible with some setups). At the very least, if you do not want users to have direct access to the directory, you should put an .htaccess file into it with the following lines:
    But when the users uploads their files, it must be copied to the images directory. When i remove access to that directory means, how the can user do that?

    Also .htaccess, i tried it before, and i read that many hosts does not support it. Is that really true?

  13. #13
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Secured PHP Query

    Quote Originally Posted by bharanidharanit View Post
    Ya may be the codes are different, but the first one throwed me an error, whereas the second does not. The second one gives me output what i needed.
    Also the second one i referred from PHP Manual, and i think i can use that. ???
    If the second one works, you should probably use that I wouldn't recommend posting your mysql username and password on the Internet though and I wouldn't recommend using the root account either.

    I would also discourage the printing of the exception message. These are in effect, error messages and can reveal sensitive infrormation about your application the the server on which it resides.

    Quote Originally Posted by bharanidharanit View Post
    Why to do this? I am not sure; only if it is on,i can able to get variables from $_POST or $_GET.
    Because register globals can be used by an attacker to poisen uninitialised variables in your script with their own input. You can check if it is on by using a call to php.ini.

    Quote Originally Posted by bharanidharanit View Post
    But when the users uploads their files, it must be copied to the images directory. When i remove access to that directory means, how the can user do that?
    At the very least, your PHP scripts will have access to directories above the root of the website. These directories are not accessible by the user but can be accessed by you.

    If your PHP scripts run under a different user ID from the webserver, you can change the permissions on the directory to make it inaccessible to Apache and thus users who attempt to access.

    Quote Originally Posted by bharanidharanit View Post
    Also .htaccess, i tried it before, and i read that many hosts does not support it. Is that really true?
    Its not the best way to deny access to a directory. If you host gives you access to the Apache config files they will probably disable .htaccess files. The best thing to do is check.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  14. #14

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: Secured PHP Query

    Ya thankyou.
    At the very least, your PHP scripts will have access to directories above the root of the website. These directories are not accessible by the user but can be accessed by you.

    If your PHP scripts run under a different user ID from the webserver, you can change the permissions on the directory to make it inaccessible to Apache and thus users who attempt to access.
    HI again i am asking the same one. But i want the users to add their photos or some files into the directory, and without directory permission how can they do that?

  15. #15
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Secured PHP Query

    users don't need to be able to access the directory for your script to be able to upload images to that directory. you can chmod the directory so that you have access to it (owner) and the user does not have any access to it. this will force you to use a script to fetch those images though, rather than just having a regular <img> tag to retrieve the images. you could just allow the users read access but not write, though. it depends how you want to deal with stuff.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width