Re: [PHP-DB] SQL Injection attack

2005-08-25 Thread Vicente
Estimado veditio,

you wrote:
> I've got a ton of forms that use the $_POST variable to send
> information into the database [...]
> Any suggestions on how to tighten up the form security, or does
> magic_quotes help enough? 

I'm not a security expert but after some attacks I have implemented
this simple thing. Until today it works for me.

You can put it before be connected to your database. I have one
only script to connect my database placed outside the /public_html.
It is and requested by means one include() in every oho script.
In this way, this security works in the whole site.

|%3c|%3e|SELECT|UNION|UPDATE|AND|exe|exec|INSERT|tmp/i';
  ...etc

//  detecting
if (preg_match($inyecc, $resto)) {

   // make something, in example sending an e-mail alert
   $ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
   $forwarded = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
   $remoteaddress = $HTTP_SERVER_VARS["REMOTE_ADDR"];

   $message = "attack injection in $mi_url \n\nchain: $resto \n\n
   from: (ip-forw-RA):- $ip - $forwarded - $remoteaddress\n\n
   - end ";
   
   mail("[EMAIL PROTECTED]", "Attack injection", $message,
   "From: [EMAIL PROTECTED]'SERVER_NAME']}", "[EMAIL 
PROTECTED]'SERVER_NAME']}");

   // kill execution
   echo 'illegal url';
   die();
}   

// DB connection
$connection=mysql_connect(...etc.

?>


if you can encode this script with Zend Encoder or a similar thing.
It will be an additional measure to avoid the reading of this file.


hope it can be useful,



Vicente,

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] SQL Injection attack

2005-08-25 Thread tg-php
Haha.. what the hell?  Ok, I know this is an older copy of the script I wrote 
because I know I took out the "All this does is escape the data" comment and I 
KNOW I saw the thing about mysql_escape_string() being deprecated...  don't 
know why it's still in there. Hah

Thanks for pointing that out.  Now off to find my newer version and make sure I 
chaned it there too.

-TG

= = = Original message = = =

no !!!

mysql_real_escape_string()

anyhow.. good luck with your security endeavors!

On 8/25/05, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> I'm pretty amateur at this too, but have done a little reading on the 
> subject.  Here's some nuggets to ponder while the real experts write their 
> responses: :)
> 
> 1. Magic quotes + mysql_escape_string = double escaped stuff.  I think the 
> general opinion is the magic quotes is evil, but I'm sure some people like 
> it.  I prefer to use mysql_escape_string() since it escapes things more 
> specific to MySQL than magic quotes does.  Using mysql_escape_string should 
> be good enough by itself.
> 
> 2. Check data type.  If an item is supposed to be an integer, use intval() 
> before inserting into the database.
> 
> 3. What your SQL statements for variables that can turn your statement into a 
> "WHERE 1 = 1" situation that will always return TRUE.
> 
> Here's something I've been playing with.. a generic function to sanitize data 
> before inserting into the database.  You pass it the data and the type of 
> data and it'll clean it up.  Nice thing about this is I designed it so if you 
> say type = "phone" and you process it the same as type = "numeric".. then 
> later you decide you want to process "phone" and "numeric" types separately, 
> you only have to check this function, not all your lines of code.
> 
> If someone has better ways of doing this, I'm all for hearing it.  Please 
> opine or criticize what I've posted above too.  I want to learn as well.
> 
> -TG
> 
> Code:
> 
>  /**
> *~DBSanitizeData() prepares data for inserting/updating into or selecting from
> * MySQL by making sure that string data is properly escaped so as not to allow
> * 'SQL injection' type security issues from happening. No direct $_POST or 
> $_GET
> * data should ever be used in a SQL string.
> *
> * Returns sanitized copy of data sent to it.
> *
> * Current sanitization only performs a mysql_escape_string() function but 
> could do
> * more later.
> *
> * Example: $result = mysql_query('INSERT INTO TableName (SomeColumn) VALUES 
> (' . DBSanitizeData($_POST['somevar']) . ')');
> *
> * 
> * Modification Log:
> * --
> * Created: ~~Trevor Gryffyn - 03/28/2005
> *
> * 
> *
> * @author Trevor Gryffyn <[EMAIL PROTECTED]>
> * @category Database Functions
> *
> */
>  function DBSanitizeData($dbdata, $datatype = "alpha") 
>switch ($datatype) 
>  case "binary":
>  case "truefalse":
>$trues = array("YES", "Y", "1", "ON", "TRUE", "T");
>$falses = array("NO", "N", "0", "OFF", "FALSE", "F");
>if (in_array(trim(strtoupper($dbdata)), $trues)) 
>  $dbdata = "Y";
> else 
>  $dbdata = "N";
>
>break;
>  case "phone":
>  case "numeric":
>  case "ssn":
>$dbdata = preg_replace ('/[^\d]+/s', '', $dbdata);
>break;
>  case "float":
>  case "money":
>  case "percent":
>// TODO: Should this be handled with floatval() or something else?
>//   Yes.. it probably should. Maybe this is better.
>if (strstr($dbdata, ".") AND trim($dbdata) <> "") 
>  #$dbdata = (preg_replace ('/[^\d]+/s', '', $dbdata) / 100) . ".00";
>  $dbdata = floatval(preg_replace ('/[^\d]+/s', '', $dbdata) / 100);
> else 
>  #$dbdata = preg_replace ('/[^\d]+/s', '', $dbdata) . ".00";
>  $dbdata = floatval(preg_replace ('/[^\d]+/s', '', $dbdata));
>
>break;
> 
>  case "name":
>  case "address":
>$dbdata = ucwords($dbdata);
>break;
>  case "state":
>$dbdata = strtoupper($dbdata);
>break;
>  case "date":
>$dbdata = date("Y-m-d", strtotime($dbdata));
>if ($dbdata == "1969-12-31") $dbdata = "";
>break;
>  case "alpha":
>  default:
>// Nothing special, just jump down to the trim/escape
>break;
>
>return trim(mysql_escape_string($dbdata));
>  
> ?>
> 
> = = = Original message = = =
> 
> Greetings all:
> 
> Using PHP 4.3.xx and MySQL 4.1 (and 3.xxx sometimes).
> 
> I've got a ton of forms that use the $_POST variable to send information into 
> the database, and I'm worried about injection attacks.
> 
> My server has magic_quotes enabled, which I thought would handle most things, 
> but am wondering now if I need to use mysql_escape_string on everything, 
> which would mean, of  course, a lot of find-and-replace and rewriting.
> 
> Also, REGISTER_GLOBALS is turned off, and errors are not shown t

