Forums / All Other Contributions/Addons / Image Handler 2 Security issue?

Image Handler 2 Security issue?

Locked

Views: 2,515

Results 1 to 20 of 23
This thread is locked. New replies are disabled.
22 Jul 2010, 7:22 PM
#1
shane78 avatar

shane78

Banned

Join Date:
Oct 2008
Location:
newcastle upon tyne (UK)
Posts:
876
Plugin Contributions:
0

Image Handler 2 Security issue?

I coudnt find the right thread so i posted here... (as its a security question, not a IH2 support question)

while i think the IH2 mod is a good mod, there is one thing that has always baffled me. That being IH2's need to have the bmz_cache folder set to 777.

now in the ZC FAQ it says that 777 folders are bad and opens the site to hacks... so, does IH2 put a ZC store at risk of a hack?

22 Jul 2010, 8:22 PM
#2
soxophoneplayer avatar

soxophoneplayer

Totally Zenned

Join Date:
Feb 2008
Posts:
534
Plugin Contributions:
0

Re: Image Handler 2 Security issue?

I had nasty gram from my server a while back about security settings in my zc that were causing 'risk'. They required me to change my folders all to 0755, including bmz_cache. They said that as 'the user' I could still write/ex files as I 'owned them' - something to do with the way they set up and cgi(whatever that is).

Further problem was that IH2 created photos were also not securely permissioned.

I found a patch here: http://www.zen-cart.com/forum/showthread.php?p=719447&highlight=permissions#post719447 that showed my how to insert 3 CHMOD statements into admin/includes/ih_manager.php.

I merged the CHMOD statements and edited the permissions to 0644 as required by my server.

That all passed their tests.

That was back in the zc 1.3.8a days and previous version of IH2. I When I upgraded zc to 1.3.9abcd and IH2 to current, I merged those three CHMOD statements along.

22 Jul 2010, 9:20 PM
#3
shane78 avatar

shane78

Banned

Join Date:
Oct 2008
Location:
newcastle upon tyne (UK)
Posts:
876
Plugin Contributions:
0

Re: Image Handler 2 Security issue?

cheers for that!

do you know of any reason why IH2 state this folder has to be set to 777?.. im wondering if they would add this security feature?...

22 Jul 2010, 9:51 PM
#4
soxophoneplayer avatar

soxophoneplayer

Totally Zenned

Join Date:
Feb 2008
Posts:
534
Plugin Contributions:
0

Re: Image Handler 2 Security issue?

