Author Topic: You have an error in your SQL syntax - help!!  (Read 268 times)

0 Members and 1 Guest are viewing this topic.

Offline runnerjpTopic starter

  • Addict
    • View Profile
You have an error in your SQL syntax - help!!
« on: February 09, 2010, 04:53:34 AM »
Hey guys.

My site got hacked at the weekend, thankfully all they did was delete my test posts and post a message on the forum. Been trying to find how they did it with my login script ect but seems to me they have not cracked the login screen as it seems tight.

Then i think i found out how.   i use page= withing my script so thought i would try the adding of ' at the end of the script. This then comes up with the error message:

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 'name',`user_id`='number'' at line 1.

Im right in saying they can change anything in my sql database with this loop hole cant they!


What im asking is how can i fix this??


Regards

Jarratt

Offline Mchl

  • Freak!
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • BOINC@Poland
Re: You have an error in your SQL syntax - help!!
« Reply #1 on: February 09, 2010, 04:59:03 AM »
Oh just delete this comma here.

Where?

Oh... You didn't post your code, so I'm not posting mine :P
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Code: [Select]
import java;

Offline RussellReal

  • Devotee
  • Gender: Male
    • View Profile
Re: You have an error in your SQL syntax - help!!
« Reply #2 on: February 09, 2010, 04:59:44 AM »
mysql_real_escape_string() but also if you're expecting an integer you might aswell typecast the variable $id = (int) $_GET['id'];
Like my help and want to hire me? Email me or add me to MSN at RussellonMSN@hotmail.com

Offline neil.johnson

  • Guru
  • Addict
  • *
    • View Profile
Re: You have an error in your SQL syntax - help!!
« Reply #3 on: February 09, 2010, 05:03:39 AM »
What you have experienced is called SQL injection. They have used part of a SQL query as a url parameter. If your site was live they could have dropped the whole database.
ALWAYS, ALWAYS, ALWAYS Escape Input & Filter Output! In other words never trust any data that comes from a url parameter, form input, cookie, etc.
Look at php's functions to escape & filter data. i.e

Code: [Select]
// strip any html tags
$x = strip_tags($x);
// escape for database insert
$x = mysql_real_escape_string($x);
// convert any special tags to their html entity
$x = htmlentities($x);
// look for certain characters and remove them
$x = str_replace("bad word", "", $x);
// check if the value is a number
if(is_numeric($x)) { }
// check if the value contains only letters
if(ctype_alpha($x)) { }
Search Google for tutorials on making data safe for the likes of database queries.
Quote
To start, press any key. Where's the 'Any' key?


Online PFMaBiSmAd

  • PHPFreaks Recommended
  • Freak!
  • *
    • View Profile
Re: You have an error in your SQL syntax - help!!
« Reply #4 on: February 09, 2010, 05:08:59 AM »
The fact that a ' will break the SQL syntax means that you are not escaping string data being put into a query. That also means that SQL can be injecting into your queries. The SQL that is injected can be used to dump all the contents of your table by adding a UNION to a SELECT query and it can also be used to cause a SELECT query to return ANY matching row instead of an exact matching row, such as in a login script.

So, for the query that is failing, are you using mysql_real_escape_string() on each piece of string data being put into it that could have come from the visitor via GET, POST, COOKIE, FILES, SERVER, ENV variables or indirectly through SESSION variables that were at one time GET, POST, ... variables?

Edit: Because mysql_query() does not support multiple queries separated by ; it is not directly possible to inject sql that will do things like drop tables.
« Last Edit: February 09, 2010, 05:09:40 AM by PFMaBiSmAd »
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline runnerjpTopic starter

  • Addict
    • View Profile
Re: You have an error in your SQL syntax - help!!
« Reply #5 on: February 09, 2010, 05:22:46 AM »
Right so all data input i need to mysql_real_escape_string and that should sort the problem. Would there be any way of avoiding the 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 'name',`user_id`='number'' at line 1.

Coming up or would mysql_real_escape_string stop this from coming up anyway?

@Mchl - sorry for supplying no code , but didnt know if it would help or just bulk up the post

Online PFMaBiSmAd

  • PHPFreaks Recommended
  • Freak!
  • *
    • View Profile
