Added OpenSSH client wrapper to support DNSSEC verifiing
[mirrors/Programs.git] / php / hfirewall / firewall.php
... / ...
CommitLineData
1#! /usr/bin/php
2<?php
3///Settings////////////////////////////
4$rulefile = "hfwrules.conf";
5$fwscript = "fwscript.sh";
6
7$chmod = true;
8$run = true;
9
10$iptbin = "/sbin/iptables";
11$iptsave = "/sbin/iptables-save";
12$iptrestore = "/sbin/iptables-restore";
13
14///Version/////////////////////////////
15$version = "0.1 Alpha";
16
17///Banner//////////////////////////////
18echo("Harvie's Firewall\n");
19echo("\t<-Harvie 2oo7\n");
20echo("\tVersion $version\n\n");
21
22///CODE////////////////////////////////
23//Load rules file
24echo("Loading rulesfile ".$rulefile."... ");
25if(is_file($rulefile)) {
26 include("$rulefile");
27 echo("Loaded!\n\n");
28} else {
29 echo("File not found!\n\n");
30}
31
32//Make iptables script file
33$ipt = fopen($fwscript, "w");
34fwrite($ipt, "#!/bin/sh\n#This firewall script was generated by Harvie's php firewall ($version)\n\n");
35
36//Rules info
37echo("Rules info: \n");
38if(isset($author)) {
39 fwrite($ipt, "#Author: $author\n");
40 echo("Author: $author\n");
41}
42if(isset($description)){
43 fwrite($ipt, "#Description: $description\n");
44 echo("Description: $description\n");
45}
46fwrite($ipt, "######################################################################################################\n");
47fwrite($ipt, "\n");
48fwrite($ipt, "\n");
49echo("\n");
50
51///Rules Others////////////////////////////////////////////////////////////////////
52fwrite($ipt, "#Rules Others:\n");
53echo("Rules Others:\n");
54
55if($icmp_echo_ignore_broadcasts) {
56 echo("Ignore ICMP echo-request messages sent to broadcast or multicast addresses\n");
57 fwrite($ipt, "echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts\n");
58} else {
59 fwrite($ipt, "echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts\n");
60}
61
62if($accept_source_route) {
63 echo("Accept source routed packets\n");
64 fwrite($ipt, "echo 1 > /proc/sys/net/ipv4/conf/all/accept_source_route\n");
65} else {
66 fwrite($ipt, "echo 0 > /proc/sys/net/ipv4/conf/all/accept_source_route\n");
67}
68
69if($tcp_syncookies) {
70 echo("Enable TCP SYN cookie protection from SYN floods\n");
71 fwrite($ipt, "echo 1 > /proc/sys/net/ipv4/tcp_syncookies\n");
72} else {
73 fwrite($ipt, "echo 0 > /proc/sys/net/ipv4/tcp_syncookies\n");
74}
75
76if($accept_redirects) {
77 echo("Accept ICMP redirect messages\n");
78 fwrite($ipt, "echo 1 > /proc/sys/net/ipv4/conf/all/accept_redirects\n");
79} else {
80 fwrite($ipt, "echo 0 > /proc/sys/net/ipv4/conf/all/accept_redirects\n");
81}
82
83if($send_redirects) {
84 echo("Send ICMP redirect messages\n");
85 fwrite($ipt, "echo 1 > /proc/sys/net/ipv4/conf/all/send_redirects\n");
86} else {
87 fwrite($ipt, "echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects\n");
88}
89
90if($rp_filter) {
91 echo("Enable source address spoofing protection\n");
92 fwrite($ipt, "echo 1 > /proc/sys/net/ipv4/conf/all/rp_filter\n");
93} else {
94 fwrite($ipt, "echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter\n");
95}
96
97if($log_martians) {
98 echo("Log packets from Martians (with impossible source addresses)\n");
99 fwrite($ipt, "echo 1 > /proc/sys/net/ipv4/conf/all/log_martians\n");
100} else {
101 fwrite($ipt, "echo 0 > /proc/sys/net/ipv4/conf/all/log_martians\n");
102}
103
104fwrite($ipt, "\n");
105echo("\n");
106
107///Rules IPTables//////////////////////////////////////////////////////////////////
108fwrite($ipt, "#Rules IPTables:\n");
109echo("Rules IPTables:\n");
110
111//Rules flush
112if($flush_rules) {
113 echo("Flush old rules\n");
114 fwrite($ipt, "$iptbin --flush\n\n");
115}
116
117//Rules loopback
118if($loopback_allow_all == true) {
119 echo("Allow all traffic on loopback\n");
120 fwrite($ipt, "$iptbin -A INPUT -i lo -j ACCEPT\n");
121 fwrite($ipt, "$iptbin -A OUTPUT -o lo -j ACCEPT\n\n");
122}
123
124//Rules policies
125echo("Default policies: ");
126foreach($default_policies as $default_policy) {
127 fwrite($ipt, "$iptbin --policy $default_policy\n");
128 echo("$default_policy, ");
129}
130fwrite($ipt, "\n");
131echo("\n");
132
133//Rules outbound traffic
134if($allow_outbound_traffic) {
135 fwrite($ipt, "$iptbin -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT\n");
136 fwrite($ipt, "$iptbin -A OUTPUT -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT\n\n");
137}
138
139//Rules open ports
140echo("Open ports: ");
141foreach($open_ports as $open_port) {
142 fwrite($ipt, "$iptbin -A INPUT -p tcp --dport $open_port -m state --state NEW -j ACCEPT\n");
143 echo("$open_port, ");
144}
145fwrite($ipt, "\n");
146echo("\n");
147
148//Drop other
149if(drop_other == true) {
150 echo("Other traffic will be droped\n");
151 fwrite($ipt, "$iptbin -A INPUT -j DROP\n");
152}
153
154
155
156//Close iptables script
157fwrite($ipt, "\n\n");
158echo("\nClosing $fwscript\n");
159fclose($ipt);
160
161//Chmod u+x iptables script
162if($chmod) {
163 echo("chmod u+x $fwscript\n");
164 system("chmod u+x $fwscript");
165}
166//Run iptables script
167if($run) {
168 echo("Running firewall script...\n\n");
169 system("./".$fwscript);
170}
This page took 0.309981 seconds and 4 git commands to generate.