11#! /bin/bash
2- # Name: searchsploit - Exploit-DB's CLI search tool
3- # Version: 3.1 (Release date: 2015-07-08 )
2+ # Name: SearchSploit - Exploit-DB's CLI search tool
3+ # Version: 3.2 (Release date: 2016-03-18 )
44# Written by: Offensive Security, Unix-Ninja & g0tmi1k
55# Homepage: https://github.com/offensive-security/exploit-database
6-
6+ #
77# # NOTE:
88# Exit code '0' means finished normally
99# Exit code '1' means finished help screen
1414gitpath=" /usr/share/exploitdb"
1515csvpath=" ${gitpath} /files.csv"
1616
17+
1718# # Program settings
1819gitremote=" https://github.com/offensive-security/exploit-database.git"
1920progname=" $( basename " $0 " ) "
2021
22+
2123# # Default options
22- TAGS=" "
23- SCASE=" tolower"
24- VERBOSE=0
25- WEBLINK=0
24+ COLOUR=1
2625EDBID=0
27- COLOUR= ' true '
26+ EXACT=0
2827FILEPATH=1
28+ OVERFLOW=0
29+ WEBLINK=0
30+ SCASE=0
31+ TAGS=" "
32+ CASE_TAG_GREP=" -i"
33+ CASE_TAG_FGREP=" tolower"
34+ COLOUR_TAG=" "
35+ SEARCH=" "
36+
37+
38+ # # Set LANG variable to avoid illegal byte sequence errors
39+ LANG=C
2940
3041
3142# # If files.csv is in the searchsploit path, use that instead
@@ -45,23 +56,25 @@ function usage()
4556 echo " ========="
4657 echo " Options "
4758 echo " ========="
48- echo " -c, --case Perform a case-sensitive search (Default is insensitive)."
49- echo " -h, --help Show this help screen."
50- echo " -t, --title Search just the exploit title (Default is title AND the file's path)."
51- echo " -u, --update Update exploit database from git."
52- echo " -v, --verbose Verbose output. Title lines are allowed to overflow their columns."
53- echo " -w, --www Show URLs to Exploit-DB.com rather than local path."
54- echo " --colour Disable colour highlighting."
55- echo " --id Display EDB-ID value rather than local path."
59+ echo " -c, --case Perform a case-sensitive search (Default is inSEnsITiVe)."
60+ echo " -e, --exact Perform an EXACT match on exploit title (Default is AND) [Implies \" -t\" ]."
61+ echo " -h, --help Show this help screen."
62+ echo " -o, --overflow Exploit title's are allowed to overflow their columns."
63+ echo " -t, --title Search just the exploit title (Default is title AND the file's path)."
64+ echo " -u, --update Update exploit database from git."
65+ echo " -w, --www Show URLs to Exploit-DB.com rather than local path."
66+ echo " --id Display EDB-ID value rather than local path."
67+ echo " --colour Disable colour highlighting."
5668 echo
5769 echo " ======="
5870 echo " Notes "
5971 echo " ======="
60- echo " * Use any number of search terms, in any order ."
72+ echo " * Use any number of search terms."
6173 echo " * Search terms are not case sensitive, and order is irrelevant."
6274 echo " * Use '-c' if you wish to reduce results by case-sensitive searching."
75+ echo " * And/Or '-e' if you wish to filter results by using an exact match."
6376 echo " * Use '-t' to exclude the file's path to filter the search results."
64- echo " * Could possibly remove false positives (especially when searching numbers)."
77+ echo " * Remove false positives (especially when searching numbers/versions )."
6578 echo " * When updating from git or displaying help, search terms will be ignored."
6679 echo " "
6780 exit 1
@@ -70,10 +83,11 @@ function usage()
7083# # Update database (via GIT)
7184function update()
7285{
86+ # # Make sure we are in the correct folder
7387 mkdir -p " ${gitpath} /"
7488 cd " ${gitpath} /"
7589
76- # Make sure a git repo is init before updating
90+ # # Make sure a git repo is init before updating
7791 if [[ " $( git rev-parse --is-inside-work-tree ) " != " true" ]]; then
7892 if [[ " $( ls ) " = " " ]]; then
7993 # If directory is empty, just clone
122136ARGS=" -"
123137for param in " $@ " ; do
124138 if [[ " ${param} " == " --case" ]]; then
125- SCASE=' '
139+ SCASE=1
140+ elif [[ " ${param} " == " --exact" ]]; then
141+ EXACT=1
126142 elif [[ " ${param} " == " --help" ]]; then
127143 usage >&2
144+ elif [[ " ${param} " == " --overflow" ]]; then
145+ OVERFLOW=1
128146 elif [[ " ${param} " == " --title" ]]; then
129147 FILEPATH=0
130148 elif [[ " ${param} " == " --update" ]]; then
131149 update
132150 elif [[ " ${param} " == " --www" ]]; then
133151 WEBLINK=1
134- elif [[ " ${param} " == " --verbose" ]]; then
135- VERBOSE=1
136152 elif [[ " ${param} " == " --colour" ]] || [[ " ${param} " == " --color" ]]; then
137- COLOUR=' '
153+ COLOUR=" "
138154 elif [[ " ${param} " == " --id" ]]; then
139155 EDBID=1
140156 else
@@ -149,24 +165,39 @@ done
149165
150166
151167# # Parse short arguments
152- while getopts " chtuvw " arg " ${ARGS} " ; do
168+ while getopts " cehotuw " arg " ${ARGS} " ; do
153169 if [[ " ${arg} " = " ?" ]]; then
154170 usage >&2 ;
155171 fi
156172 case ${arg} in
157- c) SCASE=' ' ;;
173+ c) SCASE=1;;
174+ e) EXACT=1;;
158175 h) usage >&2 ;;
176+ o) OVERFLOW=1;;
159177 t) FILEPATH=0;;
160178 u) update;;
161- v) VERBOSE=1;;
162179 w) WEBLINK=1;;
163180 esac
164181 shift $(( OPTIND - 1 ))
165182done
166183
167184
168- # # Dynamically set column widths
169- if [[ " ${WEBLINK} " -eq ' 1' ]]; then
185+ # # If we are doing an exact match, do not check folder path.
186+ if [[ " ${EXACT} " -eq 1 ]]; then
187+ FILEPATH=0
188+ fi
189+
190+
191+ # # Case sensitive?
192+ if [[ " ${SCASE} " -eq 1 ]]; then
193+ # # Remove the default flags
194+ CASE_TAG_GREP=" "
195+ CASE_TAG_FGREP=" "
196+ fi
197+
198+
199+ # # Dynamically set column widths to the current screen size
200+ if [[ " ${WEBLINK} " -eq 1 ]]; then
170201 COL2=45
171202else
172203 COL2=35
@@ -177,9 +208,9 @@ COL1=$(( $( tput cols ) - COL2 - 1 ))
177208# # Print header
178209drawline
179210printf " %-${COL1} s %s" " Exploit Title"
180- if [[ " ${WEBLINK} " -eq ' 1 ' ]]; then
211+ if [[ " ${WEBLINK} " -eq 1 ]]; then
181212 echo " | URL"
182- elif [[ " ${EDBID} " -eq ' 1 ' ]]; then
213+ elif [[ " ${EDBID} " -eq 1 ]]; then
183214 echo " | EDB-ID"
184215else
185216 echo " | Path"
@@ -188,69 +219,90 @@ else
188219fi
189220drawline
190221
191- # # Create (AND) search command
192- SEARCH=
193- for tag in ${TAGS} ; do
194222
195- if [[ " ${COLOUR} " ]]; then
196- COLOUR=" ${COLOUR} \|${tag} "
223+ # # EXACT search command?
224+ if [[ " ${EXACT} " -eq 1 ]]; then
225+ # # Case sensitive?
226+ if [[ " ${SCASE} " -eq 1 ]]; then
227+ SEARCH=" ${TAGS} "
228+ else
229+ SEARCH=" $( echo ${TAGS} | tr ' [:upper:]' ' [:lower:]' ) "
197230 fi
198231
199- if [[ " ${FILEPATH} " -eq 1 ]]; then
200- if [[ " ${SCASE} " ]]; then
201- SCASE=' -i'
202- fi
232+ # # Remove leading space
233+ SEARCH=" $( echo ${SEARCH} | sed -e ' s/^[[:space:]]//' ) "
203234
204- if [[ " ${SEARCH} " ]]; then
205- SEARCH=" ${SEARCH} |"
235+ # # If we are to use colour, add the values to search for
236+ if [[ " ${COLOUR} " -eq 1 ]]; then
237+ COLOUR_TAG=" ${SEARCH} "
238+ fi
239+ # # or AND search command?
240+ else
241+ # # For each term
242+ for tag in ${TAGS} ; do
243+ # # If we are to use colour, add the values to search for between "or"
244+ if [[ " ${COLOUR} " -eq 1 ]]; then
245+ COLOUR_TAG=" ${COLOUR_TAG} \|${tag} "
206246 fi
207247
208- SEARCH=" ${SEARCH} fgrep ${SCASE} \" ${tag} \" "
209- else
210- if [[ " ${SEARCH} " ]]; then
211- SEARCH=" ${SEARCH} / && ${SCASE} (\$ 1) ~ /"
212- fi
248+ # # Search both title and path?
249+ if [[ " ${FILEPATH} " -eq 1 ]]; then
250+ # # Is there a value already?
251+ if [[ " ${SEARCH} " ]]; then
252+ SEARCH=" ${SEARCH} |"
253+ fi
213254
214- if [[ " ${SCASE} " ]]; then
215- tag=" $( echo ${tag} | tr ' [:upper:]' ' [:lower:]' ) "
255+ # # Search command for each term
256+ SEARCH=" ${SEARCH} fgrep ${CASE_TAG_GREP} \" ${tag} \" "
257+ # # Search just the title, not the path
258+ else
259+ # # If there is already a value, prepend text to get ready
260+ if [[ " ${SEARCH} " ]]; then
261+ SEARCH=" ${SEARCH} / && ${CASE_TAG_FGREP} (\$ 1) ~ /"
262+ fi
263+
264+ # # Case sensitive?
265+ if [[ " ${SCASE} " -eq 1 ]]; then
266+ EARCH=" ${SEARCH}${tag} "
267+ else
268+ SEARCH=" ${SEARCH} $( echo ${tag} | tr ' [:upper:]' ' [:lower:]' ) "
269+ fi
216270 fi
217-
218- SEARCH=" ${SEARCH}${tag} "
219- fi
220- done
221-
222- if [[ " ${FILEPATH} " -ne 1 ]]; then
223- SEARCH=" awk -F '[|]' '${SCASE} (\$ 1) ~ /${SEARCH} / {print}'"
271+ done
224272fi
225273
226- if [[ " ${COLOUR} " ]]; then
227- SEARCH=" ${SEARCH} | grep --color=always -ie \"\$ {COLOUR}\" "
274+
275+ # # If we are not to use the path name
276+ if [[ " ${FILEPATH} " -eq 0 ]]; then
277+ SEARCH=" awk -F '[|]' '${CASE_TAG_FGREP} (\$ 1) ~ /${SEARCH} / {print}'"
228278fi
229279
230280
231- # # Set LANG variable to avoid illegal byte sequence errors
232- LANG=C
281+ # # If we are to use colour, add the value here
282+ if [[ " ${COLOUR_TAG} " ]]; then
283+ SEARCH=" ${SEARCH} | grep --color=always -ie \"\$ {COLOUR_TAG}\" "
284+ fi
233285
234286
235287# # Search, format, and print results
236- if [[ " ${VERBOSE} " -eq 0 ]]; then
237- FORMAT=${COL1} ' .' ${COL1}
238- else
288+ if [[ " ${OVERFLOW} " -eq 1 ]]; then
239289 FORMAT=${COL1}
290+ else
291+ FORMAT=${COL1} ' .' ${COL1}
240292fi
241293
242294
295+ # # Magic search Fu
243296# # Web link format?
244- if [[ " ${WEBLINK} " -eq ' 1' ]]; then
245- # # Magic search Fu
297+ if [[ " ${WEBLINK} " -eq 1 ]]; then
246298 awk -F " \" *,\" *" ' { printf "%-' ${FORMAT} ' s | %s\n", $3, "https://www.exploit-db.com/exploits/"$1"/"}' " ${csvpath} " \
247299 | eval " ${SEARCH} "
248- elif [[ " ${EDBID} " -eq ' 1 ' ]] ; then
249- # # Magic search Fu
300+ # # Just the EDB-ID?
301+ elif [[ " ${EDBID} " -eq 1 ]] ; then
250302 awk -F " \" *,\" *" ' { printf "%-' ${FORMAT} ' s | %s\n", $3, $1}' " ${csvpath} " \
251303 | eval " ${SEARCH} "
304+ # # Default view
252305else
253- # # Magic search Fu
254306 awk -F " \" *,\" *" ' { printf "%-' ${FORMAT} ' s | %s\n", $3, $2}' " ${csvpath} " \
255307 | eval " ${SEARCH} " \
256308 | sed " s/| platforms/| ./"
0 commit comments