Re: You have an error in your SQL syntax - help!!
« Reply #6 on: February 09, 2010, 06:11:35 AM »
Using mysql_real_escape_string() on string data will only prevent the sql syntax from breaking and will prevent sql injection via string data. SQL can still be injected in numeric data (see RussellReal's post above.)

Someone can still trigger mysql errors by doing things like injecting a large amount of data that exceeds the max_allowed_packet or can cause 'server has gone away' errors.

In general, you should not cause the output from mysql_error() to appear on a live web site (display_errors should be OFF and log_errors should be ON and your logic should cause mysql_error() to use a php function that makes use of the error_reporting/display_errors/log_errors settings) and you should use error checking logic on all your queries (and in fact on all functions that can fail) to detect if an error occurred, output a meaningful user error message when it does, report/log system level information so you can find and fix problems, and take an appropriate action in the remainder of your code on the page.

Typical logic to do this for SELECT, SHOW, DESCRIBE, EXPLAIN... type queries -

Code: [Select]
<?php
$query 
" ... "// your query in a string variable (makes error reporting easier because you can log or display the actual query)
// execute the query and check for success or failure
if($result mysql_query($query)){
// the query executed without any errors and returned a result resource (SELECT, SHOW, DESCRIBE, EXPLAIN... type queries)
// check if any rows were returned by the query (SELECT... type queries)
if(mysql_num_rows($result)){
// the result set contains one or more rows
// process the row(s) in the result set here...
} else {
// no rows were matched, output a helpful user error message
echo "The query did not match any rows in the database<br />";
}
} else {
// the query failed and returned a FALSE value
// do your application level error reporting here...
trigger_error("Query failed: $query<br />" mysql_error());
// output a user error message
echo "The query could not be executed at this time due to an error...<br />";
}
// the remainder of the code on the page having nothing to do with the above query logic
?>

Typical logic to do this for INSERT, UPDATE, DELETE, DROP... type queries -

Code: [Select]
<?php
$query 
" ... "// your query in a string variable (makes error reporting easier because you can log or display the actual query)
// execute the query and check for success or failure
if($result mysql_query($query)){
// the query executed without any errors and returned a TRUE value (INSERT, UPDATE, DELETE, DROP... type queries)
// check if any rows were affected by the query (INSERT... type queries)
if(mysql_affected_rows()){
// one or more rows were affected, do success processing...
// output a success user message -
echo "The row was successfully UPDATED in the table<br />";
} else {
// no rows were affected, do failure processing...
// output a user error message
echo "The query could not UPDATE the row in the table (likely because the WHERE clause is FALSE)<br />";
}
} else {
// the query failed and returned a FALSE value
// do your application level error reporting here...
trigger_error("Query failed: $query<br />" mysql_error());
// output a user error message
echo "The query could not be executed at this time due to an error...<br />";
}
// the remainder of the code on the page having nothing to do with the above query logic
?>
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline Mchl

  • Freak!
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • BOINC@Poland
Re: You have an error in your SQL syntax - help!!
« Reply #7 on: February 09, 2010, 06:27:42 AM »
@Mchl - sorry for supplying no code , but didnt know if it would help or just bulk up the post


Not a problem. Seems like other guys are kind enough to help you without seeing it ;)
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Code: [Select]
import java;

Offline runnerjpTopic starter

  • Addict
    • View Profile
Re: You have an error in your SQL syntax - help!!
« Reply #8 on: February 10, 2010, 09:59:11 AM »
Sorry about this but on this page can any 1 tell me what i need to chnage to prevent the erro message '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 'Admin',`user_id`='1'' at line 1' appearing?  as i cant seem to find the area involved/

cut down code as much as possible
Code: [Select]
<link rel="stylesheet" type="text/css" href="http://www.runningprofiles.com/css/login.css">
<link rel="stylesheet" type="text/css" href="http://www.runningprofiles.com/members/include/style.css">


<?php
//look to see if the forum is currently locked
$sQry "SELECT `locked` FROM forum_lock LIMIT 1";
$obQry mysql_query($sQry) or die(sprintf("Could not query forums (%d): %s",mysql_errno(),mysql_error()));
$record mysql_fetch_array($obQry);


