# Problème avec les commandes exec et netstat / script php

**URL:** <https://www.debian-fr.org/t/probleme-avec-les-commandes-exec-et-netstat-script-php/30109>\
**Category:** Programmation\
**Created:** [Février 21, 2016, 12:30 UTC](https://www.debian-fr.org/t/probleme-avec-les-commandes-exec-et-netstat-script-php/30109 "2016-02-21T00:30:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Didier100](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/d/a9a28c/32.png) [@Didier100](https://www.debian-fr.org/u/Didier100)\
**Post date:** [Février 21, 2016, 12:30 UTC](https://www.debian-fr.org/t/probleme-avec-les-commandes-exec-et-netstat-script-php/30109/1 "2016-02-21T00:30:26Z")

</div>

Sur un de mes serveur Debian lenny je souhaite exécuter un script php donc voici un extrait :

`$resultat = exec('netstat -natpu', $lines);
 foreach($lines as $line){
        $output.=$line;
        echo $line .'<br />' ;
 }`

et comme résultat je reçoit le message suivant:

`Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:41895 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:620 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:465 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN -
tcp 0 0 86.65.xxx.xxx:53 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:953 0.0.0.0:* LISTEN -
tcp 0 0 86.65.xxx.xxx:22 82.125.xxx.xxx:49857 ESTABLISHED -
tcp 0 0 127.0.0.1:57843 127.0.0.1:3306 TIME_WAIT -
tcp 0 0 86.65.xxx.xxx:22 82.125.xxx.xxx:50483 ESTABLISHED -
tcp 0 0 86.65.xxx.xxx:22 82.125.xxx.xxx:50082 ESTABLISHED -
tcp6 0 0 :::993 :::* LISTEN -
tcp6 0 0 :::995 :::* LISTEN -
tcp6 0 0 :::110 :::* LISTEN -
tcp6 0 0 :::143 :::* LISTEN -
tcp6 0 0 :::80 :::* LISTEN 26240/netstat
tcp6 0 0 :::53 :::* LISTEN -
tcp6 0 0 :::22 :::* LISTEN -
tcp6 0 0 ::1:953 :::* LISTEN -
tcp6 0 0 :::443 :::* LISTEN 26240/netstat
tcp6 0 0 86.65.xxx.xxx:80 82.125.xxx.xxx:50525 ESTABLISHED 26240/netstat
udp 0 0 127.0.0.1:921 0.0.0.0:* -
udp 0 0 86.65.xxx.xxx:53 0.0.0.0:* -
udp 0 0 127.0.0.1:53 0.0.0.0:* -
udp 0 0 0.0.0.0:60488 0.0.0.0:* -
udp 0 0 0.0.0.0:717 0.0.0.0:* -
udp 0 0 0.0.0.0:111 0.0.0.0:* -
udp 0 0 86.65.xxx.xxx:123 0.0.0.0:* -
udp 0 0 127.0.0.1:123 0.0.0.0:* -
udp 0 0 0.0.0.0:123 0.0.0.0:* -
udp6 0 0 :::53 :::* -
udp6 0 0 fe80::20f:b5ff:feaa:123 :::* -
udp6 0 0 ::1:123 :::* -
udp6 0 0 :::123 :::* -`

Par contre quand je passe par le terminal de mon serveur linux en exécutant la commande:

```auto

```

je reçoit ses informations:

`Connexions Internet actives (serveurs et établies)
Proto Recv-Q Send-Q Adresse locale Adresse distante Etat PID/Program name
tcp 0 0 0.0.0.0:41895 0.0.0.0:* LISTEN 1813/rpc.statd
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 2225/mysqld
tcp 0 0 127.0.0.1:620 0.0.0.0:* LISTEN 2564/famd
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1802/portmap
tcp 0 0 0.0.0.0:465 0.0.0.0:* LISTEN 2425/master
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 14716/inetd
tcp 0 0 86.65.xxx.xxx:53 0.0.0.0:* LISTEN 2113/named
tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN 2113/named
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 2148/sshd
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 2425/master
tcp 0 0 127.0.0.1:953 0.0.0.0:* LISTEN 2113/named
tcp 0 0 86.65.xxx.xxx:22 82.125.xxx.xxx:49857 ESTABLISHED 26026/sshd: cabinet
tcp 0 0 86.65.xxx.xxx:22 82.125.xxx.xxx:50483 ESTABLISHED 26187/sshd: cabinet
tcp 0 300 86.65.xxx.xxx:22 82.125.xxx.xxx:50543 ESTABLISHED 26247/0
tcp 0 0 86.65.xxx.xxx:22 82.125.xxx.xxx:50082 ESTABLISHED 26084/sshd: cabinet
tcp6 0 0 :::993 :::* LISTEN 2316/couriertcpd
tcp6 0 0 :::995 :::* LISTEN 2334/couriertcpd
tcp6 0 0 :::110 :::* LISTEN 2322/couriertcpd
tcp6 0 0 :::143 :::* LISTEN 2304/couriertcpd
tcp6 0 0 :::80 :::* LISTEN 2716/apache2
tcp6 0 0 :::53 :::* LISTEN 2113/named
tcp6 0 0 :::22 :::* LISTEN 2148/sshd
tcp6 0 0 ::1:953 :::* LISTEN 2113/named
tcp6 0 0 :::443 :::* LISTEN 2716/apache2
udp 0 0 127.0.0.1:921 0.0.0.0:* 2131/lwresd
udp 0 0 86.65.xxx.xxx:53 0.0.0.0:* 2113/named
udp 0 0 127.0.0.1:53 0.0.0.0:* 2113/named
udp 0 0 0.0.0.0:60488 0.0.0.0:* 1813/rpc.statd
udp 0 0 0.0.0.0:717 0.0.0.0:* 1813/rpc.statd
udp 0 0 0.0.0.0:111 0.0.0.0:* 1802/portmap
udp 0 0 86.65.xxx.xxx:123 0.0.0.0:* 2583/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 2583/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 2583/ntpd
udp6 0 0 :::53 :::* 2113/named
udp6 0 0 fe80::20f:b5ff:feaa:123 :::* 2583/ntpd
udp6 0 0 ::1:123 :::* 2583/ntpd
udp6 0 0 :::123 :::* 2583/ntpd
root@debian:~#`  
En exécutant le script php je ne reçoit pas les informations de la colonne PID/Program name!  
Quelqu’un à une idée pourquoi les commandes exec et netstat me livrent des informations incomplètes quand celles-ci sont exécuté avec le code php?

J’ai l’impression que tous les informations qui dépassent les 80 caractères/ligne sont ignoré … c’est le cas de de la colonne PID/Program name.

Merci d’avance pour votre aide. 😉

---

<div class="post-metadata">

**Author:** ![MisterFreez](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@MisterFreez](https://www.debian-fr.org/u/MisterFreez)\
**Post date:** [Février 21, 2016, 12:30 UTC](https://www.debian-fr.org/t/probleme-avec-les-commandes-exec-et-netstat-script-php/30109/2 "2016-02-21T00:30:41Z")

</div>

Pour avoir les informations de la colonne “PID/Program name” il faut avoir les droits administrateur.

Par contre la solution, je sais pas trop :  
[ul]  
[li]soit ajouter le bit set uid si le propriétaire du binaire et root[/li]  
[li]soit lui ajouter la capabilities qui va bien mais je ne sais pas trop la quel est ce[/li][/ul]

Peut être qu’un autre membre aura une meilleure idée.

---

<div class="post-metadata">

**Author:** ![Didier100](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/d/a9a28c/32.png) [@Didier100](https://www.debian-fr.org/u/Didier100)\
**Post date:** [Février 21, 2016, 12:30 UTC](https://www.debian-fr.org/t/probleme-avec-les-commandes-exec-et-netstat-script-php/30109/3 "2016-02-21T00:30:46Z")

</div>

Merci pour ton aide MisterFreez.

`
chown root:root test.php
chmod u+xs test.php
root@debian:/home/test/www# ls -l test.php
-rwsrwxrwx 1 root root 2078 jan 6 00:23 test.php
root@debian:/home/test/www#`

J’ai changé, pour tester, les droits du fichier test.php et indique comme propriétaire (root) mais rien à faire!  
J’ai aussi ajouté le bit uid …

Avez vous d’autres idées? ![:shifty:](https://www.debian-fr.org/uploads/default/original/1X/c2c6b2db4f768c7bf93cea265ed4e2c63ae01880.gif "Shifty")

---

<div class="post-metadata">

**Author:** ![MisterFreez](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@MisterFreez](https://www.debian-fr.org/u/MisterFreez)\
**Post date:** [Février 21, 2016, 12:30 UTC](https://www.debian-fr.org/t/probleme-avec-les-commandes-exec-et-netstat-script-php/30109/4 "2016-02-21T00:30:46Z")

</div>

Idée intéressante mais je te déconseille de mettre tes scripts php en droit root. J’ai pas de Debian sous la main donc je peux pas vérifier, mais de ce que je pense si tu fait un `ls -l /bin/netstat`  
Tu verras qu’il n’a pas de le bit setuid, si tu lui donne puis qu’en tant que simple utilisateur tu lance ta commande tout devrais bien s’afficher. Je ne vois pas pourquoi ça n’a pas fonctionné avcec ton script php, à moins qu’il y ai des sécurité que je ne connais pas sur ton serveur web.

---

<div class="post-metadata">

**Author:** ![Didier100](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/d/a9a28c/32.png) [@Didier100](https://www.debian-fr.org/u/Didier100)\
**Post date:** [Février 21, 2016, 12:30 UTC](https://www.debian-fr.org/t/probleme-avec-les-commandes-exec-et-netstat-script-php/30109/5 "2016-02-21T00:30:47Z")

</div>

Bingo, Tu es un Chef ![:041](https://www.debian-fr.org/uploads/default/original/1X/bfe857c9119752799c3eac55ca37a50312c51282.gif "applaudit")

J’ai fait un

`root@debian:~# chmod u+xs /bin/netstat
`

et ça marche.

Par contre il faut que je trouve un moyen pour que le le bit setuid ne soit pas activé en permanence … question de sécurité.

---

<div class="post-metadata">

**Author:** ![MisterFreez](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@MisterFreez](https://www.debian-fr.org/u/MisterFreez)\
**Post date:** [Février 21, 2016, 12:30 UTC](https://www.debian-fr.org/t/probleme-avec-les-commandes-exec-et-netstat-script-php/30109/6 "2016-02-21T00:30:47Z")

</div>

Au pif je dirais qu’il a besoin de la capacité :  
setcap “CAP\_NET\_ADMIN=ep” /bin/netstat

Il faut installer libcap2-bin avant, ça permet au binaire d’avoir la capacité en question sans avoir besoin de l’ensemble des droits root (donc tu peut lui enlever le setuid).

Je ne garanti rien parce que je ne suis vraiment pas sûr que ce soit la bonne capacité (tu peut en voir la liste pour essayer de trouver la bonne avec man capabilities).

Tu peut voir les capabilites du binaire avec getcap.
