diff --git a/README.md b/README.md index 6f1ff780f..49180afa5 100755 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ Example: -c, --case Perform a case-sensitive search (Default is inSEnsITiVe). -e, --exact Perform an EXACT match on exploit title (Default is AND) [Implies "-t"]. -h, --help Show this help screen. + -j, --json Show result in JSON format. -o, --overflow Exploit title's are allowed to overflow their columns. -p, --path Show the full path to an exploit (Copies path to clipboard if possible). -t, --title Search just the exploit title (Default is title AND the file's path). diff --git a/searchsploit b/searchsploit index 189bcf2c2..0f292c613 100755 --- a/searchsploit +++ b/searchsploit @@ -27,6 +27,7 @@ EDBID=0 EXACT=0 FILEPATH=1 GETPATH=0 +JSON=0 OVERFLOW=0 WEBLINK=0 SCASE=0 @@ -62,6 +63,7 @@ function usage() echo " -c, --case Perform a case-sensitive search (Default is inSEnsITiVe)." echo " -e, --exact Perform an EXACT match on exploit title (Default is AND) [Implies \"-t\"]." echo " -h, --help Show this help screen." + echo " -j, --json Show result in JSON format." echo " -o, --overflow Exploit title's are allowed to overflow their columns." echo " -p, --path Show the full path to an exploit (Copies path to clipboard if possible)." echo " -t, --title Search just the exploit title (Default is title AND the file's path)." @@ -150,6 +152,8 @@ for param in "$@"; do EXACT=1 elif [[ "${param}" == "--help" ]]; then usage >&2 + elif [[ "${param}" == "--json" ]]; then + JSON=1 elif [[ "${param}" == "--overflow" ]]; then OVERFLOW=1 elif [[ "${param}" == "--path" ]]; then @@ -176,7 +180,7 @@ done ## Parse short arguments -while getopts "cehoptuw" arg "${ARGS}"; do +while getopts "cehjoptuw" arg "${ARGS}"; do if [[ "${arg}" = "?" ]]; then usage >&2; fi @@ -184,6 +188,7 @@ while getopts "cehoptuw" arg "${ARGS}"; do c) SCASE=1;; e) EXACT=1;; h) usage >&2;; + j) JSON=1;; o) OVERFLOW=1;; p) GETPATH=1;; t) FILEPATH=0;; @@ -258,20 +263,29 @@ else fi COL1=$(( $( tput cols ) - COL2 - 1 )) +## Remove leading space +TAGS="$(echo ${TAGS} | sed -e 's/^[[:space:]]//')" -## Print header -drawline -printf "%-${COL1}s %s" " Exploit Title" -if [[ "${WEBLINK}" -eq 1 ]]; then - echo "| URL" -elif [[ "${EDBID}" -eq 1 ]]; then - echo "| EDB-ID" +## Print header if not in JSON +if [[ "${JSON}" -eq 0 ]]; then + drawline + printf "%-${COL1}s %s" " Exploit Title" + if [[ "${WEBLINK}" -eq 1 ]]; then + echo "| URL" + elif [[ "${EDBID}" -eq 1 ]]; then + echo "| EDB-ID" + else + echo "| Path" + printf "%-${COL1}s " + echo "| (${gitpath}/platforms)" + fi + drawline +## Print JSON header else - echo "| Path" - printf "%-${COL1}s " - echo "| (${gitpath}/platforms)" + echo "{" + echo " \"SEARCH\": \"${TAGS}\"," + echo " \"RESULTS\": [" fi -drawline ## EXACT search command? @@ -283,9 +297,6 @@ if [[ "${EXACT}" -eq 1 ]]; then SEARCH="$( echo ${TAGS} | tr '[:upper:]' '[:lower:]' )" fi - ## Remove leading space - SEARCH="$(echo ${SEARCH} | sed -e 's/^[[:space:]]//')" - ## If we are to use colour, add the values to search for if [[ "${COLOUR}" -eq 1 ]]; then COLOUR_TAG="${SEARCH}" @@ -336,7 +347,7 @@ fi ## If we are to use colour, add the value here -if [[ "${COLOUR_TAG}" ]]; then +if [[ "${COLOUR_TAG}" ]] && [[ "${JSON}" -eq 0 ]]; then SEARCH="${SEARCH} | grep --color=always -ie \"\${COLOUR_TAG}\"" fi @@ -352,23 +363,34 @@ fi ## Magic search Fu ## Web link format? if [[ "${WEBLINK}" -eq 1 ]]; then - awk -F "\"*,\"*" '{ printf "%-'${FORMAT}'s | %s\n", $3, "https://www.exploit-db.com/exploits/"$1"/"}' "${csvpath}" \ - | eval "${SEARCH}" + OUTPUT="$(awk -F "\"*,\"*" '{ printf "%-'${FORMAT}'s | %s\n", $3, "https://www.exploit-db.com/exploits/"$1"/"}' "${csvpath}" \ + | eval "${SEARCH}")" ## Just the EDB-ID? elif [[ "${EDBID}" -eq 1 ]]; then - awk -F "\"*,\"*" '{ printf "%-'${FORMAT}'s | %s\n", $3, $1}' "${csvpath}" \ - | eval "${SEARCH}" + OUTPUT="$(awk -F "\"*,\"*" '{ printf "%-'${FORMAT}'s | %s\n", $3, $1}' "${csvpath}" \ + | eval "${SEARCH}" )" +## Print JSON format (full options) +elif [[ "${JSON}" -eq 1 ]]; then + OUTPUT="$(awk -F "\"*,\"*" '{ printf "\r\t\t'{'\"Exploit\":\"%s\",\"Path\":\"'${gitpath}/'%s\",\"EDB-ID\":%s},\n", $3, $2, $1}' "${csvpath}" \ + | eval "${SEARCH}" \ + | sed '$ s/,$//g' )" ## Default view else - awk -F "\"*,\"*" '{ printf "%-'${FORMAT}'s | %s\n", $3, $2}' "${csvpath}" \ + OUTPUT=$OUTPUT"$(awk -F "\"*,\"*" '{ printf "%-'${FORMAT}'s | %s\n", $3, $2}' "${csvpath}" \ | eval "${SEARCH}" \ - | sed "s/| platforms/| ./" + | sed "s/| platforms/| ./" )" fi +echo $OUTPUT -## Print footer -drawline - +## Print footer if not in JSON +if [[ "${JSON}" -eq 0 ]]; then + drawline +## Print JSON footer +else + echo " ]" + echo "}" +fi ## Done exit 0