Re: [PHP-DB] SQL Injection attack

2005-08-25 Thread Jordan Miller

NOTE:
http://www.php.net/mysql_escape_string
"Version: 4.3.0
Description: This function became deprecated, do not use this  
function. Instead, use mysql_real_escape_string()."


Jordan


On Aug 25, 2005, at 2:15 PM, <[EMAIL PROTECTED]> [EMAIL PROTECTED]> wrote:




Using mysql_escape_string should be good enough by itself.



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] SQL Injection attack

2005-08-25 Thread tg-php
I'm pretty amateur at this too, but have done a little reading on the subject.  
Here's some nuggets to ponder while the real experts write their responses: :)

1. Magic quotes + mysql_escape_string = double escaped stuff.  I think the 
general opinion is the magic quotes is evil, but I'm sure some people like it.  
I prefer to use mysql_escape_string() since it escapes things more specific to 
MySQL than magic quotes does.  Using mysql_escape_string should be good enough 
by itself.

2. Check data type.  If an item is supposed to be an integer, use intval() 
before inserting into the database.

3. What your SQL statements for variables that can turn your statement into a 
"WHERE 1 = 1" situation that will always return TRUE.

Here's something I've been playing with.. a generic function to sanitize data 
before inserting into the database.  You pass it the data and the type of data 
and it'll clean it up.  Nice thing about this is I designed it so if you say 
type = "phone" and you process it the same as type = "numeric".. then later you 
decide you want to process "phone" and "numeric" types separately, you only 
have to check this function, not all your lines of code.

If someone has better ways of doing this, I'm all for hearing it.  Please opine 
or criticize what I've posted above too.  I want to learn as well.

-TG

Code:


* Modification Log:
* --
* Created: ~~Trevor Gryffyn - 03/28/2005
*
* 
*
* @author Trevor Gryffyn <[EMAIL PROTECTED]>
* @category Database Functions
*
*/
  function DBSanitizeData($dbdata, $datatype = "alpha") {
switch ($datatype) {
  case "binary":
  case "truefalse":
$trues = array("YES", "Y", "1", "ON", "TRUE", "T");
$falses = array("NO", "N", "0", "OFF", "FALSE", "F");
if (in_array(trim(strtoupper($dbdata)), $trues)) {
  $dbdata = "Y";
} else {
  $dbdata = "N";
}
break;
  case "phone":
  case "numeric":
  case "ssn":
$dbdata = preg_replace ('/[^\d]+/s', '', $dbdata);
break;
  case "float":
  case "money":
  case "percent":
// TODO: Should this be handled with floatval() or something else?
//   Yes.. it probably should. Maybe this is better.
if (strstr($dbdata, ".") AND trim($dbdata) <> "") {
  #$dbdata = (preg_replace ('/[^\d]+/s', '', $dbdata) / 100) . ".00";
  $dbdata = floatval(preg_replace ('/[^\d]+/s', '', $dbdata) / 100);
} else {
  #$dbdata = preg_replace ('/[^\d]+/s', '', $dbdata) . ".00";
  $dbdata = floatval(preg_replace ('/[^\d]+/s', '', $dbdata));
}
break;
  
  case "name":
  case "address":
$dbdata = ucwords($dbdata);
break;
  case "state":
$dbdata = strtoupper($dbdata);
break;
  case "date":
$dbdata = date("Y-m-d", strtotime($dbdata));
if ($dbdata == "1969-12-31") $dbdata = "";
break;
  case "alpha":
  default:
// Nothing special, just jump down to the trim/escape
break;
}
return trim(mysql_escape_string($dbdata));
  }
