-
Notifications
You must be signed in to change notification settings - Fork 21
/
iptables.sh
280 lines (270 loc) · 8.65 KB
/
iptables.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/bash
ip=$1
ports=$2
proto=$3
dport=$4
install_req() {
clear
# Check OS and set release variable
if [[ -f /etc/os-release ]]; then
source /etc/os-release
release=$ID
elif [[ -f /usr/lib/os-release ]]; then
source /usr/lib/os-release
release=$ID
else
echo "Failed to check the system OS, please contact the author!" >&2
exit 1
fi
echo "The OS release is: $release"
case "${release}" in
centos | fedora | almalinux)
yum upgrade && yum install -y -q net-tools
touch epfinstalled
;;
arch | manjaro)
pacman -Sy --noconfirm net-tools inetutils
touch epfinstalled
;;
*)
apt update && apt install -y -q net-tools iptables-persistent
touch epfinstalled
;;
esac
clear
}
usage() {
echo -e "Usage: iptables.sh 1 2 3\n1: Destination IP\n2: Desired Ports | Example Array of ports: 443,80,2083\n3: Protocol TCP/UDP lowercase."
}
modify() {
clear
echo -e "Easy Port Forwarder By Incognito Coder\nGithub Page: https://github.com/Incognito-Coder"
publicIP=$(hostname -I | awk '{print $1}')
interface=$(route | grep '^default' | grep -o '[^ ]*$')
echo "Enabling IP Forwarding"
sysctl net.ipv4.ip_forward=1 >/dev/null 2>&1
if iptables --table nat --list | grep -q "ssh"; then
echo "No need to forward SSH Port,Already Exist!"
else
echo "Forwarding SSH Port to $publicIP"
iptables -t nat -A PREROUTING -p tcp --dport 22 -j DNAT --to-destination $publicIP
fi
for i in $(echo $ports | tr "," "\n"); do
echo "Moving Port $i to $ip:$i"
iptables -t nat -A PREROUTING -p $proto --dport $i -j DNAT --to-destination $ip
done
echo "Finalizing Changes in IP Tables."
iptables -t nat -A POSTROUTING -j MASQUERADE -o $interface
}
modify_nat() {
clear
echo -e "Easy Port Forwarder By Incognito Coder\nGithub Page: https://github.com/Incognito-Coder"
publicIP=$(hostname -I | awk '{print $1}')
interface=$(route | grep '^default' | grep -o '[^ ]*$')
echo "Enabling IP Forwarding"
sysctl net.ipv4.ip_forward=1 >/dev/null 2>&1
if iptables --table nat --list | grep -q "ssh"; then
echo "No need to forward SSH Port,Already Exist!"
else
echo "Forwarding SSH Port to $publicIP"
iptables -t nat -A PREROUTING -p tcp --dport 22 -j DNAT --to-destination $publicIP
fi
echo "Moving NAT to $ip"
iptables -t nat -A PREROUTING -j DNAT --to-destination $ip
echo "Finalizing Changes in IP Tables."
iptables -t nat -A POSTROUTING -j MASQUERADE -o $interface
menu
}
port_to_port() {
clear
echo -e "Easy Port Forwarder By Incognito Coder\nGithub Page: https://github.com/Incognito-Coder"
publicIP=$(hostname -I | awk '{print $1}')
interface=$(route | grep '^default' | grep -o '[^ ]*$')
echo "Enabling IP Forwarding"
sysctl net.ipv4.ip_forward=1 >/dev/null 2>&1
if iptables --table nat --list | grep -q "ssh"; then
echo "No need to forward SSH Port,Already Exist!"
else
echo "Forwarding SSH Port to $publicIP"
iptables -t nat -A PREROUTING -p tcp --dport 22 -j DNAT --to-destination $publicIP
fi
echo "Moving Port $ports to $ip:$dport"
iptables -t nat -A PREROUTING -p $proto --dport $ports -j DNAT --to-destination $ip:$dport
echo "Finalizing Changes in IP Tables."
iptables -t nat -A POSTROUTING -j MASQUERADE -o $interface
menu
}
6to4() {
clear
echo "Local Tunnel ipv6 to ipv4"
publicIP=$(hostname -I | awk '{print $1}')
PS3='Your current session is? '
options=("Sender" "Receiver" "Reset Network" "Back")
select opt in "${options[@]}"; do
case $opt in
"Sender")
read -p "Enter Dest(receiver) IP: " ip
# Clean last tunnel
ip tunnel del 6to4_To_KH >/dev/null 2>&1
ip -6 tunnel del ipip6Tun_To_KH >/dev/null 2>&1
# Do new job
ip tunnel add 6to4_To_KH mode sit remote $ip local $publicIP
ip -6 addr add fc00::1/64 dev 6to4_To_KH
ip link set 6to4_To_KH mtu 1480
ip link set 6to4_To_KH up
ip -6 tunnel add ipip6Tun_To_KH mode ipip6 remote fc00::2 local fc00::1
ip addr add 192.168.13.1/30 dev ipip6Tun_To_KH
ip link set ipip6Tun_To_KH mtu 1440
ip link set ipip6Tun_To_KH up
sysctl net.ipv4.ip_forward=1 >/dev/null 2>&1
iptables -t nat -A PREROUTING -p tcp --dport 22 -j DNAT --to-destination 192.168.13.1
iptables -t nat -A PREROUTING -j DNAT --to-destination 192.168.13.2
iptables -t nat -A POSTROUTING -j MASQUERADE
echo "IP $publicIP moved to $ip"
sleep 3
menu
break
;;
"Receiver")
read -p "Enter Target(sender) IP: " ip
# Clean last tunnel
ip tunnel del 6to4_To_IR >/dev/null 2>&1
ip -6 tunnel del ipip6Tun_To_IR >/dev/null 2>&1
# Do new job
ip tunnel add 6to4_To_IR mode sit remote $ip local $publicIP
ip -6 addr add fc00::2/64 dev 6to4_To_IR
ip link set 6to4_To_IR mtu 1480
ip link set 6to4_To_IR up
ip -6 tunnel add ipip6Tun_To_IR mode ipip6 remote fc00::1 local fc00::2
ip addr add 192.168.13.2/30 dev ipip6Tun_To_IR
ip link set ipip6Tun_To_IR mtu 1440
ip link set ipip6Tun_To_IR up
echo "IP $publicIP moved to $ip"
sleep 3
menu
break
;;
"Reset Network")
if ip tunnel show | grep -q '6to4'; then
echo "Reseting 6TO4 tunnel"
ip tunnel del 6to4_To_KH >/dev/null 2>&1
ip -6 tunnel del ipip6Tun_To_KH >/dev/null 2>&1
ip tunnel del 6to4_To_IR >/dev/null 2>&1
ip -6 tunnel del ipip6Tun_To_IR >/dev/null 2>&1
echo "all tunnels deleted"
sleep 1
menu
else
echo "No tunnel found,returing to menu..."
sleep 2
menu
fi
break
;;
"Back")
menu
break
;;
*) echo "invalid option $REPLY" ;;
esac
done
}
flush() {
clear
echo "Stopping IPv4 firewall and allowing everyone..."
ipt="/sbin/iptables"
[ ! -x "$ipt" ] && {
echo "$0: \"${ipt}\" command not found."
exit 1
}
$ipt -P INPUT ACCEPT
$ipt -P FORWARD ACCEPT
$ipt -P OUTPUT ACCEPT
$ipt -F
$ipt -X
$ipt -t nat -F
$ipt -t nat -X
$ipt -t mangle -F
$ipt -t mangle -X
$ipt -t raw -F
$ipt -t raw -X
sleep 3
menu
}
menu() {
clear
echo "Welcome to Easy Port Forwarder"
PS3='Please enter your choice: '
options=("Port Forward" "NAT Forward" "Port to Port" "Tunnel 6TO4" "Flush Rules" "Save Rules" "Restore Rules" "Show Rules" "Print Usage" "Quit")
select opt in "${options[@]}"; do
case $opt in
"Port Forward")
read -p "Enter Dest IP: " ip
read -p "Enter Dest Port/Ports separated: " ports
read -p "Enter ProtocolType TCP/UDP: " proto
modify
break
;;
"NAT Forward")
read -p "Enter Dest IP: " ip
modify_nat
break
;;
"Port to Port")
read -p "Enter Dest IP: " ip
read -p "Enter IN Port: " ports
read -p "Enter OUT Port: " dport
read -p "Enter ProtocolType TCP/UDP: " proto
port_to_port
break
;;
"Tunnel 6TO4")
6to4
break
;;
"Flush Rules")
flush
break
;;
"Save Rules")
sudo /sbin/iptables-save >/etc/iptables/rules.v4
echo "Saved."
;;
"Restore Rules")
sudo /sbin/iptables-restore </etc/iptables/rules.v4
echo "Restored."
;;
"Show Rules")
iptables -t nat --list
read -n1 -r -p "Press any key to continue..."
;;
"Print Usage")
usage
break
;;
"Quit")
break
;;
*) echo "invalid option $REPLY" ;;
esac
done
}
if [[ $# -eq 0 ]]; then
usage
exit 0
else
if [[ $1 == "flush" ]]; then
flush
exit 0
elif [[ $1 == "menu" ]]; then
if ! [ -f epfinstalled ]; then
install_req
fi
menu
else
if ! [ -f epfinstalled ]; then
install_req
fi
modify
fi
fi