-
Notifications
You must be signed in to change notification settings - Fork 1
/
searchnse
135 lines (116 loc) · 3.6 KB
/
searchnse
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
#!/bin/bash
SAVEIFS=$IFS # Save current IFS
match=1
f_flag=0
################################
# HELFPUL HELP FUNCTION #
################################
function print_help() {
echo "================================================================="
echo "= - - - - - - searchnse - - - - - - ="
echo "================================================================="
echo "| SEARCH THE NMAP SCRIPTING ENGINE DATABASE |"
echo "| by midnightseer |"
echo "| |"
echo "| Description: searchnse is a simple nmap scripting engine |"
echo "| search tool. Tired of nmap github searching? Tired of |"
echo "| cat'ing and grep'ing nse files? There's a script for that! |"
echo "| |"
echo "| Usage: searchnse -h |"
echo '| searchnse -f "<arg1> <filter-term> <...>" |'
echo "| -v -f |"
echo "| -v : verbose; print the script help contents |"
echo '| -f : specify filter terms in "quotes"; 1st term |'
echo "| should align with nmap's protocol definitions |"
echo "| arg1 - typically the main search-word / protocol |"
echo "| (ie smb or http) |"
echo "================================================================="
exit
}
################################
# "PRETTY" BORDERS #
################################
function print_divs() {
a=$1
for i in $(seq ${#a})
do
echo -n "="
done
}
################################
# BEGIN MAIN LOGIC #
################################
if [[ $# -eq 0 ]]
then
print_help
exit
fi
IFS=" "
while getopts "hvf:" opt;
do
case $opt in
h) print_help;;
v) VERBOSE=1;;
f) filters=($OPTARG);f_flag=1;;
:) print_help;;
\?) print_help;;
esac
done
if [[ $f_flag -eq 0 ]]
then
echo "Error! -f is mandatory!"
print_help
fi
IFS=$'\n' # Change IFS to new line
candidates=$(grep ${filters[0]} /usr/share/nmap/scripts/script.db)
candidates=($candidates)
#### get file names of nse scripts ####
#### Big loop around each found script ####
for line in "${candidates[@]}"
do
match=1
script_name=$(echo "$line" | cut -d " " -f5 | sed 's/,//g' | sed 's/"//g')
#echo "searching through: $script_name"
script_help=$(nmap --script-help=$script_name 2>&1)
#echo "$script_help"
#### loop over filters ####
count=0
for filter in ${filters[@]}
do
#echo "Trying Filter: $filter"
#Nmap and your stupid stdin/stderror output, tough to pass stderr to functions
if [[ $match -eq 1 ]] #tests if the prev loop found a match
then
if echo $script_help | grep -qsi $filter
then
match=1
#echo "MATCH! $script_name"
else
match=0
fi
fi
done
if [[ $match -eq 1 ]]
then
if [[ $VERBOSE -eq 1 ]]
then
script_args=$(echo "$script_help" | sed -n -e '/-- @args/,/auth.*/ p')
echo -n "="
print_divs $script_name
echo "="
echo " $script_name "
echo -n "="
print_divs $script_name
echo "="
echo ""
echo -e "\e[1;4mcat /usr/share/nmap/scripts/$script_name\e[0m"
echo ""
echo "$script_help" | sed -e '1,3d'
echo "Script Arguments:"
echo "$script_args"
else
echo -e "\e[1;4mcat /usr/share/nmap/scripts/$script_name\e[0m"
fi
fi
done
IFS=$SAVEIFS