Alas, I know very little ;o(

I rely on the monkey-see monkey-do method which, oddly, has been serving me fairly well (albeit slowly!).

23 Jul 2010, 6:26 PM
#5
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Image Handler 2 Security issue?

McShane, the "777" is better interpreted as "writable". Back when IH2 was written, "777" was pretty much the only option that would work since PHP was typically configured to run in a way that required that in order to work. Nowadays "755" works for a growing number of server configurations.

The issue is not specific to IH2.

The bmz_cache folder needs to be "writable" by PHP because it's a PHP script that creates the files that get inserted into the bmz_cache folders and subfolders when it auto-sizes and/or watermarks the images it creates on-the-fly. If the folders aren't writable, it can't do its job, and thus the addon would be useless.

If your unique webserver setup allows lower permissions to still treat the files/folders as writable, then you can simply leave those files/folders at that level that works for you. Many hosts are now using a form of PHP running under CGI or suPHP which means "755" is sufficient to make the files/folders writable, without requiring them to be "777" they way other PHP configurations do.

The same principles apply to all PHP scripts, including addons and ZC core: a "writable" file is determined by the way the server and filesystem are configured.

So, yes, in the case of IH2 the specified folders need to be writable ... whatever that means for YOUR unique hosting configuration. Whether 777 or 755 or whatever.

Got it?

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

23 Jul 2010, 6:55 PM
#6
divavocals avatar

divavocals

Totally Zenned

Join Date:
Jan 2007
Location:
Los Angeles, California, United States
Posts:
10,011
Plugin Contributions:
3

Re: Image Handler 2 Security issue?

ahh the man himself has spoken.. at the end of the day ther is some language in the IH2 readme from Tim's original document which reflects this (albiet in an ambiguous manner) we never properly vetted this text and I think it is the source of this "security" warning.. With your permission I'd like to use your words here to tighten up the IH2 redme so there will be no more misgivings or misunderstandings on this matter.. Also I need to make sure it is CLEAR that there is nothing in the IH2 code that I am aware of which explicitly changes any of the permissions of any folder contained within the bmz_cache to 777.. (as has been implied) The sub folders in fact will inheirit the permissions of the bmz_cache folder..> DrByte:

McShane, the "777" is better interpreted as "writable". Back when IH2 was written, "777" was pretty much the only option that would work since PHP was typically configured to run in a way that required that in order to work. Nowadays "755" works for a growing number of server configurations.

The issue is not specific to IH2.

The bmz_cache folder needs to be "writable" by PHP because it's a PHP script that creates the files that get inserted into the bmz_cache folders and subfolders when it auto-sizes and/or watermarks the images it creates on-the-fly. If the folders aren't writable, it can't do its job, and thus the addon would be useless.

If your unique webserver setup allows lower permissions to still treat the files/folders as writable, then you can simply leave those files/folders at that level that works for you. Many hosts are now using a form of PHP running under CGI or suPHP which means "755" is sufficient to make the files/folders writable, without requiring them to be "777" they way other PHP configurations do.

The same principles apply to all PHP scripts, including addons and ZC core: a "writable" file is determined by the way the server and filesystem are configured.

So, yes, in the case of IH2 the specified folders need to be writable ... whatever that means for YOUR unique hosting configuration. Whether 777 or 755 or whatever.

Got it?
Posted via Mobile Device

My Site - Zen Cart & WordPress integration specialist
I don't answer support questions via PM. Post add-on support questions in the support thread. The question & the answer will benefit others with similar issues.

23 Jul 2010, 7:06 PM
#7
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Image Handler 2 Security issue?

It's usually best to simply say "writable", and let them sort that out with their hosting company.
But if you need to quote me insomuch as my comments related to permissions and IH2, feel free.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

23 Jul 2010, 9:16 PM
#8
divavocals avatar

divavocals

Totally Zenned

Join Date:
Jan 2007
Location:
Los Angeles, California, United States
Posts:
10,011
Plugin Contributions:
3

Re: Image Handler 2 Security issue?

Awesome!! and thanks again for the clarifiction..> DrByte:

It's usually best to simply say "writable", and let them sort that out with their hosting company.
But if you need to quote me insomuch as my comments related to permissions and IH2, feel free.
Posted via Mobile Device

My Site - Zen Cart & WordPress integration specialist
I don't answer support questions via PM. Post add-on support questions in the support thread. The question & the answer will benefit others with similar issues.

24 Jul 2010, 11:22 AM
#9
shane78 avatar

shane78

Banned

Join Date:
Oct 2008
Location:
newcastle upon tyne (UK)
Posts:
876
Plugin Contributions:
0

Re: Image Handler 2 Security issue?

DrByte:

McShane, the "777" is better interpreted as "writable". Back when IH2 was written, "777" was pretty much the only option that would work since PHP was typically configured to run in a way that required that in order to work. Nowadays "755" works for a growing number of server configurations.

The issue is not specific to IH2.

The bmz_cache folder needs to be "writable" by PHP because it's a PHP script that creates the files that get inserted into the bmz_cache folders and subfolders when it auto-sizes and/or watermarks the images it creates on-the-fly. If the folders aren't writable, it can't do its job, and thus the addon would be useless.

If your unique webserver setup allows lower permissions to still treat the files/folders as writable, then you can simply leave those files/folders at that level that works for you. Many hosts are now using a form of PHP running under CGI or suPHP which means "755" is sufficient to make the files/folders writable, without requiring them to be "777" they way other PHP configurations do.

The same principles apply to all PHP scripts, including addons and ZC core: a "writable" file is determined by the way the server and filesystem are configured.

So, yes, in the case of IH2 the specified folders need to be writable ... whatever that means for YOUR unique hosting configuration. Whether 777 or 755 or whatever.

Got it?

correct me if i am wrong.. i think you are saying
777 is bad... all mods that use 777 is bad... bad as in a naught fella can put naughty files in 777 folders...

so, as IH2 is creating 777 folders this can cause a weakness on a server that uses it??

24 Jul 2010, 4:27 PM
#10
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Image Handler 2 Security issue?

Shane78:

correct me if i am wrong.. i think you are saying
777 is bad... all mods that use 777 is bad... bad as in a naught fella can put naughty files in 777 folders...

so, as IH2 is creating 777 folders this can cause a weakness on a server that uses it??
No, I didn't say that.

Some mods, including some of the most popular ones, require "writable" folders. That's not necessarily bad. Even Zen Cart itself requires some writable files/folders for normal operation such as the admin uploading product images, and so on. There's nothing wrong with a mod requiring writable files/folders if there's good reason for it. Granted, where possible that should be avoided, but it's not a reason to call it naughty.

In the case of your fears, what's bad is using a hosting service that requires "777" in order to make the file/folder "writable by Apache/PHP" instead of being able to use a lower permission level to accomplish the same thing.

I'm not sure why you're bent on declaring all mods bad for some reason.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

24 Jul 2010, 4:30 PM
#11
shane78 avatar

shane78

Banned

Join Date:
Oct 2008
Location:
newcastle upon tyne (UK)
Posts:
876
Plugin Contributions:
0

Re: Image Handler 2 Security issue?

i'll PM you

24 Jul 2010, 5:35 PM
#12
divavocals avatar

divavocals

Totally Zenned

Join Date:
Jan 2007
Location:
Los Angeles, California, United States
Posts:
10,011
Plugin Contributions:
3

Re: Image Handler 2 Security issue?

Thanks for that DrByte.. In the case of IH2, it turns out that Tim's code was written to make sure that a wider variety of hosting configurations could use it. At that time there were many hosting services that required "777" in order to make the file/folder "writable by Apache/PHP". Though some of these hosts are still out there, things have changed a bit since Tim's codebase was released..

On some hosts, the folders INSIDE the bmz_cache folder will be created with 777 permissions. While on some hosts the folders inside the bmz_cache will inherit permissions from the parent (which should hopefully be set at 755).

I've been informed that this difference likely has something to do with how PHP is implemented some hosts server.

that is, whether it is running as an apache module

or

whether is run as CGIAfter doing a little more digging, I discovered that this was the code which drives the directory creation:

/**
 * Creates a directory hierachy.
 *
 * @link    http://www.php.net/manual/en/function.mkdir.php
 * @author  <[email protected]>
 * @author  Andreas Gohr <[email protected]>
 * @author  Tim Kroeger <[email protected]>
 */
function io_mkdir_p($target){
  global $bmzConf;

  if (is_dir($target) || empty($target)) return 1; // best case check first
  if (@file_exists($target) && !is_dir($target)) return 0;
  //recursion
  if (io_mkdir_p(substr($target, 0, strrpos($target, '/')))){
    if($bmzConf['safemodehack']){
      $dir = preg_replace('/^' . preg_quote(realpath($bmzConf['ftp']['root']), '/') . '/', '', $target);
      return io_mkdir_ftp($dir);
    }else{
      return @mkdir($target, 0755); // crawl back up & create dir tree
    }
  }
  return 0;
}
```Given that the majority or webhosts are now configured differently than when Tim wrote IH2 in 2006.. So it's possible that some of this code may be obsolete for current hosting configurations.

The changes that I suggested are as follows:

/**

if (is_dir($target) || empty($target)) return 1; // best case check first
if (@file_exists($target) && !is_dir($target)) return 0;
//recursion
if (io_mkdir_p(substr($target, 0, strrpos($target, '/')))){
/* if($bmzConf['safemodehack']){
$dir = preg_replace('/^' . preg_quote(realpath($bmzConf['ftp']['root']), '/') . '/', '', $target);
return io_mkdir_ftp($dir);
}else{*/

return @mkdir($target, 0755); // crawl back up & create dir tree
// }
}
return 0;
}


Would love your insight sir..:smile:

> **DrByte:**
>
> No, I didn't say that.
> 
> Some mods, including some of the most popular ones, require "writable" folders. That's not necessarily bad.  Even Zen Cart itself requires some writable files/folders for normal operation such as the admin uploading product images, and so on. There's nothing wrong with a mod requiring writable files/folders if there's good reason for it. Granted, where possible that should be avoided, but it's not a reason to call it naughty.
> 
> In the case of your fears, what's bad is using a hosting service that requires "777" in order to make the file/folder "writable by Apache/PHP" instead of being able to use a lower permission level to accomplish the same thing.
> 
> I'm not sure why you're bent on declaring all mods bad for some reason.

My Site - Zen Cart & WordPress integration specialist
I don't answer support questions via PM. Post add-on support questions in the support thread. The question & the answer will benefit others with similar issues.

24 Jul 2010, 5:37 PM
#13
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Image Handler 2 Security issue?

It's worth a try.

Maybe McShane will volunteer to test it to prove that it's working satisfactorily?

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

24 Jul 2010, 5:42 PM
#14
shane78 avatar

shane78

Banned

Join Date:
Oct 2008
Location:
newcastle upon tyne (UK)
Posts:
876
Plugin Contributions:
0

Re: Image Handler 2 Security issue?

DrByte:

It's worth a try.

Maybe McShane will volunteer to test it to prove that it's working satisfactorily?

i would be more than happy to test. In fact, if you wish i can create you a webspace on one of my servers, give you FTP access and you can play yourself?

just let me know, it will take 10 mins to set up.
(but please note, its almost 7pm in newcastle, fast approching beer o clock... try an let me know before 7 otherwise if you want to take me up on the offer it wil be 2moro)

24 Jul 2010, 5:46 PM
#15
divavocals avatar

divavocals

Totally Zenned

Join Date:
Jan 2007
Location:
Los Angeles, California, United States
Posts:
10,011
Plugin Contributions:
3

Re: Image Handler 2 Security issue?

DrByte:

It's worth a try.

Maybe McShane will volunteer to test it to prove that it's working satisfactorily?Thanks.. I also asked ckosloff and a couple of other programmers to take a look at my proposed code changes.. I also have a few folks testing it as well..

The downside is that it's likely that older hosting configurations will not be able to use IH2 which is why we will likely just comment the code out (versus removing it altogether) This way those who need the original code can still use it (with a little instruction of course..)

My Site - Zen Cart & WordPress integration specialist
I don't answer support questions via PM. Post add-on support questions in the support thread. The question & the answer will benefit others with similar issues.

24 Jul 2010, 5:52 PM
#16
shane78 avatar

shane78

Banned

Join Date:
Oct 2008
Location:
newcastle upon tyne (UK)
Posts:
876
Plugin Contributions:
0

Re: Image Handler 2 Security issue?

DivaVocals:

Thanks.. I also asked ckosloff and a couple of other programmers to take a look at my proposed code changes.. I also have a few folks testing it as well..

The downside is that it's likely that older hosting configurations will not be able to use IH2 which is why we will likely just comment the code out (versus removing it altogether) This way those who need the original code can still use it (with a little instruction of course..)

the offer is there if you want it

24 Jul 2010, 6:04 PM
#17
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Image Handler 2 Security issue?

Instead of commenting out the code, you could find out where the bmz safemode flag is set, and change the rules for that to be most compatible across hosting environments.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

24 Jul 2010, 6:37 PM
#18
divavocals avatar

divavocals

Totally Zenned

Join Date:
Jan 2007
Location:
Los Angeles, California, United States
Posts:
10,011
Plugin Contributions:
3

Re: Image Handler 2 Security issue?

DrByte:

Instead of commenting out the code, you could find out where the bmz safemode flag is set, and change the rules for that to be most compatible across hosting environments.
Actually that safemode function is turned off by default and is never executed at all, and unless one is rooting around in the IH2 code, I don't think many know it's even there.. Which is why I commented it out.. It's a long forgotten buried feature Tim included for some hosting configurations:

From Tim's site:

The safemodehack option tries to solve problems with creating and deleting server-created directories on servers having safe_mode or safe_mode_gid enabled. It is not thoroughly tested yet so any feedback is welcome.There is NO reference to it in the IH2 readme file (not even the one Tim created that I started out with when I updated the IH2 readme file). I wouldn't have known about it except I was digging through the IH2 code.

To use it it has to be turned on, and the settings are in a configuration file:

/* Safemode Hack */
/*$bmzConf['safemodehack'] = 0;               //read http://wiki.breakmyzencart.com/zen-cart:safemodehack !
$bmzConf['ftp']['host'] = 'localhost';
$bmzConf['ftp']['port'] = '21';
$bmzConf['ftp']['user'] = 'user';
$bmzConf['ftp']['pass'] = 'password';
$bmzConf['ftp']['root'] = DIR_FS_CATALOG;*/
```and the safemode function code is here:

/**

  • Creates a directory using FTP
  • This is used when the safemode workaround is enabled
  • @author [email protected]
    */
    function io_mkdir_ftp($dir){
    global $messageStack;
    global $bmzConf;

if(!function_exists('ftp_connect')){
$messageStack->add("FTP support not found - safemode workaround not usable", "error");
return false;
}

$conn = @ftp_connect($bmzConf['ftp']['host'], $bmzConf['ftp']['port'], 10);
if(!$conn){
$messageStack->add("FTP connection failed", "error");
return false;
}

if(!@ftp_login($conn, $bmzConf['ftp']['user'], $bmzConf['ftp']['pass'])){
$messageStack->add("FTP login failed", "error");
return false;
}

//create directory
$ok = @ftp_mkdir($conn, $dir);
//set permissions (using the directory umask)
@ftp_site($conn, sprintf("CHMOD %04o %s", (0777 - $bmzConf['dmask']), $dir));

@ftp_close($conn);
return $ok;
}


Your thoughts.. Should we approach this differently??

My Site - Zen Cart & WordPress integration specialist
I don't answer support questions via PM. Post add-on support questions in the support thread. The question & the answer will benefit others with similar issues.

24 Jul 2010, 6:50 PM
#19
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Image Handler 2 Security issue?

Safe Mode is highly discouraged and will no longer even exist in PHP 6.
I see no reason not to drop support for it altogether. Anyone using such old hosting that requires safe mode shouldn't be running an ecommerce site on that hosting service anyway.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

24 Jul 2010, 7:04 PM
#20
divavocals avatar

divavocals

Totally Zenned

Join Date:
Jan 2007
Location:
Los Angeles, California, United States
Posts:
10,011
Plugin Contributions:
3

Re: Image Handler 2 Security issue?

My thoughts exactly.. ckosloff seems to be erring on the side of caution.. I want to disable the safemode function, by commenting it out. Though I am not opposed to simply deleting the code a together and simply adding a line to the readme that indicates hosting setups that will NOT be able to use IH2..

So I'm am waiting for feedback from the code gurus who I have asked to look at this.. Plus I am waiting for feedback from my testers.. (My testing on my own servers is all good..) Then I can wrap this all up and get it resubmitted..

Thanks again for your input!

DrByte:

Safe Mode is highly discouraged and will no longer even exist in PHP 6.
I see no reason not to drop support for it altogether. Anyone using such old hosting that requires safe mode shouldn't be running an ecommerce site on that hosting service anyway.

My Site - Zen Cart & WordPress integration specialist
I don't answer support questions via PM. Post add-on support questions in the support thread. The question & the answer will benefit others with similar issues.