This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# requirements: curl, html2text, sed | |
######### | |
# | |
# Author: BBK | |
# Date: 25 Feb 2016 | |
# Title: Kali Tools Search Engine | |
# | |
######### | |
# Colors! | |
YEL='\033[1;33m' | |
NC='\033[0m' # No Color | |
# ensure the directory is present | |
if [ ! -d ~/kali-tools-db ]; then | |
mkdir ~/kali-tools-db | |
fi | |
# ensure correct tools are present | |
command -v html2text >/dev/null 2>&1 || { echo >&2 "I require html2text, but it's not installed. Aborting."; exit 1;} | |
command -v sed >/dev/null 2>&1 || { echo >&2 "I require sed, but it's not installed. Aborting."; exit 1;} | |
command -v curl >/dev/null 2>&1 || { echo >&2 "I require curl, but it's not installed. Aborting."; exit 1;} | |
# Process NUL arguments | |
# Then menu options | |
# Lastly, if no flags are present, perform the default search | |
################################ | |
# Below is the body of the script | |
################################ | |
## NUL Arguments | |
if [ -z "$1" ] | |
then | |
echo "No arguments provided, use -h for help" | |
## Help Menu | |
elif [[ $1 == -h ]] | |
then | |
echo "{Word} - Searches database for keyword. Returns first result from each file. Same as -s, but limits results" | |
echo "-s {Word} - Searches database for keyword. Same as no flag, but does NOT limit results" | |
echo "-c [filename] {Word1} {Word2} {Word3} - Displys tool information and highlights up to 3 keywords" | |
echo "-u - Update Database. Use sparingly, scrapes Kali's site, play nice" | |
echo "-h - Help - This menu" | |
## Update the Database | |
elif [[ $1 == -u ]] | |
then | |
echo "Updating Database" | |
echo "Fetching URLs" | |
curl -s http://tools.kali.org/tools-listing | sed -n '/titlebar imgsize_cover/,/footer layout_compact/p' | grep -o -E 'href="([^"#]+)"' | cut -d'"' -f2 > ~/kali-tools-db/kali-tools-list.txt | |
echo "Fetching Pages" | |
while read p; do | |
newfile=`echo $p | cut -f5 -d"/"` | |
newdir=`echo $p | cut -f4 -d"/"` | |
if [ ! -d ~/kali-tools-db/$newdir ]; then | |
mkdir ~/kali-tools-db/$newdir | |
fi | |
curl -s $p | html2text | sed -n '/Metapackages/,/Related Articles/p' | sed '1d; $d' > ~/kali-tools-db/$newdir/$newfile | |
# curl -s http://tools.kali.org/information-gathering/acccheck | html2text | sed -n '/Metapackages/,/Related Articles/p' | sed '1d; $d' > acccheck | |
done < ~/kali-tools-db/kali-tools-list.txt | |
echo -e "${YEL}Searching for 404, if found, something is wrong!${NC}" | |
grep 'Error 404' --color=always ~/kali-tools-db/* | |
echo -e "${YEL}Done${NC}" | |
## "cat" the file, we use grep (rather than cat) and match everything, so that we can optionally highlight keywords | |
elif [[ $1 == -c ]] | |
then | |
echo -e "${YEL}Displaying $2${NC}" | |
path=`find /root/kali-tools-db/ -name $2` | |
result=`grep --color=always -i -E "^|$3|$4|$5" $path` | |
echo -e "$result" | |
echo -e "${YEL}$2 Done${NC}" | |
## (S)earch provides more verbose serching, matches multiple lines per file. If you want to know why I didn't leave this, search for "password" | |
elif [[ $1 == -s ]] | |
then | |
result=`grep -r -i -n --color=always $2 ~/kali-tools-db/*` | |
echo -e "$result" | |
## You used a dash (-) indicating a flag, but no flag was used. What are you trying to do? | |
elif [[ $1 == -* ]] | |
then | |
echo -e "${YEL}Unknown Flag. Try -h for help${NC}" | |
## Lastly, default to a regular search, limiting ourselves to one result per file | |
else | |
result=`grep -i -r -m1 -n --color=always $1 ~/kali-tools-db/* | sed 's/\/root\/kali-tools-db\///g'` # /root/kali-tools-db/ | |
echo -e "$result" | |
if [[ $2 != "" ]] | |
then | |
echo "" # future - grep -l to display only file names, then grep those files again for the second term, then output results | |
fi | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment