开发者

Output of sub-processes in DOS batch script not visible in Apache

I'm running Apache 2.2 (launched via console) on Vista. I have simple batch script in cgi-bin. Unfortunately, Apache does not seem to serve any content generated by sub-processes.

For example, given the following script:

@echo off
echo Content-T开发者_StackOverflowype: text/html
echo.
echo Visible in browser
cmd /c echo Hidden from browser
echo End of script

All three lines of text will appear in the console if executed directly from a command prompt. However the middle line ("Hidden from browser") will not appear if the script is launched from Apache.

This script is just illustrative -- I'm actually using the batch file to launch a number of separate console based applications (not cmd.exe)

What am I doing wrong?


I've been looking at this, over at : Pipe Java output to calling script

FWIW, all of this C:\wamp\bin\apache\apache2.2.22\cgi-bin\testbat.bat's echo output appears in both the command window and the served webpage :

@echo off
  rem  This works in Wampserver's Apache cgi-bin...
  rem     http://localhost/cgi-bin/testbat.bat
echo Content-Type: text/html
echo.
echo ^<html^>^<head^>^</head^>^<body^>
echo ^<H1^>Hello world!!!^</H1^>
echo ^<PRE^>
FOR /F "usebackq delims==" %%i IN (`dir`) do echo %%i
echo ^</PRE^>
FOR /F "usebackq delims==" %%i IN (`cmd /c echo NOT hidden!`) do echo %%i
echo ^</body^>^</html^>


Many more details aside, this behavior happens when cmd is invoked via CreateProcess() with DETACHED_PROCESS, which is what Apache does in ap_cgi_build_command() through apr_proc_create() (reference to Apache 2.2.25 source code).

For some reason, the child processes from cmd are also spawned detached. This also happens in other situations (e.g., invoking WScript.CreateObject("WScript.Shell").Run() in the same context) which might or might not involve cmd in the background.

Unrelated lesson learned: if CreateObject(DETACHED_PROCESS) and I/O redirection (STARTF_USESTDHANDLES) are mixed together, results might be surprising.

As far as I know, there is no solution other than avoiding batch, WSH and others. The httpd team might look into this for a workaround in the future.


Maybe you need to redirect the output to your STDOUT. I haven't tried it on Windows machine, but you could try

cmd /c echo Hidden from browser >&1

or redirect it to a temp file and call type on the file.


It would work from the command line as expected but what are the applications that you are trying to run in apache's cgi-bin? I have not heard of Apache's cgi-bin being a batch file...and that could be a potential exploit...maybe the permissions are not set for the batch file...or that there is no plugin available for Apache to actually execute a batch file, think of the mod handlers used for ssl (secure sockets layer)...like this as an example found in Apache's config file...httpd.conf

<LoadModule ssl_module modules/mod_ssl.so>
....
<if mod_ssl>
....
</if>

This would explain why you cannot run a batch file as a cgi-bin script...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