-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathiptables.sh
498 lines (478 loc) · 14.9 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#!/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
# shellcheck disable=SC1091
source /etc/os-release
release=$ID
elif [[ -f /usr/lib/os-release ]]; then
# shellcheck disable=SC1091
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 iptables
touch .epfinstalled
;;
arch | manjaro)
pacman -Sy --noconfirm net-tools inetutils iptables
touch .epfinstalled
;;
*)
apt update && apt install -y -q net-tools iptables-persistent iptables
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='Select an option? '
options=("Sender" "Receiver" "Reset Network" "Remove Startup" "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
startup_prompt 1 "$ip" "$publicIP"
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
startup_prompt 2 "$ip" "$publicIP"
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
flush
else
echo "No tunnel found,returing to menu..."
sleep 2
menu
fi
break
;;
"Remove Startup")
if systemctl is-enabled epftunnel.service | grep -q 'enabled'; then
systemctl disable epftunnel &> /dev/null
echo "EPF Tunnel disabled at startup."
sleep 2
menu
else
echo "epftunnel.service already disabled"
sleep 2
menu
fi
break
;;
"Back")
menu
break
;;
*) echo "invalid option $REPLY" ;;
esac
done
}
show_rules() {
iptables -t nat -L PREROUTING -n --line-numbers | awk '
$1 ~ /^[0-9]+$/ {
protocol = $3
line_number = $1
forwarding = ""
port_type = ""
ports = ""
ip = ""
for (i = 6; i <= NF; i++) {
if ($i ~ /dpt:/ || $i ~ /dports/) {
port_type = ($i ~ /dpt:/) ? "Port:" : "Ports:"
ports = $i
sub(/dpt:|dports /, "", ports)
if (port_type == "Ports:") {
for (j = i + 1; j <= NF && $j !~ /to:/; j++) {
ports = ports "," $j
}
sub(/^,/, "", ports)
sub(/dports,/, "", ports)
}
for (j = i + 1; j <= NF; j++) {
if ($j ~ /to:/) {
ip = $j
for (k = j + 1; k <= NF; k++) {
ip = ip " " $k
}
sub(/to:/, "IP: ", ip)
break
}
}
break
}
}
print line_number ") " ip " " port_type " " ports " Protocol: " protocol
}'
}
remove_rules() {
clear
show_rules
echo "*) Flush all Rules"
echo "0) Back"
read -p "Select a Remove option: " Choice
case "$Choice" in
"*")
flush
;;
"0")
menu
;;
[1-9]*)
iptables -t nat -D PREROUTING "$Choice"
iptables -t nat -D POSTROUTING "$Choice"
netfilter-persistent save >/dev/null 2>&1
echo "Rule $Choice removed successfully"
read -p "Press Enter To Continue"
;;
*)
echo "Invalid Choice"
sleep 1
;;
esac
}
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
}
startup_prompt(){
read -p "Would you like to run this tunnel at system boot? [y/n]: " choose
if [[ "$choose" == "y" || "$choose" == "Y" ]]; then
case $1 in
"1")
cat > /etc/epftunnel.sh <<EOF
#!/bin/bash
ip tunnel add 6to4_To_KH mode sit remote "$2" local "$3"
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
EOF
echo "Tunnel setup script created at /etc/epftunnel.sh"
chmod +x /etc/epftunnel.sh
cat > /etc/systemd/system/epftunnel.service <<EOF
[Unit]
Description=EPF Tunnel
After=network.target
[Service]
ExecStart=/bin/bash /etc/epftunnel.sh
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload &> /dev/null && systemctl enable epftunnel &> /dev/null
;;
"2")
cat > /etc/epftunnel.sh <<EOF
#!/bin/bash
ip tunnel add 6to4_To_IR mode sit remote "$2" local "$3"
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
EOF
echo "Tunnel setup script created at /etc/epftunnel.sh"
chmod +x /etc/epftunnel.sh
cat > /etc/systemd/system/epftunnel.service <<EOF
[Unit]
Description=EPF Tunnel
After=network.target
[Service]
ExecStart=/bin/bash /etc/epftunnel.sh
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload &> /dev/null && systemctl enable epftunnel &> /dev/null
;;
esac
fi
}
set_mtu(){
PS3='Please select your desired MTU: '
interface=$(route | grep '^default' | grep -o '[^ ]*$')
options=("1300" "1420" "1480" "1500" "9000" "Back")
select opt in "${options[@]}"; do
case $opt in
"1300")
ip li set mtu 1300 dev "$interface"
echo "MTU of $interface sets to 1300"
sleep 2
menu
break
;;
"1420")
ip li set mtu 1420 dev "$interface"
echo "MTU of $interface sets to 1420"
sleep 2
menu
break
;;
"1480")
ip li set mtu 1480 dev "$interface"
echo "MTU of $interface sets to 1480"
sleep 2
menu
break
;;
"1500")
ip li set mtu 1500 dev "$interface"
echo "MTU of $interface sets to 1500"
sleep 2
menu
break
;;
"9000")
ip li set mtu 9000 dev "$interface"
echo "MTU of $interface sets to 9000"
sleep 2
menu
break
;;
"Back")
menu
break
;;
*) echo "invalid option $REPLY" ;;
esac
done
}
update_script(){
wget --no-check-certificate -O epf.sh https://raw.githubusercontent.com/Incognito-Coder/EPF/master/iptables.sh &> /dev/null
chmod +x epf.sh
if [[ $? == 0 ]]; then
echo -e "Upgrade script succeeded, Please rerun the script"
exit 0
else
echo -e "Failed to update the script."
return 1
fi
}
menu() {
clear
echo "Welcome to Easy Port Forwarder & Tunneling"
PS3='Please enter your choice: '
options=("Port Forward" "NAT Forward" "Port to Port" "Tunnel 6TO4" "Remove Rules" "Save Rules" "Restore Rules" "Set MTU" "Show Rules" "Update Script" "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
;;
"Remove Rules")
remove_rules
break
;;
"Save Rules")
/sbin/iptables-save >/etc/iptables/rules.v4
echo "Saved."
;;
"Restore Rules")
/sbin/iptables-restore </etc/iptables/rules.v4
echo "Restored."
;;
"Set MTU")
set_mtu
break
;;
"Show Rules")
iptables -t nat --list
read -n1 -r -p "Press any key to continue..."
;;
"Update Script")
update_script
;;
"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