?>

= = = Original message = = =

Greetings all:

Using PHP 4.3.xx and MySQL 4.1 (and 3.xxx sometimes).

I've got a ton of forms that use the $_POST variable to send information into 
the database, and I'm worried about injection attacks.

My server has magic_quotes enabled, which I thought would handle most things, 
but am wondering now if I need to use mysql_escape_string on everything, which 
would mean, of  course, a lot of find-and-replace and rewriting.

Also, REGISTER_GLOBALS is turned off, and errors are not shown to the user when 
the site is live.

Any suggestions on how to tighten up the form security, or does magic_quotes 
help enough?

For what it's worth, I've tried to enter things like "pw=''" and other 
simulated attackes using the $_GET method, but haven't been able to crack the 
site. But I'm a noob at that kind of thing, so I try not to get too carried 
away with myself.

Thanks,
V


___
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP-DB] SQL Injection attack

2005-08-25 Thread veditio
Greetings all:

Using PHP 4.3.xx and MySQL 4.1 (and 3.xxx sometimes).

I've got a ton of forms that use the $_POST variable to send information into 
the database, and I'm worried about injection attacks.

My server has magic_quotes enabled, which I thought would handle most things, 
but am wondering now if I need to use mysql_escape_string on everything, which 
would mean, of  course, a lot of find-and-replace and rewriting.

Also, REGISTER_GLOBALS is turned off, and errors are not shown to the user when 
the site is live.

Any suggestions on how to tighten up the form security, or does magic_quotes 
help enough?

For what it's worth, I've tried to enter things like "pw=''" and other 
simulated attackes using the $_GET method, but haven't been able to crack the 
site. But I'm a noob at that kind of thing, so I try not to get too carried 
away with myself.

Thanks,
V

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP-DB] sql injection attack, protection from

2005-05-17 Thread Juffermans, Jos
Personally, I always check variables that I'm using in a query. If I'm
expecting eg a session id (32 hex characters) I check that the session id is
a valid one - ie "!$[0-9a-f]{32}$!" (I use ! as delimiter in regexps).

Allthough mysql_escape_string will probably protects me from injections, I
still verify the data.

Jos

-Original Message-
From: mayo [mailto:[EMAIL PROTECTED]
Sent: 16 May 2005 23:55
To: php-db@lists.php.net
Subject: [PHP-DB] sql injection attack, protection from


I'm new to PHP and would like to make certain that I have the basic
protection for the site:
 
Use double quotes to contain variable
Use mysql_escape_string so that query is considered part of the WHERE
clause.
 
$result=mysql_query('SELECT * FROM users WHERE
username="'.mysql_escape_string($_GET['username']).'"');
 
I'm pulling prices from a database and sending the item ID which has 4
characters (1001, 1002, etc.)
 
Is the following unnecessary with mysql_escape_string?
 
if (preg_match("/^\w{4,4}$/", $_GET['username'], $matches))
   $result = mysql_query("SELECT * FROM items WHERE
itemID=$matches[0]");
 else // we don't bother querying the database
   echo "itemID not accepted";
 
Thanks
 
 

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP-DB] sql injection attack, protection from

2005-05-16 Thread mayo
I'm new to PHP and would like to make certain that I have the basic
protection for the site:
 
Use double quotes to contain variable
Use mysql_escape_string so that query is considered part of the WHERE
clause.
 
$result=mysql_query('SELECT * FROM users WHERE
username="'.mysql_escape_string($_GET['username']).'"');
 
I'm pulling prices from a database and sending the item ID which has 4
characters (1001, 1002, etc.)
 
Is the following unnecessary with mysql_escape_string?
 
if (preg_match("/^\w{4,4}$/", $_GET['username'], $matches))
   $result = mysql_query("SELECT * FROM items WHERE
itemID=$matches[0]");
 else // we don't bother querying the database
   echo "itemID not accepted";
 
Thanks