LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 03-25-2006, 04:47 AM   #1
rookiepaul
Member
 
Registered: Jul 2005
Posts: 73

Rep: Reputation: 15
running iptables from php


Hi Guys,

I've done extensive seaching on google and this forum and everything I try doesn't work. I want to be able to use iptable commands from php running on apache on FC4. Security isn't a problem at the moment as I'm just testing on a private isolated network.

I've generated the following script (pardon the comments):

<HTML>
<BODY>
<?php
//$source = $_POST["source"];
//echo $source;
echo exec('iptables -I INPUT 1 -s 192.168.0.6 -j DROP');
echo exec ('who');
?>
</BODY>
</HTML>

Nothing happens except for the following output:

root pts/2 Mar 25 10:26 (:0.0)

I'm guessing this means that apache is logged in as root. Therefore shouldn't the iptables command work? It doesn't.

Could someone shed some light. Many thanks.

Rookie.
 
Old 03-25-2006, 06:46 AM   #2
Hangdog42
LQ Veteran
 
Registered: Feb 2003
Location: Maryland
Distribution: Slackware
Posts: 7,803
Blog Entries: 1

Rep: Reputation: 422Reputation: 422Reputation: 422Reputation: 422Reputation: 422
I'm going to guess that Apache isn't actually running as root. I believe most distros have it set up to use a much less privileged user. Have a look in your httpd.conf file (or at the output of lsof -i) it should tell you what user Apache is running under.

After you find the user, the best way to do this would be to use sudo and give the Apache user permission to use the iptables command.
 
Old 03-25-2006, 06:58 AM   #3
rookiepaul
Member
 
Registered: Jul 2005
Posts: 73

Original Poster
Rep: Reputation: 15
I think it's running as "nobody", therefore what would I need to enter into my sudoers file to give it access to iptables. Also, if it is running as nobody, why is it showing as root when I use the who command from php? Also, when I have edited the sudoer file, do I need to restart it or something?

Last edited by rookiepaul; 03-25-2006 at 07:02 AM.
 
Old 03-25-2006, 07:53 AM   #4
Hangdog42
LQ Veteran
 
Registered: Feb 2003
Location: Maryland
Distribution: Slackware
Posts: 7,803
Blog Entries: 1

Rep: Reputation: 422Reputation: 422Reputation: 422Reputation: 422Reputation: 422
The nobody user is a fequent Apache user, so that makes sense. If I remember correctly, the initial Apache thread starts as root, but that really only spawns the threads owned by nobody, and those do all the heavy lifting. I'm far from an Apache expert, but that original root thread probably has something to do with the output you're seeing. However, I am very sure that the php commands are actually being run under the nobody user, not root.

After editing the sudoers file, you shouldn't need to restart. At least I've never had to. However, you will want to let nobody have passwordless access to iptables otherwise you will have to figure out how to authenticate from within your PHP script.
 
Old 03-25-2006, 08:17 AM   #5
rookiepaul
Member
 
Registered: Jul 2005
Posts: 73

Original Poster
Rep: Reputation: 15
Thanks, so what should my nobody entry look like in sudoers?
 
Old 03-25-2006, 08:46 AM   #6
Hangdog42
LQ Veteran
 
Registered: Feb 2003
Location: Maryland
Distribution: Slackware
Posts: 7,803
Blog Entries: 1

Rep: Reputation: 422Reputation: 422Reputation: 422Reputation: 422Reputation: 422
I think it should look something like this:

nobody localhost=NOPASSWD: /usr/sbin/iptables

Just check on the actual path for your iptables executable. By the way, there is a decent explanation of sudoers syntax here in case my example is wrong.
 
Old 03-25-2006, 12:19 PM   #7
rookiepaul
Member
 
Registered: Jul 2005
Posts: 73

Original Poster
Rep: Reputation: 15
Argh! this is still not working! Am I calling it wrong?

exec('sudo iptables -I INPUT 1 -s 192.168.0.6 -j DROP');

This is doing my head in.

My Sudoer file looks like this:

nobody localhost=NOPASSWD: /usr/sbin/iptables
 
