Será que está faltando uma aspa no comando passado ao shell_exec?
Versão Imprimível
Será que está faltando uma aspa no comando passado ao shell_exec?
eu estou utilizando a distribuiçao ubuntu 9.04 , mas ainda estou tentando dar permissao de root a meu usuario ,pois se nao nao e possivel dar determinados comandos no cmd.
sempre da esse retorno
CMD QUE VAI SER EXECUTADO:
sudo /usr/sbin/iptables -t filter -A FORWARD -p udp -s 192.168.0.1 --dport 80 -j REJECT
(CASO FALHE) TESTE O CMD NA CONSOLA PARA VERIFICA A SUA VALIDADE:
O CMD não devolveu NADA:
PAGINA INICIAL
editei o visudo
/etc/sudoers.tmp
para nao pedir senha para o meu usuario comum
# /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root ALL=(ALL) ALL
jefferson ALL=(ALL)ALL
# Uncomment to allow members of group sudo to not need a password
# (Note that later entries override this, so you might need to move
# it further down)
%sudo ALL=NOPASSWD: ALL
jefferson ALL=NOPASSWD: /usr/sbin/iptables, /usr/sbin/iptables-save, /usr/sbin/iptables-restore
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
jefferson ALL=NOPASSWD: /usr/sbin/iptables, /usr/sbin/iptables-save, /usr/sbin/iptables-restore
Acho que o correto do seria "/sbin/iptables" no Ubunto.
Tente isso...
# User alias specification
www-data ALL=NOPASSWD:/sbin/iptables, /bin/sed, /usr/bin/awk, /bin/grep
sudo ALL=NOPASSWD:/usr/sbin/iptraf, /bin/sed, /usr/bin/awk, /bin/grep
///////////////////////// PHP
$ips = $_GET['ip'];
$masc = "iptables -t nat -A PREROUTING -p tcp -s $ips --dport 80 -j DNAT --to-destination 189.xx.xx.3\n";
$regras = '/dados/ip_web.sh';
if (!$ips){
echo "Arquivo não encontrado";
exit;
}
$fp = fopen($regras, 'a+');
fwrite($fp, $masc);
fclose($fp);
if($ips){
shell_exec("/usr/bin/sudo /sbin/iptables -t nat -A PREROUTING -p tcp -s $ips --dport 80 -j DNAT --to-destination 189.xx.xx.3");
echo "<br>O ip $ips foi adicionado para bloqueio</br>";
echo "<br>";
echo "<a href='bloquear.php'>Bloquear outros</a></br>";
}
else {
echo "Error, Regras não aplicadas...";
}
Esse script guarda dentro do arquivo "/dados/ip_web.sh" e aplica a grega. Fica quardado para caso vc precise restatar o iptables execultar as regras dentro do arquivo.
Dentro do arquivo que fica as regras do firewall vc coloca: "sh /dados/ip_web.sh"
index1.php
<html>
<head>
<title>..::Firewall com php::..</title>
<body>
<form action="firewall1.php" method=post>
<table border="1">
<thead>
<tr>
<th colspan="2"> EXECUCAO DE FIREWALL IPTABLES</th>
</tr>
</thead>
<tbody>
<tr>
<td>TABELA</td>
<td>
<select id="tabela" name="tabela" value="baba">
<option value="nat" selected="selected">nat</option>
<option value="mangle">mangle</option>
<option value="filter">filter</option>
</select>
</td>
</tr>
<tr>
<td>CADEIA(Chain)</td>
<td>
<input type="text" size="50" name="cadeia" value="">
</td>
</tr>
<tr>
<td>PROTOCOLO</td>
<td>
<select id="protocolo" name="protocolo">
<option value="tcp" selected="tcp" >TCP</option>
<option value="udp">UDP</option>
<option value="icmp">ICMP</option>
<option value="all">TODOS</option>
</select>
</td>
</tr>
<tr>
<td>IP</td>
<td><input type="text" size="11" maxlength="11" name="ip" id="ip" Value=""></td>
</tr>
<tr>
<td>PORTA(max:65535)</td>
<td><input type="text" size="11" maxlength="5" name="porta" value=""></td>
</tr>
<tr>
<td>Permissoes(Target)</td>
<td>
<select id="alvo" name="alvo">
<option value="ACCEPT" selected="selected">ACCEPT</option>
<option value="REJECT">REJECT</option>
<option value="DROP">DROP</option>
</select>
</td>
</tr>
</tbody>
</table>
<p><input type="submit" class="style3" name="activar" value="Ativar Escolha"></p>
</form>
</body>
</html>
firewall.php
<?php
/**CODIGO firewall.php
*
* #iptables [-t tabela] [opção] [chain] [dados] -j [ação]
*/
//Verifica se foi pressiona o bottão na outra pagina
if(isset ($_POST['activar'])) {
//captura as variaveis do formulario
$Tabela = $_POST['tabela'];
$Cadeia = $_POST['cadeia'];
$Protocolo = $_POST['protocolo'];
$IP = $_POST['ip'];
$Porta = $_POST['porta'];
$Alvo = $_POST['alvo'];
//string com o comando a ser executado
$CMD = "sudo /usr/sbin/iptables -t $Tabela -A $Cadeia -p $Protocolo -s $IP --dport $Porta -j $Alvo";
//Execução do Comando
$resultado = shell_exec($CMD);
echo "<p><b>CMD QUE VAI SER EXECUTADO</b>:</p> $CMD";
echo "<p><b>(CASO FALHE) TESTE O CMD NA CONSOLA PARA VERIFICA A SUA VALIDADE</b>:</p>";
/* Verifa se foi devolvido alguma mensagem é mostra*/
if(!empty($resultado)) {
echo "<p><b>O CMD devolveu o seguinte</b>:</p> $resultado";
}else {
echo "<p><b>O CMD não devolveu NADA</b>:</p>";
}
}
?>
visudo
# /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
# Host alias specification
# User alias specification
www-data ALL=NOPASSWD:/sbin/iptables, /bin/sed, /usr/bin/awk, /bin/grep
sudo ALL=NOPASSWD:/usr/sbin/iptraf, /bin/sed, /usr/bin/awk, /bin/grep
# Cmnd alias specification
# User privilege specification
root ALL=(ALL) ALL
www-data ALL=NOPASSWD: /var/www/logar.php
jefferson ALL=NOPASSWD: /usr/sbin/iptables, /usr/sbin/iptables-save, /usr/sbin/iptables-restore
nobody ALL=NOPASSWD: /usr/sbin/iptables, /usr/sbin/iptables-save, /usr/sbin/iptables-restore
# Uncomment to allow members of group sudo to not need a password
# (Note that later entries override this, so you might need to move
# it further down)
%sudo ALL=NOPASSWD: ALL
jefferson ALL=NOPASSWD: /usr/sbin/iptables, /usr/sbin/iptables-save, /usr/sbin/iptables-restore
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
jefferson ALL=NOPASSWD: /sbin/iptables, /sbin/iptables-save, /sbin/iptables-restore
apache2 ALL=NOPASSWD:/sbin/iptables,/sbin/useradd,/sbin/userdel,/sbin/chpasswd, /bin/smbpasswd,/sbin/cbq,/sbin/ip,/sbin/tc,/sbin/dhcpd,/sbin/arping,/bin/r
esta execuntando cetinho sem erro , so nao insere o comando no sistema, quando vc da iptables -nL nao tem nada la ..
ja quebrei a cabeça de tudo que foi jeito.
alguem tem uma luz ai..