forked from Kiya6955/IR-ISP-Blocker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathir-isp-blocker.sh
229 lines (191 loc) · 6.72 KB
/
ir-isp-blocker.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
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
clear
echo "You should run this script with root!"
echo "Use sudo -i to change user to root"
exit 1
fi
function main_menu {
clear
echo "---------- Iran ISP Blocker ----------"
echo "https://github.com/Kiya6955/IR-ISP-Blocker"
echo "--------------------------------------"
echo "Which ISP do you want block/unblock?"
echo "--------------------------------------"
echo "1-MCI(Hamrah Aval)"
echo "2-MTN(Irancell)"
echo "3-TCI(Mokhaberat)"
echo "4-Exit"
read -p "Enter your choice: " isp
case $isp in
1) isp="MCI" blocking_menu ;;
2) isp="MTN" blocking_menu ;;
3) isp="TCI" blocking_menu ;;
4) echo "Exiting..."; exit 0 ;;
*) echo "Invalid option"; main_menu ;;
esac
}
function blocking_menu {
clear
echo "---------- $isp Menu ----------"
echo "1-Block $isp"
echo "2-UnBlock All"
echo "3-Back to Main Menu"
read -p "Enter your choice: " choice
case $choice in
1) blocker ;;
2) unblocker ;;
3) main_menu ;;
*) echo "Invalid option press enter"; blocking_menu ;;
esac
}
function blocker {
clear
# Install iptables
if ! command -v iptables &> /dev/null; then
sudo apt-get update
sudo apt-get install -y iptables
fi
if ! dpkg -s iptables-persistent &> /dev/null; then
sudo apt-get install -y iptables-persistent
fi
# Create chain
if ! iptables -L isp-blocker -n >/dev/null 2>&1; then
iptables -N isp-blocker
fi
if ! iptables -C INPUT -j isp-blocker &> /dev/null; then
iptables -I INPUT 1 -j isp-blocker
fi
clear
# Ask User
read -p "Are you sure about blocking $isp? [Y/N] : " confirm
if [[ $confirm == [Yy]* ]]; then
clear
# Get the IP list from Github
if [ "$isp" == "MCI" ]; then
IP_LIST=$(curl -s 'https://raw.githubusercontent.com/Kiya6955/IR-ISP-Blocker/main/mci-ips.ipv4')
if [ $? -ne 0 ]; then
echo "Failed to fetch the MTN IP list. Please contact with @Kiya6955"
read -p "Press enter to return to Menu" dummy
blocking_menu
fi
elif [ "$isp" == "TCI" ]; then
IP_LIST=$(curl -s 'https://raw.githubusercontent.com/Kiya6955/IR-ISP-Blocker/main/tci-ips.ipv4')
if [ $? -ne 0 ]; then
echo "Failed to fetch the MTN IP list. Please contact with @Kiya6955"
read -p "Press enter to return to Menu" dummy
blocking_menu
fi
else
IP_LIST=$(curl -s 'https://raw.githubusercontent.com/Kiya6955/IR-ISP-Blocker/main/mtn-ips.ipv4')
if [ $? -ne 0 ]; then
echo "Failed to fetch the MTN IP list. Please contact with @Kiya6955"
read -p "Press enter to return to Menu" dummy
blocking_menu
fi
sudo iptables -A INPUT -s $IP -p $protocol --match multiport --dport $ports -j DROP
fi
clear
echo "Choose an option:"
echo "1-Block specific ports for $isp"
echo "2-Block all ports for $isp"
echo "3-Back to Main Menu"
read -p "Enter your choice: " choice
clear
# Save ports in array
if [[ $choice == 1 ]]; then
read -p "Enter the ports you want block for $isp(enter single like 443 or separated by comma like 443,8443): " ports
IFS=' ' read -r -a portArray <<< "$ports"
fi
case $choice in
1)
clear
# Ask user to block TCP or UDP or Both
echo "Choose Protocol that you want to block for $isp"
echo "1-TCP & UDP"
echo "2-TCP"
echo "3-UDP"
read -p "Enter your choice: " protocol
case $protocol in
1) protocol="all" ;;
2) protocol="tcp" ;;
3) protocol="udp" ;;
*) echo "Invalid option"; blocker ;;
esac
clear
read -p "Do you want to delete the previous rules? [Y/N] : " confirm
if [[ $confirm == [Yy]* ]]; then
sudo iptables -F isp-blocker
echo "Previous rules deleted successfully"
sleep 2s
fi
clear
echo "Blocking [$ports] for $isp started please Wait..."
for ports in "${portArray[@]}"
do
for IP in $IP_LIST; do
if [ "$protocol" == "all" ]; then
# Add Rules for both TCP and UDP
sudo iptables -A isp-blocker -s $IP -p tcp --match multiport --dport $ports -j DROP
sudo iptables -A isp-blocker -s $IP -p udp --match multiport --dport $ports -j DROP
else
# Add Rules for either TCP or UDP
sudo iptables -A isp-blocker -s $IP -p $protocol --match multiport --dport $ports -j DROP
fi
done
done
# Save rules
sudo iptables-save > /etc/iptables/rules.v4
clear
if [ "$protocol" == "all" ]; then
echo "TCP & UDP [$ports] successfully blocked for $isp."
else
echo "$protocol [$ports] successfully blocked for $isp."
fi
;;
2)
clear
read -p "Do you want to delete the previous rules? [Y/N] : " confirm
if [[ $confirm == [Yy]* ]]; then
sudo iptables -F isp-blocker
echo "Previous rules deleted successfully"
sleep 2s
fi
clear
# Open SSH Port
read -p "Enter the SSH port you want to open (default is 22): " SSH_PORT
SSH_PORT=${SSH_PORT:-22}
sudo iptables -A isp-blocker -p tcp --dport $SSH_PORT -j ACCEPT
clear
echo "Blocking all ports for $isp started please Wait..."
# Add new rules
for IP in $IP_LIST; do
sudo iptables -A isp-blocker -s $IP -j DROP
done
# Save rules
sudo iptables-save > /etc/iptables/rules.v4
clear
echo "$isp successfully blocked for all ports."
echo "Port $SSH_PORT has been opened for SSH."
;;
*) echo "Invalid option"; blocking_menu ;;
esac
read -p "Press enter to return to Menu" dummy
blocking_menu
else
echo "Cancelled."
read -p "Press enter to return to Menu" dummy
blocking_menu
fi
}
function unblocker {
clear
sudo iptables -F isp-blocker
sudo iptables-save > /etc/iptables/rules.v4
clear
echo "All ISPs UnBlocked successfully!"
read -p "Press enter to return to Menu" dummy
blocking_menu
}
# Start the script
main_menu