Skip to content

Commit da8a6f2

Browse files
committed
Fix pattern for matching /etc/hosts entries
`grep -w` matches also `string1-whatsoever` so that entries like ``` 192.168.0.10 anystring anystring-apache 192.168.0.11 anystring-tomcat ``` matched 3 entries over 2 lines. This PR fixes #2937 by improving the match pattern, so that `string1` needs a trailing whitespace or a EOL -- besides a leaing whitespace.
1 parent c19d3ff commit da8a6f2

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

testssl.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22206,24 +22206,27 @@ filter_ip4_address() {
2220622206

2220722207
# For security testing sometimes we have local entries. Getent is BS under Linux for localhost: No network, no resolution
2220822208
# arg1 is the entry we want to look up in the host file
22209+
#
2220922210
get_local_aaaa() {
2221022211
local ip6=""
2221122212
local etchosts="/etc/hosts /c/Windows/System32/drivers/etc/hosts"
2221222213

2221322214
[[ -z "$1" ]] && echo "" && return 1
22214-
# Also multiple records should work fine
22215-
ip6=$(grep -wih "$1" $etchosts 2>/dev/null | grep ':' | grep -Ev '^#|\.local' | grep -Ei "[[:space:]]$1" | awk '{ print $1 }')
22215+
# grep: find hostname with trailing lf or space. -w doesn't work here
22216+
ip6=$(grep -Eih "[[:space:]]$1([[:space:]]|$)" $etchosts 2>/dev/null | grep ':' | grep -Ev '^#|\.local' | awk '{ print $1 }')
2221622217
if is_ipv6addr "$ip6"; then
2221722218
echo "$ip6"
2221822219
else
2221922220
echo ""
2222022221
fi
2222122222
}
22223+
2222222224
get_local_a() {
2222322225
local ip4=""
2222422226
local etchosts="/etc/hosts /c/Windows/System32/drivers/etc/hosts"
2222522227

22226-
ip4=$(grep -wih "$1" $etchosts 2>/dev/null | grep -Ev ':|^#|\.local' | grep -Ei "[[:space:]]$1" | awk '{ print $1 }')
22228+
# grep: find hostname with trailing lf or space. -w doesn't work here
22229+
ip4=$(grep -Eih "[[:space:]]$1([[:space:]]|$)" $etchosts 2>/dev/null | grep -Ev ':|^#|\.local' | awk '{ print $1 }')
2222722230
if is_ipv4addr "$ip4"; then
2222822231
echo "$ip4"
2222922232
else

0 commit comments

Comments
 (0)