-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.sh
254 lines (213 loc) · 6.61 KB
/
scanner.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/bin/bash
#Author : - shivam rai
#debug mode off
#set -x
if [ $EUID != 0 ]
then
echo "Please run this script as root! Better run with sudo."
exit 1
fi
#initial vars
start=`date +%s`
bannerFlag=1
function banner {
echo '---------------'
echo -e "\033[7m Scanner v0.0.1\033[0m"
echo '---------------'
echo ''
}
if [ $bannerFlag -eq 1 ]; then
banner;
bannerFlag=2
else
echo 'Unexpected error'
exit 1
fi
# Error module returns usage info with error code 1
function usage {
echo ''
#echo 'Error! illegal parameter(s)'
cat << EOF
Usage 1: bash scanner.sh -d <path/to/Node/Project> -r <path/to/generate/report> -node
(If you only want to include node_modules)
Usage 2: bash scanner.sh -d <path/to/Node/Project> -r <path/to/generate/report>
(If you want to include Entire Directory including node_modules)
EOF
exit 1
}
node_path=$#
if [ $# -eq 4 ];then
echo ''
elif [ $# -eq 5 ];then
if [ "$5" != "-node" ]; then
echo ''
echo "missing -node flag, usage of ${5} is invalid"
echo ''
usage;
fi
else
usage;
fi
# check dir flag
if [ "$1" != "-d" ]; then
echo ''
echo "missing -d flag, usage of ${1} is invalid"
echo ''
usage;
fi
#check output flag
if [ "$3" != "-r" ]; then
echo ''
echo "missing -r flag, usage of ${3} is invalid"
echo ''
usage;
fi
#append dir node_modules if flag is -node
if [ $# -eq 5 ]; then
node_path="/node_modules"
elif [ $# -eq 4 ]; then
node_path=""
fi
#path of node
pathToNode=$2
#path to generate report
pathToReport=$4
#if report folder is same as current folder
if [ "$pathToReport" == "." ]; then
pathToReport=$(pwd)
fi
#check if directory exists
if [ ! -d "$pathToNode" ]; then
echo "provided node directory doesn't exist"
exit 1
fi
#check if report directory exists if not create one
if [ ! -d "$pathToReport" ]; then
echo "report directory doesn't exist"
read -p "Do you wish to create this directory? (yes/no) " yn
case $yn in
y|yes|Y|Yes) mkdir $pathToReport
;;
n|no|N|No ) exit 1
;;
* ) echo "Invalid option"
exit 1;;
esac
fi
#check directory and remove if extra '/' is present
if [[ $pathToNode =~ \/$ ]]; then
pathToNode=$(echo $pathToNode|sed 's/.$//')
fi
if [[ $pathToReport =~ \/$ ]]; then
pathToReport=$(echo $pathToReport|sed 's/.$//')
fi
#echo "$pathToNode""$node_path"
#find folder named as node_modules and if found counts
findTrue=$((find $pathToNode -maxdepth 1 -type d -name node_modules | wc -l) 2>&1)
exitC=$(echo $findTrue| sed "s/.*No such file or directory.*//")
if [ -z $exitC ]; then
echo 'Error ! Remove spaces from Node folder name'
exit 1
fi
if [ $findTrue -eq 0 ]; then
echo "node_modules folder not found, make sure to specify correct path"
exit 1
fi
#counts total number of folder in node_modules
totalScannedModules=$(ls $pathToNode/node_modules | wc -l)
#Eval regex
keyword='\beval(.*)'
#eval comment regex
falsePositive='\/\/\s.*eval(.*)'
echo ''
echo -e "\033[7mIn progress...please wait\033[0m"
echo ''
#recursive finding file having eval
directoryFile=$(grep -nr "$keyword" "$pathToNode""$node_path" --include=\*.js|cut -d: -f1,2) #! | rev|cut -d/ -f1| rev
#filtering file having eval in comments
falsePosDirectory=$(grep -nr "$falsePositive" "$pathToNode""$node_path" --include=\*.js|cut -d: -f1,2)
#stores the directory of the file which are not false positive
directoryFile=($(echo ${directoryFile[@]} ${falsePosDirectory[@]} | tr ' ' '\n'| sort |uniq -u))
#( IFS=$'\n'; echo "${directoryFile[*]}" )
foundListLength=${#directoryFile[@]}
#stores module names
declare -a modulename
for (( i=0; i<${foundListLength}; i++ ));
do
modulename[i]=$(echo -e "${directoryFile[$i]} \n" | sed "s/.*\(\/node_modules\/\)/\1/"| grep -o '/node_modules/.*' | cut -d/ -f3)
done
#stores uniq module name
modulename=($(( IFS=$'\n'; echo "${modulename[*]}" ) | sort -u))
#getting script time while scanning
totaltime=$((($(date +%s)-$start)))
#generating report
DATE=$(date '+%Y-%m-%d %H:%M:%S')
reportName="report_$DATE"
touch "$reportName".html
echo "<html>" >> "$reportName".html
echo "<meta http-equiv=\"Content-Language\" content=\"en\">" >> "$reportName".html
echo "<body bgcolor=\"#E6E6FA\">" >> "$reportName".html
echo "<title>Scanner v0.0.1</title>" >> "$reportName".html
echo "<h1>Report generated by Scanner v0.0.1</h1>" >> "$reportName".html
echo -e "<h2>Total Scan time : $totaltime seconds</h2>" >> "$reportName".html
echo -e "<h2>Total Modules found in node_modules: $totalScannedModules</h2>" >> "$reportName".html
echo -e "<h2>Total eval found : $foundListLength</h2>" >> "$reportName".html
echo -e "<fieldset><legend>node_Modules using Eval</legend>" >> "$reportName".html
for module in "${modulename[@]}";
do
echo -e "- $module <br>" >> "$reportName".html
done
echo -e "</fieldset>" >> "$reportName".html
echo "<h3>File Location</h3>" >> "$reportName".html
for link in "${directoryFile[@]}";
do
lnk=$(echo $link | cut -d: -f1)
lineNumber=$(echo $link | cut -d: -f2)
#sleep 200
#outer if begin
if [ $# -eq 5 ];then
short=$(echo "$link" | sed "s/.*\(\/node_modules\/\)/\1/"| grep -o '/node_modules/.*' |cut -d/ -f3- | cut -d: -f1)
else
tmp=$(echo "$pathToNode" | wc -c)
short=$(echo "$link" | cut -b $tmp- | cut -d: -f1)
dir=$(echo $short | cut -d/ -f2)
#inner if begin
if [ "$dir" == "node_modules" ];
then
short=$(echo "$link" | sed "s/.*\(\/node_modules\/\)/\1/"| grep -o '/node_modules/.*' |cut -d/ -f3- | cut -d: -f1)
stringnodename=" [node_modules]"
#inner if
fi
#outer if
fi
#echo -e "<a href="$lnk">"$short"</a>" >> report.html;
#check if node_modules
#if [ -z $short ]; then
#echo 'eval not found'
#exit 1
#fi
echo -e "<div style=\"background: #E6E6FA; overflow:auto;width:auto;border:solid black;;border-width:.1em .1em .1em .1em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><a href="$lnk" STYLE=\"text-decoration: none\"><span style=\"color: #008800; font-weight: bold\"></span> <span style=\"background-color: #96D7FC\">open</a><h>" $short"<h><span style='font-weight: bold;color:blue;'>$stringnodename</span></h></h></span></a>\
<table align=\"right\">\
<tr>\
<th align=\"right\">Line Number: $lineNumber</th>\
</tr>\
</table>\
</pre>\
</div>" >> "$reportName".html
done
echo "</body>" >> "$reportName".html
echo "</html>" >> "$reportName".html
if [ $bannerFlag -eq 2 ]; then
clear
banner;
fi
echo -e "\033[7mScan Complete with Report\033[0m\r"
echo "Moving Report to the destination $pathToReport"
sleep 2
mv "$reportName".html $pathToReport/
echo "Report File path : $pathToReport/"$reportName".html"
#checking os version to use open command
osV=$(uname)
if [ "$osV" == "Darwin" ]; then
open $pathToReport/"$reportName".html
fi