if (isset($record['locked']) && $record['locked'])
{
die("Sorry, the forums are currently locked."); //error message
}
else
{


//Here we count the number of results
$data mysql_query("Select * from forumtutorial_posts where parentid='0' AND forum = '$forum' ORDER BY important, lastrepliedto")or die("Could not get users");
$rows mysql_num_rows($data);
$page_rows 25//This is the number of results displayed per page
$pagenum $_GET['pagenum']; //This sets the range to display in our query

if ($pagenum === "last")
{
$query "Select COUNT(*) as C from forumtutorial_posts where parentid='$id'";
$result mysql_query($query);
$data mysql_fetch_array($result);
$pagenum ceil($data['C'] / $page_rows);
}
$pagenum = (is_numeric($pagenum) && $pagenum >= 1) ? (int)$pagenum 1;
$max 'limit ' . ($pagenum 1) * $page_rows ',' $page_rows;




{
/* gets users online */
$getusersonline "SELECT user_id,user FROM useronline    WHERE  file = 'http://www.runningprofiles.com/members/index.php?page=forum&forum=$forum' AND
timestamp > " 
. (time() - 900); //grab from sql users on in last 15 minutes
$getusersonline2 mysql_query($getusersonline) or die("Could not get users");
$num mysql_num_rows($getusersonline2);


$getthreads "Select * from forumtutorial_posts where parentid='0' and forum = '$forum' ORDER BY important ASC, lastrepliedto DESC $max";

$getthreads2 mysql_query($getthreads) or die("Could not get threads");
while (
$getthreads3 mysql_fetch_array($getthreads2))
{

$important $getthreads3['important'];

$query1 mysql_query("SELECT COUNT(postid) FROM forumtutorial_posts WHERE( postid= '$getthreads3[postid]' OR parentid = '$getthreads3[postid]' ) AND author='$username'");
$count mysql_result($query100);
 echo (
$count != 0)  ? '<img src="/images/posted.jpg" />' '<img src="/images/posted2.jpg" />'
  
?>






























« Last Edit: February 10, 2010, 10:12:00 AM by runnerjp »

Offline Mchl

  • Freak!
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • BOINC@Poland
Re: You have an error in your SQL syntax - help!!
« Reply #9 on: February 10, 2010, 11:36:25 AM »
Are you sure it's on this page? No query here looks like the one from error message.
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Code: [Select]
import java;

Offline runnerjpTopic starter

  • Addict
    • View Profile
Re: You have an error in your SQL syntax - help!!
« Reply #10 on: February 10, 2010, 11:45:05 AM »
Yes i enter  http://www.runningprofiles.com/members/index.php?page=forum&forum=General'   ( note the ' )  and it comes up with this error '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 'Admin',`user_id`='1'' at line 1'

Offline Mchl

  • Freak!
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • BOINC@Poland
Re: You have an error in your SQL syntax - help!!
« Reply #11 on: February 10, 2010, 12:32:23 PM »
This error message comes from mysql_error() function. Only query in your code that this function is used after is:
Code: [Select]
SELECT `locked` FROM forum_lock LIMIT 1, which is neither wrong, nor affected by any user input variables.

Perhaps some other php file is being included by your script? Try looking for other usages of mysql_error()
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Code: [Select]
import java;

Offline runnerjpTopic starter

  • Addict
    • View Profile
Re: You have an error in your SQL syntax - help!!
« Reply #12 on: February 10, 2010, 12:51:26 PM »
Wierd now when i enter '  i dont get the error message :S


Offline Mchl

  • Freak!
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • BOINC@Poland
Re: You have an error in your SQL syntax - help!!
« Reply #13 on: February 10, 2010, 12:55:55 PM »
Please see 'PHP Debugging: A Beginner's guide' from my signature for some tips on how to find out where errors occur.
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Code: [Select]
import java;

Offline runnerjpTopic starter

  • Addict
    • View Profile
Re: You have an error in your SQL syntax - help!!
« Reply #14 on: February 10, 2010, 01:04:35 PM »
Sorry i should point out im tryin to cause the error as my website was hacked

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.