PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 16th, 2013, 05:33 PM
KenHorse KenHorse is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 5 KenHorse User rank is Private First Class (20 - 50 Reputation Level)KenHorse User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 26 m 7 sec
Reputation Power: 0
MySQL newbie here

I'm taking old (PHP) code from the mid 2000's and trying to get it working with the latest MySQL (5.1.66-0+squeeze1) running under Debian. I am very well versed in Linux and even PHP but new(ish) to MySQL. I am using phpMyAdmin to work with the database.

Anyway, this older code makes this call:
Code:
if( $port != 0 ) {
	$query="select * from config 
			where type like 'prog' and 
			port = $port and 
			command not like '*3%'
			order by command, sub";
} else {
	$query="select * from config 
			where type like 'prog' and 
			tab = '$tab' and 
			command not like '*3%'
			order by command, sub";
}



And SQL balks as:

ack! query failed:
errorno=1064
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 'order by description' at line 1
query=select * from config where type like 'prog' and command like '*3%' and port = order by description

There are no mysql error logs to look at, nor anything is syslogs either

Reply With Quote
  #2  
Old January 16th, 2013, 05:56 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,833 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 1 h 5 m 22 sec
Reputation Power: 811
Hi,

the queries of your PHP snippet are not the ones throwing the error, as you can see in the error message.

This is the query you wanna look at:
Code:
select * from config where type like 'prog' and command like '*3%' and port = order by description

After the "port =", there's no value. So you have to add something there (whatever that might be):
Code:
SELECT
	...
FROM
	...
WHERE
	...
	AND port = ???
ORDER BY
	description

Reply With Quote
  #3  
Old January 16th, 2013, 06:53 PM
KenHorse KenHorse is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 5 KenHorse User rank is Private First Class (20 - 50 Reputation Level)KenHorse User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 26 m 7 sec
Reputation Power: 0
Yea, I see I copied from the wrong file. The following is correct:

Code:
$query="select * from config where type like 'prog' and command like '*3%' and port = $port order by description";


I'm trying to figure out where $port is being assigned as that appears to be the cause of the failure.

(this is a suite of files, generating an interactive website)

Reply With Quote
  #4  
Old January 17th, 2013, 02:56 PM
KenHorse KenHorse is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 5 KenHorse User rank is Private First Class (20 - 50 Reputation Level)KenHorse User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 26 m 7 sec
Reputation Power: 0
Man o man, I don't get it.

The exact same php files run perfectly on the original machine they came from. And the database files likewise work perfectly. But on the new machine, the error(s) as I show above are all over the place. For example, I don't see where $port is being set ANYWHERE in the code so I'm thinking it's some sort of environment variable. (there are other similar errors in other parts of the code too - the same problem that a variable isn't being seen)

The old machine is running a very old version of both MySQL and PHP and I'm figuring that is part of the problem. I did transfer the database to the new machine and can query it just fine

Grrrrrrr.....

Reply With Quote
  #5  
Old January 17th, 2013, 04:31 PM
KenHorse KenHorse is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 5 KenHorse User rank is Private First Class (20 - 50 Reputation Level)KenHorse User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 26 m 7 sec
Reputation Power: 0
Resolved

Duh....turned out I didn't have global variables enabled in php.ini.

That fixed everything

Reply With Quote
  #6  
Old January 17th, 2013, 06:22 PM
KenHorse KenHorse is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 5 KenHorse User rank is Private First Class (20 - 50 Reputation Level)KenHorse User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 26 m 7 sec
Reputation Power: 0
Red face

Funny thing about learning curves. They're a lot like code development in that, just when you think you have it done, something creeps in and ruins your day!

Well.. I got everything going on the new machine just fine and upgraded a bunch of packages, including PHP... well.. that broke the register_globals setting! So some research showed I was stupid for using them in the first place as they're a serious security risk!

Since my suite of scripts is pretty small (only 12 or so of 'em), I simply added
Code:
$port = $_GET['port'];


to each of the files and everything seems happy now.

Whew!
Comments on this post
MrFujin agrees: You managed to find the security risk... that is not stupid.

Reply With Quote
  #7  
Old January 17th, 2013, 08:23 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,833 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 1 h 5 m 22 sec
Reputation Power: 811
If you still have the
Code:
WHERE port = $port

then your script is wide open to SQL injections.

Anybody can manipulate the query and fetch any critical data (like passwords, email addresses etc.) simply by passing SQL commands through the "port" parameter:
Code:
''
UNION
SELECT
	, user_name
	, credit_card_number
	, password
	, email_address
FROM
	members

Put into your query, this will display a convenient list of credit card numbers etc. instead of the config values.

To put it bluntly: That code is very, very bad. Whoever wrote it obviously had no idea about security and didn't even care. So if you want your website to actually withstand attacks and not just somehow "run" until the first script kiddie comes around, you'll need to do much more than replace $port with $_GET['port']. It's probably a major rewrite, because all code snippets you posted have this vulnerability, and I fear the rest isn't better.

Every value must be escaped with mysql_real_escape_string() before being put into a query strings. The same goes for values that are put into the HTML markup (with "echo", "print" etc.). Those have to be escaped with htmlentities(). There might be many other vulnerabilities like internal error messages being output and whatnot.

Reply With Quote
  #8  
Old January 17th, 2013, 09:31 PM
NotionCommotion NotionCommotion is offline
Contributing User
Click here for more information.
 
Join Date: Sep 2006
Posts: 1,464 NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 6 h 21 m 9 sec
Reputation Power: 526
I totally agree with Jacques1, but would recommend PDO to implement.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > MySQL newbie here

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap