Jump to content

Passing The Table To Query From The Address Bar


JustinK101

Recommended Posts

Hello, is there a risk passing the table to do a mysql query on via the query string. For example I have the following:

"SELECT * FROM " . $_GET['tb'] . "";

I usually do smart_quote() on all my passed in parameters, but you cant quote the table name or else the query will fail. So are their any ways to lock this down a bit? Thanks.
Link to comment
Share on other sites

I dont know what smart_quote() is, but I guess it's a functions that escapes strings. For every charater it escapes, the string length grows by one. You probbly dont have charaters that need to be escaped in your table names, so a check like this one can prevent SQL-Injections. I dont know if 100% secure, but that's what I'd do-
[code]<?php

if(strlen(smart_quote($_GET['tb'])) != strlen($_GET['tb'])) die("Injection attempt.");

?>[/code]

Orio.
Link to comment
Share on other sites

Yeah I use:

function mysql_smart_quote($var)
{
  if (get_magic_quotes_gpc())
  {
      $var = stripslashes($var);
  }
 
  if (!is_numeric($var))
  {
      $var = "'" . mysql_real_escape_string($var) . "'";
  }
  return $var;
}

Just wondering though about passing in the table name in the query string. Somebody could simply change the variable and access different tables.
Link to comment
Share on other sites

Yes they can. It's always good to avoid using GET if there's another option.


Maybe creating another table that has two columns:
1) Table name
2) Random hash

Then when you generate links to use diffrent tables you use the random hash. When you need to pull out the table, search the hash in and match it to the required table in your database. Once again, the hash can be manually changed by people who want to try and view other tables, but this will be much more secure.


Orio.
Link to comment
Share on other sites

hummm md5 wont work, because I cant undo. How can I scramble the text, then be able to undo the scrambled and get back my orginal input?

I don't want to create a whole table for this purpose. The scrambling doesnst have to be insane, just something where your typical trouble maker wont be able to figure out.
Link to comment
Share on other sites

if you insist on using $_GET, then here is some advice:

keep an array of explicitly allowed field names.  if the $_GET variable is not holding that allowed name, then do not run the query.

example:

[code]
<?php
  $allowed = array('field1','field2','field3');
  $field =  (in_array($_GET['fieldid'], $allowed)) ? $_GET['fieldid'] : 'field1';
  $sql = "select $field from table...";
?>
[/code]
okay depending on what your script is actually doing, you might want to alter what returns from the ternary operator, should the condition be false.  The code above sets the "default" to 'field1' so if you want it to default to a certain field, then put that fieldname there.  If you are wanting it to not do anything at all, then putt NULL instead of 'field1' and then do a condition on your sql like

if ($field != NULL) {
  $sql = "...";
} else {
  echo "some error message or hack attempt! or something";
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.