forked from Kiya6955/IR-ISP-Blocker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ir-isp-blocker.sh
315 lines (265 loc) · 10.3 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
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
#!/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 "----------- IR-ISP-Blocker -----------"
echo "https://github.com/Kiya6955/IR-ISP-Blocker"
echo "--------------------------------------"
echo "Which ISP do you want to perform an action on?"
echo "--------------------------------------"
echo "1-Hamrah Aval"
echo "2-Irancell"
echo "3-Mokhaberat"
echo "4-Rightel"
echo "5-Shatel"
echo "6-AsiaTech"
echo "7-Pishgaman"
echo "8-MobinNet"
echo "9-ParsOnline"
echo "10-Unblock All"
echo "11-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) isp="Rightel" blocking_menu ;;
5) isp="Shatel" blocking_menu ;;
6) isp="AsiaTech" blocking_menu ;;
7) isp="Pishgaman" blocking_menu ;;
8) isp="MobinNet" blocking_menu ;;
9) isp="ParsOnline" blocking_menu ;;
10) unblocker ;;
11) echo "Exiting..."; exit 0 ;;
*) echo "Invalid option"; main_menu ;;
esac
}
function blocking_menu {
echo "ٌWait a minute, installing prerequisites..."
sleep 2s
clear
if ! dpkg -l | grep -q iptables; then
apt update
apt install -y iptables
fi
if ! dpkg -l | grep -q iptables-persistent; then
apt update
apt install -y iptables-persistent
fi
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 -j isp-blocker
fi
clear
case $isp in
"MCI")
IP_LIST=$(curl -s 'https://raw.githubusercontent.com/Kiya6955/IR-ISP-Blocker/main/mci-ips.ipv4')
;;
"MTN")
IP_LIST=$(curl -s 'https://raw.githubusercontent.com/Kiya6955/IR-ISP-Blocker/main/mtn-ips.ipv4')
;;
"TCI")
IP_LIST=$(curl -s 'https://raw.githubusercontent.com/Kiya6955/IR-ISP-Blocker/main/tci-ips.ipv4')
;;
"Rightel")
IP_LIST=$(curl -s 'https://raw.githubusercontent.com/Kiya6955/IR-ISP-Blocker/main/rightel-ips.ipv4')
;;
"Shatel")
IP_LIST=$(curl -s 'https://raw.githubusercontent.com/Kiya6955/IR-ISP-Blocker/main/shatel-ips.ipv4')
;;
"AsiaTech")
IP_LIST=$(curl -s 'https://raw.githubusercontent.com/Kiya6955/IR-ISP-Blocker/main/asiatech-ips.ipv4')
;;
"Pishgaman")
IP_LIST=$(curl -s 'https://raw.githubusercontent.com/Kiya6955/IR-ISP-Blocker/main/pishgaman-ips.ipv4')
;;
"MobinNet")
IP_LIST=$(curl -s 'https://raw.githubusercontent.com/Kiya6955/IR-ISP-Blocker/main/mobinnet-ips.ipv4')
;;
"ParsOnline")
IP_LIST=$(curl -s 'https://raw.githubusercontent.com/Kiya6955/IR-ISP-Blocker/main/parsan-ips.ipv4')
;;
esac
if [ $? -ne 0 ]; then
echo "Failed to fetch the IP list. Please contact @Kiya6955"
read -p "Press enter to return to Menu" dummy
blocking_menu
fi
clear
echo "---------- $isp Menu ----------"
echo "1-Block $isp"
echo "2-$isp Only"
echo "3-Back to Main Menu"
read -p "Enter your choice: " choice
case $choice in
1) blocker ;;
2) only_mode ;;
3) main_menu ;;
*) echo "Invalid option press enter"; blocking_menu ;;
esac
}
function blocker {
clear
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
read -p "Enter IP addresses you want whitelist for $isp (separate with comma like 1.1.1.1,8.8.8.8 or leave empty for none): " whitelist_ips
IFS=',' read -r -a whitelistIPArray <<< "$whitelist_ips"
clear
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
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
iptables -F isp-blocker
echo "Previous rules deleted successfully"
sleep 2s
fi
clear
echo "Blocking [$ports] for $isp started please wait..."
for ip in "${whitelistIPArray[@]}"; do
iptables -I isp-blocker -s $ip -j ACCEPT
done
for port in "${portArray[@]}"
do
for IP in $IP_LIST; do
if [ "$protocol" == "all" ]; then
iptables -A isp-blocker -s $IP -p tcp --dport $port -j DROP
iptables -A isp-blocker -s $IP -p udp --dport $port -j DROP
else
iptables -A isp-blocker -s $IP -p $protocol --dport $port -j DROP
fi
done
done
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 "Enter ports you want whitelist for $isp (separate with comma like 443,8443 or leave empty for none): " whitelist_ports
IFS=',' read -r -a whitelistPortArray <<< "$whitelist_ports"
clear
read -p "Enter the SSH port you want open for $isp (default is 22): " SSH_PORT
SSH_PORT=${SSH_PORT:-22}
clear
read -p "Do you want to delete the previous rules? [Y/N] : " confirm
if [[ $confirm == [Yy]* ]]; then
iptables -F isp-blocker
echo "Previous rules deleted successfully"
sleep 2s
fi
clear
echo "Blocking all ports for $isp started please wait..."
for ip in "${whitelistIPArray[@]}"; do
iptables -I isp-blocker -s $ip -j ACCEPT
done
for port in "${whitelistPortArray[@]}"; do
iptables -I isp-blocker -p tcp --dport $port -j ACCEPT
iptables -I isp-blocker -p udp --dport $port -j ACCEPT
done
if ! iptables -C isp-blocker -p tcp --dport "$SSH_PORT" -j ACCEPT 2>/dev/null; then
iptables -I isp-blocker -p tcp --dport "$SSH_PORT" -j ACCEPT
fi
for IP in $IP_LIST; do
iptables -A isp-blocker -s $IP -j DROP
done
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
}
function only_mode {
clear
read -p "Enter the your SSH port (default is 22): " SSH_PORT
SSH_PORT=${SSH_PORT:-22}
clear
read -p "Enter ports to block for all ISPs except $isp (separate with comma like 443,8443): " blocklist_ports
IFS=',' read -r -a blocklistPortArray <<< "$blocklist_ports"
if [[ -z "$blocklist_ports" ]]; then
blocking_menu
return
fi
blocklistPortArray=("${blocklistPortArray[@]}")
clear
read -p "Enter IP addresses you want whitelist for (${blocklistPortArray[*]// /, }) (separate with comma like 1.1.1.1,8.8.8.8 or leave empty for none): " whitelist_ips
IFS=',' read -r -a whitelistIPArray <<< "$whitelist_ips"
clear
read -p "Do you want to delete the previous rules? [Y/N]: " confirm
if [[ $confirm == [Yy]* ]]; then
iptables -F isp-blocker
echo "Previous rules deleted successfully"
sleep 2s
fi
clear
echo "Blocking all ISPs for ports (${blocklistPortArray[*]// /, }) except $isp started, please wait..."
for IP in $IP_LIST; do
for port in "${blocklistPortArray[@]}"; do
iptables -I isp-blocker -s $IP -p tcp --dport $port -j ACCEPT
iptables -I isp-blocker -s $IP -p udp --dport $port -j ACCEPT
done
done
for ip in "${whitelistIPArray[@]}"; do
iptables -I isp-blocker -s $ip -j ACCEPT
done
if ! iptables -C isp-blocker -p tcp --dport "$SSH_PORT" -j ACCEPT 2>/dev/null; then
iptables -I isp-blocker -p tcp --dport "$SSH_PORT" -j ACCEPT
fi
for port in "${blocklistPortArray[@]}"; do
if ! iptables -C isp-blocker -p tcp --dport $port -j DROP 2>/dev/null; then
iptables -A isp-blocker -p tcp --dport $port -j DROP
fi
if ! iptables -C isp-blocker -p udp --dport $port -j DROP 2>/dev/null; then
iptables -A isp-blocker -p udp --dport $port -j DROP
fi
done
iptables-save > /etc/iptables/rules.v4
clear
echo "Ports (${blocklistPortArray[*]// /, }) blocked for all isps except $isp successfully"
}
function unblocker {
clear
iptables -F isp-blocker
iptables-save > /etc/iptables/rules.v4
clear
echo "All ISPs UnBlocked successfully!"
read -p "Press enter to return to Menu" dummy
blocking_menu
}
main_menu