-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathbuild-nmap-scripts-index-by-category.sh
executable file
·80 lines (71 loc) · 2.84 KB
/
build-nmap-scripts-index-by-category.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
#!/bin/bash
CDIR=$(pwd)
INDEX_FILE="$CDIR/docs/7-NMAP_SCRIPTS_INDEX_BY_CATEGORY.md"
NMAP_REPO="/tmp/nmap"
NMAP_GITHUB="https://github.com/nmap/nmap.git"
NMAP_GITHUB_BASE="https://github.com/nmap/nmap/blob/master/scripts"
# Initialize index file
echo "[+] Init the index..."
echo "# 💼 Index of the Nmap built-in scripts (by category)" > $INDEX_FILE
echo "" >> $INDEX_FILE
echo "<!-- markdown-link-check-disable -->" >> $INDEX_FILE
echo "" >> $INDEX_FILE
echo "> **Note**: 🕒 Updated on $(date +'%Y-%m-%d at %T')." >> $INDEX_FILE
echo "" >> $INDEX_FILE
echo "🏡 [Back to home](README.md)." >> $INDEX_FILE
echo "" >> $INDEX_FILE
echo "[+] Clone nmap GH repo..."
rm -rf $NMAP_REPO 2>/dev/null
git clone --quiet --depth 1 $NMAP_GITHUB $NMAP_REPO
echo "[+] Build the index based on present NSE scripts..."
cd "$NMAP_REPO/scripts"
export CDE='```'
# Create associative arrays for categories and script details
declare -A script_categories
declare -A script_details
# Process each script
cd "$NMAP_REPO/scripts"
for script in $(ls | sort)
do
# Extract categories, remove single quotes, and split into lines
raw_categories=$(sed -n '/^categories = /{s/^categories = {\(.*\)}.*$/\1/p}' $script | tr -d '"' | tr -d "'")
categories=${raw_categories//,/ }
for category in $categories
do
script_categories[$category]+="${script%.nse} "
done
# Extract and truncate the description without space before "..."
script_descr=$(sed -n '/^description = \[\[/,/\]\]/p' $script | sed '1d;$d' | tr -d '\0' | tr '\n' ' ' | awk '{print substr($0, 1, 97)"..."}')
# Save script details in an associative array
script_details[${script%.nse}]="$script_descr | $raw_categories"
done
# Create a sorted array of unique categories
IFS=$'\n' sorted_categories=($(sort <<<"${!script_categories[*]}"))
unset IFS
# Generate Table of Contents
echo "## Categories" >> $INDEX_FILE
for category in "${sorted_categories[@]}"
do
echo "- [$category](#$category)" >> $INDEX_FILE
done
echo "" >> $INDEX_FILE
# Generate the list of scripts by category
for category in "${sorted_categories[@]}"
do
echo "## $category" >> $INDEX_FILE
for script in ${script_categories[$category]}
do
echo "### $script" >> $INDEX_FILE
echo "* 📝 [Source]($NMAP_GITHUB_BASE/$script.nse)" >> $INDEX_FILE
# Extract script categories for anchor links
IFS='|' read -r script_description script_cats <<< "${script_details[$script]}"
script_cats_links=$(echo $script_cats | sed -e 's/, /\n/g' | awk '{print "[#" $1 "](#" $1 ")"}' | paste -sd' ' -)
echo "* 📂 $script_cats_links" >> $INDEX_FILE
echo "" >> $INDEX_FILE
echo "$CDE" >> $INDEX_FILE
echo "$script_description" >> $INDEX_FILE # No space before "..."
echo "$CDE" >> $INDEX_FILE
echo "" >> $INDEX_FILE
done
done
echo "[V] Category index built."