Old 03-25-2006, 12:42 PM   #8
Hangdog42
LQ Veteran
 
Registered: Feb 2003
Location: Maryland
Distribution: Slackware
Posts: 7,803
Blog Entries: 1

Rep: Reputation: 422Reputation: 422Reputation: 422Reputation: 422Reputation: 422
I think you need to figure out where this is bombing out and a quick way to test this would be to run the sudo iptables command from the command line. If that works, but it doesn't in PHP, you know that you have sudo right and you've got a php problem. If it doesn't work, then at least you would know it was a sudo problem.
 
Old 03-25-2006, 01:02 PM   #9
rookiepaul
Member
 
Registered: Jul 2005
Posts: 73

Original Poster
Rep: Reputation: 15
Sudo iptables doesn't work. What does this mean?
 
Old 03-26-2006, 07:19 AM   #10
Hangdog42
LQ Veteran
 
Registered: Feb 2003
Location: Maryland
Distribution: Slackware
Posts: 7,803
Blog Entries: 1

Rep: Reputation: 422Reputation: 422Reputation: 422Reputation: 422Reputation: 422
Can you expand a bit on what you mean by "doesn't work"? What did you try? and what was the outcome. How did you become the nobody user to test this?
 
Old 03-26-2006, 09:14 AM   #11
rookiepaul
Member
 
Registered: Jul 2005
Posts: 73

Original Poster
Rep: Reputation: 15
Ok sorry.

Logged in as "paul" which is a general admin user on my linux system, I issued this command:

sudo -u nobody iptables -L --line

I got the response:

paul is not in the sudoers file. This incident will be reported.

my sudoer file is as above. Any clues?
 
Old 03-26-2006, 09:37 AM   #12
Hangdog42
LQ Veteran
 
Registered: Feb 2003
Location: Maryland
Distribution: Slackware
Posts: 7,803
Blog Entries: 1

Rep: Reputation: 422Reputation: 422Reputation: 422Reputation: 422Reputation: 422
I think the problem is that paul doesn't have permission to run things as nobody. Here is a way that should test this properly. First, either log in or su to root. Then su to nobody (and run whoami to make sure you are nobody). Then you should be able to test the sudo iptables command.
 
Old 03-26-2006, 10:23 AM   #13
rookiepaul
Member
 
Registered: Jul 2005
Posts: 73

Original Poster
Rep: Reputation: 15
When I try to su nobody I get:

"This account is currently not available".

That is after I have su root.
 
Old 03-26-2006, 05:34 PM   #14
Hangdog42
LQ Veteran
 
Registered: Feb 2003
Location: Maryland
Distribution: Slackware
Posts: 7,803
Blog Entries: 1

Rep: Reputation: 422Reputation: 422Reputation: 422Reputation: 422Reputation: 422
OK, I messed around a bit with this on my system and I think that using localhost in the sudoers file may be causing an issue. Try this

nobody ALL=NOPASSWD: /usr/sbin/iptables

At least on my system that allowed me to execute iptables commands as nobody. However, I am able to su to nobody, so it could be that in Fedora the nobody account has more restrictions than on Slackware. If this doesn't allow your script to run, you could always look at running Apache under a more normal user than nobody.
 
Old 03-26-2006, 08:34 PM   #15
Capt_Caveman
Senior Member
 
Registered: Mar 2003
Distribution: Fedora
Posts: 3,658

Rep: Reputation: 69
Quote:
Originally Posted by rookiepaul
When I try to su nobody I get:

"This account is currently not available".

That is after I have su root.
You need to specify a shell if you are doing it that way:

su -s /bin/bash nobody

For scripting purposes it's likely unnecessary.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
IPTABLES is running ? How? aurelio26 Linux - Newbie 7 11-20-2004 01:30 AM
Running nat with Iptables muppski Linux - Networking 1 11-10-2004 04:00 AM
running fedora iptables nightmare6667 Linux - Newbie 1 02-26-2004 08:07 AM
php and iptables hkerssies Programming 3 10-22-2003 02:14 PM
Iptables with gShield running over it Belyle Linux - Newbie 3 04-17-2003 07:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 07:30 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