LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-26-2009, 09:02 AM   #1
odocoileus
LQ Newbie
 
Registered: May 2008
Posts: 15

Rep: Reputation: 1
can I determine if program is called from a specific php script ?


I would like to have certain knowledge inside a binary routine (foo) about what called it. Foo will be called from certain php scripts, and I am looking for a way for foo to verify that it is really called from that script. Is there any way for foo to get this information? I apologize if this question is offensively stupid, but this is completely new territory for me.
 
Old 03-26-2009, 05:02 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
It probably depend upon how sure you want to be that it was called from a specific php script.

The php script would be run by another program (either the web server or the cli version of php) so looking at the PPID could give you some idea of how the program was run but not down to the script level. You could use your script to write some details to a file so that you know that the script has requested the binary to run and the binary could check that file but obviously there are ways around that if you are worried about this from a security perspective.

Looking at it from a security perspective you would want to put your binary in a directory so that the permissions to run it are restricted, such as only by the web server user and then you might be able to configure some clever .htaccess rules to restrict its usage even further.
 
Old 03-26-2009, 05:32 PM   #3
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
that depends on how you call the binary routine, and if and how arguments are passed to it, perhaps you could simply add the name of the script as a static argument in each script, then simply store that information..
 
Old 03-27-2009, 08:00 AM   #4
odocoileus
LQ Newbie
 
Registered: May 2008
Posts: 15

Original Poster
Rep: Reputation: 1
I am looking at this from a security perspective. I am trying to build as many defense layers as I can. The script will be running under apache, and the binary routine will be called using something like system(), exec(), or pcntl_exec(), etc. I am not sure yet which one will be best. As suggested, I will be doing some sort of incantation from the php script when calling the binary routine, and this adds one layer of defense. As graemef pointed out however, this (like most anything else) can be overcome.

Giving the module the ability to perform its own independent verification of the calling script would seem to be a strong defense layer.

I would like to be able to do something in the module along the lines of walking the stack to find this info. But sadly I do not know if this is possible, or what info can be determined if it is. It seems there must at least be a reference for the modules return value to go to.

Another thought would be to access the apache info, perhaps through apachectl, or maybe a custom module. If I could get a list of pending http requests and verify that there is an active request with url/parameters that would coincide with the type of request being made to the binary module... Browsing through the apache docs leads me to believe that this could be possible, but it is still not as secure as the first method.

Can anyone show me the path to enlightenment?

Thank you for your responses.
 
Old 03-29-2009, 05:54 PM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
The only thought that I have had is that it might be easier to make your binary a php module, then it can only be run from php. You may then be able to add extra checks to ensure that it is being run from the correct script.
 
Old 03-30-2009, 08:47 AM   #6
odocoileus
LQ Newbie
 
Registered: May 2008
Posts: 15

Original Poster
Rep: Reputation: 1
That is true. However, suppose this were to be a encryption module. One that used a key pushed from a remote server, not stored on the drive anywhere. Now if the key is broken up and encrypted in memory it should be fairly safe, but if someone penetrated the server and saw the php code they could run their own script to fetch the key. On the other hand a binary module that simply decrypted/encrypted strings that were passed to it would be more difficult to thwart. Since the key itself is never revealed, an intruder would have to pass the data to it and get it back unencrypted. Now supposing this server had something in place that would trigger an alarm if a legitimate php script was altered in any way, then it should be sufficient to verify that the module was being called from a legitimate php script.
 
Old 03-30-2009, 06:47 PM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I was actually thinking along the lines of a php extension (I had the terminology wrong, sorry), compiled in C using the ZEND conventions to make it a bespoke php extension. Maybe you can then use debug_backtrace() to work out how your function is called, possibly using md5 checksums to ensure that none of the source code has been tampered with and all of this will be in a compiled binary.

But if your server has been compromised then you have bigger problems to worry about!
 
Old 03-31-2009, 07:29 AM   #8
odocoileus
LQ Newbie
 
Registered: May 2008
Posts: 15

Original Poster
Rep: Reputation: 1
Oh, I see. I will have to give this some reading, but initially I am hopeful about it. Thank you for your help.

And yes I agree that a compromised server is a mighty big problem, but it is all about trying to add defense layers in the event of a worst case scenario. This way if someone grabs the database and code, even if they copy the whole server, the important data is still encrypted and they do not have the keys. If I can make getting the decrypted data more effort than it is worth, then all is well.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to determine which executables are called when running an application frznchckn Linux - General 4 02-06-2009 03:36 PM
How to find and determine size of specific filetype crions Linux - Newbie 5 09-01-2008 11:27 PM
Killing a specific instance of a program created by a script derriva Programming 2 12-27-2006 04:42 AM
Cannot create folders with bash script called from php keyF Linux - Software 4 06-25-2006 10:58 AM
Is there something that can transform a program made with php to shell-script? stormrider_may Programming 4 01-31-2006 10:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration