-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql-canary.sh
executable file
·167 lines (149 loc) · 4.24 KB
/
sql-canary.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
#!/bin/sh
#
# sql-canary.sh - Ensure there's no SQL dumps, compressed or not, in
# web server docroot
#
# Copyright (C) 2018 Michael Davies <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
SCRIPT=$(basename $0)
# User-editable bits
CANARY_DOCROOT="${CANARY_DOCROOT:-/var/www/html}"
SUPPORTED_SUFFIXES=("" ".gz" ".Z" ".bz" ".bz2" ".tgz" ".cpio" ".pax" ".zip" ".rar" ".rsrc")
BASE_SUFFIX=".sql"
CHOSEN_DATE=10
# End user-editable bits
# We're only going to generate output if we find something out of place
# or if it's the 1st of the month,
# or if the command line switch asking for output is provided
DISPLAY_OUTPUT=0
# Build up a list of all file suffixes we should check for
SUFFIXES=()
for i in "${SUPPORTED_SUFFIXES[@]}"
do
SUFFIXES+=(${i})
done
for i in "${SUPPORTED_SUFFIXES[@]}"
do
SUFFIXES+=(${BASE_SUFFIX}${i})
done
# Thanks https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-a|--add-suffix)
SUFFIXES+=(${2})
shift # past argument
shift # past value
;;
-d|--docroot)
CANARY_DOCROOT="$2"
shift # past argument
shift # past value
;;
-h|--help)
HELP=1
shift # past argument
;;
-o|--output)
DISPLAY_OUTPUT=1
shift # past argument
;;
-v|--verbose)
VERBOSE=1
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
#
# Display help
#
BLANK=" "
display_cmd_option ()
{
printf " %-2s %-13s %-8s %-44s\n" "$1" "$2" "$3" "$4"
}
if [[ ! -z ${HELP} ]]
then
printf "${SCRIPT}: Look for SQL dumps that shouldn't be there.\n"
printf "Usage: ${SCRIPT} [options]\n"
display_cmd_option "-a" "--add-suffix" "<suffix>" "Add <suffix> to list to search for"
display_cmd_option "-d" "--docroot" "<dir>" "Search in directory <dir>, otherwise use the environment variable CANARY_DOCROOT"
display_cmd_option "-h" "--help" "${BLANK}" "Display this help information"
display_cmd_option "-o" "--output" "${BLANK}" "Always print the output of the search"
display_cmd_option "-v" "--verbose" "${BLANK}" "Be verbose about what we're doing"
printf "\n"
printf "If you want this to run automatically, add the following line to your crontab:\n"
printf "\t0 3 * * * /directory/to/${SCRIPT}\n"
fi
#
# Be verbose
#
if [[ ! -z ${VERBOSE} ]]
then
echo -n "Searching for files with the following suffixes: "
for i in "${SUFFIXES[@]}"
do
echo -n "$i "
done
echo
echo "Searching in directory: ${CANARY_DOCROOT}"
echo "Print output of the search? ${DISPLAY_OUTPUT}"
fi
#
# Force output once per month
#
DATE=$(date +%d)
if [[ ${DATE} -eq ${CHOSEN_DATE} ]]
then
printf "${SCRIPT}: Forcing output one day per month\n"
DISPLAY_OUTPUT=1
fi
#
# Now do what we came here to do
#
TMPFILE=$(mktemp /tmp/${SCRIPT}.XXXXX)
for i in "${SUFFIXES[@]}"
do
THIS="*${i}"
find ${CANARY_DOCROOT} -name "${THIS}" -print >> ${TMPFILE} 2>/dev/null
done
OUTPUT=$(sort ${TMPFILE} | uniq)
if [[ ${DISPLAY_OUTPUT} -eq 1 ]] || [[ ! -z ${OUTPUT} ]]
then
echo "Files that might be dubious:"
fi
if [[ ! -z ${OUTPUT} ]]
then
echo ${OUTPUT}
rm ${TMPFILE}
exit 1
else
if [[ ${DISPLAY_OUTPUT} -eq 1 ]]
then
echo "<<< No files found \o/ >>>"
fi
fi
# Cleanup
rm ${TMPFILE}