Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
craig
GitHub Repository: craig/ge.mine.nu
Path: blob/master/lbd/lbd.sh
100 views
1
#!/bin/bash
2
# lbd (load balancing detector) detects if a given domain uses
3
# DNS and/or HTTP Load-Balancing (via Server: and Date: header and diffs between server answers)
4
# Copyright (C) 2010-2014 Stefan Behte
5
#
6
# This program is free software; you can redistribute it and/or
7
# modify it under the terms of the GNU General Public License
8
# as published by the Free Software Foundation; either version 2
9
# of the License, or (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
#
20
# License: GNU General Public License, version 2
21
# http://www.gnu.org/licenses/gpl-2.0.html
22
#
23
# Contact me, if you have any new ideas, bugs/bugfixes, recommondations or questions!
24
# Please also contact me, if you just like the tool. :)
25
#
26
# craig at haquarter dot de
27
#
28
# 0.1: - initial release
29
# 0.2: - fix license for fedora
30
# - fix indenting
31
# 0.3: - fix bug if dns server returns same IP multiple times
32
# (fix by bit bori, thanks!)
33
# - fix bug if there is no date header
34
# (fix by Paul Rib, thanks!)
35
# 0.4: - support HTTPs, support different ports
36
# (thanks Bharadwaj Machiraju)
37
38
QUERIES=50
39
DOMAIN=$1
40
PORT=${2-80} # Use default port 80, if not given
41
if [ "$3" = "https" ]
42
then
43
HTTPS=true
44
else
45
HTTPS=false
46
fi
47
METHODS=""
48
49
echo
50
echo "lbd - load balancing detector 0.4 - Checks if a given domain uses load-balancing."
51
echo " Written by Stefan Behte (http://ge.mine.nu)"
52
echo " Proof-of-concept! Might give false positives."
53
54
if [ "$1" = "" ]
55
then
56
echo "usage: $0 domain [port] {https}"
57
echo
58
exit -1
59
fi
60
61
echo -e -n "\nChecking for DNS-Loadbalancing:"
62
NR=`host $DOMAIN | grep "has add" | uniq | wc -l`
63
64
if [ $NR -gt 1 ]
65
then
66
METHODS="DNS"
67
echo " FOUND"
68
host $DOMAIN | grep "has add" | uniq
69
echo
70
else
71
echo " NOT FOUND"
72
fi
73
74
echo -e "Checking for HTTP-Loadbalancing [Server]: "
75
for ((i=0 ; i< $QUERIES ; i++))
76
do
77
if [ $HTTPS = true ]
78
then
79
printf "HEAD / HTTP/1.1\r\nhost: $DOMAIN\r\nConnection: close\r\n\r\n" | openssl s_client -host $DOMAIN -port $PORT -quiet > .nlog 2> /dev/null
80
else
81
printf "HEAD / HTTP/1.1\r\nhost: $DOMAIN\r\nConnection: close\r\n\r\n" | nc $DOMAIN $PORT > .nlog 2>/dev/null
82
fi
83
84
S=`grep -i "Server:" .nlog | awk -F: '{print $2}'`
85
86
if ! grep "`echo ${S}| cut -b2-`" .log &>/dev/null
87
then
88
echo "${S}"
89
fi
90
cat .nlog >> .log
91
done
92
93
NR=`sort .log | uniq | grep -c "Server:"`
94
95
if [ $NR -gt 1 ]
96
then
97
echo " FOUND"
98
METHODS="$METHODS HTTP[Server]"
99
else
100
echo " NOT FOUND"
101
fi
102
echo
103
rm .nlog .log
104
105
106
echo -e -n "Checking for HTTP-Loadbalancing [Date]: "
107
D4=
108
109
for ((i=0 ; i<$QUERIES ; i++))
110
do
111
if [ $HTTPS = true ]
112
then
113
D=`printf "HEAD / HTTP/1.1\r\nhost: $DOMAIN\r\nConnection: close\r\n\r\n" | openssl s_client -host $DOMAIN -port $PORT -quiet 2> /dev/null | grep "Date:" | awk '{print $6}'`
114
else
115
D=`printf "HEAD / HTTP/1.1\r\nhost: $DOMAIN\r\nConnection: close\r\n\r\n" | nc $DOMAIN $PORT 2>/dev/null | grep "Date:" | awk '{print $6}'`
116
fi
117
printf "$D, "
118
119
if [ "$D" == "" ]
120
then
121
echo "No date header found, skipping."
122
break
123
fi
124
125
Df=$(echo " $D" | sed -e 's/:0/:/g' -e 's/ 0/ /g')
126
D1=$(echo ${Df} | awk -F: '{print $1}')
127
D2=$(echo ${Df} | awk -F: '{print $2}')
128
D3=$(echo ${Df} | awk -F: '{print $3}')
129
130
if [ "$D4" = "" ]; then D4=0; fi
131
132
if [ $[ $D1 * 3600 + $D2 * 60 + $D3 ] -lt $D4 ]
133
then
134
echo "FOUND"
135
METHODS="$METHODS HTTP[Date]"
136
break;
137
fi
138
139
D4="$[ $D1 * 3600 + $D2 * 60 + $D3 ]"
140
141
if [ $i -eq $[$QUERIES - 1] ]
142
then
143
echo "NOT FOUND"
144
fi
145
done
146
147
echo -e -n "\nChecking for HTTP-Loadbalancing [Diff]: "
148
for ((i=0 ; i<$QUERIES ; i++))
149
do
150
if [ $HTTPS = true ]
151
then
152
printf "HEAD / HTTP/1.1\r\nhost: $DOMAIN\r\nConnection: close\r\n\r\n" | openssl s_client -host $DOMAIN -port $PORT -quiet 2> /dev/null | grep -v -e "Date:" -e "Set-Cookie" > .nlog
153
else
154
printf "HEAD / HTTP/1.1\r\nhost: $DOMAIN\r\nConnection: close\r\n\r\n" | nc $DOMAIN $PORT 2>/dev/null | grep -v -e "Date:" -e "Set-Cookie" > .nlog
155
fi
156
157
if ! cmp .log .nlog &>/dev/null && [ -e .log ]
158
then
159
echo "FOUND"
160
diff .log .nlog | grep -e ">" -e "<"
161
METHODS="$METHODS HTTP[Diff]"
162
break;
163
fi
164
165
cp .nlog .log
166
167
if [ $i -eq $[$QUERIES - 1] ]
168
then
169
echo "NOT FOUND"
170
fi
171
done
172
173
rm .nlog .log
174
175
176
if [ "$METHODS" != "" ]
177
then
178
echo
179
echo $DOMAIN does Load-balancing. Found via Methods: $METHODS
180
echo
181
else
182
echo
183
echo $DOMAIN does NOT use Load-balancing.
184
echo
185
fi
186
187
188