-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsuricata_stats.sh
429 lines (373 loc) · 13.7 KB
/
suricata_stats.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/bin/sh
##
#
# __ __
# (_ ` _ o _ _ _)_ _ (_ ` _)_ _ _)_ _
#.__) (_( ) ( (_ (_( (_ (_( .__) (_ (_( (_ (
# _)
#
## by @juched - Generate Stats for GUI tab
## with credit to @JackYaz for his shared scripts
## v1.0 - initial stats
readonly SCRIPT_VERSION="v1.0"
#define www script names
readonly SCRIPT_WEBPAGE_DIR="$(readlink /www/user)"
readonly SCRIPT_NAME="Suricata_Stats.sh"
readonly LOGSCRIPT_NAME="Suricata_Log.sh"
readonly SCRIPT_NAME_LOWER="suricata_stats.sh"
readonly LOGSCRIPT_NAME_LOWER="suricata_log.sh"
readonly SCRIPT_WEB_DIR="$SCRIPT_WEBPAGE_DIR/$SCRIPT_NAME_LOWER"
readonly SCRIPT_DIR="/jffs/addons/suricata"
#needed for shared jy graph files from @JackYaz
readonly SHARED_DIR="/jffs/addons/shared-jy"
readonly SHARED_REPO="https://raw.githubusercontent.com/jackyaz/shared-jy/master"
readonly SHARED_WEB_DIR="$SCRIPT_WEBPAGE_DIR/shared-jy"
#define data file names
statsTitleFile="$SCRIPT_WEB_DIR/suricatastatstitle.txt"
statsTitleFileJS="$SCRIPT_WEB_DIR/suricatastatstitle.js"
statsThreatsFileJS="$SCRIPT_WEB_DIR/suricatastats.js"
statsThreatsHitsFileJS="$SCRIPT_WEB_DIR/suricatahits.js"
#DB file to hold data for uptime graph
dbLogs="/opt/var/lib/suricata/suricata_log.db"
#save md5 of last installed www ASP file so you can find it again later (in case of www ASP update)
installedMD5File="$SCRIPT_DIR/www-installed.md5"
#get sqlite path
[ -f /opt/bin/sqlite3 ] && SQLITE3_PATH=/opt/bin/sqlite3 || SQLITE3_PATH=/usr/sbin/sqlite3
#function to create JS file with data
WriteStats_ToJS(){
[ -f $2 ] && rm -f "$2"
echo "function $3(){" >> "$2"
html='document.getElementById("'"$4"'").innerHTML="'
while IFS='' read -r line || [ -n "$line" ]; do
html="$html""$line""\\r\\n"
done < "$1"
html="$html"'"'
printf "%s\\r\\n}\\r\\n" "$html" >> "$2"
}
WriteData_ToJS(){
{
echo "var $3;"
echo "$3 = [];"; } >> "$2"
contents="$3"'.unshift( '
while IFS='' read -r line || [ -n "$line" ]; do
if echo "$line" | grep -q "NaN"; then continue; fi
if [ "$4" == "date-day" ]; then
datapoint="{ x: moment(\"""$(echo "$line" | awk 'BEGIN{FS=","}{ print $1 }' | awk '{$1=$1};1')""\", \"YYYY-MM-DD\"), y: ""$(echo "$line" | awk 'BEGIN{FS=","}{ print $2 }' | awk '{$1=$1};1')"" }"
else
datapoint="{ x: moment.unix(""$(echo "$line" | awk 'BEGIN{FS=","}{ print $1 }' | awk '{$1=$1};1')""), y: ""$(echo "$line" | awk 'BEGIN{FS=","}{ print $2 }' | awk '{$1=$1};1')"" }"
fi
contents="$contents""$datapoint"","
done < "$1"
contents=$(echo "$contents" | sed 's/.$//')
contents="$contents"");"
printf "%s\\r\\n\\r\\n" "$contents" >> "$2"
}
#$1sql table $2 label column $3 count column $4 limit count $5 csv file $6 sql file $7 where clasue if needed
WriteSuricataSqlLog_ToFile(){
{
echo ".mode csv"
echo ".output $5"
} > "$6"
echo "SELECT $2 FROM $1 $7 GROUP BY $2 ORDER BY $3 LIMIT $4;" >> "$6"
}
#$1 csv file $2 JS file $3 JS func name $4 html tag
WriteSuricataCSV_ToJS_Table() {
#clean up any null (or "") strings with null string
sed -i 's/""/null/g' "$1"
sed -i 's/"//g' "$1"
[ -f $2 ] && rm -f "$2"
echo "function $3(){" >> "$2"
html='document.getElementById("'"$4"'").outerHTML="'
numLines="$(wc -l < $1)"
if [ "$numLines" -lt 1 ]; then
html="$html""<tr><td colspan="4" class="nodata">No data to display</td></tr>"
else
html="$html""$(cat "$1" | awk 'BEGIN{FS=","}{ print "<tr><td>" $1 "</td><td>" $2 "</td><td>"$3 "</td><td>" $4 "</td><td>" $5 "</td><td>" $6 "</td><td>" $7 "</td><td>" $8 "</td></tr> \\" }' | awk '{$1=$1};1')"
fi
html=${html%?}
html="$html"'"'
printf "%s}" "$html" >> "$2"
}
#$1 fieldname $2 tablename $3 frequency (hours) $4 length (days) $5 outputfile $6 sqlfile
WriteSql_ToFile(){
{
echo ".mode csv"
echo ".output $5"
} >> "$6"
COUNTER=0
timenow="$(date '+%s')"
until [ $COUNTER -gt "$((24*$4/$3))" ]; do
echo "select $timenow - ((60*60*$3)*($COUNTER)),IFNULL(avg([$1]),'NaN') from $2 WHERE ([Timestamp] >= $timenow - ((60*60*$3)*($COUNTER+1))) AND ([Timestamp] <= $timenow - ((60*60*$3)*$COUNTER));" >> "$6"
COUNTER=$((COUNTER + 1))
done
}
Generate_SuricataStats () {
echo "Suricata Stats generated on $(date +"%c")" > $statsTitleFile
WriteStats_ToJS "$statsTitleFile" "$statsTitleFileJS" "SetSuricataStatsTitle" "suricatastatstitle"
#generate Threat Events
echo "Calculating Threats data..."
{
echo ".mode csv"
echo ".output /tmp/suricata-threats-monthly.csv"
echo "select [date],SUM(count) from threat_log GROUP BY date ORDER BY date;"
} > /tmp/suricata-threats-monthly.sql
"$SQLITE3_PATH" "$dbLogs" < /tmp/suricata-threats-monthly.sql
[ -f $statsThreatsFileJS ] && rm -f "$statsThreatsFileJS"
WriteData_ToJS "/tmp/suricata-threats-monthly.csv" "$statsThreatsFileJS" "DatadivLineChartThreatsMonthly" "date-day"
#generate table data for all known Threats
echo "Outputting Threats ..."
[ -f $statsThreatsHitsFileJS ] && rm -f $statsThreatsHitsFileJS
whereString=""
WriteSuricataSqlLog_ToFile "threat_log" "date, threat_id, threat_desc, threat_class, threat_priority, threat_src_ip, threat_dst_ip, count" "date DESC, count DESC" "250" "/tmp/suricata-threats.csv" "/tmp/suricata-threats.sql" "$whereString"
"$SQLITE3_PATH" "$dbLogs" < /tmp/suricata-threats.sql
dos2unix "/tmp/suricata-threats.csv"
WriteSuricataCSV_ToJS_Table "/tmp/suricata-threats.csv" $statsThreatsHitsFileJS "LoadThreatsTable" "DatadivTableThreats"
#cleanup temp files
rm -f "/tmp/suricata-"*".csv"
rm -f "/tmp/suricata-"*".sql"
[ -f $statsTitleFile ] && rm -f $statsTitleFile
}
Auto_Startup(){
case $1 in
create)
if [ -f /jffs/scripts/post-mount ]; then
STARTUPLINECOUNTEX=$(grep -cx "$SCRIPT_DIR/$SCRIPT_NAME_LOWER startup"' # '"$SCRIPT_NAME" /jffs/scripts/post-mount)
if [ "$STARTUPLINECOUNTEX" -eq 0 ]; then
echo "$SCRIPT_DIR/$SCRIPT_NAME_LOWER startup"' # '"$SCRIPT_NAME" >> /jffs/scripts/post-mount
fi
else
echo "#!/bin/sh" > /jffs/scripts/post-mount
echo "" >> /jffs/scripts/post-mount
echo "$SCRIPT_DIR/$SCRIPT_NAME_LOWER startup"' # '"$SCRIPT_NAME" >> /jffs/scripts/post-mount
chmod 0755 /jffs/scripts/post-mount
fi
;;
delete)
if [ -f /jffs/scripts/post-mount ]; then
STARTUPLINECOUNT=$(grep -c '# '"$SCRIPT_NAME" /jffs/scripts/post-mount)
if [ "$STARTUPLINECOUNT" -gt 0 ]; then
sed -i -e '/# '"$SCRIPT_NAME"'/d' /jffs/scripts/post-mount
fi
fi
;;
esac
}
Auto_Cron(){
case $1 in
create)
STARTUPLINECOUNT=$(cru l | grep -c "$SCRIPT_NAME")
if [ "$STARTUPLINECOUNT" -eq 0 ]; then
cru a "$SCRIPT_NAME" "14 * * * * $SCRIPT_DIR/$SCRIPT_NAME_LOWER generate"
fi
STARTUPLINECOUNT=$(cru l | grep -c "$LOGSCRIPT_NAME")
if [ "$STARTUPLINECOUNT" -eq 0 ]; then
cru a "$LOGSCRIPT_NAME" "13 * * * * $SCRIPT_DIR/$LOGSCRIPT_NAME_LOWER"
fi
;;
delete)
STARTUPLINECOUNT=$(cru l | grep -c "$SCRIPT_NAME")
if [ "$STARTUPLINECOUNT" -gt 0 ]; then
cru d "$SCRIPT_NAME"
fi
STARTUPLINECOUNT=$(cru l | grep -c "$LOGSCRIPT_NAME")
if [ "$STARTUPLINECOUNT" -gt 0 ]; then
cru d "$LOGSCRIPT_NAME"
fi
;;
esac
}
Auto_ServiceEvent(){
case $1 in
create)
if [ -f /jffs/scripts/service-event ]; then
# shellcheck disable=SC2016
STARTUPLINECOUNTEX=$(grep -cx "$SCRIPT_DIR/$SCRIPT_NAME_LOWER generate"' "$1" "$2" &'' # '"$SCRIPT_NAME" /jffs/scripts/service-event)
if [ "$STARTUPLINECOUNTEX" -eq 0 ]; then
# shellcheck disable=SC2016
echo "$SCRIPT_DIR/$SCRIPT_NAME_LOWER generate"' "$1" "$2" &'' # '"$SCRIPT_NAME" >> /jffs/scripts/service-event
fi
else
echo "#!/bin/sh" > /jffs/scripts/service-event
echo "" >> /jffs/scripts/service-event
# shellcheck disable=SC2016
echo "/jffs/scripts/$SCRIPT_NAME_LOWER generate"' "$1" "$2" &'' # '"$SCRIPT_NAME" >> /jffs/scripts/service-event
chmod 0755 /jffs/scripts/service-event
fi
;;
delete)
if [ -f /jffs/scripts/service-event ]; then
STARTUPLINECOUNT=$(grep -c '# '"$SCRIPT_NAME" /jffs/scripts/service-event)
if [ "$STARTUPLINECOUNT" -gt 0 ]; then
sed -i -e '/# '"$SCRIPT_NAME"'/d' /jffs/scripts/service-event
fi
fi
;;
esac
}
Create_Dirs(){
if [ ! -d "$SCRIPT_WEBPAGE_DIR" ]; then
mkdir -p "$SCRIPT_WEBPAGE_DIR"
fi
if [ ! -d "$SCRIPT_WEB_DIR" ]; then
mkdir -p "$SCRIPT_WEB_DIR"
fi
}
Get_WebUI_Installed () {
md5_installed="0"
if [ -f $installedMD5File ]; then
md5_installed="$(cat $installedMD5File)"
fi
}
Get_WebUI_Page () {
for i in 1 2 3 4 5 6 7 8 9 10; do
page="$SCRIPT_WEBPAGE_DIR/user$i.asp"
if [ ! -f "$page" ] || [ "$(md5sum < "$1")" = "$(md5sum < "$page")" ] || [ "$2" = "$(md5sum < "$page")" ]; then
MyPage="user$i.asp"
return
fi
done
MyPage="none"
}
Mount_WebUI(){
if nvram get rc_support | grep -qF "am_addons"; then
Get_WebUI_Installed
Get_WebUI_Page "$SCRIPT_DIR/suricatastats_www.asp" "$md5_installed"
if [ "$MyPage" = "none" ]; then
echo "Unable to mount $SCRIPT_NAME WebUI page, exiting"
exit 1
fi
echo "Mounting $SCRIPT_NAME WebUI page as $MyPage"
cp -f "$SCRIPT_DIR/suricatastats_www.asp" "$SCRIPT_WEBPAGE_DIR/$MyPage"
echo "Saving MD5 of installed file $SCRIPT_DIR/suricatastats_www.asp to $installedMD5File"
md5sum < "$SCRIPT_DIR/suricatastats_www.asp" > $installedMD5File
if [ ! -f "/tmp/index_style.css" ]; then
cp -f "/www/index_style.css" "/tmp/"
fi
if ! grep -q '.menu_Addons' /tmp/index_style.css ; then
echo ".menu_Addons { background: url(ext/shared-jy/addons.png); }" >> /tmp/index_style.css
fi
umount /www/index_style.css 2>/dev/null
mount -o bind /tmp/index_style.css /www/index_style.css
if [ ! -f "/tmp/menuTree.js" ]; then
cp -f "/www/require/modules/menuTree.js" "/tmp/"
fi
sed -i "\\~$MyPage~d" /tmp/menuTree.js
if ! grep -q 'menuName: "Addons"' /tmp/menuTree.js ; then
lineinsbefore="$(( $(grep -n "exclude:" /tmp/menuTree.js | cut -f1 -d':') - 1))"
sed -i "$lineinsbefore"'i,\n{\nmenuName: "Addons",\nindex: "menu_Addons",\ntab: [\n{url: "ext/shared-jy/redirect.htm", tabName: "Help & Support"},\n{url: "NULL", tabName: "__INHERIT__"}\n]\n}' /tmp/menuTree.js
fi
if ! grep -q "javascript:window.open('/ext/shared-jy/redirect.htm'" /tmp/menuTree.js ; then
sed -i "s~ext/shared-jy/redirect.htm~javascript:window.open('/ext/shared-jy/redirect.htm','_blank')~" /tmp/menuTree.js
fi
sed -i "/url: \"javascript:window.open('\/ext\/shared-jy\/redirect.htm'/i {url: \"$MyPage\", tabName: \"Suricata\"}," /tmp/menuTree.js
umount /www/require/modules/menuTree.js 2>/dev/null
mount -o bind /tmp/menuTree.js /www/require/modules/menuTree.js
fi
}
Unmount_WebUI(){
Get_WebUI_Installed
Get_WebUI_Page "$SCRIPT_DIR/suricatastats_www.asp" "$md5_installed"
echo "$MyPage"
if [ -n "$MyPage" ] && [ "$MyPage" != "none" ] && [ -f "/tmp/menuTree.js" ]; then
sed -i "\\~$MyPage~d" /tmp/menuTree.js
umount /www/require/modules/menuTree.js
mount -o bind /tmp/menuTree.js /www/require/modules/menuTree.js
rm -rf "$SCRIPT_WEBPAGE_DIR/$MyPage"
rm -rf "$SCRIPT_WEB_DIR"
fi
}
# $1 show commands
ScriptHeader() {
printf "\\n"
printf "##\\n"
printf "##Suricata Stats\\n"
printf "## by @juched - Generate Stats for GUI tab - %s \\n" "$SCRIPT_VERSION"
printf "## with credit to @JackYaz for his shared scripts \\n"
printf "\\n"
if [ ! -z $1 ]; then
printf "suricata_stats.sh\\n"
printf " install - Installs the needed files to show UI and update stats\\n"
printf " generate - enerates statistics now for UI\\n"
printf " uninstall - Removes files needed for UI and stops stats update\\n"
fi
}
Download_File(){
/usr/sbin/curl -fsL --retry 3 "$1" -o "$2"
}
Install_Dependancies(){
#install SQLite if not installed
if [ ! -f /opt/bin/sqlite3 ]; then
echo "Installing required version of sqlite3 from Entware"
opkg update
opkg install sqlite3-cli
fi
# make shared JY charts directory, and download if needed
if [ ! -d "$SHARED_DIR" ]; then
echo "Shared JY directory doesn't exist, let's make it..."
mkdir "$SHARED_DIR"
fi
if [ ! -f "$SHARED_DIR/shared-jy.tar.gz.md5" ]; then
Download_File "$SHARED_REPO/shared-jy.tar.gz" "$SHARED_DIR/shared-jy.tar.gz"
Download_File "$SHARED_REPO/shared-jy.tar.gz.md5" "$SHARED_DIR/shared-jy.tar.gz.md5"
tar -xzf "$SHARED_DIR/shared-jy.tar.gz" -C "$SHARED_DIR"
rm -f "$SHARED_DIR/shared-jy.tar.gz"
echo "New version of shared-jy.tar.gz downloaded"
else
localmd5="$(cat "$SHARED_DIR/shared-jy.tar.gz.md5")"
remotemd5="$(curl -fsL --retry 3 "$SHARED_REPO/shared-jy.tar.gz.md5")"
if [ "$localmd5" != "$remotemd5" ]; then
Download_File "$SHARED_REPO/shared-jy.tar.gz" "$SHARED_DIR/shared-jy.tar.gz"
Download_File "$SHARED_REPO/shared-jy.tar.gz.md5" "$SHARED_DIR/shared-jy.tar.gz.md5"
tar -xzf "$SHARED_DIR/shared-jy.tar.gz" -C "$SHARED_DIR"
rm -f "$SHARED_DIR/shared-jy.tar.gz"
echo "New version of shared-jy.tar.gz downloaded"
fi
fi
#Symlink the shared jy folder if it doesn't exist
if [ ! -d "$SHARED_WEB_DIR" ]; then
ln -s "$SHARED_DIR" "$SHARED_WEB_DIR" 2>/dev/null
fi
}
#Main loop
if [ -z "$1" ]; then
ScriptHeader show_commands
exit 0
fi
ScriptHeader
case "$1" in
install)
Install_Dependancies
Auto_Startup create
Auto_ServiceEvent create
Auto_Cron create
Mount_WebUI
Create_Dirs
sh /jffs/addons/suricata/suricata_log.sh
Generate_SuricataStats
exit 0
;;
startup)
Auto_Cron create
Mount_WebUI
Create_Dirs
Generate_SuricataStats
exit 0
;;
generate)
if [ -z "$2" ] && [ -z "$3" ]; then
Generate_SuricataStats
elif [ "$2" = "start" ] && [ "$3" = "$SCRIPT_NAME_LOWER" ]; then
Generate_SuricataStats
fi
exit 0
;;
uninstall)
Auto_Startup delete
Auto_ServiceEvent delete
Auto_Cron delete
Unmount_WebUI
[ -f $installedMD5File ] && rm -f $installedMD5File
[ -f $dbLogs ] && rm -f $dbLogs
exit 0
;;
esac