PHP "tool" to edit dnsmasq and psad entries
by voncloft from LinuxQuestions.org on (#568SC)
Long story short I have my own router (linux from scratch) that uses dnsmasq for name cacheing and psad for intrusion detection. Occasionally a website becomes blocked (youtube and google) is there a way to have php edit the /etc/hosts and /etc/psad/auto_dl entries
I listed a configuration I have so far. The website is hosted on port 90 and is ONLY accessible from my LAN I have it blocked to the outside world. I have port 80 open and it does not touch this directory it is strictly just for me - however security is still wanted, since port 80 uses php as well - so I don't want to grant php root permissions with free range. I only have the /srv/www/php/text files to "test" out the formats of the files.
The whoami command in php is "apache".
/srv/www/php/ip/html/index.html
Code:
<html>
<body>
<form action="../php/index.php" method="post">
Domain Name: <input type="text" name="url"><br>
IP address: <input type="text" name="ip"><br>
<input type="submit">
</form>
</body>
</html>/srv/www/ip/php/index.php
Code:
<html>
<body>
Domain Name: <?php echo $_POST["url"]; ?><br>
IP Address: <?php echo $_POST["ip"]; ?>
<?php
#echo exec('whoami');
$myfile=fopen("../text/hosts-file","a") or die ("Unable to open file!");
$txt = $_POST["ip"]." ".$_POST["url"]."\n";
fwrite($myfile, $txt);
fclose($myfile);
$myfile=fopen("../text/hosts-ip","a") or die ("Unable to open file!");
$txt = $_POST["ip"]." 0;\n";
fwrite($myfile, $txt);
fclose($myfile);
//exec('sudo cp hosts-tests hosts-test.txt');
?>
</body>
</html>/srv/php/ip/text/hosts-ip
Code:
75.75.75.75 0;
75.75.76.76 0;
192.168.1.0/24 0;
192.168.42.0/24 0;
8.8.8.8 0;
8.8.4.4 0;
192.168.0.0/24 0;
0.0.0.0/24 0;
www.youtube.com 0;
youtube-ui.l.google.com 0;
www.google.com 0;
172.217.5.4 0;
216.58.192.142 0;
172.217.4.206 0;/srv/www/ip/text/hosts-file
Code:
# Begin /etc/hosts
127.0.0.1 localhost
192.168.1.1 router voncloft.com www.voncloft.com
192.168.1.100 voncloft
192.168.1.150 voncloft-2
192.168.1.180 voncloft-3
192.168.42.2 phone
192.168.42.4 TV
172.217.5.4 www.google.com google.com
216.58.192.142 www.youtube.com youtube.com
172.217.4.206 youtube-ui.l.google.com
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
# End /etc/hosts/srv/www/ip/cron/update.sh
(Ran as root once a change has been made to the /srv/www/php/ip/text files)
Code:#create commands have ran in cron job every minute
#have it add the ip file to /etc/psad/auto_dl
#TEST!!!TEST!!!TEST!!!!!!
#to add to /etc/hosts for the url file - make a /etc/hosts-test
##ONCE YOU ARE SURE IT WORKS make it /etc/hosts
#TO DO:
#FIND A WAY TO APPEND DO NOT MOVE FILE - although both files on
#system are owned by root - so its a possibility
sudo cp /srv/www/ip/text/hosts-file /etc/hosts
sudo cp /srv/www/ip/text/hosts-ip /etc/psad/auto_dl
sudo /etc/init.d/iptables clear
sudo /etc/init.d/iptables start
sudo /etc/init.d/dnsmasq restart
sudo /etc/init.d/psad restartI have considered two ways to do this:
1) a cron job that watches for files to change in the text directory every minute the move and overwrite /etc/hosts and /etc/psad/auto_dl when it changes then restart iptables, dnsmasq, and psad
2) grant apache "god rights" - not looking to do that and restart the processes mentioned in option 1.
Is there another way I could possibly do this? I would prefer a "submit button" that updates the files needed and restarts the service.
I'd like to just do this via a website as opposed to manual entries.


I listed a configuration I have so far. The website is hosted on port 90 and is ONLY accessible from my LAN I have it blocked to the outside world. I have port 80 open and it does not touch this directory it is strictly just for me - however security is still wanted, since port 80 uses php as well - so I don't want to grant php root permissions with free range. I only have the /srv/www/php/text files to "test" out the formats of the files.
The whoami command in php is "apache".
/srv/www/php/ip/html/index.html
Code:
<html>
<body>
<form action="../php/index.php" method="post">
Domain Name: <input type="text" name="url"><br>
IP address: <input type="text" name="ip"><br>
<input type="submit">
</form>
</body>
</html>/srv/www/ip/php/index.php
Code:
<html>
<body>
Domain Name: <?php echo $_POST["url"]; ?><br>
IP Address: <?php echo $_POST["ip"]; ?>
<?php
#echo exec('whoami');
$myfile=fopen("../text/hosts-file","a") or die ("Unable to open file!");
$txt = $_POST["ip"]." ".$_POST["url"]."\n";
fwrite($myfile, $txt);
fclose($myfile);
$myfile=fopen("../text/hosts-ip","a") or die ("Unable to open file!");
$txt = $_POST["ip"]." 0;\n";
fwrite($myfile, $txt);
fclose($myfile);
//exec('sudo cp hosts-tests hosts-test.txt');
?>
</body>
</html>/srv/php/ip/text/hosts-ip
Code:
75.75.75.75 0;
75.75.76.76 0;
192.168.1.0/24 0;
192.168.42.0/24 0;
8.8.8.8 0;
8.8.4.4 0;
192.168.0.0/24 0;
0.0.0.0/24 0;
www.youtube.com 0;
youtube-ui.l.google.com 0;
www.google.com 0;
172.217.5.4 0;
216.58.192.142 0;
172.217.4.206 0;/srv/www/ip/text/hosts-file
Code:
# Begin /etc/hosts
127.0.0.1 localhost
192.168.1.1 router voncloft.com www.voncloft.com
192.168.1.100 voncloft
192.168.1.150 voncloft-2
192.168.1.180 voncloft-3
192.168.42.2 phone
192.168.42.4 TV
172.217.5.4 www.google.com google.com
216.58.192.142 www.youtube.com youtube.com
172.217.4.206 youtube-ui.l.google.com
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
# End /etc/hosts/srv/www/ip/cron/update.sh
(Ran as root once a change has been made to the /srv/www/php/ip/text files)
Code:#create commands have ran in cron job every minute
#have it add the ip file to /etc/psad/auto_dl
#TEST!!!TEST!!!TEST!!!!!!
#to add to /etc/hosts for the url file - make a /etc/hosts-test
##ONCE YOU ARE SURE IT WORKS make it /etc/hosts
#TO DO:
#FIND A WAY TO APPEND DO NOT MOVE FILE - although both files on
#system are owned by root - so its a possibility
sudo cp /srv/www/ip/text/hosts-file /etc/hosts
sudo cp /srv/www/ip/text/hosts-ip /etc/psad/auto_dl
sudo /etc/init.d/iptables clear
sudo /etc/init.d/iptables start
sudo /etc/init.d/dnsmasq restart
sudo /etc/init.d/psad restartI have considered two ways to do this:
1) a cron job that watches for files to change in the text directory every minute the move and overwrite /etc/hosts and /etc/psad/auto_dl when it changes then restart iptables, dnsmasq, and psad
2) grant apache "god rights" - not looking to do that and restart the processes mentioned in option 1.
Is there another way I could possibly do this? I would prefer a "submit button" that updates the files needed and restarts the service.
I'd like to just do this via a website as opposed to manual entries.