-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountry-filter.sh
201 lines (170 loc) · 7.99 KB
/
country-filter.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
#!/usr/bin/env bash
# ---------------------------------------------------------------------------------------- #
# Description #
# ---------------------------------------------------------------------------------------- #
# Implement country level blocking via TCP Wrappers. Requires geoiplookup to identify the #
# country for a given IP address and then applies the default 'ACTION'. #
# #
# Action: #
# ALLOW: Allow connections ONLY from the specified countries. #
# DENY: Deny all connections from specified countries. #
# ---------------------------------------------------------------------------------------- #
# TCP Wrapper config: #
# #
# /etc/hosts.allow #
# sshd: ALL: aclexec /usr/sbin/county-filter %a #
# #
# /etc/hosts.deny #
# sshd: ALL #
# ---------------------------------------------------------------------------------------- #
ALLOW_ACTION='ALLOW'
DENY_ACTION='DENY'
# space-separated list of country codes
COUNTRIES=''
# The action to take when a country is matched
ACTION=$DENY_ACTION
# ---------------------------------------------------------------------------------------- #
# In multiplexer #
# ---------------------------------------------------------------------------------------- #
# A simple wrapper to check if the script is being run via the multiplex or not. #
# ---------------------------------------------------------------------------------------- #
function in_multiplexer
{
[[ "${MUX}" = true ]] && return 0 || return 1;
}
# ---------------------------------------------------------------------------------------- #
# In terminal #
# ---------------------------------------------------------------------------------------- #
# A wrapper to check if the script is being run in a terminal or not. #
# ---------------------------------------------------------------------------------------- #
function in_terminal
{
[[ -t 1 ]] && return 0 || return 1;
}
# ---------------------------------------------------------------------------------------- #
# Debug #
# ---------------------------------------------------------------------------------------- #
# Show output only if we are running in a terminal, but always log the message. #
# ---------------------------------------------------------------------------------------- #
function debug()
{
local message="${1:-}"
if [[ -n "${message}" ]]; then
if in_terminal || in_multiplexer; then
echo "${message}"
fi
logger "${message}"
fi
}
# ---------------------------------------------------------------------------------------- #
# Check results #
# ---------------------------------------------------------------------------------------- #
# Check individual results against a given array and deny where applicable. #
# ---------------------------------------------------------------------------------------- #
function check_results()
{
local item="${1:-}"
local list="${2:-}"
#
# Check the current item and list and decide what action to take
#
if [[ "${ACTION}" == "${DENY_ACTION}" ]]; then
[[ $list =~ $item ]] && RESPONSE=${DENY_ACTION} || RESPONSE=${ALLOW_ACTION}
else
[[ $list =~ $item ]] && RESPONSE=${ALLOW_ACTION} || RESPONSE=${DENY_ACTION}
fi
if [[ $RESPONSE = "${DENY_ACTION}" ]]; then
debug "$RESPONSE sshd connection from ${IP} ($item)"
exit 1
fi
#
# Default (REPONSE=ALLOW) is to do nothing
#
}
# ---------------------------------------------------------------------------------------- #
# Handle country blocks #
# ---------------------------------------------------------------------------------------- #
# Lookup the country for a given IP, it should only have, at most, one entry, capture the #
# country code and test each it to ensure it is not bocked. #
# ---------------------------------------------------------------------------------------- #
function handle_country_blocks
{
#
# Local variables
#
local GEOLOOKUP
local VERSION
local v6_regex='^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$'
#
# Workout if the IP is a V6 address or not
#
if [[ ${IP} =~ $v6_regex ]]; then
GEOLOOKUP=$(command -v geoiplookup6)
VERSION=6
else
GEOLOOKUP=$(command -v geoiplookup)
fi
#
# Do the lookup and let check_results handle the blocking
#
if [[ -z "${GEOLOOKUP}" ]]; then
debug "geoiplookup${VERSION} is not installed - Skipping"
else
COUNTRY=$("${GEOLOOKUP}" "${IP}" | awk -F ": " '{ print $2 }' | awk -F "," '{ print $1 }' | head -n 1)
#
# If we cannot find the country then set a default value we can match on
#
if [[ "${COUNTRY}" == 'IP Address not found' ]]; then
COUNTRY='XX'
fi
check_results "${COUNTRY}" "${COUNTRIES}"
fi
}
# ---------------------------------------------------------------------------------------- #
# Main() #
# ---------------------------------------------------------------------------------------- #
# The main function where all of the heavy lifting and script config is done. #
# ---------------------------------------------------------------------------------------- #
function main()
{
#
# NO IP given - error and abort
#
if [[ -z "${1}" ]]; then
debug 'Ip addressed not supplied - Aborting'
exit 0
fi
#
# Set a variable (Could pass it at function call)
#
declare -g IP="${1}"
#
# Are we being called from the multiplexer?
#
if [[ -n "${2}" ]]; then
declare -g MUX=true
else
declare -g MUX=false
fi
#
# Turn off case sensitivity
#
shopt -s nocasematch
#
# Country level blocking
#
handle_country_blocks
# Default allow
exit 0
}
# ---------------------------------------------------------------------------------------- #
# Main() #
# ---------------------------------------------------------------------------------------- #
# The actual 'script' and the functions/sub routines are called in order. #
# ---------------------------------------------------------------------------------------- #
main "${@}"
# ---------------------------------------------------------------------------------------- #
# End of Script #
# ---------------------------------------------------------------------------------------- #
# This is the end - nothing more to see here. #
# ---------------------------------------------------------------------------------------- #