views:

115

answers:

3

Hey,

is it possible to call a shellscript or the shellcode in a java class?

My Code (in a static method):

Runtime rtime = Runtime.getRuntime();
Process child = rtime.exec("/bin/bash");
BufferedWriter outCommand = new BufferedWriter(new
OutputStreamWriter(child.getOutputStream()));
outCommand.write("streamer -c /dev/video0 -b32 -o test.jpeg");
outCommand.flush();
outCommand.close();
child.destroy();

But I get this error, if i try this code in my jsp page (tomcat6):

java.security.AccessControlException: access denied (java.io.FilePermission /bin/bash execute)

Any solution for this problem?

EDIT: ls -l /bin/bash shows me follwing line:

-rwxr-xr-x 1 root root 875596 2009-09-14 07:09 /bin/bash

Thanks

+1  A: 

Try to disable the security manager. Edit /etc/init.d/tomcat6 and change:

TOMCAT_SECURITY=yes

Change that to:

TOMCAT_SECURITY=no

And then restart Tomcat:

/etc/init.d/tomcat6 restart

If it works, it's up to you to see if fully disabling the security manager is acceptable or not (using it is actually unusual IMO).

Pascal Thivent
+1  A: 

Also if possible, attempt to run it in the single exec call.

Process p = Runtime.getRuntime().exec(args);

where args is a string array of arguments.

String[] args = new String[] {"/bin/bash", "streamer", "-c", "/dev/video0", "-b32", "-o", "test.jpeg" };
dekz
Yes, passing the args as individual strings is highly recommended, especially if any of them could be changed by a user who might try to do some shell code injection.
Fly
+2  A: 

If you want to keep the security manager running, and you may have good reasons for doing so, then you can simply change the policy file that Tomcat is using. The files may be located in /var/lib/tomcat6/conf/policy.d.

A nice SO thread discusses this already. Even if you grant AllPermissions to your application, running with the SecurityManager will allow you to use sandboxes with limited security. You might want to do that, say, to run JavaScript that could be uploaded at runtime or something similar, or there may be some other code in your application that you don't trust for some reason.

Fly
I mean "sandboxes with limited permissions."
Fly
And if there is more than one web-app running, you perhaps do not want them all to have free reign.
Fly